]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/if_index.c
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
24 #define IF_INET6_FILENAME "/proc/net/if_inet6"
26 /* /proc/net/if_inet6 contains lines that look like this:
28 * fe8000000000000000000000836fc168 0b 00 20 80 sit7
31 * address --' | | | | |
32 * index --------' | | | |
33 * prefix length ---' | | |
34 * scope --------------' | |
35 * flags -----------------' |
36 * name -------------------------'
40 static int get_one_interface(FILE *fd
, char *interface
, int iflen
, unsigned int *index
)
43 static char seps
[] = " \012";
46 if (!fgets(buffer
, 80, fd
))
48 if (strtok_r(buffer
, seps
, &sp
) == NULL
) return 1;
49 if (c
= strtok_r(NULL
, seps
, &sp
), c
== NULL
) return 1;
50 *index
= strtoul(c
, NULL
, 16);
51 if (strtok_r(NULL
, seps
, &sp
) == NULL
) return 1;
52 if (strtok_r(NULL
, seps
, &sp
) == NULL
) return 1;
53 if (strtok_r(NULL
, seps
, &sp
) == NULL
) return 1;
54 if (c
= strtok_r(NULL
, seps
, &sp
), c
== NULL
) return 1;
55 strncpy(interface
, c
, iflen
);
59 unsigned int if_nametoindex(const char *ifname
)
61 FILE *fd
= fopen(IF_INET6_FILENAME
, "r");
62 char this_ifname
[IFNAMSIZ
];
63 unsigned int this_index
;
65 while (get_one_interface(fd
, this_ifname
, IFNAMSIZ
, &this_index
) == 0) {
66 if (!strcmp(this_ifname
, ifname
)) {
75 char *if_indextoname(unsigned int ifindex
, char *ifname
)
77 FILE *fd
= fopen(IF_INET6_FILENAME
, "r");
78 unsigned int this_index
;
80 while (get_one_interface(fd
, ifname
, IFNAMSIZ
, &this_index
) == 0) {
81 if (this_index
== ifindex
) {
90 void if_freenameindex(struct if_nameindex
*ifn
)
92 struct if_nameindex
*ptr
= ifn
;
93 while (ptr
->if_name
|| ptr
->if_index
)
102 struct if_nameindex
*if_nameindex(void)
104 FILE *fd
= fopen(IF_INET6_FILENAME
, "r");
105 struct if_nameindex
*ifn
= NULL
;
107 if (!fd
) return NULL
;
110 struct if_nameindex
*newifn
;
112 newifn
= realloc(ifn
, nifs
*sizeof(struct if_nameindex
));
115 /* We ran out of memory. */
118 free(ifn
[nifs
-1].if_name
);
119 ifn
[nifs
-1].if_name
= 0;
120 ifn
[nifs
-1].if_index
= 0;
121 if_freenameindex(ifn
);
126 ifn
[nifs
-1].if_index
= 0;
127 ifn
[nifs
-1].if_name
= malloc(IFNAMSIZ
);
128 if (ifn
[nifs
-1].if_name
== NULL
)
130 if_freenameindex(ifn
);
134 while (get_one_interface(fd
, ifn
[nifs
-1].if_name
, IFNAMSIZ
,
135 &ifn
[nifs
-1].if_index
) == 0);
136 free(ifn
[nifs
-1].if_name
);
137 ifn
[nifs
-1].if_name
= NULL
;