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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [src/] [ifaddrs.c] - Blame information for rev 694

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/ifaddrs.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
//
23
// Adapted from KAME getifaddrs.c, if_nametoindex.c, if_indextoname.c
24
//
25
 
26
/*      $KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $      */
27
/*      $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $  */
28
/*      $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $  */
29
 
30
/*
31
 * Copyright (c) 1995, 1999
32
 *      Berkeley Software Design, Inc.  All rights reserved.
33
 *
34
 * Redistribution and use in source and binary forms, with or without
35
 * modification, are permitted provided that the following conditions
36
 * are met:
37
 * 1. Redistributions of source code must retain the above copyright
38
 *    notice, this list of conditions and the following disclaimer.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 *      BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
53
 */
54
 
55
#include <unistd.h>
56
#include <stdlib.h>
57
#include <string.h>
58
 
59
#undef _KERNEL
60
#undef __INSIDE_NET
61
#include <sys/param.h>
62
#include <sys/types.h>
63
#include <sys/ioctl.h>
64
#include <sys/socket.h>
65
#include <net/if.h>
66
#include <net/if_dl.h>
67
#include <errno.h>
68
#include <netinet/in.h>
69
#include <net/netdb.h>
70
#include <ifaddrs.h>
71
 
72
#if !defined(AF_LINK)
73
#define SA_LEN(sa)      sizeof(struct sockaddr)
74
#endif
75
 
76
#if !defined(SA_LEN)
77
#define SA_LEN(sa)      (sa)->sa_len
78
#endif
79
 
80
#define SALIGN  (sizeof(long) - 1)
81
#define SA_RLEN(sa)     ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
82
 
83
#ifndef ALIGNBYTES
84
/*
85
 * On systems with a routing socket, ALIGNBYTES should match the value
86
 * that the kernel uses when building the messages.
87
 */
88
#define ALIGNBYTES      XXX
89
#endif
90
#ifndef ALIGN
91
#define ALIGN(p)        (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
92
#endif
93
 
94
int
95
getifaddrs(struct ifaddrs **pif)
96
{
97
    int icnt = 1;  // Interface count
98
    int dcnt = 0;  // Data [length] count
99
    int ncnt = 0;  // Length of interface names
100
    char buf[1024];
101
    int i, sock;
102
    struct ifconf ifc;
103
    struct ifreq *ifr, *lifr;
104
    char *data, *names;
105
    struct ifaddrs *ifa, *ift;
106
 
107
    ifc.ifc_buf = buf;
108
    ifc.ifc_len = sizeof(buf);
109
 
110
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
111
        return (-1);
112
    i =  ioctl(sock, SIOCGIFCONF, (char *)&ifc);
113
    close(sock);
114
    if (i < 0)
115
        return (-1);
116
 
117
    ifr = ifc.ifc_req;
118
    lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
119
 
120
    while (ifr < lifr) {
121
        struct sockaddr *sa;
122
 
123
        sa = &ifr->ifr_addr;
124
        ++icnt;
125
        dcnt += SA_RLEN(sa);
126
        ncnt += sizeof(ifr->ifr_name) + 1;
127
 
128
        if (SA_LEN(sa) < sizeof(*sa))
129
            ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
130
        else
131
            ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
132
    }
133
 
134
    if (icnt + dcnt + ncnt == 1) {
135
        // Nothing found
136
        *pif = NULL;
137
        free(buf);
138
        return (0);
139
    }
140
    data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
141
    if (data == NULL) {
142
        free(buf);
143
        return(-1);
144
    }
145
 
146
    ifa = (struct ifaddrs *)(void *)data;
147
    data += sizeof(struct ifaddrs) * icnt;
148
    names = data + dcnt;
149
 
150
    memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
151
    ift = ifa;
152
 
153
    ifr = ifc.ifc_req;
154
    lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
155
 
156
    while (ifr < lifr) {
157
        struct sockaddr *sa;
158
 
159
        ift->ifa_name = names;
160
        names[sizeof(ifr->ifr_name)] = 0;
161
        strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
162
        while (*names++) ;
163
 
164
        ift->ifa_addr = (struct sockaddr *)data;
165
        sa = &ifr->ifr_addr;
166
        memcpy(data, sa, SA_LEN(sa));
167
        data += SA_RLEN(sa);
168
 
169
        if (SA_LEN(sa) < sizeof(*sa))
170
            ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
171
        else
172
            ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
173
        ift = (ift->ifa_next = ift + 1);
174
    }
175
 
176
    if (--ift >= ifa) {
177
        ift->ifa_next = NULL;
178
        *pif = ifa;
179
    } else {
180
        *pif = NULL;
181
        free(ifa);
182
    }
183
    return (0);
184
}
185
 
186
void
187
freeifaddrs(struct ifaddrs *ifp)
188
{
189
    free(ifp);
190
}
191
 
192
void
193
_show_all_interfaces(void)
194
{
195
    struct ifaddrs *iflist, *ifp;
196
    char addr[64];
197
    int indx;
198
 
199
    if (getifaddrs(&iflist) < 0) {
200
        diag_printf("Can't get interface information!!\n");
201
        return;
202
    }
203
    ifp = iflist;
204
    while (ifp != (struct ifaddrs *)NULL) {
205
        if (ifp->ifa_addr->sa_family != AF_LINK) {
206
            getnameinfo (ifp->ifa_addr, ifp->ifa_addr->sa_len, addr, sizeof(addr), 0, 0, 0);
207
            diag_printf("%p - %s - %s\n", ifp, ifp->ifa_name, addr);
208
        }
209
        ifp = ifp->ifa_next;
210
    }
211
    indx = if_nametoindex(iflist->ifa_name);
212
    diag_printf("indx(%s) = %d\n", iflist->ifa_name, indx);
213
    if (indx > 0) {
214
        if (if_indextoname(indx, addr)) {
215
            diag_printf("index(%s) = %d/%s\n", iflist->ifa_name, indx, addr);
216
        } else {
217
            diag_printf("index(%s) = %d: %s\n", iflist->ifa_name, indx, strerror(errno));
218
        }
219
    } else {
220
        diag_printf("index(%s): %s\n", iflist->ifa_name, strerror(errno));
221
    }
222
    freeifaddrs(iflist);
223
}
224
 
225
/*
226
 * From RFC 2553:
227
 *
228
 * 4.1 Name-to-Index
229
 *
230
 *
231
 *    The first function maps an interface name into its corresponding
232
 *    index.
233
 *
234
 *       #include <net/if.h>
235
 *
236
 *       unsigned int  if_nametoindex(const char *ifname);
237
 *
238
 *    If the specified interface name does not exist, the return value is
239
 *    0, and errno is set to ENXIO.  If there was a system error (such as
240
 *    running out of memory), the return value is 0 and errno is set to the
241
 *    proper value (e.g., ENOMEM).
242
 */
243
 
244
unsigned int
245
if_nametoindex(const char *ifname)
246
{
247
    struct ifaddrs *ifaddrs, *ifa;
248
    unsigned int ni;
249
 
250
    if (getifaddrs(&ifaddrs) < 0)
251
        return(0);
252
 
253
    ni = 0;
254
 
255
    for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
256
        if (ifa->ifa_addr &&
257
            ifa->ifa_addr->sa_family == AF_LINK &&
258
            strcmp(ifa->ifa_name, ifname) == 0) {
259
            ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
260
            break;
261
        }
262
    }
263
 
264
    freeifaddrs(ifaddrs);
265
    if (!ni)
266
        errno = ENXIO;
267
    return(ni);
268
}
269
 
270
/*
271
 * From RFC 2533:
272
 *
273
 * The second function maps an interface index into its corresponding
274
 * name.
275
 *
276
 *    #include <net/if.h>
277
 *
278
 *    char  *if_indextoname(unsigned int ifindex, char *ifname);
279
 *
280
 * The ifname argument must point to a buffer of at least IF_NAMESIZE
281
 * bytes into which the interface name corresponding to the specified
282
 * index is returned.  (IF_NAMESIZE is also defined in <net/if.h> and
283
 * its value includes a terminating null byte at the end of the
284
 * interface name.) This pointer is also the return value of the
285
 * function.  If there is no interface corresponding to the specified
286
 * index, NULL is returned, and errno is set to ENXIO, if there was a
287
 * system error (such as running out of memory), if_indextoname returns
288
 * NULL and errno would be set to the proper value (e.g., ENOMEM).
289
 */
290
 
291
char *
292
if_indextoname(unsigned int ifindex, char *ifname)
293
{
294
    struct ifaddrs *ifaddrs, *ifa;
295
    int error = 0;
296
 
297
    if (getifaddrs(&ifaddrs) < 0)
298
        return(NULL);   /* getifaddrs properly set errno */
299
 
300
    for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
301
        if (ifa->ifa_addr &&
302
            ifa->ifa_addr->sa_family == AF_LINK &&
303
            ifindex == ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index)
304
            break;
305
    }
306
 
307
    if (ifa == NULL) {
308
        error = ENXIO;
309
        ifname = NULL;
310
    }
311
    else
312
        strncpy(ifname, ifa->ifa_name, IFNAMSIZ);
313
 
314
    freeifaddrs(ifaddrs);
315
 
316
    errno = error;
317
    return(ifname);
318
}

powered by: WebSVN 2.1.0

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