Author: viethen Date: Mon Apr 11 13:06:22 2011 New Revision: 2526 Log: Get rid of custom mac2ip_ipv4_t, replacing it with the standard "struct in_addr" type for IPv4 addresses. Advantages: a) can now use standard htonl() and ntohl() for conversions between network and host byte order instead of having to fiddle with array elements b) can entirely do without custom "string_to_ipv4_t()" conversion routine (standard inet_pton() will do nicely) Modified: trunk/tools/dhcp/mac2ip.c trunk/tools/dhcp/mac2ip.h Modified: trunk/tools/dhcp/mac2ip.c ============================================================================== --- trunk/tools/dhcp/mac2ip.c Fri Apr 8 02:42:41 2011 (r2525) +++ trunk/tools/dhcp/mac2ip.c Mon Apr 11 13:06:22 2011 (r2526) @@ -6,6 +6,7 @@ */ #include "mac2ip.h" +#include <arpa/inet.h> #include <assert.h> #include <inttypes.h> #include <stdbool.h> @@ -41,7 +42,7 @@ } /* hash mac random ip */ -void hash_mac_to_rnd_ip_OAAT(const mac2ip_mac_t *mac, mac2ip_ipv4_t *ip) +void hash_mac_to_rnd_ip_OAAT(const mac2ip_mac_t *mac, struct in_addr *ip) { const uint8_t uint8_max_value = 255; time_t cur_time; @@ -65,21 +66,7 @@ hash = jenkins_one_at_a_time_hash(mac_duplicate.x, sizeof(mac_duplicate.x)); /* store the hash value (making sure it ends up in network byte order) */ - ip->x[0] = (hash & 0xff000000) >> 24; - ip->x[1] = (hash & 0x00ff0000) >> 16; - ip->x[2] = (hash & 0x0000ff00) >> 8; - ip->x[3] = hash & 0x000000ff; -} - -/* converts IPv4 address in the common dotted-quad notation to ipv4_t */ -/* returns -1 in case s.th. goes wrong, otherwise 0 */ -static int string_to_ipv4_t(const char *string, mac2ip_ipv4_t *ip) -{ - if (sscanf(string, "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, &ip->x[0], &ip->x[1], &ip->x[2], &ip->x[3]) == 4) { - return 0; - } else { - return -1; - } + ip->s_addr = htonl(hash); } static char *readline(FILE *fp, char **buf, size_t *buflen) @@ -213,7 +200,7 @@ if (strcmp(variable, "lower_client_ip_bound") == 0) { - if (string_to_ipv4_t(value, &bounds->lower_client.bound) == 0) { + if (inet_pton(AF_INET, value, &bounds->lower_client.bound.s_addr) == 1) { bounds->lower_client.valid = true; retval++; } @@ -221,7 +208,7 @@ } if (strcmp(variable, "upper_client_ip_bound") == 0) { - if (string_to_ipv4_t(value, &bounds->upper_client.bound) == 0) { + if (inet_pton(AF_INET, value, &bounds->upper_client.bound.s_addr) == 1) { bounds->upper_client.valid = true; retval++; } @@ -229,7 +216,7 @@ } if (strcmp(variable, "lower_service_ip_bound") == 0) { - if (string_to_ipv4_t(value, &bounds->lower_service.bound) == 0) { + if (inet_pton(AF_INET, value, &bounds->lower_service.bound.s_addr) == 1) { bounds->lower_service.valid = true; retval++; } @@ -237,7 +224,7 @@ } if (strcmp(variable, "upper_service_ip_bound") == 0) { - if (string_to_ipv4_t(value, &bounds->upper_service.bound) == 0) { + if (inet_pton(AF_INET, value, &bounds->upper_service.bound.s_addr) == 1) { bounds->upper_service.valid = true; retval++; } @@ -252,32 +239,27 @@ } /* adjusts the ip to the given bounds */ -void adjust_ip_to_bounds(mac2ip_ipv4_t *ip, const mac2ip_ipv4_t *upper_bound, const mac2ip_ipv4_t *lower_bound) +void adjust_ip_to_bounds(struct in_addr *ip, const struct in_addr *upper_bound, const struct in_addr *lower_bound) { uint32_t lower_bounds_value; - uint32_t upper_bounds_value; uint64_t num_bounds_ips; uint32_t old_ip_value, new_ip_value; /* determine number of IPs that fit within bounds */ - lower_bounds_value = ((lower_bound->x[0] * 256 + lower_bound->x[1]) * 256 + lower_bound->x[2]) * 256 + lower_bound->x[3]; - upper_bounds_value = ((upper_bound->x[0] * 256 + upper_bound->x[1]) * 256 + upper_bound->x[2]) * 256 + upper_bound->x[3]; - num_bounds_ips = upper_bounds_value - lower_bounds_value + 1; + lower_bounds_value = ntohl(lower_bound->s_addr); + num_bounds_ips = ntohl(upper_bound->s_addr) - lower_bounds_value + 1; - /* convert the input IP into a numerical value */ - old_ip_value = ((ip->x[0] * 256 + ip->x[1]) * 256 + ip->x[2]) * 256 + ip->x[3]; + /* turn IP into host byte order value to enable the following calculation */ + old_ip_value = ntohl(ip->s_addr); /* old_ip_value modulo "number of IPs within the bounds" is offset within the bounds */ new_ip_value = lower_bounds_value + (old_ip_value % num_bounds_ips); - /* put the result back into "ip->x" array */ - ip->x[0] = (new_ip_value & 0xff000000) >> 24; - ip->x[1] = (new_ip_value & 0x00ff0000) >> 16; - ip->x[2] = (new_ip_value & 0x0000ff00) >> 8; - ip->x[3] = new_ip_value & 0x000000ff; + /* ... and back into network byte order */ + ip->s_addr = htonl(new_ip_value); } -int mac_to_ip(const mac2ip_mac_t *mac, mac2ip_ipv4_t *ip) +int mac_to_ip(const mac2ip_mac_t *mac, struct in_addr *ip) { ip_bounds bounds; @@ -293,6 +275,7 @@ return -1; } + /* make the IP fit inbetween these bounds */ adjust_ip_to_bounds(ip, &bounds.upper_client.bound, &bounds.lower_client.bound); return 0; Modified: trunk/tools/dhcp/mac2ip.h ============================================================================== --- trunk/tools/dhcp/mac2ip.h Fri Apr 8 02:42:41 2011 (r2525) +++ trunk/tools/dhcp/mac2ip.h Mon Apr 11 13:06:22 2011 (r2526) @@ -20,19 +20,16 @@ #ifndef __MAC2IP_H__ #define __MAC2IP_H__ +#include <netinet/in.h> #include <stdint.h> -typedef struct mac2ip_ipv4_t { - uint8_t x[4]; -} mac2ip_ipv4_t; - typedef struct mac2ip_mac_t { uint8_t x[6]; } mac2ip_mac_t; struct ip_bound { - mac2ip_ipv4_t bound; - _Bool valid; + struct in_addr bound; + _Bool valid; }; typedef struct ip_bounds { @@ -47,7 +44,7 @@ * configured in /etc/mac2ip.conf * result will be stored in 'ip' */ -int mac_to_ip(const mac2ip_mac_t *, mac2ip_ipv4_t *); +int mac_to_ip(const mac2ip_mac_t *, struct in_addr *); int read_ip_bounds(ip_bounds *); -- This is the pisa developer mailing list. Please also subscribe to the main pisa list at: //www.freelists.org/list/pisa