OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [sys/] [linux/] [net/] [ifname.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*      $KAME: ifname.c,v 1.4 2001/08/20 02:32:40 itojun Exp $  */
/*      $KAME: ifname.c,v 1.4 2001/08/20 02:32:40 itojun Exp $  */
 
 
/*
/*
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
 * All rights reserved.
 * 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
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/types.h>
 
 
/*
/*
 * TODO:
 * TODO:
 * - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02)
 * - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02)
 */
 */
 
 
#include <sys/param.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if.h>
#include <net/route.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <net/if_dl.h>
 
 
#include <unistd.h>
#include <unistd.h>
#include <errno.h>
#include <errno.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
 
 
#define ROUNDUP(a) \
#define ROUNDUP(a) \
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define ADVANCE(x, n)
#define ADVANCE(x, n)
 
 
static unsigned int
static unsigned int
if_onametoindex(ifname)
if_onametoindex(ifname)
        const char *ifname;
        const char *ifname;
{
{
        struct if_nameindex *iff = if_nameindex(), *ifx;
        struct if_nameindex *iff = if_nameindex(), *ifx;
        int ret;
        int ret;
 
 
        if (iff == NULL) return 0;
        if (iff == NULL) return 0;
        ifx = iff;
        ifx = iff;
        while (ifx->if_name != NULL) {
        while (ifx->if_name != NULL) {
                if (strcmp(ifx->if_name, ifname) == 0) {
                if (strcmp(ifx->if_name, ifname) == 0) {
                        ret = ifx->if_index;
                        ret = ifx->if_index;
                        if_freenameindex(iff);
                        if_freenameindex(iff);
                        return ret;
                        return ret;
                }
                }
                ifx++;
                ifx++;
        }
        }
        if_freenameindex(iff);
        if_freenameindex(iff);
        errno = ENXIO;
        errno = ENXIO;
        return 0;
        return 0;
}
}
 
 
unsigned int
unsigned int
if_nametoindex(ifname)
if_nametoindex(ifname)
        const char *ifname;
        const char *ifname;
{
{
        int s;
        int s;
        struct ifreq ifr;
        struct ifreq ifr;
 
 
        s = socket(AF_INET, SOCK_DGRAM, 0);
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s == -1)
        if (s == -1)
                return (0);
                return (0);
        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
        if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
        if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
                close (s);
                close (s);
                return (if_onametoindex(ifname));
                return (if_onametoindex(ifname));
        }
        }
        close(s);
        close(s);
        return (ifr.ifr_index);
        return (ifr.ifr_index);
}
}
 
 
char *
char *
if_indextoname(ifindex, ifname)
if_indextoname(ifindex, ifname)
        unsigned int ifindex;
        unsigned int ifindex;
        char *ifname; /* at least IF_NAMESIZE */
        char *ifname; /* at least IF_NAMESIZE */
{
{
        struct if_nameindex *iff = if_nameindex(), *ifx;
        struct if_nameindex *iff = if_nameindex(), *ifx;
        char *cp, *dp;
        char *cp, *dp;
 
 
        if (iff == NULL) return NULL;
        if (iff == NULL) return NULL;
        ifx = iff;
        ifx = iff;
        while (ifx->if_index != 0) {
        while (ifx->if_index != 0) {
                if (ifx->if_index == ifindex) {
                if (ifx->if_index == ifindex) {
                        cp = ifname;
                        cp = ifname;
                        dp = ifx->if_name;
                        dp = ifx->if_name;
                        while ((*cp++ = *dp++)) ;
                        while ((*cp++ = *dp++)) ;
                        if_freenameindex(iff);
                        if_freenameindex(iff);
                        return (ifname);
                        return (ifname);
                }
                }
                ifx++;
                ifx++;
        }
        }
        if_freenameindex(iff);
        if_freenameindex(iff);
        errno = ENXIO;
        errno = ENXIO;
        return NULL;
        return NULL;
}
}
 
 
struct if_nameindex *
struct if_nameindex *
if_nameindex()
if_nameindex()
{
{
        size_t needed;
        size_t needed;
        int mib[6], i, ifn = 0, off = 0, hlen;
        int mib[6], i, ifn = 0, off = 0, hlen;
        char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL;
        char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL;
        struct rt_msghdr *rtm;
        struct rt_msghdr *rtm;
        struct if_msghdr *ifm;
        struct if_msghdr *ifm;
        struct sockaddr *sa;
        struct sockaddr *sa;
        struct if_nameindex *ret = NULL;
        struct if_nameindex *ret = NULL;
        static int ifxs = 64;   /* initial upper limit */
        static int ifxs = 64;   /* initial upper limit */
        struct _ifx {
        struct _ifx {
                int if_index;
                int if_index;
                int if_off;
                int if_off;
        } *ifx = NULL;
        } *ifx = NULL;
 
 
        mib[0] = CTL_NET;
        mib[0] = CTL_NET;
        mib[1] = PF_ROUTE;
        mib[1] = PF_ROUTE;
        mib[2] = 0;              /* protocol */
        mib[2] = 0;              /* protocol */
        mib[3] = 0;              /* wildcard address family */
        mib[3] = 0;              /* wildcard address family */
        mib[4] = 0;
        mib[4] = 0;
        mib[5] = 0;              /* no flags */
        mib[5] = 0;              /* no flags */
        if (__sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
        if (__sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
                return NULL;
                return NULL;
        if ((buf = malloc(needed)) == NULL) {
        if ((buf = malloc(needed)) == NULL) {
                errno = ENOMEM;
                errno = ENOMEM;
                goto end;
                goto end;
        }
        }
        /* XXX: we may have allocated too much than necessary */
        /* XXX: we may have allocated too much than necessary */
        if ((ifbuf = malloc(needed)) == NULL) {
        if ((ifbuf = malloc(needed)) == NULL) {
                errno = ENOMEM;
                errno = ENOMEM;
                goto end;
                goto end;
        }
        }
        if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) {
        if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) {
                errno = ENOMEM;
                errno = ENOMEM;
                goto end;
                goto end;
        }
        }
        if (__sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
        if (__sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
                /* sysctl has set errno */
                /* sysctl has set errno */
                goto end;
                goto end;
        }
        }
        lim = buf + needed;
        lim = buf + needed;
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                rtm = (struct rt_msghdr *)next;
                rtm = (struct rt_msghdr *)next;
                if (rtm->rtm_version != RTM_VERSION) {
                if (rtm->rtm_version != RTM_VERSION) {
                        errno = EPROTONOSUPPORT;
                        errno = EPROTONOSUPPORT;
                        goto end;
                        goto end;
                }
                }
                switch (rtm->rtm_type) {
                switch (rtm->rtm_type) {
                case RTM_IFINFO:
                case RTM_IFINFO:
                        ifm = (struct if_msghdr *)rtm;
                        ifm = (struct if_msghdr *)rtm;
                        ifx[ifn].if_index = ifm->ifm_index;
                        ifx[ifn].if_index = ifm->ifm_index;
                        ifx[ifn].if_off = off;
                        ifx[ifn].if_off = off;
                        cp = (char *)(ifm + 1);
                        cp = (char *)(ifm + 1);
                        for (i = 1; i; i <<= 1) {
                        for (i = 1; i; i <<= 1) {
                                if (i & ifm->ifm_addrs) {
                                if (i & ifm->ifm_addrs) {
                                        sa = (struct sockaddr *)cp;
                                        sa = (struct sockaddr *)cp;
                                        ADVANCE(cp, sa);
                                        ADVANCE(cp, sa);
                                }
                                }
                        }
                        }
                        if (++ifn == ifxs) {
                        if (++ifn == ifxs) {
                                /* we need more memory */
                                /* we need more memory */
                                struct _ifx *newifx;
                                struct _ifx *newifx;
 
 
                                ifxs *= 2;
                                ifxs *= 2;
                                if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) {
                                if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) {
                                        errno = ENOMEM;
                                        errno = ENOMEM;
                                        goto end;
                                        goto end;
                                }
                                }
 
 
                                /* copy and free old data */
                                /* copy and free old data */
                                memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2);
                                memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2);
                                free(ifx);
                                free(ifx);
                                ifx = newifx;
                                ifx = newifx;
                        }
                        }
                }
                }
        }
        }
        hlen = sizeof(struct if_nameindex) * (ifn + 1);
        hlen = sizeof(struct if_nameindex) * (ifn + 1);
        if ((cp = (char *)malloc(hlen + off)) == NULL) {
        if ((cp = (char *)malloc(hlen + off)) == NULL) {
                errno = ENOMEM;
                errno = ENOMEM;
                goto end;
                goto end;
        }
        }
        bcopy(ifbuf, cp + hlen, off);
        bcopy(ifbuf, cp + hlen, off);
        ret = (struct if_nameindex *)cp;
        ret = (struct if_nameindex *)cp;
        for (i = 0; i < ifn; i++) {
        for (i = 0; i < ifn; i++) {
                ret[i].if_index = ifx[i].if_index;
                ret[i].if_index = ifx[i].if_index;
                ret[i].if_name = cp + hlen + ifx[i].if_off;
                ret[i].if_name = cp + hlen + ifx[i].if_off;
        }
        }
        ret[ifn].if_index = 0;
        ret[ifn].if_index = 0;
        ret[ifn].if_name = NULL;
        ret[ifn].if_name = NULL;
 
 
  end:
  end:
        if (buf) free(buf);
        if (buf) free(buf);
        if (ifbuf) free(ifbuf);
        if (ifbuf) free(ifbuf);
        if (ifx) free(ifx);
        if (ifx) free(ifx);
 
 
        return ret;
        return ret;
}
}
 
 
void if_freenameindex(ptr)
void if_freenameindex(ptr)
        struct if_nameindex *ptr;
        struct if_nameindex *ptr;
{
{
        free(ptr);
        free(ptr);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.