Ported API and internal structures to struct in_addr.

This commit is contained in:
Florian Pose
2024-05-15 13:04:34 +02:00
parent 66c5793e7b
commit c2f9baf96a
9 changed files with 40 additions and 48 deletions

View File

@@ -160,10 +160,12 @@
#include <asm/byteorder.h>
#include <linux/types.h>
#include <linux/time.h>
#include <linux/in.h> // struct in_addr
#else
#include <stdlib.h> // for size_t
#include <stdint.h>
#include <sys/time.h> // for struct timeval
#include <netinet/in.h> // struct in_addr
#endif
/*****************************************************************************
@@ -1885,12 +1887,12 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_mac_address(
*
* \code{.c}
* #include <arpa/inet.h>
* unsigned char buf[sizeof(struct in_addr)];
* if (inet_pton(AF_INET, "192.168.0.1", buf) <= 0) {
* struct in_addr addr;
* if (inet_aton("192.168.0.1", &addr) == 0) {
* fprintf(stderr, "Failed to convert IP address.\n");
* return -1;
* }
* if (ecrt_slave_config_eoe_ip_address(sc, *(uint32_t *) buf)) {
* if (ecrt_slave_config_eoe_ip_address(sc, addr)) {
* fprintf(stderr, "Failed to set IP address.\n");
* return -1;
* }
@@ -1902,7 +1904,7 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_mac_address(
*/
EC_PUBLIC_API int ecrt_slave_config_eoe_ip_address(
ec_slave_config_t *sc, /**< Slave configuration. */
uint32_t ip_address /**< IPv4 address. */
struct in_addr ip_address /**< IPv4 address. */
);
/** Sets the subnet mask for Ethernet-over-EtherCAT (EoE) operation.
@@ -1915,7 +1917,7 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_ip_address(
*/
EC_PUBLIC_API int ecrt_slave_config_eoe_subnet_mask(
ec_slave_config_t *sc, /**< Slave configuration. */
uint32_t subnet_mask /**< IPv4 subnet mask. */
struct in_addr subnet_mask /**< IPv4 subnet mask. */
);
/** Sets the gateway address for Ethernet-over-EtherCAT (EoE) operation.
@@ -1928,7 +1930,7 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_subnet_mask(
*/
EC_PUBLIC_API int ecrt_slave_config_eoe_default_gateway(
ec_slave_config_t *sc, /**< Slave configuration. */
uint32_t gateway_address /**< Gateway's IPv4 address. */
struct in_addr gateway_address /**< Gateway's IPv4 address. */
);
/** Sets the DNS server address for Ethernet-over-EtherCAT (EoE) operation.
@@ -1941,7 +1943,7 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_default_gateway(
*/
EC_PUBLIC_API int ecrt_slave_config_eoe_dns_address(
ec_slave_config_t *sc, /**< Slave configuration. */
uint32_t dns_address /**< IPv4 address of the DNS server. */
struct in_addr dns_address /**< IPv4 address of the DNS server. */
);
/** Sets the host name for Ethernet-over-EtherCAT (EoE) operation.

View File

@@ -928,7 +928,7 @@ int ecrt_slave_config_eoe_mac_address(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_ip_address(ec_slave_config_t *sc,
uint32_t ip_address)
struct in_addr ip_address)
{
ec_ioctl_eoe_ip_t io = {};
int ret;
@@ -950,7 +950,7 @@ int ecrt_slave_config_eoe_ip_address(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_subnet_mask(ec_slave_config_t *sc,
uint32_t subnet_mask)
struct in_addr subnet_mask)
{
ec_ioctl_eoe_ip_t io = {};
int ret;
@@ -972,7 +972,7 @@ int ecrt_slave_config_eoe_subnet_mask(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_default_gateway(ec_slave_config_t *sc,
uint32_t gateway_address)
struct in_addr gateway_address)
{
ec_ioctl_eoe_ip_t io = {};
int ret;
@@ -994,7 +994,7 @@ int ecrt_slave_config_eoe_default_gateway(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_dns_address(ec_slave_config_t *sc,
uint32_t dns_address)
struct in_addr dns_address)
{
ec_ioctl_eoe_ip_t io = {};
int ret;

View File

@@ -51,10 +51,10 @@ void ec_eoe_request_init(
req->name_included = 0;
memset(req->mac_address, 0x00, ETH_ALEN);
req->ip_address = 0;
req->subnet_mask = 0;
req->gateway = 0;
req->dns = 0;
req->ip_address.s_addr = 0;
req->subnet_mask.s_addr = 0;
req->gateway.s_addr = 0;
req->dns.s_addr = 0;
req->name[0] = 0x00;
req->result = 0x0000;

View File

@@ -51,10 +51,10 @@ typedef struct {
uint8_t name_included;
unsigned char mac_address[ETH_ALEN];
uint32_t ip_address;
uint32_t subnet_mask;
uint32_t gateway;
uint32_t dns;
struct in_addr ip_address;
struct in_addr subnet_mask;
struct in_addr gateway;
struct in_addr dns;
char name[EC_MAX_HOSTNAME_SIZE];
uint16_t result;

View File

@@ -198,26 +198,22 @@ int ec_fsm_eoe_prepare_set(
cur += ETH_ALEN;
if (req->ip_address_included) {
uint32_t swapped = htonl(req->ip_address);
memcpy(cur, &swapped, 4);
memcpy(cur, &req->ip_address, 4);
}
cur += 4;
if (req->subnet_mask_included) {
uint32_t swapped = htonl(req->subnet_mask);
memcpy(cur, &swapped, 4);
memcpy(cur, &req->subnet_mask, 4);
}
cur += 4;
if (req->gateway_included) {
uint32_t swapped = htonl(req->gateway);
memcpy(cur, &swapped, 4);
memcpy(cur, &req->gateway, 4);
}
cur += 4;
if (req->dns_included) {
uint32_t swapped = htonl(req->dns);
memcpy(cur, &swapped, 4);
memcpy(cur, &req->dns, 4);
}
cur += 4;

View File

@@ -635,10 +635,10 @@ typedef struct {
uint8_t name_included;
unsigned char mac_address[EC_ETH_ALEN];
uint32_t ip_address;
uint32_t subnet_mask;
uint32_t gateway;
uint32_t dns;
struct in_addr ip_address;
struct in_addr subnet_mask;
struct in_addr gateway;
struct in_addr dns;
char name[EC_MAX_HOSTNAME_SIZE];
// output

View File

@@ -1453,7 +1453,7 @@ int ecrt_slave_config_eoe_mac_address(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_ip_address(ec_slave_config_t *sc,
uint32_t ip_address)
struct in_addr ip_address)
{
sc->eoe_ip_param_request.ip_address = ip_address;
sc->eoe_ip_param_request.ip_address_included = 1;
@@ -1463,7 +1463,7 @@ int ecrt_slave_config_eoe_ip_address(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_subnet_mask(ec_slave_config_t *sc,
uint32_t subnet_mask)
struct in_addr subnet_mask)
{
sc->eoe_ip_param_request.subnet_mask = subnet_mask;
sc->eoe_ip_param_request.subnet_mask_included = 1;
@@ -1473,7 +1473,7 @@ int ecrt_slave_config_eoe_subnet_mask(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_default_gateway(ec_slave_config_t *sc,
uint32_t gateway_address)
struct in_addr gateway_address)
{
sc->eoe_ip_param_request.gateway = gateway_address;
sc->eoe_ip_param_request.gateway_included = 1;
@@ -1483,7 +1483,7 @@ int ecrt_slave_config_eoe_default_gateway(ec_slave_config_t *sc,
/****************************************************************************/
int ecrt_slave_config_eoe_dns_address(ec_slave_config_t *sc,
uint32_t dns_address)
struct in_addr dns_address)
{
sc->eoe_ip_param_request.dns = dns_address;
sc->eoe_ip_param_request.dns_included = 1;

View File

@@ -206,11 +206,8 @@ void CommandIp::parseIpv4Prefix(ec_ioctl_eoe_ip_t *io,
err << "Invalid prefix '" << prefixStr << "'!";
throwInvalidUsageException(err);
}
uint32_t mask = 0;
for (unsigned int bit = 0; bit < prefix; bit++) {
mask |= (1 << (31 - bit));
}
io->subnet_mask = htonl(mask);
uint32_t mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF;
io->subnet_mask.s_addr = htonl(mask);
}
resolveIpv4(&io->ip_address, host);
@@ -218,7 +215,7 @@ void CommandIp::parseIpv4Prefix(ec_ioctl_eoe_ip_t *io,
/****************************************************************************/
void CommandIp::resolveIpv4(uint32_t *addr, const string &str)
void CommandIp::resolveIpv4(struct in_addr *dst, const string &str)
{
struct addrinfo hints = {};
struct addrinfo *res;
@@ -239,12 +236,9 @@ void CommandIp::resolveIpv4(uint32_t *addr, const string &str)
throwCommandException(err.str());
}
sockaddr_in *sin = (sockaddr_in *) res->ai_addr;
for (unsigned int i = 0; i < 4; i++) {
((unsigned char *) addr)[i] =
((unsigned char *) &sin->sin_addr.s_addr)[i];
}
const struct sockaddr_in *in_addr =
(const struct sockaddr_in *) res->ai_addr;
*dst = in_addr->sin_addr;
freeaddrinfo(res);
}

View File

@@ -38,7 +38,7 @@ class CommandIp:
protected:
void parseMac(unsigned char [6], const string &);
void parseIpv4Prefix(ec_ioctl_eoe_ip_t *, const string &);
void resolveIpv4(uint32_t *, const string &);
void resolveIpv4(struct in_addr *, const string &);
};
/****************************************************************************/