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