source: rtems/cpukit/libnetworking/libc/if_nameindex.c @ cb68253

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*      $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $    */
4
5/*-
6 * Copyright (c) 1997, 2000
7 *      Berkeley Software Design, Inc.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *      BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include <sys/cdefs.h>
35
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <net/if_dl.h>
39#include <net/if.h>
40#include <ifaddrs.h>
41#include <stdlib.h>
42#include <string.h>
43
44/*
45 * From RFC 2553:
46 *
47 * 4.3 Return All Interface Names and Indexes
48 *
49 *    The if_nameindex structure holds the information about a single
50 *    interface and is defined as a result of including the <net/if.h>
51 *    header.
52 *
53 *       struct if_nameindex {
54 *         unsigned int   if_index;
55 *         char          *if_name;
56 *       };
57 *
58 *    The final function returns an array of if_nameindex structures, one
59 *    structure per interface.
60 *
61 *       struct if_nameindex  *if_nameindex(void);
62 *
63 *    The end of the array of structures is indicated by a structure with
64 *    an if_index of 0 and an if_name of NULL.  The function returns a NULL
65 *    pointer upon an error, and would set errno to the appropriate value.
66 *
67 *    The memory used for this array of structures along with the interface
68 *    names pointed to by the if_name members is obtained dynamically.
69 *    This memory is freed by the next function.
70 *
71 * 4.4.  Free Memory
72 *
73 *    The following function frees the dynamic memory that was allocated by
74 *    if_nameindex().
75 *
76 *        #include <net/if.h>
77 *
78 *        void  if_freenameindex(struct if_nameindex *ptr);
79 *
80 *    The argument to this function must be a pointer that was returned by
81 *    if_nameindex().
82 */
83
84struct if_nameindex *
85if_nameindex(void)
86{
87        struct ifaddrs *ifaddrs, *ifa;
88        unsigned int ni;
89        int nbytes;
90        struct if_nameindex *ifni, *ifni2;
91        char *cp;
92
93        if (getifaddrs(&ifaddrs) < 0)
94                return(NULL);
95
96        /*
97         * First, find out how many interfaces there are, and how
98         * much space we need for the string names.
99         */
100        ni = 0;
101        nbytes = 0;
102        for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
103                if (ifa->ifa_addr &&
104                    ifa->ifa_addr->sa_family == AF_LINK) {
105                        nbytes += strlen(ifa->ifa_name) + 1;
106                        ni++;
107                }
108        }
109
110        /*
111         * Next, allocate a chunk of memory, use the first part
112         * for the array of structures, and the last part for
113         * the strings.
114         */
115        cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes);
116        ifni = (struct if_nameindex *)cp;
117        if (ifni == NULL)
118                goto out;
119        cp += (ni + 1) * sizeof(struct if_nameindex);
120
121        /*
122         * Now just loop through the list of interfaces again,
123         * filling in the if_nameindex array and making copies
124         * of all the strings.
125         */
126        ifni2 = ifni;
127        for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
128                if (ifa->ifa_addr &&
129                    ifa->ifa_addr->sa_family == AF_LINK) {
130                        ifni2->if_index =
131                            ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index;
132                        ifni2->if_name = cp;
133                        strcpy(cp, ifa->ifa_name);
134                        ifni2++;
135                        cp += strlen(cp) + 1;
136                }
137        }
138        /*
139         * Finally, don't forget to terminate the array.
140         */
141        ifni2->if_index = 0;
142        ifni2->if_name = NULL;
143out:
144        freeifaddrs(ifaddrs);
145        return(ifni);
146}
147
148void
149if_freenameindex(struct if_nameindex *ptr)
150{
151        free(ptr);
152}
Note: See TracBrowser for help on using the repository browser.