version 1.1.1.1.6.1, 2013/05/26 20:03:19
|
version 1.3.2.1, 2013/06/03 22:25:09
|
Line 281 e_usleep(u_int usec)
|
Line 281 e_usleep(u_int usec)
|
|
|
return select(0, NULL, NULL, NULL, &tv); |
return select(0, NULL, NULL, NULL, &tv); |
} |
} |
|
|
|
/* |
|
* e_innet() - Test address match in network |
|
* |
|
* @net = network |
|
* @addr = address |
|
* return: -1 error, 0 match or 1 not match |
|
*/ |
|
int |
|
e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr) |
|
{ |
|
register int i; |
|
int ret = 0; |
|
|
|
if (!net || !addr) |
|
return -1; |
|
|
|
switch (net->addr.sa.sa_family) { |
|
case AF_INET: |
|
for (i = 0; i < sizeof(struct in_addr); i++) { |
|
ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & |
|
net->mask.in.s4_addr[i]; |
|
ret -= addr->in.s4_addr[i] & net->mask.in.s4_addr[i]; |
|
if (ret) |
|
break; |
|
} |
|
break; |
|
case AF_INET6: |
|
for (i = 0; i < sizeof(struct in6_addr); i++) { |
|
ret = net->addr.sin6.sin6_addr.s6_addr[i] & |
|
net->mask.in6.s6_addr[i]; |
|
ret -= addr->in6.s6_addr[i] & net->mask.in6.s6_addr[i]; |
|
if (ret) |
|
break; |
|
} |
|
break; |
|
default: |
|
return -1; |
|
} |
|
|
|
return !!ret; |
|
} |
|
|
|
/* |
|
* e_getnet() - Get network from string |
|
* |
|
* @net = Network string (format: <net[/cidr]>) |
|
* return: NULL error or !=NULL network should be e_free() |
|
*/ |
|
netaddr_t * |
|
e_getnet(const char *net) |
|
{ |
|
netaddr_t *n; |
|
char *str, *wrk; |
|
struct hostent *host; |
|
|
|
n = e_malloc(sizeof(netaddr_t)); |
|
if (!n) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
memset(n, 0, sizeof(netaddr_t)); |
|
str = e_strdup(net); |
|
if (!str) { |
|
LOGERR; |
|
e_free(n); |
|
return NULL; |
|
} |
|
wrk = strchr(str, '/'); |
|
if (wrk) |
|
*wrk++ = 0; |
|
|
|
host = gethostbyname2(str, strchr(str, ':') ? AF_INET6 : AF_INET); |
|
if (!host) { |
|
elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); |
|
e_free(str); |
|
e_free(n); |
|
return NULL; |
|
} |
|
switch (host->h_addrtype) { |
|
case AF_INET: |
|
n->addr.sin.sin_len = sizeof(struct sockaddr_in); |
|
n->addr.sin.sin_family = host->h_addrtype; |
|
memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr); |
|
if (wrk) |
|
n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10)); |
|
else |
|
n->mask.in.s_addr = 0xFFFFFFFF; |
|
break; |
|
case AF_INET6: |
|
n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6); |
|
n->addr.sin6.sin6_family = host->h_addrtype; |
|
memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr); |
|
/* TODO: should support ipv6 mask */ |
|
break; |
|
default: |
|
elwix_SetErr(EINVAL, "Unsupported family #%d", host->h_addrtype); |
|
e_free(str); |
|
e_free(n); |
|
return NULL; |
|
} |
|
|
|
e_free(str); |
|
return 0; |
|
} |