Annotation of libelwix/src/net.c, revision 1.13

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.13    ! misho       6: * $Id: net.c,v 1.12.4.6 2016/05/18 12:38:16 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.13    ! 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.13    ! 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.13    ! 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.13    ! 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.13    ! 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.13    ! 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.13    ! misho     287: #ifndef __linux__
1.1       misho     288:                        addr->sin.sin_len = sizeof(struct sockaddr_in);
1.13    ! 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.13    ! misho     295: #ifndef __linux__
1.1       misho     296:                        addr->sin6.sin6_len = sizeof(struct sockaddr_in6);
1.13    ! 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.13    ! misho     303: #ifndef __linux__
1.1       misho     304:                        addr->sun.sun_len = sizeof(struct sockaddr_un);
1.13    ! 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.13    ! 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.13    ! 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.13    ! misho     451: #ifndef __linux__
1.4       misho     452:                        n->addr.sin.sin_len = sizeof(struct sockaddr_in);
1.13    ! 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.13    ! misho     462: #ifndef __linux__
1.4       misho     463:                        n->addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
1.13    ! 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
1.13    ! misho     484:  * @addr = if addr is !=NULL then set new ethernet address
        !           485:  * return: NULL error or !=NULL get current 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.13    ! misho     492:        struct ifreq req;
        !           493:        int s;
        !           494:        sockaddr_t sa = E_SOCKADDR_INIT;
1.7       misho     495: 
1.13    ! misho     496:        memset(&req, 0, sizeof req);
1.7       misho     497:        if (!ifname)
                    498:                return NULL;
                    499: 
                    500:        getifaddrs(&ifa);
1.13    ! misho     501:        for (p = ifa; p && p->ifa_name; p = p->ifa_next) {
        !           502: #ifndef __linux__
1.7       misho     503:                if (p->ifa_name && !strcmp(p->ifa_name, ifname) && p->ifa_addr && 
                    504:                                p->ifa_addr->sa_family == AF_LINK) {
1.13    ! misho     505:                        a = e_malloc(sizeof(ether_addr_t));
        !           506:                        if (a)
        !           507:                                memcpy(a, LLADDR((struct sockaddr_dl*) p->ifa_addr), 
        !           508:                                                sizeof(ether_addr_t));
        !           509: 
        !           510:                        /* should set mac address */
        !           511:                        if (addr && (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) > 0) {
        !           512:                                strlcpy(req.ifr_name, ifname, sizeof req.ifr_name);
        !           513:                                sa.sa.sa_family = AF_LINK;
        !           514:                                sa.sa.sa_len = sizeof(ether_addr_t);
        !           515:                                memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t));
        !           516:                                req.ifr_ifru.ifru_addr = sa.sa;
        !           517:                                ioctl(s, SIOCSIFLLADDR, &req);
        !           518:                                close(s);
        !           519:                        }
1.7       misho     520:                        break;
                    521:                }
1.13    ! misho     522: #else
        !           523:                if (p->ifa_name && !strcmp(p->ifa_name, ifname)) {
        !           524:                        s = socket(AF_INET, SOCK_DGRAM, 0);
        !           525:                        if (s == -1)
        !           526:                                break;
        !           527:                        strlcpy(req.ifr_name, ifname, sizeof req.ifr_name);
        !           528:                        if (!ioctl(s, SIOCGIFHWADDR, &req)) {
        !           529:                                a = e_malloc(sizeof(ether_addr_t));
        !           530:                                if (a)
        !           531:                                        memcpy(a, req.ifr_addr.sa_data, sizeof(ether_addr_t));
        !           532: 
        !           533:                                /* should set mac address */
        !           534:                                if (addr) {
        !           535:                                        memset(&req, 0, sizeof req);
        !           536:                                        strlcpy(req.ifr_name, ifname, sizeof req.ifr_name);
        !           537:                                        sa.sa.sa_family = ARPHRD_ETHER;
        !           538:                                        memcpy(sa.sa.sa_data, addr, sizeof(ether_addr_t));
        !           539:                                        req.ifr_hwaddr = sa.sa;
        !           540:                                        ioctl(s, SIOCSIFHWADDR, &req);
        !           541:                                }
        !           542:                        }
        !           543:                        close(s);
        !           544:                        break;
        !           545:                }
        !           546: #endif
        !           547:        }
1.7       misho     548:        freeifaddrs(ifa);
                    549: 
                    550:        return a;
                    551: }
1.11      misho     552: 
                    553: /*
                    554:  * e_get1stiface() - Get first interface of host
                    555:  *
                    556:  * @szIface = interface string buffer
                    557:  * @iflen = size of interface buffer
                    558:  * return: -1 error or 0 ok
                    559:  */
                    560: int
                    561: e_get1stiface(char *szIface, int iflen)
                    562: {
                    563:        struct ifaddrs *ifa;
                    564: 
                    565:        if (!szIface || !iflen)
                    566:                return -1;
                    567: 
                    568:        getifaddrs(&ifa);
                    569:        strlcpy(szIface, ifa->ifa_name, iflen);
                    570:        freeifaddrs(ifa);
                    571:        return 0;
                    572: }
                    573: 
1.13    ! misho     574: #ifndef __linux__
1.11      misho     575: /*
                    576:  * e_getifacebyname() - Get interface and make network structure
                    577:  *
                    578:  * @psIface = Interface, if =NULL first interface
                    579:  * @addr = Network address structure
                    580:  * return: NULL error or !=NULL network structure
                    581:  */
                    582: sockaddr_t *
                    583: e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr)
                    584: {
                    585:        char szIface[64] = { [0 ... 63] = 0 };
                    586:        struct ifaddrs *p, *ifa = NULL;
                    587: 
                    588:        if (!addr)
                    589:                return NULL;
                    590: 
                    591:        memset(addr, 0, sizeof(sockaddr_t));
                    592:        getifaddrs(&ifa);
                    593:        strlcpy(szIface, psIface ? psIface : ifa->ifa_name, sizeof szIface);
1.13    ! misho     594:        for (p = ifa; p && p->ifa_name; p = p->ifa_next)
1.11      misho     595:                if (p->ifa_name && !strcmp(p->ifa_name, szIface) && p->ifa_addr && 
                    596:                                p->ifa_addr->sa_family == AF_LINK) {
                    597:                        memcpy(&addr->sdl, p->ifa_addr, sizeof(struct sockaddr_dl));
                    598:                        break;
                    599:                }
                    600:        freeifaddrs(ifa);
                    601: 
                    602:        return addr;
                    603: }
                    604: 
                    605: /*
                    606:  * e_getlinkbyname() - Get host ethernet address and make network structure
                    607:  *
                    608:  * @psHost = Host ethernet address
                    609:  * @addr = Network address structure
                    610:  * return: NULL error or !=NULL network structure
                    611:  */
                    612: sockaddr_t *
                    613: e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr)
                    614: {
                    615:        ait_val_t v;
                    616:        sockaddr_t *a = addr;
                    617: 
                    618:        if (!psHost || !addr)
                    619:                return NULL;
                    620:        else
                    621:                memset(addr, 0, sizeof(sockaddr_t));
                    622: 
                    623:        AIT_INIT_VAL2(&v, string);
                    624:        if (!strchr(psHost, '.'))
                    625:                AIT_SET_STR(&v, ":");
                    626:        AIT_SET_STRCAT(&v, psHost);
                    627: 
                    628:        addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
                    629:        if (e_link_addr(AIT_GET_STR(&v), &addr->sdl))
                    630:                a = NULL;
                    631: 
                    632:        AIT_FREE_VAL(&v);
                    633:        return a;
                    634: }
                    635: 
                    636: /*
                    637:  * e_getlinkbyether() - Get ethernet address and make network structure
                    638:  *
                    639:  * @mac = Ethernet address
                    640:  * @idx = Interface index
                    641:  * @type = Interface type
                    642:  * @iface = Interface name
                    643:  * @addr = Network address structure
                    644:  * return: NULL error or !=NULL network structure
                    645:  */
                    646: sockaddr_t *
                    647: e_getlinkbyether(const ether_addr_t * __restrict mac, u_short idx, u_char type, 
                    648:                const char *iface, sockaddr_t * __restrict addr)
                    649: {
                    650:        sockaddr_t *a = addr;
                    651: 
                    652:        if (!addr)
                    653:                return NULL;
                    654:        else
                    655:                memset(addr, 0, sizeof(sockaddr_t));
                    656: 
                    657:        addr->sdl.sdl_len = sizeof(struct sockaddr_dl);
                    658:        addr->sdl.sdl_family = AF_LINK;
                    659:        addr->sdl.sdl_index = idx;
                    660:        addr->sdl.sdl_type = type;
                    661:        if (iface && *iface) {
                    662:                addr->sdl.sdl_nlen = strlen(iface);
                    663:                memcpy(addr->sdl.sdl_data, iface, addr->sdl.sdl_nlen);
                    664:        }
                    665:        addr->sdl.sdl_alen = sizeof(ether_addr_t);
                    666:        memcpy(LLADDR(&addr->sdl), mac, addr->sdl.sdl_alen);
                    667: 
                    668:        return a;
                    669: }
1.13    ! misho     670: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>