version 1.10.10.6, 2014/12/11 01:51:24
|
version 1.19.22.1, 2019/01/31 22:19:46
|
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 - 2014 | Copyright 2004 - 2019 |
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" |
|
|
|
|
|
#ifndef __linux__ |
static char hexlist[] = "0123456789abcdef"; |
static char hexlist[] = "0123456789abcdef"; |
|
|
|
#ifndef HAVE_LINK_ADDR |
|
|
|
/* States*/ |
|
#define NAMING 0 |
|
#define GOTONE 1 |
|
#define GOTTWO 2 |
|
#define RESET 3 |
|
/* Inputs */ |
|
#define DIGIT (4*0) |
|
#define END (4*1) |
|
#define DELIM (4*2) |
|
#define LETTER (4*3) |
|
|
|
void |
|
link_addr(const char *addr, struct sockaddr_dl *sdl) |
|
{ |
|
char *cp = sdl->sdl_data; |
|
char *cplim = sdl->sdl_len + (char *)sdl; |
|
int byte = 0, state = NAMING, new = 0; |
|
|
|
bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); |
|
sdl->sdl_family = AF_LINK; |
|
do { |
|
state &= ~LETTER; |
|
if ((*addr >= '0') && (*addr <= '9')) { |
|
new = *addr - '0'; |
|
} else if ((*addr >= 'a') && (*addr <= 'f')) { |
|
new = *addr - 'a' + 10; |
|
} else if ((*addr >= 'A') && (*addr <= 'F')) { |
|
new = *addr - 'A' + 10; |
|
} else if (*addr == 0) { |
|
state |= END; |
|
} else if (state == NAMING && |
|
(((*addr >= 'A') && (*addr <= 'Z')) || |
|
((*addr >= 'a') && (*addr <= 'z')))) |
|
state |= LETTER; |
|
else |
|
state |= DELIM; |
|
addr++; |
|
switch (state /* | INPUT */) { |
|
case NAMING | DIGIT: |
|
case NAMING | LETTER: |
|
*cp++ = addr[-1]; |
|
continue; |
|
case NAMING | DELIM: |
|
state = RESET; |
|
sdl->sdl_nlen = cp - sdl->sdl_data; |
|
continue; |
|
case GOTTWO | DIGIT: |
|
*cp++ = byte; |
|
/* FALLTHROUGH */ |
|
case RESET | DIGIT: |
|
state = GOTONE; |
|
byte = new; |
|
continue; |
|
case GOTONE | DIGIT: |
|
state = GOTTWO; |
|
byte = new + (byte << 4); |
|
continue; |
|
default: /* | DELIM */ |
|
state = RESET; |
|
*cp++ = byte; |
|
byte = 0; |
|
continue; |
|
case GOTONE | END: |
|
case GOTTWO | END: |
|
*cp++ = byte; |
|
/* FALLTHROUGH */ |
|
case RESET | END: |
|
break; |
|
} |
|
break; |
|
} while (cp < cplim); |
|
sdl->sdl_alen = cp - LLADDR(sdl); |
|
new = cp - (char *)sdl; |
|
if (new > sizeof(*sdl)) |
|
sdl->sdl_len = new; |
|
return; |
|
} |
|
#endif |
|
|
|
|
/* |
/* |
* e_link_ntoa() - String ethernet address from link address |
* e_link_ntoa() - String ethernet address from link address |
* |
* |
Line 112 e_link_addr(const char *mac, struct sockaddr_dl * __re
|
Line 195 e_link_addr(const char *mac, struct sockaddr_dl * __re
|
link_addr(mac, sdl); |
link_addr(mac, sdl); |
return 0; |
return 0; |
} |
} |
|
#endif |
|
|
/* |
/* |
* e_ether_ntoa() - Convert ethernet address to string |
* e_ether_ntoa() - Convert ethernet address to string |
Line 129 e_ether_ntoa(const ether_addr_t * __restrict n, char *
|
Line 213 e_ether_ntoa(const ether_addr_t * __restrict n, char *
|
|
|
memset(a, 0, len); |
memset(a, 0, len); |
if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
n->ether_addr_octet[0], n->ether_addr_octet[1], | n->octet[0], n->octet[1], |
n->ether_addr_octet[2], n->ether_addr_octet[3], | n->octet[2], n->octet[3], |
n->ether_addr_octet[4], n->ether_addr_octet[5]) < 17) | n->octet[4], n->octet[5]) < 17) |
return NULL; |
return NULL; |
|
|
return a; |
return a; |
Line 153 e_ether_aton(const char *a, ether_addr_t * __restrict
|
Line 237 e_ether_aton(const char *a, ether_addr_t * __restrict
|
return NULL; |
return NULL; |
|
|
i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
&e->ether_addr_octet[0], | &e->octet[0], |
&e->ether_addr_octet[1], | &e->octet[1], |
&e->ether_addr_octet[2], | &e->octet[2], |
&e->ether_addr_octet[3], | &e->octet[3], |
&e->ether_addr_octet[4], | &e->octet[4], |
&e->ether_addr_octet[5]); | &e->octet[5]); |
if (i != 6) |
if (i != 6) |
return NULL; |
return NULL; |
|
|
Line 201 e_n2port(sockaddr_t * __restrict addr)
|
Line 285 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 *s, str[INET6_ADDRSTRLEN] = { 0 }; | #ifndef __linux__ |
| char *s; |
| #endif |
| char str[INET6_ADDRSTRLEN] = { 0 }; |
const char *ret = NULL; |
const char *ret = NULL; |
|
|
if (!addr || !val) |
if (!addr || !val) |
Line 226 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
Line 313 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; |
|
#ifndef __linux__ |
case AF_LINK: |
case AF_LINK: |
if (!(s = e_link_ntoa(&addr->sdl))) { |
if (!(s = e_link_ntoa(&addr->sdl))) { |
LOGERR; |
LOGERR; |
Line 233 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
Line 321 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
} else |
} else |
ret = s; |
ret = s; |
break; |
break; |
|
#endif |
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 249 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
Line 338 e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __r
|
* @psHost = Hostname |
* @psHost = Hostname |
* @port = Port |
* @port = Port |
* @addr = Network address structure |
* @addr = Network address structure |
* return: NULL error or !=NULL network structure | * return: 0 is error or >0 length of network structure |
*/ |
*/ |
sockaddr_t * | socklen_t |
e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr) |
e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr) |
{ |
{ |
struct hostent *host = NULL; |
struct hostent *host = NULL; |
|
|
if (!psHost || !addr) |
if (!psHost || !addr) |
return NULL; | return 0; |
|
|
if (*psHost != '/') { |
if (*psHost != '/') { |
/* resolver */ |
/* resolver */ |
host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); |
host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6); |
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 0; |
} else { |
} else { |
memset(addr, 0, sizeof(sockaddr_t)); |
memset(addr, 0, sizeof(sockaddr_t)); |
addr->sa.sa_family = host->h_addrtype; |
addr->sa.sa_family = host->h_addrtype; |
Line 277 e_gethostbyname(const char *psHost, u_short port, sock
|
Line 366 e_gethostbyname(const char *psHost, u_short port, sock
|
|
|
switch (addr->sa.sa_family) { |
switch (addr->sa.sa_family) { |
case AF_INET: |
case AF_INET: |
|
#ifndef __linux__ |
addr->sin.sin_len = sizeof(struct sockaddr_in); |
addr->sin.sin_len = sizeof(struct sockaddr_in); |
|
#endif |
addr->sin.sin_family = AF_INET; |
addr->sin.sin_family = AF_INET; |
addr->sin.sin_port = htons(port); |
addr->sin.sin_port = htons(port); |
memcpy(&addr->sin.sin_addr, host->h_addr, sizeof addr->sin.sin_addr); |
memcpy(&addr->sin.sin_addr, host->h_addr, sizeof addr->sin.sin_addr); |
return addr; | return sizeof addr->sin; |
case AF_INET6: |
case AF_INET6: |
|
#ifndef __linux__ |
addr->sin6.sin6_len = sizeof(struct sockaddr_in6); |
addr->sin6.sin6_len = sizeof(struct sockaddr_in6); |
|
#endif |
addr->sin6.sin6_family = AF_INET6; |
addr->sin6.sin6_family = AF_INET6; |
addr->sin6.sin6_port = htons(port); |
addr->sin6.sin6_port = htons(port); |
memcpy(&addr->sin6.sin6_addr, host->h_addr, sizeof addr->sin6.sin6_addr); |
memcpy(&addr->sin6.sin6_addr, host->h_addr, sizeof addr->sin6.sin6_addr); |
return addr; | return sizeof addr->sin6; |
case AF_LOCAL: |
case AF_LOCAL: |
|
#ifndef __linux__ |
addr->sun.sun_len = sizeof(struct sockaddr_un); |
addr->sun.sun_len = sizeof(struct sockaddr_un); |
|
#endif |
addr->sun.sun_family = AF_LOCAL; |
addr->sun.sun_family = AF_LOCAL; |
memset(addr->sun.sun_path, 0, sizeof addr->sun.sun_path); |
memset(addr->sun.sun_path, 0, sizeof addr->sun.sun_path); |
snprintf(addr->sun.sun_path, sizeof addr->sun.sun_path, "%s-%hu", psHost, port); |
snprintf(addr->sun.sun_path, sizeof addr->sun.sun_path, "%s-%hu", psHost, port); |
return addr; | return sizeof addr->sun; |
default: |
default: |
elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); |
elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family); |
break; |
break; |
} |
} |
|
|
return NULL; | return 0; |
} |
} |
|
|
/* |
/* |
|
* e_addrlen() - Get address length from network structure |
|
* |
|
* @addr = address |
|
* return: 0 is error or >0 length of network structure |
|
*/ |
|
socklen_t |
|
e_addrlen(const sockaddr_t *addr) |
|
{ |
|
if (!addr) |
|
return 0; |
|
|
|
switch (addr->sa.sa_family) { |
|
case AF_INET: |
|
return sizeof addr->sin; |
|
case AF_INET6: |
|
return sizeof addr->sin6; |
|
case AF_LOCAL: |
|
return sizeof addr->sun; |
|
#ifndef __linux__ |
|
case AF_LINK: |
|
return sizeof addr->sdl; |
|
#endif |
|
} |
|
|
|
return E_SOCKADDR_MAX; |
|
} |
|
|
|
/* |
* e_addrcmp() - Compare network addresses |
* e_addrcmp() - Compare network addresses |
* |
* |
* @a = 1st address |
* @a = 1st address |
Line 329 e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __re
|
Line 452 e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __re
|
else |
else |
return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, |
return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, |
sizeof a->sin6.sin6_addr); |
sizeof a->sin6.sin6_addr); |
|
#ifndef __linux__ |
case AF_LINK: |
case AF_LINK: |
return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data, |
return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data, |
sizeof a->sdl.sdl_data); |
sizeof a->sdl.sdl_data); |
|
#endif |
|
case AF_UNSPEC: |
|
return memcmp(a, b, sizeof(sockaddr_t)); |
} |
} |
|
|
return (int) !!(a - b); |
return (int) !!(a - b); |
Line 371 e_innet(netaddr_t * __restrict net, inaddr_t * __restr
|
Line 498 e_innet(netaddr_t * __restrict net, inaddr_t * __restr
|
case AF_INET: |
case AF_INET: |
for (i = 0; i < sizeof(struct in_addr); i++) { |
for (i = 0; i < sizeof(struct in_addr); i++) { |
ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & |
ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] & |
net->mask.in.s4_addr[i]; | net->mask.in4.s4_addr[i]; |
ret -= addr->in.s4_addr[i] & net->mask.in.s4_addr[i]; | ret -= addr->in4.s4_addr[i] & net->mask.in4.s4_addr[i]; |
if (ret) |
if (ret) |
break; |
break; |
} |
} |
Line 431 e_getnet(const char *net)
|
Line 558 e_getnet(const char *net)
|
} |
} |
switch (host->h_addrtype) { |
switch (host->h_addrtype) { |
case AF_INET: |
case AF_INET: |
|
#ifndef __linux__ |
n->addr.sin.sin_len = sizeof(struct sockaddr_in); |
n->addr.sin.sin_len = sizeof(struct sockaddr_in); |
|
#endif |
n->addr.sin.sin_family = host->h_addrtype; |
n->addr.sin.sin_family = host->h_addrtype; |
memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr); |
memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr); |
if (wrk) | if (wrk && strtol(wrk, NULL, 10) != 32) |
n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10)); |
n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10)); |
else |
else |
n->mask.in.s_addr = 0xFFFFFFFF; |
n->mask.in.s_addr = 0xFFFFFFFF; |
break; |
break; |
case AF_INET6: |
case AF_INET6: |
|
#ifndef __linux__ |
n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6); |
n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6); |
|
#endif |
n->addr.sin6.sin6_family = host->h_addrtype; |
n->addr.sin6.sin6_family = host->h_addrtype; |
memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr); |
memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr); |
/* TODO: should support ipv6 mask */ |
/* TODO: should support ipv6 mask */ |
Line 460 e_getnet(const char *net)
|
Line 591 e_getnet(const char *net)
|
* e_ether_addr() - Get or set ethernet address from interface name |
* e_ether_addr() - Get or set ethernet address from interface name |
* |
* |
* @ifname = interface name |
* @ifname = interface name |
* @addr = if addr is !=NULL then set this for new address | * @addr = if addr is !=NULL then set new ethernet address |
* return: NULL error or !=NULL get ethernet address should be e_free() | * return: NULL error or !=NULL get current ethernet address should be e_free() |
*/ |
*/ |
ether_addr_t * |
ether_addr_t * |
e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) |
e_ether_addr(const char *ifname, ether_addr_t * __restrict addr) |
{ |
{ |
ether_addr_t *a = NULL; |
ether_addr_t *a = NULL; |
struct ifaddrs *p, *ifa = NULL; |
struct ifaddrs *p, *ifa = NULL; |
|
struct ifreq req; |
|
int s; |
|
sockaddr_t sa = E_SOCKADDR_INIT; |
|
|
|
memset(&req, 0, sizeof req); |
if (!ifname) |
if (!ifname) |
return NULL; |
return NULL; |
|
|
a = e_malloc(sizeof(ether_addr_t)); |
|
if (!a) |
|
return NULL; |
|
else |
|
memset(a, 0, sizeof(ether_addr_t)); |
|
|
|
getifaddrs(&ifa); |
getifaddrs(&ifa); |
for (p = ifa; p && p->ifa_name; p++) | for (p = ifa; p && p->ifa_name; p = p->ifa_next) { |
| #ifndef __linux__ |
if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && |
if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && |
p->ifa_addr->sa_family == AF_LINK) { |
p->ifa_addr->sa_family == AF_LINK) { |
memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t)); | a = e_malloc(sizeof(ether_addr_t)); |
| if (a) |
| memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), |
| sizeof(ether_addr_t)); |
| |
| /* should set mac address */ |
| if (addr && (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) > 0) { |
| strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); |
| sa.sa.sa_family = AF_LINK; |
| sa.sa.sa_len = sizeof(ether_addr_t); |
| memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t)); |
| req.ifr_ifru.ifru_addr = sa.sa; |
| ioctl(s, SIOCSIFLLADDR, &req); |
| close(s); |
| } |
break; |
break; |
} |
} |
|
#else |
|
if (p->ifa_name && !strcmp(p->ifa_name, ifname)) { |
|
s = socket(AF_INET, SOCK_DGRAM, 0); |
|
if (s == -1) |
|
break; |
|
strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); |
|
if (!ioctl(s, SIOCGIFHWADDR, &req)) { |
|
a = e_malloc(sizeof(ether_addr_t)); |
|
if (a) |
|
memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t)); |
|
|
|
/* should set mac address */ |
|
if (addr) { |
|
memset(&req, 0, sizeof req); |
|
strlcpy(req.ifr_name, ifname, sizeof req.ifr_name); |
|
sa.sa.sa_family = ARPHRD_ETHER; |
|
memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t)); |
|
req.ifr_hwaddr = sa.sa; |
|
ioctl(s, SIOCSIFHWADDR, &req); |
|
} |
|
} |
|
close(s); |
|
break; |
|
} |
|
#endif |
|
} |
freeifaddrs(ifa); |
freeifaddrs(ifa); |
|
|
return a; |
return a; |
Line 511 e_get1stiface(char *szIface, int iflen)
|
Line 681 e_get1stiface(char *szIface, int iflen)
|
return 0; |
return 0; |
} |
} |
|
|
|
#ifndef __linux__ |
/* |
/* |
* e_getifacebyname() - Get interface and make network structure |
* e_getifacebyname() - Get interface and make network structure |
* |
* |
Line 530 e_getifacebyname(const char *psIface, sockaddr_t * __r
|
Line 701 e_getifacebyname(const char *psIface, sockaddr_t * __r
|
memset(addr, 0, sizeof(sockaddr_t)); |
memset(addr, 0, sizeof(sockaddr_t)); |
getifaddrs(&ifa); |
getifaddrs(&ifa); |
strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface); |
strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface); |
for (p = ifa; p && p->ifa_name; p++) | for (p = ifa; p && p->ifa_name; p = p->ifa_next) |
if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && |
if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && |
p->ifa_addr->sa_family == AF_LINK) { |
p->ifa_addr->sa_family == AF_LINK) { |
memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl)); |
memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl)); |
Line 571 e_getlinkbyname(const char *psHost, sockaddr_t * __res
|
Line 742 e_getlinkbyname(const char *psHost, sockaddr_t * __res
|
AIT_FREE_VAL(&v); |
AIT_FREE_VAL(&v); |
return a; |
return a; |
} |
} |
|
|
|
/* |
|
* e_getlinkbyether() - Get ethernet address and make network structure |
|
* |
|
* @mac = Ethernet address |
|
* @idx = Interface index |
|
* @type = Interface type |
|
* @iface = Interface name |
|
* @addr = Network address structure |
|
* return: NULL error or !=NULL network structure |
|
*/ |
|
sockaddr_t * |
|
e_getlinkbyether(const ether_addr_t * __restrict mac, u_short idx, u_char type, |
|
const char *iface, sockaddr_t * __restrict addr) |
|
{ |
|
sockaddr_t *a = addr; |
|
|
|
if (!addr) |
|
return NULL; |
|
else |
|
memset(addr, 0, sizeof(sockaddr_t)); |
|
|
|
addr->sdl.sdl_len = sizeof(struct sockaddr_dl); |
|
addr->sdl.sdl_family = AF_LINK; |
|
addr->sdl.sdl_index = idx; |
|
addr->sdl.sdl_type = type; |
|
if (iface && *iface) { |
|
addr->sdl.sdl_nlen = strlen(iface); |
|
memcpy(addr->sdl.sdl_data, iface, addr->sdl.sdl_nlen); |
|
} |
|
addr->sdl.sdl_alen = sizeof(ether_addr_t); |
|
memcpy(LLADDR(&addr->sdl), mac, addr->sdl.sdl_alen); |
|
|
|
return a; |
|
} |
|
#endif |