aboutsummaryrefslogtreecommitdiff
path: root/src/util/os_network.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-11-29 11:47:40 +0000
committerChristian Grothoff <christian@grothoff.org>2011-11-29 11:47:40 +0000
commitd155690a24d645e746d7cd12e5c1d47646d96558 (patch)
tree657f47d48cf70950ba240ebc0414a71f6cac6bed /src/util/os_network.c
parentba232c9d203303cd2beb2994fc49c5d5b700d85c (diff)
downloadgnunet-d155690a24d645e746d7cd12e5c1d47646d96558.tar.gz
gnunet-d155690a24d645e746d7cd12e5c1d47646d96558.zip
LRN: new code for GNUNET_OS_network_interfaces_list on W32 improving support for IPv6 and subnets/masks, see Mantis 1958
Diffstat (limited to 'src/util/os_network.c')
-rw-r--r--src/util/os_network.c135
1 files changed, 17 insertions, 118 deletions
diff --git a/src/util/os_network.c b/src/util/os_network.c
index 54a4e7577..a4758496e 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -25,6 +25,7 @@
25 * @author Nils Durner 25 * @author Nils Durner
26 * @author Heikki Lindholm 26 * @author Heikki Lindholm
27 * @author Jake Dust 27 * @author Jake Dust
28 * @author LRN
28 */ 29 */
29 30
30#include "platform.h" 31#include "platform.h"
@@ -46,128 +47,26 @@ GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
46 void *proc_cls) 47 void *proc_cls)
47{ 48{
48#ifdef MINGW 49#ifdef MINGW
49 PMIB_IFTABLE pTable; 50 int r;
50 PMIB_IPADDRTABLE pAddrTable; 51 int i;
51 DWORD dwIfIdx, dwExternalNIC; 52 struct EnumNICs3_results *results = NULL;
52 IPAddr theIP; 53 int results_count;
53 54
54 /* Determine our external NIC */ 55 r = EnumNICs3 (&results, &results_count);
55 theIP = inet_addr ("192.0.34.166"); /* www.example.com */ 56 if (r != GNUNET_OK)
56 if ((!GNGetBestInterface) || 57 return;
57 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
58 {
59 dwExternalNIC = 0;
60 }
61
62 /* Enumerate NICs */
63 EnumNICs (&pTable, &pAddrTable);
64 58
65 if (pTable) 59 for (i = 0; i < results_count; i++)
66 { 60 {
67 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++) 61 if (GNUNET_OK != proc (proc_cls, results[i].pretty_name,
68 { 62 results[i].is_default,
69 char szEntry[1001]; 63 &results[i].address,
70 DWORD dwIP = 0; 64 results[i].flags & ENUMNICS3_MASK_OK ? &results[i].mask : NULL,
71 PIP_ADAPTER_INFO pAdapterInfo; 65 results[i].flags & ENUMNICS3_BCAST_OK ? &results[i].broadcast : NULL,
72 PIP_ADAPTER_INFO pAdapter = NULL; 66 results[i].addr_size))
73 DWORD dwRetVal = 0; 67 break;
74
75 /* Get IP-Address */
76 int i;
77
78 for (i = 0; i < pAddrTable->dwNumEntries; i++)
79 {
80 if (pAddrTable->table[i].dwIndex == pTable->table[dwIfIdx].dwIndex)
81 {
82 dwIP = pAddrTable->table[i].dwAddr;
83 break;
84 }
85 }
86
87 if (dwIP)
88 {
89 BYTE bPhysAddr[MAXLEN_PHYSADDR];
90 char *pszIfName = NULL;
91 char dst[INET_ADDRSTRLEN];
92 struct sockaddr_in sa;
93
94 /* Get friendly interface name */
95 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
96 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
97
98 /* Make an initial call to GetAdaptersInfo to get
99 * the necessary size into the ulOutBufLen variable */
100 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
101 ERROR_BUFFER_OVERFLOW)
102 {
103 free (pAdapterInfo);
104 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
105 }
106
107 if ((dwRetVal =
108 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
109 {
110 pAdapter = pAdapterInfo;
111 while (pAdapter)
112 {
113 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
114 {
115 char szKey[251];
116 long lLen = 250;
117
118 sprintf (szKey,
119 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
120 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
121 pAdapter->AdapterName);
122 pszIfName = (char *) malloc (251);
123 if (QueryRegistry
124 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
125 &lLen) != ERROR_SUCCESS)
126 {
127 free (pszIfName);
128 pszIfName = NULL;
129 }
130 }
131 pAdapter = pAdapter->Next;
132 }
133 }
134 free (pAdapterInfo);
135
136 /* Set entry */
137 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
138 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr,
139 pTable->table[dwIfIdx].dwPhysAddrLen);
140
141 snprintf (szEntry, 1000, "%s (%s - %I64u)",
142 pszIfName ? pszIfName : (char *) pTable->
143 table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst,
144 INET_ADDRSTRLEN),
145 *((unsigned long long *) bPhysAddr));
146 szEntry[1000] = 0;
147
148 if (pszIfName)
149 free (pszIfName);
150
151 sa.sin_family = AF_INET;
152#if HAVE_SOCKADDR_IN_SIN_LEN
153 sa.sin_len = (u_char) sizeof (struct sockaddr_in);
154#endif
155 sa.sin_addr.S_un.S_addr = dwIP;
156
157 if (GNUNET_OK !=
158 proc (proc_cls, szEntry,
159 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
160 (const struct sockaddr *) &sa,
161 NULL,
162 NULL,
163 sizeof (sa)))
164 break;
165 }
166 }
167 GlobalFree (pAddrTable);
168 GlobalFree (pTable);
169 } 68 }
170 69 EnumNICs3_free (results);
171 return; 70 return;
172 71
173#elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS 72#elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS