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