aboutsummaryrefslogtreecommitdiff
path: root/src/util/os_network.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/os_network.c')
-rw-r--r--src/util/os_network.c351
1 files changed, 174 insertions, 177 deletions
diff --git a/src/util/os_network.c b/src/util/os_network.c
index ccf326c05..af8046814 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -51,120 +51,118 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
51 theIP = inet_addr ("192.0.34.166"); /* www.example.com */ 51 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
52 if ((!GNGetBestInterface) || 52 if ((!GNGetBestInterface) ||
53 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR)) 53 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
54 { 54 {
55 dwExternalNIC = 0; 55 dwExternalNIC = 0;
56 } 56 }
57 57
58 /* Enumerate NICs */ 58 /* Enumerate NICs */
59 EnumNICs (&pTable, &pAddrTable); 59 EnumNICs (&pTable, &pAddrTable);
60 60
61 if (pTable) 61 if (pTable)
62 {
63 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
62 { 64 {
63 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++) 65 char szEntry[1001];
64 { 66 DWORD dwIP = 0;
65 char szEntry[1001]; 67 PIP_ADAPTER_INFO pAdapterInfo;
66 DWORD dwIP = 0; 68 PIP_ADAPTER_INFO pAdapter = NULL;
67 PIP_ADAPTER_INFO pAdapterInfo; 69 DWORD dwRetVal = 0;
68 PIP_ADAPTER_INFO pAdapter = NULL;
69 DWORD dwRetVal = 0;
70 70
71 /* Get IP-Address */ 71 /* Get IP-Address */
72 int i; 72 int i;
73 for (i = 0; i < pAddrTable->dwNumEntries; i++)
74 {
75 if (pAddrTable->table[i].dwIndex ==
76 pTable->table[dwIfIdx].dwIndex)
77 {
78 dwIP = pAddrTable->table[i].dwAddr;
79 break;
80 }
81 }
82 73
83 if (dwIP) 74 for (i = 0; i < pAddrTable->dwNumEntries; i++)
84 { 75 {
85 BYTE bPhysAddr[MAXLEN_PHYSADDR]; 76 if (pAddrTable->table[i].dwIndex == pTable->table[dwIfIdx].dwIndex)
86 char *pszIfName = NULL; 77 {
87 char dst[INET_ADDRSTRLEN]; 78 dwIP = pAddrTable->table[i].dwAddr;
88 struct sockaddr_in sa; 79 break;
80 }
81 }
89 82
90 /* Get friendly interface name */ 83 if (dwIP)
91 pAdapterInfo = 84 {
92 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO)); 85 BYTE bPhysAddr[MAXLEN_PHYSADDR];
93 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); 86 char *pszIfName = NULL;
87 char dst[INET_ADDRSTRLEN];
88 struct sockaddr_in sa;
94 89
95 /* Make an initial call to GetAdaptersInfo to get 90 /* Get friendly interface name */
96 the necessary size into the ulOutBufLen variable */ 91 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
97 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) == 92 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
98 ERROR_BUFFER_OVERFLOW)
99 {
100 free (pAdapterInfo);
101 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
102 }
103 93
104 if ((dwRetVal = 94 /* Make an initial call to GetAdaptersInfo to get
105 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR) 95 * the necessary size into the ulOutBufLen variable */
106 { 96 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
107 pAdapter = pAdapterInfo; 97 ERROR_BUFFER_OVERFLOW)
108 while (pAdapter) 98 {
109 { 99 free (pAdapterInfo);
110 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index) 100 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
111 { 101 }
112 char szKey[251];
113 long lLen = 250;
114 102
115 sprintf (szKey, 103 if ((dwRetVal =
116 "SYSTEM\\CurrentControlSet\\Control\\Network\\" 104 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
117 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection", 105 {
118 pAdapter->AdapterName); 106 pAdapter = pAdapterInfo;
119 pszIfName = (char *) malloc (251); 107 while (pAdapter)
120 if (QueryRegistry 108 {
121 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName, 109 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
122 &lLen) != ERROR_SUCCESS) 110 {
123 { 111 char szKey[251];
124 free (pszIfName); 112 long lLen = 250;
125 pszIfName = NULL;
126 }
127 }
128 pAdapter = pAdapter->Next;
129 }
130 }
131 free (pAdapterInfo);
132 113
133 /* Set entry */ 114 sprintf (szKey,
134 memset (bPhysAddr, 0, MAXLEN_PHYSADDR); 115 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
135 memcpy (bPhysAddr, 116 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
136 pTable->table[dwIfIdx].bPhysAddr, 117 pAdapter->AdapterName);
137 pTable->table[dwIfIdx].dwPhysAddrLen); 118 pszIfName = (char *) malloc (251);
119 if (QueryRegistry
120 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
121 &lLen) != ERROR_SUCCESS)
122 {
123 free (pszIfName);
124 pszIfName = NULL;
125 }
126 }
127 pAdapter = pAdapter->Next;
128 }
129 }
130 free (pAdapterInfo);
138 131
139 snprintf (szEntry, 1000, "%s (%s - %I64u)", 132 /* Set entry */
140 pszIfName ? pszIfName : (char *) 133 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
141 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET, 134 memcpy (bPhysAddr,
142 &dwIP, dst, 135 pTable->table[dwIfIdx].bPhysAddr,
143 INET_ADDRSTRLEN), 136 pTable->table[dwIfIdx].dwPhysAddrLen);
144 *((unsigned long long *) bPhysAddr));
145 szEntry[1000] = 0;
146 137
147 if (pszIfName) 138 snprintf (szEntry, 1000, "%s (%s - %I64u)",
148 free (pszIfName); 139 pszIfName ? pszIfName : (char *)
140 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
141 &dwIP, dst,
142 INET_ADDRSTRLEN),
143 *((unsigned long long *) bPhysAddr));
144 szEntry[1000] = 0;
149 145
150 sa.sin_family = AF_INET; 146 if (pszIfName)
147 free (pszIfName);
148
149 sa.sin_family = AF_INET;
151#if HAVE_SOCKADDR_IN_SIN_LEN 150#if HAVE_SOCKADDR_IN_SIN_LEN
152 sa.sin_len = (u_char) sizeof (struct sockaddr_in); 151 sa.sin_len = (u_char) sizeof (struct sockaddr_in);
153#endif 152#endif
154 sa.sin_addr.S_un.S_addr = dwIP; 153 sa.sin_addr.S_un.S_addr = dwIP;
155 154
156 if (GNUNET_OK != 155 if (GNUNET_OK !=
157 proc (proc_cls, 156 proc (proc_cls,
158 szEntry, 157 szEntry,
159 pTable->table[dwIfIdx].dwIndex == dwExternalNIC, 158 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
160 (const struct sockaddr *) &sa, 159 (const struct sockaddr *) &sa, sizeof (sa)))
161 sizeof (sa))) 160 break;
162 break; 161 }
163 }
164 }
165 GlobalFree (pAddrTable);
166 GlobalFree (pTable);
167 } 162 }
163 GlobalFree (pAddrTable);
164 GlobalFree (pTable);
165 }
168 166
169 return; 167 return;
170 168
@@ -175,29 +173,29 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
175 socklen_t alen; 173 socklen_t alen;
176 174
177 if (getifaddrs (&ifa_first) == 0) 175 if (getifaddrs (&ifa_first) == 0)
176 {
177 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
178 { 178 {
179 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) 179 if (ifa_ptr->ifa_name != NULL &&
180 { 180 ifa_ptr->ifa_addr != NULL && (ifa_ptr->ifa_flags & IFF_UP) != 0)
181 if (ifa_ptr->ifa_name != NULL && 181 {
182 ifa_ptr->ifa_addr != NULL && (ifa_ptr->ifa_flags & IFF_UP) != 0) 182 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
183 { 183 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
184 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && 184 continue;
185 (ifa_ptr->ifa_addr->sa_family != AF_INET6)) 185 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
186 continue; 186 alen = sizeof (struct sockaddr_in);
187 if (ifa_ptr->ifa_addr->sa_family == AF_INET) 187 else
188 alen = sizeof (struct sockaddr_in); 188 alen = sizeof (struct sockaddr_in6);
189 else 189 if (GNUNET_OK != proc (proc_cls,
190 alen = sizeof (struct sockaddr_in6); 190 ifa_ptr->ifa_name,
191 if (GNUNET_OK != proc (proc_cls, 191 0 == strcmp (ifa_ptr->ifa_name,
192 ifa_ptr->ifa_name, 192 GNUNET_DEFAULT_INTERFACE),
193 0 == strcmp (ifa_ptr->ifa_name, 193 ifa_ptr->ifa_addr, alen))
194 GNUNET_DEFAULT_INTERFACE), 194 break;
195 ifa_ptr->ifa_addr, alen)) 195 }
196 break;
197 }
198 }
199 freeifaddrs (ifa_first);
200 } 196 }
197 freeifaddrs (ifa_first);
198 }
201#else 199#else
202 char line[1024]; 200 char line[1024];
203 const char *start; 201 const char *start;
@@ -218,78 +216,77 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
218 else 216 else
219 f = popen ("ifconfig -a 2> /dev/null", "r"); 217 f = popen ("ifconfig -a 2> /dev/null", "r");
220 if (!f) 218 if (!f)
221 { 219 {
222 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | 220 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
223 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig"); 221 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
224 return; 222 return;
225 } 223 }
226 224
227 have_ifc = GNUNET_NO; 225 have_ifc = GNUNET_NO;
228 ifc[11] = '\0'; 226 ifc[11] = '\0';
229 while (NULL != fgets (line, sizeof (line), f)) 227 while (NULL != fgets (line, sizeof (line), f))
228 {
229 if (strlen (line) == 0)
230 { 230 {
231 if (strlen (line) == 0) 231 have_ifc = GNUNET_NO;
232 { 232 continue;
233 have_ifc = GNUNET_NO; 233 }
234 continue; 234 if (!isspace (line[0]))
235 } 235 {
236 if (!isspace (line[0])) 236 have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
237 { 237 /* would end with ':' on OSX, fix it! */
238 have_ifc = 238 if (ifc[strlen (ifc) - 1] == ':')
239 (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO; 239 ifc[strlen (ifc) - 1] = '\0';
240 /* would end with ':' on OSX, fix it! */ 240 continue;
241 if (ifc[strlen (ifc) - 1] == ':') 241 }
242 ifc[strlen (ifc) - 1] = '\0'; 242 if (!have_ifc)
243 continue; 243 continue; /* strange input, hope for the best */
244 } 244 start = line;
245 if (!have_ifc) 245 while (('\0' != *start) && (isspace (*start)))
246 continue; /* strange input, hope for the best */ 246 start++;
247 start = line; 247 if ( /* Linux */
248 while (('\0' != *start) && (isspace (*start))) 248 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
249 start++; 249 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
250 if ( /* Linux */ 250 /* Solaris, OS X */
251 (1 == SSCANF (start, "inet addr:%127s", addrstr)) || 251 (1 == SSCANF (start, "inet %127s", addrstr)) ||
252 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) || 252 (1 == SSCANF (start, "inet6 %127s", addrstr)))
253 /* Solaris, OS X */ 253 {
254 (1 == SSCANF (start, "inet %127s", addrstr)) || 254 /* IPv4 */
255 (1 == SSCANF (start, "inet6 %127s", addrstr))) 255 if (1 == inet_pton (AF_INET, addrstr, &v4))
256 { 256 {
257 /* IPv4 */ 257 memset (&a4, 0, sizeof (a4));
258 if (1 == inet_pton (AF_INET, addrstr, &v4)) 258 a4.sin_family = AF_INET;
259 {
260 memset (&a4, 0, sizeof (a4));
261 a4.sin_family = AF_INET;
262#if HAVE_SOCKADDR_IN_SIN_LEN 259#if HAVE_SOCKADDR_IN_SIN_LEN
263 a4.sin_len = (u_char) sizeof (struct sockaddr_in); 260 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
264#endif 261#endif
265 a4.sin_addr = v4; 262 a4.sin_addr = v4;
266 if (GNUNET_OK != 263 if (GNUNET_OK !=
267 proc (proc_cls, 264 proc (proc_cls,
268 ifc, 265 ifc,
269 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 266 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
270 (const struct sockaddr *) &a4, sizeof (a4))) 267 (const struct sockaddr *) &a4, sizeof (a4)))
271 break; 268 break;
272 continue; 269 continue;
273 } 270 }
274 /* IPv6 */ 271 /* IPv6 */
275 if (1 == inet_pton (AF_INET6, addrstr, &v6)) 272 if (1 == inet_pton (AF_INET6, addrstr, &v6))
276 { 273 {
277 memset (&a6, 0, sizeof (a6)); 274 memset (&a6, 0, sizeof (a6));
278 a6.sin6_family = AF_INET6; 275 a6.sin6_family = AF_INET6;
279#if HAVE_SOCKADDR_IN_SIN_LEN 276#if HAVE_SOCKADDR_IN_SIN_LEN
280 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6); 277 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
281#endif 278#endif
282 a6.sin6_addr = v6; 279 a6.sin6_addr = v6;
283 if (GNUNET_OK != 280 if (GNUNET_OK !=
284 proc (proc_cls, 281 proc (proc_cls,
285 ifc, 282 ifc,
286 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE), 283 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
287 (const struct sockaddr *) &a6, sizeof (a6))) 284 (const struct sockaddr *) &a6, sizeof (a6)))
288 break; 285 break;
289 continue; 286 continue;
290 } 287 }
291 }
292 } 288 }
289 }
293 pclose (f); 290 pclose (f);
294#endif 291#endif
295} 292}