Annotation of libelwix/src/net.c, revision 1.12.4.3
1.1 misho 1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.12.4.3! misho 6: * $Id: net.c,v 1.12.4.2 2016/05/14 13:12:43 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.12.4.2 misho 15: Copyright 2004 - 2016
1.1 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
1.12.4.1 misho 49: #ifndef __linux__
1.6 misho 50: static char hexlist[] = "0123456789abcdef";
51:
52: /*
53: * e_link_ntoa() - String ethernet address from link address
54: *
55: * @sdl = link address
56: * return: =NULL error or !=NULL ethernet address, should be e_free()
57: */
58: char *
59: e_link_ntoa(const struct sockaddr_dl *sdl)
60: {
61: static char obuf[64];
62: char *out = obuf;
63: int i;
64: u_char *in = (u_char*) LLADDR(sdl);
65: u_char *inlim = in + sdl->sdl_alen;
66: int firsttime = 1;
67:
68: if (sdl->sdl_nlen) {
69: memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen);
70: out += sdl->sdl_nlen;
71: if (sdl->sdl_alen)
72: *out++ = '!';
73: }
74:
75: while (in < inlim) {
76: if (firsttime)
77: firsttime ^= firsttime;
78: else
79: *out++ = ':';
1.8 misho 80:
1.6 misho 81: i = *in++;
82: if (i > 0xf) {
83: out[1] = hexlist[i & 0xf];
84: i >>= 4;
1.8 misho 85: } else {
86: out[1] = hexlist[i];
87: i = 0;
88: }
1.6 misho 89:
90: out[0] = hexlist[i];
91: out += 2;
92: }
93:
94: *out = 0;
95: return obuf;
96: }
97:
1.1 misho 98: /*
1.11 misho 99: * e_link_addr() - String ethernet address to link address
100: *
101: * @mac = ethernet address
102: * @sdl = link address
103: * return: -1 error or 0 ok
104: */
105: int
106: e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl)
107: {
108: if (!mac || !sdl)
109: return -1;
110: if (!sdl->sdl_len)
111: sdl->sdl_len = sizeof(struct sockaddr_dl);
112:
113: link_addr(mac, sdl);
114: return 0;
115: }
1.12.4.1 misho 116: #endif
1.11 misho 117:
118: /*
1.1 misho 119: * e_ether_ntoa() - Convert ethernet address to string
120: *
121: * @n = ethernet address structure, like struct ether_addr
122: * @a = string
123: * @len = string length
124: * return: NULL error or !=NULL string a
125: */
1.2 misho 126: char *
1.7 misho 127: e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len)
1.1 misho 128: {
129: if (!n || !a)
130: return NULL;
131:
132: memset(a, 0, len);
1.8 misho 133: if (snprintf(a, len, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1.11 misho 134: n->octet[0], n->octet[1],
135: n->octet[2], n->octet[3],
136: n->octet[4], n->octet[5]) < 17)
1.1 misho 137: return NULL;
138:
139: return a;
140: }
141:
142: /*
143: * e_ether_aton() - Convert string to ethernet address
144: *
145: * @a = string
146: * @e = ethernet address structure, like struct ether_addr
147: * return: NULL error or !=NULL ethernet address structure
148: */
1.7 misho 149: ether_addr_t *
150: e_ether_aton(const char *a, ether_addr_t * __restrict e)
1.1 misho 151: {
152: int i;
153:
154: if (!a || !e)
155: return NULL;
156:
1.8 misho 157: i = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
1.11 misho 158: &e->octet[0],
159: &e->octet[1],
160: &e->octet[2],
161: &e->octet[3],
162: &e->octet[4],
163: &e->octet[5]);
1.1 misho 164: if (i != 6)
165: return NULL;
166:
167: return e;
168: }
169:
170: /*
171: * e_n2port() - Extract port from network structure
172: *
173: * @addr = Address
174: * return: 0 not supported family type or port number
175: */
1.2 misho 176: u_short
1.1 misho 177: e_n2port(sockaddr_t * __restrict addr)
178: {
179: u_short port = 0;
180:
181: if (!addr)
182: return port;
183:
184: switch (addr->sa.sa_family) {
185: case AF_INET:
186: return ntohs(addr->sin.sin_port);
187: case AF_INET6:
188: return ntohs(addr->sin6.sin6_port);
189: default:
190: break;
191: }
192:
193: return port;
194: }
195:
196: /*
197: * e_n2addr() - Extract address from network structure
198: *
199: * @addr = Address
200: * @val = Value for store string address
201: * return: NULL error or !=NULL string address from val
202: */
203: const char *
204: e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val)
205: {
1.12.4.1 misho 206: #ifndef __linux__
207: char *s;
208: #endif
209: char str[INET6_ADDRSTRLEN] = { 0 };
1.1 misho 210: const char *ret = NULL;
211:
212: if (!addr || !val)
213: return ret;
214:
215: AIT_INIT_VAL(val);
216: switch (addr->sa.sa_family) {
217: case AF_INET:
218: if (!inet_ntop(AF_INET, &addr->sin.sin_addr, str, INET_ADDRSTRLEN)) {
219: LOGERR;
220: return ret;
221: } else
222: ret = str;
223: break;
224: case AF_INET6:
225: if (!inet_ntop(AF_INET6, &addr->sin6.sin6_addr, str, INET6_ADDRSTRLEN)) {
226: LOGERR;
227: return ret;
228: } else
229: ret = str;
230: break;
231: case AF_LOCAL:
232: ret = addr->sun.sun_path;
233: break;
1.12.4.1 misho 234: #ifndef __linux__
1.5 misho 235: case AF_LINK:
1.6 misho 236: if (!(s = e_link_ntoa(&addr->sdl))) {
1.5 misho 237: LOGERR;
238: return ret;
239: } else
240: ret = s;
241: break;
1.12.4.1 misho 242: #endif
1.1 misho 243: default:
244: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d",
245: addr->sa.sa_family);
246: return ret;
247: }
248:
249: AIT_SET_STR(val, ret);
250: return (const char*) AIT_GET_STR(val);
251: }
252:
253: /*
254: * e_gethostbyname() - Get host and port and make network structure
255: *
256: * @psHost = Hostname
257: * @port = Port
258: * @addr = Network address structure
259: * return: NULL error or !=NULL network structure
260: */
261: sockaddr_t *
262: e_gethostbyname(const char *psHost, u_short port, sockaddr_t * __restrict addr)
263: {
264: struct hostent *host = NULL;
265:
266: if (!psHost || !addr)
267: return NULL;
268:
269: if (*psHost != '/') {
270: /* resolver */
1.10 misho 271: host = gethostbyname2(psHost, !strchr(psHost, ':') ? AF_INET : AF_INET6);
1.1 misho 272: if (!host) {
273: elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno));
274: return NULL;
275: } else {
276: memset(addr, 0, sizeof(sockaddr_t));
277: addr->sa.sa_family = host->h_addrtype;
278: }
279: } else {
280: memset(addr, 0, sizeof(sockaddr_t));
281: addr->sa.sa_family = AF_LOCAL;
282: }
283:
284:
285: switch (addr->sa.sa_family) {
286: case AF_INET:
1.12.4.1 misho 287: #ifndef __linux__
1.1 misho 288: addr->sin.sin_len = sizeof(struct sockaddr_in);
1.12.4.1 misho 289: #endif
1.1 misho 290: addr->sin.sin_family = AF_INET;
291: addr->sin.sin_port = htons(port);
292: memcpy(&addr->sin.sin_addr, host->h_addr, sizeof addr->sin.sin_addr);
293: return addr;
294: case AF_INET6:
1.12.4.1 misho 295: #ifndef __linux__
1.1 misho 296: addr->sin6.sin6_len = sizeof(struct sockaddr_in6);
1.12.4.1 misho 297: #endif
1.1 misho 298: addr->sin6.sin6_family = AF_INET6;
299: addr->sin6.sin6_port = htons(port);
300: memcpy(&addr->sin6.sin6_addr, host->h_addr, sizeof addr->sin6.sin6_addr);
301: return addr;
302: case AF_LOCAL:
1.12.4.1 misho 303: #ifndef __linux__
1.1 misho 304: addr->sun.sun_len = sizeof(struct sockaddr_un);
1.12.4.1 misho 305: #endif
1.1 misho 306: addr->sun.sun_family = AF_LOCAL;
307: memset(addr->sun.sun_path, 0, sizeof addr->sun.sun_path);
308: snprintf(addr->sun.sun_path, sizeof addr->sun.sun_path, "%s-%hu", psHost, port);
309: return addr;
310: default:
311: elwix_SetErr(EPROTONOSUPPORT, "Unsuported address family %d", addr->sa.sa_family);
312: break;
313: }
314:
315: return NULL;
316: }
317:
318: /*
319: * e_addrcmp() - Compare network addresses
320: *
321: * @a = 1st address
322: * @b = 2nd address
323: * @p = compare and ports, if family is AF_INET or AF_INET6
324: * return: 0 is equal or !=0 is different
325: */
326: int
327: e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p)
328: {
329: if (a && b && a->sa.sa_family == b->sa.sa_family)
330: switch (a->sa.sa_family) {
331: case AF_LOCAL:
332: return strcmp(a->sun.sun_path, b->sun.sun_path);
333: case AF_INET:
334: if (p && (a->sin.sin_port - b->sin.sin_port))
335: return (int) !!(a->sin.sin_port - b->sin.sin_port);
336: else
337: return memcmp(&a->sin.sin_addr, &b->sin.sin_addr,
338: sizeof a->sin.sin_addr);
339: case AF_INET6:
340: if (p && (a->sin6.sin6_port - b->sin6.sin6_port))
341: return (int) !!(a->sin6.sin6_port - b->sin6.sin6_port);
342: else
343: return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr,
344: sizeof a->sin6.sin6_addr);
1.12.4.1 misho 345: #ifndef __linux__
1.1 misho 346: case AF_LINK:
347: return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data,
348: sizeof a->sdl.sdl_data);
1.12.4.1 misho 349: #endif
1.11 misho 350: case AF_UNSPEC:
351: return memcmp(a, b, sizeof(sockaddr_t));
1.1 misho 352: }
353:
354: return (int) !!(a - b);
355: }
356:
357: /*
358: * e_usleep() - usleep() replacement for ELWIX
359: *
360: * @usec = microseconds for sleep
361: * return: -1 interrupted by signal or 0 ok
362: */
1.2 misho 363: int
1.1 misho 364: e_usleep(u_int usec)
365: {
366: struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) };
367:
368: return select(0, NULL, NULL, NULL, &tv);
369: }
1.3 misho 370:
371: /*
372: * e_innet() - Test address match in network
373: *
374: * @net = network
375: * @addr = address
376: * return: -1 error, 0 match or 1 not match
377: */
378: int
379: e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr)
380: {
381: register int i;
382: int ret = 0;
383:
384: if (!net || !addr)
385: return -1;
386:
387: switch (net->addr.sa.sa_family) {
388: case AF_INET:
389: for (i = 0; i < sizeof(struct in_addr); i++) {
390: ret = ((caddr_t) &net->addr.sin.sin_addr.s_addr)[i] &
391: net->mask.in.s4_addr[i];
392: ret -= addr->in.s4_addr[i] & net->mask.in.s4_addr[i];
393: if (ret)
394: break;
395: }
396: break;
397: case AF_INET6:
398: for (i = 0; i < sizeof(struct in6_addr); i++) {
399: ret = net->addr.sin6.sin6_addr.s6_addr[i] &
400: net->mask.in6.s6_addr[i];
401: ret -= addr->in6.s6_addr[i] & net->mask.in6.s6_addr[i];
402: if (ret)
403: break;
404: }
405: break;
406: default:
407: return -1;
408: }
409:
410: return !!ret;
411: }
1.4 misho 412:
413: /*
414: * e_getnet() - Get network from string
415: *
416: * @net = Network string (format: <net[/cidr]>)
417: * return: NULL error or !=NULL network should be e_free()
418: */
419: netaddr_t *
420: e_getnet(const char *net)
421: {
422: netaddr_t *n;
423: char *str, *wrk;
424: struct hostent *host;
425:
426: n = e_malloc(sizeof(netaddr_t));
427: if (!n) {
428: LOGERR;
429: return NULL;
430: } else
431: memset(n, 0, sizeof(netaddr_t));
432: str = e_strdup(net);
433: if (!str) {
434: LOGERR;
435: e_free(n);
436: return NULL;
437: }
438: wrk = strchr(str, '/');
439: if (wrk)
440: *wrk++ = 0;
441:
442: host = gethostbyname2(str, strchr(str, ':') ? AF_INET6 : AF_INET);
443: if (!host) {
444: elwix_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno));
445: e_free(str);
446: e_free(n);
447: return NULL;
448: }
449: switch (host->h_addrtype) {
450: case AF_INET:
1.12.4.1 misho 451: #ifndef __linux__
1.4 misho 452: n->addr.sin.sin_len = sizeof(struct sockaddr_in);
1.12.4.1 misho 453: #endif
1.4 misho 454: n->addr.sin.sin_family = host->h_addrtype;
455: memcpy(&n->addr.sin.sin_addr, host->h_addr, sizeof n->addr.sin.sin_addr);
456: if (wrk)
457: n->mask.in.s_addr = E_CIDRMASK(strtol(wrk, NULL, 10));
458: else
459: n->mask.in.s_addr = 0xFFFFFFFF;
460: break;
461: case AF_INET6:
1.12.4.1 misho 462: #ifndef __linux__
1.4 misho 463: n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
1.12.4.1 misho 464: #endif
1.4 misho 465: n->addr.sin6.sin6_family = host->h_addrtype;
466: memcpy(&n->addr.sin6.sin6_addr, host->h_addr, sizeof n->addr.sin6.sin6_addr);
467: /* TODO: should support ipv6 mask */
468: break;
469: default:
470: elwix_SetErr(EINVAL, "Unsupported family #%d", host->h_addrtype);
471: e_free(str);
472: e_free(n);
473: return NULL;
474: }
475:
476: e_free(str);
477: return n;
478: }
1.7 misho 479:
480: /*
481: * e_ether_addr() - Get or set ethernet address from interface name
482: *
483: * @ifname = interface name
484: * @addr = if addr is !=NULL then set this for new address
1.11 misho 485: * return: NULL error or !=NULL get ethernet address should be e_free()
1.7 misho 486: */
487: ether_addr_t *
488: e_ether_addr(const char *ifname, ether_addr_t * __restrict addr)
489: {
490: ether_addr_t *a = NULL;
491: struct ifaddrs *p, *ifa = NULL;
1.12.4.1 misho 492: #ifdef __linux__
493: int s;
494: struct ifreq req;
495:
496: memset(&req, 0, sizeof req);
497: #endif
1.7 misho 498:
499: if (!ifname)
500: return NULL;
501:
502: getifaddrs(&ifa);
1.12.4.2 misho 503: for (p = ifa; p && p->ifa_name; p = p->ifa_next) {
1.12.4.1 misho 504: #ifndef __linux__
1.7 misho 505: if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr &&
506: p->ifa_addr->sa_family == AF_LINK) {
1.12.4.3! misho 507: a = e_malloc(sizeof(ether_addr_t));
! 508: if (a)
! 509: memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), sizeof(ether_addr_t));
1.7 misho 510: break;
511: }
1.12.4.1 misho 512: #else
513: if (p->ifa_name && !strcmp(p->ifa_name, ifname)) {
514: s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1.12.4.3! misho 515: if (s == -1)
1.12.4.1 misho 516: break;
1.12.4.3! misho 517: strlcpy(req.ifr_name, ifname, sizeof req.ifr_name);
! 518: if (!ioctl(s, SIOCGIFHWADDR, &req)) {
! 519: a = e_malloc(sizeof(ether_addr_t));
! 520: if (a)
! 521: memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t));
! 522: }
1.12.4.1 misho 523: break;
524: }
525: #endif
1.12.4.2 misho 526: }
1.7 misho 527: freeifaddrs(ifa);
528:
529: return a;
530: }
1.11 misho 531:
532: /*
533: * e_get1stiface() - Get first interface of host
534: *
535: * @szIface = interface string buffer
536: * @iflen = size of interface buffer
537: * return: -1 error or 0 ok
538: */
539: int
540: e_get1stiface(char *szIface, int iflen)
541: {
542: struct ifaddrs *ifa;
543:
544: if (!szIface || !iflen)
545: return -1;
546:
547: getifaddrs(&ifa);
548: strlcpy(szIface, ifa->ifa_name, iflen);
549: freeifaddrs(ifa);
550: return 0;
551: }
552:
1.12.4.2 misho 553: #ifndef __linux__
1.11 misho 554: /*
555: * e_getifacebyname() - Get interface and make network structure
556: *
557: * @psIface = Interface, if =NULL first interface
558: * @addr = Network address structure
559: * return: NULL error or !=NULL network structure
560: */
561: sockaddr_t *
562: e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr)
563: {
564: char szIface[64] = { [0 ... 63] = 0 };
565: struct ifaddrs *p, *ifa = NULL;
566:
567: if (!addr)
568: return NULL;
569:
570: memset(addr, 0, sizeof(sockaddr_t));
571: getifaddrs(&ifa);
572: strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface);
1.12.4.2 misho 573: for (p = ifa; p && p->ifa_name; p = p->ifa_next)
1.11 misho 574: if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr &&
575: p->ifa_addr->sa_family == AF_LINK) {
576: memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl));
577: break;
578: }
579: freeifaddrs(ifa);
580:
581: return addr;
582: }
583:
584: /*
585: * e_getlinkbyname() - Get host ethernet address and make network structure
586: *
587: * @psHost = Host ethernet address
588: * @addr = Network address structure
589: * return: NULL error or !=NULL network structure
590: */
591: sockaddr_t *
592: e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr)
593: {
594: ait_val_t v;
595: sockaddr_t *a = addr;
596:
597: if (!psHost || !addr)
598: return NULL;
599: else
600: memset(addr, 0, sizeof(sockaddr_t));
601:
602: AIT_INIT_VAL2(&v, string);
603: if (!strchr(psHost, '.'))
604: AIT_SET_STR(&v, ":");
605: AIT_SET_STRCAT(&v, psHost);
606:
607: addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
608: if (e_link_addr(AIT_GET_STR(&v), &addr->sdl))
609: a = NULL;
610:
611: AIT_FREE_VAL(&v);
612: return a;
613: }
614:
615: /*
616: * e_getlinkbyether() - Get ethernet address and make network structure
617: *
618: * @mac = Ethernet address
619: * @idx = Interface index
620: * @type = Interface type
621: * @iface = Interface name
622: * @addr = Network address structure
623: * return: NULL error or !=NULL network structure
624: */
625: sockaddr_t *
626: e_getlinkbyether(const ether_addr_t * __restrict mac, u_short idx, u_char type,
627: const char *iface, sockaddr_t * __restrict addr)
628: {
629: sockaddr_t *a = addr;
630:
631: if (!addr)
632: return NULL;
633: else
634: memset(addr, 0, sizeof(sockaddr_t));
635:
636: addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
637: addr->sdl.sdl_family = AF_LINK;
638: addr->sdl.sdl_index = idx;
639: addr->sdl.sdl_type = type;
640: if (iface && *iface) {
641: addr->sdl.sdl_nlen = strlen(iface);
642: memcpy(addr->sdl.sdl_data, iface, addr->sdl.sdl_nlen);
643: }
644: addr->sdl.sdl_alen = sizeof(ether_addr_t);
645: memcpy(LLADDR(&addr->sdl), mac, addr->sdl.sdl_alen);
646:
647: return a;
648: }
1.12.4.2 misho 649: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>