diff --git a/include/ecrt.h b/include/ecrt.h index 6589554c..623b2d9d 100644 --- a/include/ecrt.h +++ b/include/ecrt.h @@ -160,10 +160,12 @@ #include #include #include +#include // struct in_addr #else #include // for size_t #include #include // for struct timeval +#include // struct in_addr #endif /***************************************************************************** @@ -1885,12 +1887,12 @@ EC_PUBLIC_API int ecrt_slave_config_eoe_mac_address( * * \code{.c} * #include - * 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. diff --git a/lib/slave_config.c b/lib/slave_config.c index 42ac20a1..d64a99cd 100644 --- a/lib/slave_config.c +++ b/lib/slave_config.c @@ -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; diff --git a/master/eoe_request.c b/master/eoe_request.c index 68b6d25b..f336cf34 100644 --- a/master/eoe_request.c +++ b/master/eoe_request.c @@ -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; diff --git a/master/eoe_request.h b/master/eoe_request.h index c1dcb478..6751f1f9 100644 --- a/master/eoe_request.h +++ b/master/eoe_request.h @@ -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; diff --git a/master/fsm_eoe.c b/master/fsm_eoe.c index 90bfa1ee..28964740 100644 --- a/master/fsm_eoe.c +++ b/master/fsm_eoe.c @@ -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; diff --git a/master/ioctl.h b/master/ioctl.h index 036a6cc7..2cb90103 100644 --- a/master/ioctl.h +++ b/master/ioctl.h @@ -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 diff --git a/master/slave_config.c b/master/slave_config.c index cf94f19a..c289eda4 100644 --- a/master/slave_config.c +++ b/master/slave_config.c @@ -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; diff --git a/tool/CommandIp.cpp b/tool/CommandIp.cpp index 19875c88..3c0484a5 100644 --- a/tool/CommandIp.cpp +++ b/tool/CommandIp.cpp @@ -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); } diff --git a/tool/CommandIp.h b/tool/CommandIp.h index c7e105da..bc6d9b8c 100644 --- a/tool/CommandIp.h +++ b/tool/CommandIp.h @@ -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 &); }; /****************************************************************************/