forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif_nameindex.c
67 lines (59 loc) · 1.56 KB
/
if_nameindex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* include if_nameindex */
#include "unpifi.h"
#include "unproute.h"
struct if_nameindex *
if_nameindex(void)
{
char *buf, *next, *lim;
size_t len;
struct if_msghdr *ifm;
struct sockaddr *sa, *rti_info[RTAX_MAX];
struct sockaddr_dl *sdl;
struct if_nameindex *result, *ifptr;
char *namptr;
if ( (buf = net_rt_iflist(0, 0, &len)) == NULL)
return(NULL);
if ( (result = malloc(len)) == NULL) /* overestimate */
return(NULL);
ifptr = result;
namptr = (char *) result + len; /* names start at end of buffer */
lim = buf + len;
for (next = buf; next < lim; next += ifm->ifm_msglen) {
ifm = (struct if_msghdr *) next;
if (ifm->ifm_type == RTM_IFINFO) {
sa = (struct sockaddr *) (ifm + 1);
get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
if ( (sa = rti_info[RTAX_IFP]) != NULL) {
if (sa->sa_family == AF_LINK) {
sdl = (struct sockaddr_dl *) sa;
namptr -= sdl->sdl_nlen + 1;
strncpy(namptr, &sdl->sdl_data[0], sdl->sdl_nlen);
namptr[sdl->sdl_nlen] = 0; /* null terminate */
ifptr->if_name = namptr;
ifptr->if_index = sdl->sdl_index;
ifptr++;
}
}
}
}
ifptr->if_name = NULL; /* mark end of array of structs */
ifptr->if_index = 0;
free(buf);
return(result); /* caller must free() this when done */
}
/* end if_nameindex */
/* include if_freenameindex */
void
if_freenameindex(struct if_nameindex *ptr)
{
free(ptr);
}
/* end if_freenameindex */
struct if_nameindex *
If_nameindex(void)
{
struct if_nameindex *ifptr;
if ( (ifptr = if_nameindex()) == NULL)
err_quit("if_nameindex error");
return(ifptr);
}