version 1.1.1.1.6.1, 2013/05/26 20:03:19
|
version 1.10.10.3, 2014/11/17 23:00:36
|
Line 12 terms:
|
Line 12 terms:
|
All of the documentation and software included in the ELWIX and AITNET |
All of the documentation and software included in the ELWIX and AITNET |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
|
|
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 | Copyright 2004 - 2014 |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
|
|
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
Line 46 SUCH DAMAGE.
|
Line 46 SUCH DAMAGE.
|
#include "global.h" |
#include "global.h" |
|
|
|
|
|
static char hexlist[] = "0123456789abcdef"; |
|
|
/* |
/* |
|
* e_link_ntoa() - String ethernet address from link address |
|
* |
|
* @sdl = link address |
|
* return: =NULL error or !=NULL ethernet address, should be e_free() |
|
*/ |
|
char * |
|
e_link_ntoa(const struct sockaddr_dl *sdl) |
|
{ |
|
static char obuf[64]; |
|
char *out = obuf; |
|
int i; |
|
u_char *in = (u_char*) LLADDR(sdl); |
|
u_char *inlim = in + sdl->sdl_alen; |
|
int firsttime = 1; |
|
|
|
if (sdl->sdl_nlen) { |
|
memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen); |
|
out += sdl->sdl_nlen; |
|
if (sdl->sdl_alen) |
|
*out++ = '!'; |
|
} |
|
|
|
while (in < inlim) { |
|
if (firsttime) |
|
firsttime ^= firsttime; |
|
else |
|
*out++ = ':'; |
|
|
|
i = *in++; |
|
if (i > 0xf) { |
|
out[1] = hexlist[i & 0xf]; |
|
i >>= 4; |
|
} else { |
|
out[1] = hexlist[i]; |
|
i = 0; |
|
} |
|
|
|
out[0] = hexlist[i]; |
|
out += 2; |
|
} |
|
|
|
*out = 0; |
|
return obuf; |
|
} |
|
|
|
/* |
* e_ether_ntoa() - Convert ethernet address to string |
* e_ether_ntoa() - Convert ethernet address to string |
* |
* |
* @n = ethernet address structure, like struct ether_addr |
* @n = ethernet address structure, like struct ether_addr |
Line 55 SUCH DAMAGE.
|
Line 103 SUCH DAMAGE.
|
* return: NULL error or !=NULL string a |
* return: NULL error or !=NULL string a |
*/ |
*/ |
char * |
char * |
e_ether_ntoa(const struct e_ether_addr *n, char * __restrict a, int len) | e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len) |
{ |
{ |
if (!n || !a) |
if (!n || !a) |
return NULL; |
return NULL; |
|
|
memset(a, 0, len); |
memset(a, 0, len); |
if (snprintf(a, len, "%02x:%02x:%02x:%02x:%02x:%02x", | if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
n->ether_addr_octet[0], n->ether_addr_octet[1], |
n->ether_addr_octet[0], n->ether_addr_octet[1], |
n->ether_addr_octet[2], n->ether_addr_octet[3], |
n->ether_addr_octet[2], n->ether_addr_octet[3], |
n->ether_addr_octet[4], n->ether_addr_octet[5]) < 17) |
n->ether_addr_octet[4], n->ether_addr_octet[5]) < 17) |
Line 77 e_ether_ntoa(const struct e_ether_addr *n, char * __re
|
Line 125 e_ether_ntoa(const struct e_ether_addr *n, char * __re
|
* @e = ethernet address structure, like struct ether_addr |
* @e = ethernet address structure, like struct ether_addr |
* return: NULL error or !=NULL ethernet address structure |
* return: NULL error or !=NULL ethernet address structure |
*/ |
*/ |
struct e_ether_addr * | ether_addr_t * |
e_ether_aton(const char *a, struct e_ether_addr *e) | e_ether_aton(const char *a, ether_addr_t * __restrict e) |
{ |
{ |
int i; |
int i; |
u_int o0, o1, o2, o3, o4, o5; |
|
|
|
if (!a || !e) |
if (!a || !e) |
return NULL; |
return NULL; |
|
|
i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); | i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| &e->ether_addr_octet[0], |
| &e->ether_addr_octet[1], |
| &e->ether_addr_octet[2], |
| &e->ether_addr_octet[3], |
| &e->ether_addr_octet[4], |
| &e->ether_addr_octet[5]); |
if (i != 6) |
if (i != 6) |
return NULL; |
return NULL; |
|
|
e->ether_addr_octet[0] = o0; |
|
e->ether_addr_octet[1] = o1; |
|
e->ether_addr_octet[2] = o2; |
|
e->ether_addr_octet[3] = o3; |
|
e->ether_addr_octet[4] = o4; |
|
e->ether_addr_octet[5] = o5; |
|
|
|
return e; |
return e; |
} |
} |
|
|
Line 136 e_n2port(sockaddr_t * __restrict addr)
|
Line 182 e_n2port(sockaddr_t * __restrict addr)
|
const char * |
const char * |
e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val) |
e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val) |
{ |
{ |
char str[INET6_ADDRSTRLEN] = { 0 }; | char *s, str[INET6_ADDRSTRLEN] = { 0 }; |
const char *ret = NULL; |
const char *ret = NULL; |
|
|
if (!addr || !val) |
if (!addr || !val) |
Line 161 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
Line 207 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
case AF_LOCAL: |
case AF_LOCAL: |
ret = addr->sun.sun_path; |
ret = addr->sun.sun_path; |
break; |
break; |
|
case AF_LINK: |
|
if (!(s = e_link_ntoa(&addr->sdl))) { |
|
LOGERR; |
|
return ret; |
|
} else |
|
ret = s; |
|
break; |
default: |
default: |
elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", |
elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", |
addr->sa.sa_family); |
addr->sa.sa_family); |
Line 189 e_gethostbyname(const char *psHost, u_short port, sock
|
Line 242 e_gethostbyname(const char *psHost, u_short port, sock
|
|
|
if (*psHost != '/') { |
if (*psHost != '/') { |
/* resolver */ |
/* resolver */ |
if (!addr->sa.sa_family) | host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); |
host = gethostbyname(psHost); | |
else | |
host = gethostbyname2(psHost, addr->sa.sa_family); | |
if (!host) { |
if (!host) { |
elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); |
elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); |
return NULL; |
return NULL; |
Line 280 e_usleep(u_int usec)
|
Line 330 e_usleep(u_int usec)
|
struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) }; |
struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) }; |
|
|
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 n; |
|
} |
|
|
|
/* |
|
* e_ether_addr() - Get or set ethernet address from interface name |
|
* |
|
* @ifname = interface name |
|
* @addr = if addr is !=NULL then set this for new address |
|
* return: NULL error or !=NULL get ethernet address should be e_free() |
|
*/ |
|
ether_addr_t * |
|
e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) |
|
{ |
|
ether_addr_t *a = NULL; |
|
struct ifaddrs *p, *ifa = NULL; |
|
|
|
if (!ifname) |
|
return NULL; |
|
|
|
a = e_malloc(sizeof(ether_addr_t)); |
|
if (!a) |
|
return NULL; |
|
else |
|
memset(a, 0, sizeof(ether_addr_t)); |
|
|
|
getifaddrs(&ifa); |
|
for (p = ifa; p && p->ifa_name; p++) |
|
if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && |
|
p->ifa_addr->sa_family == AF_LINK) { |
|
memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t)); |
|
break; |
|
} |
|
freeifaddrs(ifa); |
|
|
|
return a; |
|
} |
|
|
|
/* |
|
* e_get1stiface() - Get first interface of host |
|
* |
|
* @szIface = interface string buffer |
|
* @iflen = size of interface buffer |
|
* return: -1 error or 0 ok |
|
*/ |
|
int |
|
e_get1stiface(char *szIface, int iflen) |
|
{ |
|
struct ifaddrs *ifa; |
|
|
|
if (!szIface || !iflen) |
|
return -1; |
|
|
|
getifaddrs(&ifa); |
|
strlcpy(szIface, ifa->ifa_name, iflen); |
|
freeifaddrs(ifa); |
|
return 0; |
|
} |
|
|
|
/* |
|
* e_getifacebyname() - Get interface and make network structure |
|
* |
|
* @psIface = Interface, if =NULL first interface |
|
* @addr = Network address structure |
|
* return: NULL error or !=NULL network structure |
|
*/ |
|
sockaddr_t * |
|
e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr) |
|
{ |
|
char szIface[64] = { [0 ... 63] = 0 }; |
|
struct ifaddrs *p, *ifa = NULL; |
|
|
|
if (!addr) |
|
return NULL; |
|
|
|
memset(addr, 0, sizeof(sockaddr_t)); |
|
getifaddrs(&ifa); |
|
strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface); |
|
for (p = ifa; p && p->ifa_name; p++) |
|
if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && |
|
p->ifa_addr->sa_family == AF_LINK) { |
|
memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl)); |
|
break; |
|
} |
|
freeifaddrs(ifa); |
|
|
|
return addr; |
} |
} |