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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [net/] [ifname.c] - Blame information for rev 148

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

Line No. Rev Author Line
1 148 jeremybenn
/*      $KAME: ifname.c,v 1.4 2001/08/20 02:32:40 itojun Exp $  */
2
 
3
/*
4
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the project nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
 
32
#include <sys/cdefs.h>
33
#include <sys/types.h>
34
#include <sys/types.h>
35
 
36
/*
37
 * TODO:
38
 * - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02)
39
 */
40
 
41
#include <sys/param.h>
42
#include <sys/socket.h>
43
#include <sys/sockio.h>
44
#include <sys/sysctl.h>
45
#include <net/if.h>
46
#include <net/route.h>
47
#include <net/if_dl.h>
48
 
49
#include <unistd.h>
50
#include <errno.h>
51
#include <stdlib.h>
52
#include <string.h>
53
 
54
#define ROUNDUP(a) \
55
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
56
#define ADVANCE(x, n)
57
 
58
static unsigned int
59
if_onametoindex(ifname)
60
        const char *ifname;
61
{
62
        struct if_nameindex *iff = if_nameindex(), *ifx;
63
        int ret;
64
 
65
        if (iff == NULL) return 0;
66
        ifx = iff;
67
        while (ifx->if_name != NULL) {
68
                if (strcmp(ifx->if_name, ifname) == 0) {
69
                        ret = ifx->if_index;
70
                        if_freenameindex(iff);
71
                        return ret;
72
                }
73
                ifx++;
74
        }
75
        if_freenameindex(iff);
76
        errno = ENXIO;
77
        return 0;
78
}
79
 
80
unsigned int
81
if_nametoindex(ifname)
82
        const char *ifname;
83
{
84
        int s;
85
        struct ifreq ifr;
86
 
87
        s = socket(AF_INET, SOCK_DGRAM, 0);
88
        if (s == -1)
89
                return (0);
90
        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
91
        if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
92
                close (s);
93
                return (if_onametoindex(ifname));
94
        }
95
        close(s);
96
        return (ifr.ifr_index);
97
}
98
 
99
char *
100
if_indextoname(ifindex, ifname)
101
        unsigned int ifindex;
102
        char *ifname; /* at least IF_NAMESIZE */
103
{
104
        struct if_nameindex *iff = if_nameindex(), *ifx;
105
        char *cp, *dp;
106
 
107
        if (iff == NULL) return NULL;
108
        ifx = iff;
109
        while (ifx->if_index != 0) {
110
                if (ifx->if_index == ifindex) {
111
                        cp = ifname;
112
                        dp = ifx->if_name;
113
                        while ((*cp++ = *dp++)) ;
114
                        if_freenameindex(iff);
115
                        return (ifname);
116
                }
117
                ifx++;
118
        }
119
        if_freenameindex(iff);
120
        errno = ENXIO;
121
        return NULL;
122
}
123
 
124
struct if_nameindex *
125
if_nameindex()
126
{
127
        size_t needed;
128
        int mib[6], i, ifn = 0, off = 0, hlen;
129
        char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL;
130
        struct rt_msghdr *rtm;
131
        struct if_msghdr *ifm;
132
        struct sockaddr *sa;
133
        struct if_nameindex *ret = NULL;
134
        static int ifxs = 64;   /* initial upper limit */
135
        struct _ifx {
136
                int if_index;
137
                int if_off;
138
        } *ifx = NULL;
139
 
140
        mib[0] = CTL_NET;
141
        mib[1] = PF_ROUTE;
142
        mib[2] = 0;              /* protocol */
143
        mib[3] = 0;              /* wildcard address family */
144
        mib[4] = 0;
145
        mib[5] = 0;              /* no flags */
146
        if (__sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
147
                return NULL;
148
        if ((buf = malloc(needed)) == NULL) {
149
                errno = ENOMEM;
150
                goto end;
151
        }
152
        /* XXX: we may have allocated too much than necessary */
153
        if ((ifbuf = malloc(needed)) == NULL) {
154
                errno = ENOMEM;
155
                goto end;
156
        }
157
        if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) {
158
                errno = ENOMEM;
159
                goto end;
160
        }
161
        if (__sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
162
                /* sysctl has set errno */
163
                goto end;
164
        }
165
        lim = buf + needed;
166
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
167
                rtm = (struct rt_msghdr *)next;
168
                if (rtm->rtm_version != RTM_VERSION) {
169
                        errno = EPROTONOSUPPORT;
170
                        goto end;
171
                }
172
                switch (rtm->rtm_type) {
173
                case RTM_IFINFO:
174
                        ifm = (struct if_msghdr *)rtm;
175
                        ifx[ifn].if_index = ifm->ifm_index;
176
                        ifx[ifn].if_off = off;
177
                        cp = (char *)(ifm + 1);
178
                        for (i = 1; i; i <<= 1) {
179
                                if (i & ifm->ifm_addrs) {
180
                                        sa = (struct sockaddr *)cp;
181
                                        ADVANCE(cp, sa);
182
                                }
183
                        }
184
                        if (++ifn == ifxs) {
185
                                /* we need more memory */
186
                                struct _ifx *newifx;
187
 
188
                                ifxs *= 2;
189
                                if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) {
190
                                        errno = ENOMEM;
191
                                        goto end;
192
                                }
193
 
194
                                /* copy and free old data */
195
                                memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2);
196
                                free(ifx);
197
                                ifx = newifx;
198
                        }
199
                }
200
        }
201
        hlen = sizeof(struct if_nameindex) * (ifn + 1);
202
        if ((cp = (char *)malloc(hlen + off)) == NULL) {
203
                errno = ENOMEM;
204
                goto end;
205
        }
206
        bcopy(ifbuf, cp + hlen, off);
207
        ret = (struct if_nameindex *)cp;
208
        for (i = 0; i < ifn; i++) {
209
                ret[i].if_index = ifx[i].if_index;
210
                ret[i].if_name = cp + hlen + ifx[i].if_off;
211
        }
212
        ret[ifn].if_index = 0;
213
        ret[ifn].if_name = NULL;
214
 
215
  end:
216
        if (buf) free(buf);
217
        if (ifbuf) free(ifbuf);
218
        if (ifx) free(ifx);
219
 
220
        return ret;
221
}
222
 
223
void if_freenameindex(ptr)
224
        struct if_nameindex *ptr;
225
{
226
        free(ptr);
227
}

powered by: WebSVN 2.1.0

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