@@ -155,183 +155,3 @@ int uv_exepath(char* buffer, size_t* size) {
155
155
return UV_EINVAL ;
156
156
}
157
157
}
158
-
159
-
160
- int uv_interface_addresses (uv_interface_address_t * * addresses , int * count ) {
161
- uv_interface_address_t * address ;
162
- int sockfd , sock6fd , inet6 , i , r , size = 1 ;
163
- struct ifconf ifc ;
164
- struct ifreq * ifr , * p , flg ;
165
- struct in6_ifreq if6 ;
166
- struct sockaddr_dl * sa_addr ;
167
-
168
- ifc .ifc_req = NULL ;
169
- sock6fd = -1 ;
170
- r = 0 ;
171
- * count = 0 ;
172
- * addresses = NULL ;
173
-
174
- if (0 > (sockfd = socket (AF_INET , SOCK_DGRAM , IPPROTO_IP ))) {
175
- r = UV__ERR (errno );
176
- goto cleanup ;
177
- }
178
-
179
- if (0 > (sock6fd = socket (AF_INET6 , SOCK_DGRAM , IPPROTO_IP ))) {
180
- r = UV__ERR (errno );
181
- goto cleanup ;
182
- }
183
-
184
- if (ioctl (sockfd , SIOCGSIZIFCONF , & size ) == -1 ) {
185
- r = UV__ERR (errno );
186
- goto cleanup ;
187
- }
188
-
189
- ifc .ifc_req = (struct ifreq * )uv__malloc (size );
190
- if (ifc .ifc_req == NULL ) {
191
- r = UV_ENOMEM ;
192
- goto cleanup ;
193
- }
194
- ifc .ifc_len = size ;
195
- if (ioctl (sockfd , SIOCGIFCONF , & ifc ) == -1 ) {
196
- r = UV__ERR (errno );
197
- goto cleanup ;
198
- }
199
-
200
- #define ADDR_SIZE (p ) MAX((p).sa_len, sizeof(p))
201
-
202
- /* Count all up and running ipv4/ipv6 addresses */
203
- ifr = ifc .ifc_req ;
204
- while ((char * )ifr < (char * )ifc .ifc_req + ifc .ifc_len ) {
205
- p = ifr ;
206
- ifr = (struct ifreq * )
207
- ((char * )ifr + sizeof (ifr -> ifr_name ) + ADDR_SIZE (ifr -> ifr_addr ));
208
-
209
- if (!(p -> ifr_addr .sa_family == AF_INET6 ||
210
- p -> ifr_addr .sa_family == AF_INET ))
211
- continue ;
212
-
213
- memcpy (flg .ifr_name , p -> ifr_name , sizeof (flg .ifr_name ));
214
- if (ioctl (sockfd , SIOCGIFFLAGS , & flg ) == -1 ) {
215
- r = UV__ERR (errno );
216
- goto cleanup ;
217
- }
218
-
219
- if (!(flg .ifr_flags & IFF_UP && flg .ifr_flags & IFF_RUNNING ))
220
- continue ;
221
-
222
- (* count )++ ;
223
- }
224
-
225
- if (* count == 0 )
226
- goto cleanup ;
227
-
228
- /* Alloc the return interface structs */
229
- * addresses = uv__calloc (* count , sizeof (* * addresses ));
230
- if (!(* addresses )) {
231
- r = UV_ENOMEM ;
232
- goto cleanup ;
233
- }
234
- address = * addresses ;
235
-
236
- ifr = ifc .ifc_req ;
237
- while ((char * )ifr < (char * )ifc .ifc_req + ifc .ifc_len ) {
238
- p = ifr ;
239
- ifr = (struct ifreq * )
240
- ((char * )ifr + sizeof (ifr -> ifr_name ) + ADDR_SIZE (ifr -> ifr_addr ));
241
-
242
- if (!(p -> ifr_addr .sa_family == AF_INET6 ||
243
- p -> ifr_addr .sa_family == AF_INET ))
244
- continue ;
245
-
246
- inet6 = (p -> ifr_addr .sa_family == AF_INET6 );
247
-
248
- memcpy (flg .ifr_name , p -> ifr_name , sizeof (flg .ifr_name ));
249
- if (ioctl (sockfd , SIOCGIFFLAGS , & flg ) == -1 )
250
- goto syserror ;
251
-
252
- if (!(flg .ifr_flags & IFF_UP && flg .ifr_flags & IFF_RUNNING ))
253
- continue ;
254
-
255
- /* All conditions above must match count loop */
256
-
257
- address -> name = uv__strdup (p -> ifr_name );
258
-
259
- if (inet6 )
260
- address -> address .address6 = * ((struct sockaddr_in6 * ) & p -> ifr_addr );
261
- else
262
- address -> address .address4 = * ((struct sockaddr_in * ) & p -> ifr_addr );
263
-
264
- if (inet6 ) {
265
- memset (& if6 , 0 , sizeof (if6 ));
266
- r = uv__strscpy (if6 .ifr_name , p -> ifr_name , sizeof (if6 .ifr_name ));
267
- if (r == UV_E2BIG )
268
- goto cleanup ;
269
- r = 0 ;
270
- memcpy (& if6 .ifr_Addr , & p -> ifr_addr , sizeof (if6 .ifr_Addr ));
271
- if (ioctl (sock6fd , SIOCGIFNETMASK6 , & if6 ) == -1 )
272
- goto syserror ;
273
- address -> netmask .netmask6 = * ((struct sockaddr_in6 * ) & if6 .ifr_Addr );
274
- /* Explicitly set family as the ioctl call appears to return it as 0. */
275
- address -> netmask .netmask6 .sin6_family = AF_INET6 ;
276
- } else {
277
- if (ioctl (sockfd , SIOCGIFNETMASK , p ) == -1 )
278
- goto syserror ;
279
- address -> netmask .netmask4 = * ((struct sockaddr_in * ) & p -> ifr_addr );
280
- /* Explicitly set family as the ioctl call appears to return it as 0. */
281
- address -> netmask .netmask4 .sin_family = AF_INET ;
282
- }
283
-
284
- address -> is_internal = flg .ifr_flags & IFF_LOOPBACK ? 1 : 0 ;
285
-
286
- address ++ ;
287
- }
288
-
289
- /* Fill in physical addresses. */
290
- ifr = ifc .ifc_req ;
291
- while ((char * )ifr < (char * )ifc .ifc_req + ifc .ifc_len ) {
292
- p = ifr ;
293
- ifr = (struct ifreq * )
294
- ((char * )ifr + sizeof (ifr -> ifr_name ) + ADDR_SIZE (ifr -> ifr_addr ));
295
-
296
- if (p -> ifr_addr .sa_family != AF_LINK )
297
- continue ;
298
-
299
- address = * addresses ;
300
- for (i = 0 ; i < * count ; i ++ ) {
301
- if (strcmp (address -> name , p -> ifr_name ) == 0 ) {
302
- sa_addr = (struct sockaddr_dl * ) & p -> ifr_addr ;
303
- memcpy (address -> phys_addr , LLADDR (sa_addr ), sizeof (address -> phys_addr ));
304
- }
305
- address ++ ;
306
- }
307
- }
308
-
309
- #undef ADDR_SIZE
310
- goto cleanup ;
311
-
312
- syserror :
313
- uv_free_interface_addresses (* addresses , * count );
314
- * addresses = NULL ;
315
- * count = 0 ;
316
- r = UV_ENOSYS ;
317
-
318
- cleanup :
319
- if (sockfd != -1 )
320
- uv__close (sockfd );
321
- if (sock6fd != -1 )
322
- uv__close (sock6fd );
323
- uv__free (ifc .ifc_req );
324
- return r ;
325
- }
326
-
327
-
328
- void uv_free_interface_addresses (uv_interface_address_t * addresses ,
329
- int count ) {
330
- int i ;
331
-
332
- for (i = 0 ; i < count ; ++ i ) {
333
- uv__free (addresses [i ].name );
334
- }
335
-
336
- uv__free (addresses );
337
- }
0 commit comments