blob: a88bcc1c3bdd7a9fd8e6361c010c182e732b23c0 [file] [log] [blame]
Samuel Tan71df7772015-08-20 11:04:41 -07001/* external/dhcpcd-6.8.2/ifaddrs.c
2** Copied from external/dhcpcd/ifaddrs.c.
3**
4** Copyright 2011, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");.
7** you may not use this file except in compliance with the License..
8** You may obtain a copy of the License at.
9**
10** http://www.apache.org/licenses/LICENSE-2.0.
11**
12** Unless required by applicable law or agreed to in writing, software.
13** distributed under the License is distributed on an "AS IS" BASIS,.
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied..
15** See the License for the specific language governing permissions and.
16** limitations under the License.
17*/
18
19#include <arpa/inet.h>
20#include <sys/socket.h>
21#include "ifaddrs.h"
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <dirent.h>
28#include <netinet/ether.h>
29#include <netdb.h>
30#include <linux/if_packet.h>
31#include <netinet/if_ether.h>
32#include <linux/if_arp.h>
33#include <netutils/ifc.h>
34
35struct ifaddrs *get_interface(const char *name, sa_family_t family)
36{
37 unsigned addr, flags;
38 int masklen;
39 struct ifaddrs *ifa;
40 struct sockaddr_in *saddr = NULL;
41 struct sockaddr_in *smask = NULL;
42 struct sockaddr_ll *hwaddr = NULL;
43 unsigned char hwbuf[ETH_ALEN];
44
45 if (ifc_get_info(name, &addr, &masklen, &flags))
46 return NULL;
47
48 if ((family == AF_INET) && (addr == 0))
49 return NULL;
50
51 ifa = malloc(sizeof(struct ifaddrs));
52 if (!ifa)
53 return NULL;
54 memset(ifa, 0, sizeof(struct ifaddrs));
55
56 ifa->ifa_name = malloc(strlen(name)+1);
57 if (!ifa->ifa_name) {
58 free(ifa);
59 return NULL;
60 }
61 strcpy(ifa->ifa_name, name);
62 ifa->ifa_flags = flags;
63
64 if (family == AF_INET) {
65 saddr = malloc(sizeof(struct sockaddr_in));
66 if (saddr) {
67 saddr->sin_addr.s_addr = addr;
68 saddr->sin_family = family;
69 }
70 ifa->ifa_addr = (struct sockaddr *)saddr;
71
72 if (masklen != 0) {
73 smask = malloc(sizeof(struct sockaddr_in));
74 if (smask) {
75 smask->sin_addr.s_addr = prefixLengthToIpv4Netmask(masklen);
76 smask->sin_family = family;
77 }
78 }
79 ifa->ifa_netmask = (struct sockaddr *)smask;
80 } else if (family == AF_PACKET) {
81 if (!ifc_get_hwaddr(name, hwbuf)) {
82 hwaddr = malloc(sizeof(struct sockaddr_ll));
83 if (hwaddr) {
84 memset(hwaddr, 0, sizeof(struct sockaddr_ll));
85 hwaddr->sll_family = family;
86 /* hwaddr->sll_protocol = ETHERTYPE_IP; */
87 hwaddr->sll_hatype = ARPHRD_ETHER;
88 hwaddr->sll_halen = ETH_ALEN;
89 memcpy(hwaddr->sll_addr, hwbuf, ETH_ALEN);
90 }
91 }
92 ifa->ifa_addr = (struct sockaddr *)hwaddr;
93 ifa->ifa_netmask = (struct sockaddr *)smask;
94 }
95 return ifa;
96}
97
98int getifaddrs(struct ifaddrs **ifap)
99{
100 DIR *d;
101 struct dirent *de;
102 struct ifaddrs *ifa;
103 struct ifaddrs *ifah = NULL;
104
105 if (!ifap)
106 return -1;
107 *ifap = NULL;
108
109 if (ifc_init())
110 return -1;
111
112 d = opendir("/sys/class/net");
113 if (d == 0)
114 return -1;
115 while ((de = readdir(d))) {
116 if (de->d_name[0] == '.')
117 continue;
118 ifa = get_interface(de->d_name, AF_INET);
119 if (ifa != NULL) {
120 ifa->ifa_next = ifah;
121 ifah = ifa;
122 }
123 ifa = get_interface(de->d_name, AF_PACKET);
124 if (ifa != NULL) {
125 ifa->ifa_next = ifah;
126 ifah = ifa;
127 }
128 }
129 *ifap = ifah;
130 closedir(d);
131 ifc_close();
132 return 0;
133}
134
135void freeifaddrs(struct ifaddrs *ifa)
136{
137 struct ifaddrs *ifp;
138
139 while (ifa) {
140 ifp = ifa;
141 free(ifp->ifa_name);
142 if (ifp->ifa_addr)
143 free(ifp->ifa_addr);
144 if (ifp->ifa_netmask)
145 free(ifp->ifa_netmask);
146 ifa = ifa->ifa_next;
147 free(ifp);
148 }
149}