OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [if_nametoindex.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1997,98,99,2000,02 Free Software Foundation, Inc.
2
 * This file is part of the GNU C Library.
3
 *
4
 * The GNU C Library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * The GNU C Library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with the GNU C Library; if not, write to the Free
16
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17
 * 02111-1307 USA.
18
 *
19
 * Reworked Dec 2002 by Erik Andersen <andersen@codepoet.org>
20
 */
21
 
22
#define __FORCE_GLIBC
23
#include <features.h>
24
#include <string.h>
25
#include <errno.h>
26
#include <net/if.h>
27
#include <sys/ioctl.h>
28
#include <unistd.h>
29
#include <stdlib.h>
30
 
31
static int __opensock(void)
32
{
33
    int fd;
34
#ifdef __UCLIBC_HAS_IPV6__
35
    fd=socket(AF_INET6,SOCK_DGRAM,0);
36
    if (fd<0)
37
#endif /* __UCLIBC_HAS_IPV6__ */
38
        fd=socket(AF_INET,SOCK_DGRAM,0);
39
    return(fd);
40
}
41
 
42
unsigned int if_nametoindex(const char* ifname)
43
{
44
#ifndef SIOCGIFINDEX
45
    __set_errno (ENOSYS);
46
    return 0;
47
#else
48
    int fd;
49
    struct ifreq ifr;
50
 
51
    fd = __opensock();
52
    if (fd < 0)
53
        return 0;
54
    strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
55
    if (ioctl(fd,SIOCGIFINDEX,&ifr) < 0) {
56
        int saved_errno = errno;
57
        close(fd);
58
        if (saved_errno == EINVAL)
59
            __set_errno(ENOSYS);
60
        return 0;
61
    }
62
    close(fd);
63
    return ifr.ifr_ifindex;
64
 
65
#endif /* SIOCGIFINDEX */
66
}
67
 
68
void if_freenameindex (struct if_nameindex *ifn)
69
{
70
    struct if_nameindex *ptr = ifn;
71
    while (ptr->if_name || ptr->if_index) {
72
        if (ptr->if_name) {
73
            free (ptr->if_name);
74
        }
75
        ++ptr;
76
    }
77
    free (ifn);
78
}
79
 
80
struct if_nameindex * if_nameindex (void)
81
{
82
#ifndef SIOCGIFINDEX
83
    __set_errno (ENOSYS);
84
    return NULL;
85
#else
86
    int fd;
87
    struct ifconf ifc;
88
    unsigned int nifs, i;
89
    int rq_len;
90
    struct if_nameindex *idx = NULL;
91
# define RQ_IFS 4
92
 
93
    fd = __opensock();
94
    if (fd < 0)
95
        return 0;
96
 
97
    ifc.ifc_buf = NULL;
98
 
99
    /* Guess on the correct buffer size... */
100
    rq_len = RQ_IFS * sizeof (struct ifreq);
101
 
102
    /* Read all the interfaces out of the kernel.  */
103
    do {
104
        ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len = rq_len);
105
        if (ifc.ifc_buf == NULL || ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
106
            close(fd);
107
            return NULL;
108
        }
109
        rq_len *= 2;
110
    } while (ifc.ifc_len == rq_len);
111
 
112
    nifs = ifc.ifc_len / sizeof(struct ifreq);
113
 
114
    idx = malloc ((nifs + 1) * sizeof(struct if_nameindex));
115
    if (idx == NULL) {
116
        close(fd);
117
        __set_errno(ENOBUFS);
118
        return NULL;
119
    }
120
 
121
    for (i = 0; i < nifs; ++i) {
122
        struct ifreq *ifr = &ifc.ifc_req[i];
123
        idx[i].if_name = strdup (ifr->ifr_name);
124
        if (idx[i].if_name == NULL || ioctl(fd,SIOCGIFINDEX,ifr) < 0) {
125
            int saved_errno = errno;
126
            unsigned int j;
127
            for (j =  0; j < i; ++j)
128
                free (idx[j].if_name);
129
            free(idx);
130
            close(fd);
131
            if (saved_errno == EINVAL)
132
                saved_errno = ENOSYS;
133
            else if (saved_errno == ENOMEM)
134
                saved_errno = ENOBUFS;
135
            __set_errno (saved_errno);
136
            return NULL;
137
        }
138
        idx[i].if_index = ifr->ifr_ifindex;
139
    }
140
 
141
    idx[i].if_index = 0;
142
    idx[i].if_name = NULL;
143
 
144
    close(fd);
145
    return idx;
146
#endif
147
}
148
 
149
char * if_indextoname (unsigned int ifindex, char *ifname)
150
{
151
    struct if_nameindex *idx;
152
    struct if_nameindex *p;
153
    char *result = NULL;
154
 
155
    idx = if_nameindex();
156
    if (idx != NULL) {
157
        for (p = idx; p->if_index || p->if_name; ++p) {
158
            if (p->if_index == ifindex) {
159
                result = strncpy (ifname, p->if_name, IFNAMSIZ);
160
                break;
161
            }
162
        }
163
        if_freenameindex (idx);
164
    }
165
    return result;
166
}
167
 

powered by: WebSVN 2.1.0

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