Debian Codegraph @

psutil/arch/sunos/v10/ifaddrs.c

raw
1 /* References:
2 * https://lists.samba.org/archive/samba-technical/2009-February/063079.html
3 * http://stackoverflow.com/questions/4139405/#4139811
4 * https://github.com/steve-o/openpgm/blob/master/openpgm/pgm/getifaddrs.c
5 */
6
7 #include <string.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <net/if.h>
11 #include <netinet/in.h>
12 #include <sys/ioctl.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/sockio.h>
16
17 #include "ifaddrs.h"
18
19 #define MAX(x,y) ((x)>(y)?(x):(y))
20 #define SIZE(p) MAX((p).ss_len,sizeof(p))
21
22
23 static struct sockaddr *
24 sa_dup (struct sockaddr_storage *sa1)
25 {
26 struct sockaddr *sa2;
27 size_t sz = sizeof(struct sockaddr_storage);
28 sa2 = (struct sockaddr *) calloc(1,sz);
29 memcpy(sa2,sa1,sz);
30 return(sa2);
31 }
32
33
34 void freeifaddrs (struct ifaddrs *ifp)
35 {
36 if (NULL == ifp) return;
37 free(ifp->ifa_name);
38 free(ifp->ifa_addr);
39 free(ifp->ifa_netmask);
40 free(ifp->ifa_dstaddr);
41 freeifaddrs(ifp->ifa_next);
42 free(ifp);
43 }
44
45
46 int getifaddrs (struct ifaddrs **ifap)
47 {
48 int sd = -1;
49 char *ccp, *ecp;
50 struct lifconf ifc;
51 struct lifreq *ifr;
52 struct lifnum lifn;
53 struct ifaddrs *cifa = NULL; /* current */
54 struct ifaddrs *pifa = NULL; /* previous */
55 const size_t IFREQSZ = sizeof(struct lifreq);
56
57 sd = socket(AF_INET, SOCK_STREAM, 0);
58 if (sd < 0)
59 goto error;
60
61 ifc.lifc_buf = NULL;
62 *ifap = NULL;
63 /* find how much memory to allocate for the SIOCGLIFCONF call */
64 lifn.lifn_family = AF_UNSPEC;
65 lifn.lifn_flags = 0;
66 if (ioctl(sd, SIOCGLIFNUM, &lifn) < 0)
67 goto error;
68
69 /* Sun and Apple code likes to pad the interface count here in case interfaces
70 * are coming up between calls */
71 lifn.lifn_count += 4;
72
73 ifc.lifc_family = AF_UNSPEC;
74 ifc.lifc_len = lifn.lifn_count * sizeof(struct lifreq);
75 ifc.lifc_buf = calloc(1, ifc.lifc_len);
76 if (ioctl(sd, SIOCGLIFCONF, &ifc) < 0)
77 goto error;
78
79 ccp = (char *)ifc.lifc_req;
80 ecp = ccp + ifc.lifc_len;
81
82 while (ccp < ecp) {
83
84 ifr = (struct lifreq *) ccp;
85 cifa = (struct ifaddrs *) calloc(1, sizeof(struct ifaddrs));
86 cifa->ifa_next = NULL;
87 cifa->ifa_name = strdup(ifr->lifr_name);
88
89 if (pifa == NULL) *ifap = cifa; /* first one */
90 else pifa->ifa_next = cifa;
91
92 if (ioctl(sd, SIOCGLIFADDR, ifr, IFREQSZ) < 0)
93 goto error;
94 cifa->ifa_addr = sa_dup(&ifr->lifr_addr);
95
96 if (ioctl(sd, SIOCGLIFNETMASK, ifr, IFREQSZ) < 0)
97 goto error;
98 cifa->ifa_netmask = sa_dup(&ifr->lifr_addr);
99
100 cifa->ifa_flags = 0;
101 cifa->ifa_dstaddr = NULL;
102
103 if (0 == ioctl(sd, SIOCGLIFFLAGS, ifr)) /* optional */
104 cifa->ifa_flags = ifr->lifr_flags;
105
106 if (ioctl(sd, SIOCGLIFDSTADDR, ifr, IFREQSZ) < 0) {
107 if (0 == ioctl(sd, SIOCGLIFBRDADDR, ifr, IFREQSZ))
108 cifa->ifa_dstaddr = sa_dup(&ifr->lifr_addr);
109 }
110 else cifa->ifa_dstaddr = sa_dup(&ifr->lifr_addr);
111
112 pifa = cifa;
113 ccp += IFREQSZ;
114 }
115 free(ifc.lifc_buf);
116 close(sd);
117 return 0;
118 error:
119 if (ifc.lifc_buf != NULL)
120 free(ifc.lifc_buf);
121 if (sd != -1)
122 close(sd);
123 freeifaddrs(*ifap);
124 return (-1);
125 }