interfaces.c 18.4 KB
Newer Older
Youness Alaoui's avatar
Youness Alaoui committed
1
2
3
4
5
/*
 * interfaces.c - Source for interface discovery code
 *
 * Copyright (C) 2006 Youness Alaoui <kakaroto@kakaroto.homelinux.net>
 * Copyright (C) 2007 Collabora, Nokia
6
 *  Contact: Youness Alaoui
Youness Alaoui's avatar
Youness Alaoui committed
7
8
 * Copyright (C) 2008 Haakon Sporsheim <haakon.sporsheim@tandberg.com>
 *
9
10
11
12
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
Youness Alaoui's avatar
Youness Alaoui committed
13
 *
14
15
16
17
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
Youness Alaoui's avatar
Youness Alaoui committed
18
 *
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
 *   Youness Alaoui, Collabora Ltd.
 *   Kai Vehmanen, Nokia
 *   Philip Withnall, Collabora Ltd.
 *   Haakon Sporsheim
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
Youness Alaoui's avatar
Youness Alaoui committed
40
41
42
43
44
45
46
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "interfaces.h"
47
#include "agent-priv.h"
Youness Alaoui's avatar
Youness Alaoui committed
48
49
50
51
52
53
54
55
56
57
58

#ifdef G_OS_UNIX

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>

59
#include <sys/socket.h>
Alexis Ballier's avatar
Alexis Ballier committed
60
#include <netinet/in.h>
61
#include <netdb.h>
62

Apostolos Syropoulos's avatar
Apostolos Syropoulos committed
63
64
65
66
#ifdef __sun
#include <sys/sockio.h>
#endif

Youness Alaoui's avatar
Youness Alaoui committed
67
68
69
70
71
72
73
#ifdef HAVE_GETIFADDRS
 #include <ifaddrs.h>
#endif

#include <net/if.h>
#include <arpa/inet.h>

74
75
#endif /* G_OS_UNIX */

76
77
78
79
80
81
82
#ifdef IGNORED_IFACE_PREFIX
static const gchar *ignored_iface_prefix_list[] = {
  IGNORED_IFACE_PREFIX,
  NULL
};
#endif

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#if (defined(G_OS_UNIX) && defined(HAVE_GETIFADDRS)) || defined(G_OS_WIN32)
/* Works on both UNIX and Windows. Magic! */
static gchar *
sockaddr_to_string (const struct sockaddr *addr)
{
  char addr_as_string[INET6_ADDRSTRLEN+1];
  size_t addr_len;

  switch (addr->sa_family) {
    case AF_INET: addr_len = sizeof (struct sockaddr_in); break;
    case AF_INET6: addr_len = sizeof (struct sockaddr_in6); break;
    default: return NULL;
  }

  if (getnameinfo (addr, addr_len,
          addr_as_string, sizeof (addr_as_string), NULL, 0,
          NI_NUMERICHOST) != 0) {
    return NULL;
  }

  return g_strdup (addr_as_string);
}
#endif

#ifdef G_OS_UNIX

Youness Alaoui's avatar
Youness Alaoui committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifdef HAVE_GETIFADDRS

GList *
nice_interfaces_get_local_interfaces (void)
{
  GList *interfaces = NULL;
  struct ifaddrs *ifa, *results;

  if (getifaddrs (&results) < 0) {
    return NULL;
  }

  /* Loop and get each interface the system has, one by one... */
  for (ifa = results; ifa; ifa = ifa->ifa_next) {
    /* no ip address from interface that is down */
    if ((ifa->ifa_flags & IFF_UP) == 0)
      continue;

127
    if (ifa->ifa_addr == NULL)
Youness Alaoui's avatar
Youness Alaoui committed
128
129
      continue;

130
131
132
133
    if (ifa->ifa_addr->sa_family == AF_INET || ifa->ifa_addr->sa_family == AF_INET6) {
      nice_debug ("Found interface : %s", ifa->ifa_name);
      interfaces = g_list_prepend (interfaces, g_strdup (ifa->ifa_name));
    }
Youness Alaoui's avatar
Youness Alaoui committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  }

  freeifaddrs (results);

  return interfaces;
}

#else /* ! HAVE_GETIFADDRS */

GList *
nice_interfaces_get_local_interfaces (void)
{
  GList *interfaces = NULL;
  gint sockfd;
  gint size = 0;
  struct ifreq *ifr;
  struct ifconf ifc;

  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
Jakub Adam's avatar
Jakub Adam committed
153
    nice_debug ("error : Cannot open socket to retrieve interface list");
Youness Alaoui's avatar
Youness Alaoui committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    return NULL;
  }

  ifc.ifc_len = 0;
  ifc.ifc_req = NULL;

  /* Loop and get each interface the system has, one by one... */
  do {
    size += sizeof (struct ifreq);
    /* realloc buffer size until no overflow occurs  */
    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
      nice_debug ("Error : Out of memory while allocation interface"
          "configuration structure");
      close (sockfd);
      return NULL;
    }
    ifc.ifc_len = size;

    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
      perror ("ioctl SIOCFIFCONF");
      close (sockfd);
      free (ifc.ifc_req);
      return NULL;
    }
  } while (size <= ifc.ifc_len);


  /* Loop throught the interface list and get the IP address of each IF */
  for (ifr = ifc.ifc_req;
       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
       ++ifr) {
    nice_debug ("Found interface : %s", ifr->ifr_name);
    interfaces = g_list_prepend (interfaces, g_strdup (ifr->ifr_name));
  }

  free (ifc.ifc_req);
  close (sockfd);

  return interfaces;
}
#endif /* HAVE_GETIFADDRS */


static gboolean
198
nice_interfaces_is_private_ip (const struct sockaddr *_sa)
Youness Alaoui's avatar
Youness Alaoui committed
199
{
200
201
202
  union {
    const struct sockaddr *addr;
    const struct sockaddr_in *in;
203
    const struct sockaddr_in6 *in6;
204
  } sa;
Youness Alaoui's avatar
Youness Alaoui committed
205

206
207
208
  sa.addr = _sa;

  if (sa.addr->sa_family == AF_INET) {
209
    /* 10.x.x.x/8 */
210
    if (sa.in->sin_addr.s_addr >> 24 == 0x0A)
211
      return TRUE;
Youness Alaoui's avatar
Youness Alaoui committed
212

213
    /* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */
214
    if (sa.in->sin_addr.s_addr >> 20 == 0xAC1)
215
      return TRUE;
Youness Alaoui's avatar
Youness Alaoui committed
216

217
    /* 192.168.x.x/16 */
218
    if (sa.in->sin_addr.s_addr >> 16 == 0xC0A8)
219
      return TRUE;
Youness Alaoui's avatar
Youness Alaoui committed
220

221
    /* 169.254.x.x/16  (for APIPA) */
222
    if (sa.in->sin_addr.s_addr >> 16 == 0xA9FE)
223
      return TRUE;
224
225
226
227
228
229
230
231
232
  } else if (sa.addr->sa_family == AF_INET6) {
    /* fc00::/7 Unique local address (ULA) */
    if ((sa.in6->sin6_addr.s6_addr[0] & 0xFE) == 0xFC)
      return TRUE;

    /* fe80::/10 link-local address */
    if ( (sa.in6->sin6_addr.s6_addr[0]         == 0xFE)  &&
        ((sa.in6->sin6_addr.s6_addr[1] & 0xC0) == 0x80))
      return TRUE;
233
234
  }
  
Youness Alaoui's avatar
Youness Alaoui committed
235
236
237
  return FALSE;
}

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
static GList *
add_ip_to_list (GList *list, gchar *ip, gboolean append)
{
  GList *i;

  for (i = list; i; i = i->next) {
    gchar *addr = (gchar *) i->data;

    if (g_strcmp0 (addr, ip) == 0)
      return list;
  }
  if (append)
    return g_list_append (list, ip);
  else
    return g_list_prepend (list, ip);
}

255
256
#ifdef HAVE_GETIFADDRS

Youness Alaoui's avatar
Youness Alaoui committed
257
258
259
260
261
GList *
nice_interfaces_get_local_ips (gboolean include_loopback)
{
  GList *ips = NULL;
  struct ifaddrs *ifa, *results;
262
  GList *loopbacks = NULL;
263
264
265
266
#ifdef IGNORED_IFACE_PREFIX
  const gchar **prefix;
  gboolean ignored = FALSE;
#endif
Marcus Lundblad's avatar
Marcus Lundblad committed
267

Youness Alaoui's avatar
Youness Alaoui committed
268
269
270
271
272
  if (getifaddrs (&results) < 0)
      return NULL;

  /* Loop through the interface list and get the IP address of each IF */
  for (ifa = results; ifa; ifa = ifa->ifa_next) {
273
    gchar *addr_string;
274

Youness Alaoui's avatar
Youness Alaoui committed
275
276
277
278
    /* no ip address from interface that is down */
    if ((ifa->ifa_flags & IFF_UP) == 0)
      continue;

279
280
281
282
    /* no ip address from interface that isn't running */
    if ((ifa->ifa_flags & IFF_RUNNING) == 0)
      continue;

283
    if (ifa->ifa_addr == NULL)
Youness Alaoui's avatar
Youness Alaoui committed
284
285
      continue;

286
    /* Convert to a string. */
287
288
    addr_string = sockaddr_to_string (ifa->ifa_addr);
    if (addr_string == NULL) {
289
290
291
292
      nice_debug ("Failed to convert address to string for interface ‘%s’.",
          ifa->ifa_name);
      continue;
    }
Youness Alaoui's avatar
Youness Alaoui committed
293
294

    nice_debug ("Interface:  %s", ifa->ifa_name);
295
    nice_debug ("IP Address: %s", addr_string);
Youness Alaoui's avatar
Youness Alaoui committed
296
    if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK) {
297
      if (include_loopback) {
298
        loopbacks = add_ip_to_list (loopbacks, addr_string, TRUE);
299
      } else {
Youness Alaoui's avatar
Youness Alaoui committed
300
        nice_debug ("Ignoring loopback interface");
301
        g_free (addr_string);
302
      }
303
304
305
      continue;
    }

306
#ifdef IGNORED_IFACE_PREFIX
307
    ignored = FALSE;
308
309
310
311
312
313
314
315
    for (prefix = ignored_iface_prefix_list; *prefix; prefix++) {
      if (g_str_has_prefix (ifa->ifa_name, *prefix)) {
        nice_debug ("Ignoring interface %s as it matches prefix %s",
            ifa->ifa_name, *prefix);
        g_free (addr_string);
        ignored = true;
        break;
      }
Youness Alaoui's avatar
Youness Alaoui committed
316
    }
317
318
319
320
321
322
323
324
325

    if (ignored)
      continue;
#endif

    if (nice_interfaces_is_private_ip (ifa->ifa_addr))
      ips = add_ip_to_list (ips, addr_string, TRUE);
    else
      ips = add_ip_to_list (ips, addr_string, FALSE);
Youness Alaoui's avatar
Youness Alaoui committed
326
327
328
329
  }

  freeifaddrs (results);

330
331
  if (loopbacks)
    ips = g_list_concat (ips, loopbacks);
Youness Alaoui's avatar
Youness Alaoui committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

  return ips;
}

#else /* ! HAVE_GETIFADDRS */

GList *
nice_interfaces_get_local_ips (gboolean include_loopback)
{
  GList *ips = NULL;
  gint sockfd;
  gint size = 0;
  struct ifreq *ifr;
  struct ifconf ifc;
  struct sockaddr_in *sa;
347
  GList *loopbacks = NULL;
348
349
350
351
#ifdef IGNORED_IFACE_PREFIX
  const gchar **prefix;
  gboolean ignored = FALSE;
#endif
Youness Alaoui's avatar
Youness Alaoui committed
352
353

  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
Jakub Adam's avatar
Jakub Adam committed
354
    nice_debug ("Error : Cannot open socket to retrieve interface list");
Youness Alaoui's avatar
Youness Alaoui committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
    return NULL;
  }

  ifc.ifc_len = 0;
  ifc.ifc_req = NULL;

  /* Loop and get each interface the system has, one by one... */
  do {
    size += sizeof (struct ifreq);
    /* realloc buffer size until no overflow occurs  */
    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
      nice_debug ("Error : Out of memory while allocation interface"
          " configuration structure");
      close (sockfd);
      return NULL;
    }
    ifc.ifc_len = size;

    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
      perror ("ioctl SIOCFIFCONF");
      close (sockfd);
      free (ifc.ifc_req);
      return NULL;
    }
  } while  (size <= ifc.ifc_len);


  /* Loop throught the interface list and get the IP address of each IF */
  for (ifr = ifc.ifc_req;
       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
       ++ifr) {

    if (ioctl (sockfd, SIOCGIFFLAGS, ifr)) {
      nice_debug ("Error : Unable to get IP information for interface %s."
          " Skipping...", ifr->ifr_name);
      continue;  /* failed to get flags, skip it */
    }
392
393
394
395
396
397
398
399
400

    /* no ip address from interface that is down */
    if ((ifr->ifr_flags & IFF_UP) == 0)
      continue;

    /* no ip address from interface that isn't running */
    if ((ifr->ifr_flags & IFF_RUNNING) == 0)
      continue;

Youness Alaoui's avatar
Youness Alaoui committed
401
402
403
404
405
    sa = (struct sockaddr_in *) &ifr->ifr_addr;
    nice_debug ("Interface:  %s", ifr->ifr_name);
    nice_debug ("IP Address: %s", inet_ntoa (sa->sin_addr));
    if ((ifr->ifr_flags & IFF_LOOPBACK) == IFF_LOOPBACK){
      if (include_loopback)
406
        loopbacks = add_ip_to_list (loopbacks, g_strdup (inet_ntoa (sa->sin_addr)), TRUE);
Youness Alaoui's avatar
Youness Alaoui committed
407
408
      else
        nice_debug ("Ignoring loopback interface");
409
410
411
      continue;
    }

412
#ifdef IGNORED_IFACE_PREFIX
413
414
415
416
417
418
419
420
421
422
423
    for (prefix = ignored_iface_prefix_list; *prefix; prefix++) {
      if (g_str_has_prefix (ifr->ifr_name, *prefix)) {
        nice_debug ("Ignoring interface %s as it matches prefix %s",
            ifr->ifr_name, *prefix);
        ignored = true;
        break;
      }
    }

    if (ignored)
      continue;
424
#endif
425
426
427

    if (nice_interfaces_is_private_ip ((struct sockaddr *) sa)) {
      ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), TRUE);
Youness Alaoui's avatar
Youness Alaoui committed
428
    } else {
429
      ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), FALSE);
Youness Alaoui's avatar
Youness Alaoui committed
430
431
432
433
434
435
    }
  }

  close (sockfd);
  free (ifc.ifc_req);

436
437
  if (loopbacks)
    ips = g_list_concat (ips, loopbacks);
Youness Alaoui's avatar
Youness Alaoui committed
438
439
440
441
442
443
444
445
446
447

  return ips;
}

#endif /* HAVE_GETIFADDRS */

gchar *
nice_interfaces_get_ip_for_interface (gchar *interface_name)
{
  struct ifreq ifr;
448
449
450
451
  union {
    struct sockaddr *addr;
    struct sockaddr_in *in;
  } sa;
Youness Alaoui's avatar
Youness Alaoui committed
452
453
  gint sockfd;

454
  g_return_val_if_fail (interface_name != NULL, NULL);
Youness Alaoui's avatar
Youness Alaoui committed
455
456
457

  ifr.ifr_addr.sa_family = AF_INET;
  memset (ifr.ifr_name, 0, sizeof (ifr.ifr_name));
458
  g_strlcpy (ifr.ifr_name, interface_name, sizeof (ifr.ifr_name));
Youness Alaoui's avatar
Youness Alaoui committed
459
460

  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
Jakub Adam's avatar
Jakub Adam committed
461
    nice_debug ("Error : Cannot open socket to retrieve interface list");
Youness Alaoui's avatar
Youness Alaoui committed
462
463
464
465
466
467
468
469
470
471
472
    return NULL;
  }

  if (ioctl (sockfd, SIOCGIFADDR, &ifr) < 0) {
    nice_debug ("Error : Unable to get IP information for interface %s",
      interface_name);
    close (sockfd);
    return NULL;
  }

  close (sockfd);
473
474
475
  sa.addr = &ifr.ifr_addr;
  nice_debug ("Address for %s: %s", interface_name, inet_ntoa (sa.in->sin_addr));
  return g_strdup (inet_ntoa (sa.in->sin_addr));
Youness Alaoui's avatar
Youness Alaoui committed
476
477
478
479
480
481
}

#else /* G_OS_UNIX */
#ifdef G_OS_WIN32

#include <winsock2.h>
482
#include <iphlpapi.h>
Youness Alaoui's avatar
Youness Alaoui committed
483

484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Should be in Iphlpapi.h, but mingw doesn't seem to have these
// Values copied directly from:
// http://msdn.microsoft.com/en-us/library/aa366845(v=vs.85).aspx
// (Title: MIB_IPADDRROW structure)

#ifndef MIB_IPADDR_DISCONNECTED
#define MIB_IPADDR_DISCONNECTED 0x0008
#endif

#ifndef MIB_IPADDR_DELETED
#define MIB_IPADDR_DELETED 0x0040
#endif

#if 0
Youness Alaoui's avatar
Youness Alaoui committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
static gboolean started_wsa_engine = FALSE;

/*
 * private function that initializes the WinSock engine and
 *  returns a prebuilt socket
 */
SOCKET nice_interfaces_get_WSA_socket ()
{
  WORD wVersionRequested;
  WSADATA wsaData;
  int err;
  SOCKET sock;

  if (started_wsa_engine == FALSE) {
    wVersionRequested = MAKEWORD ( 2, 0 );

    err = WSAStartup ( wVersionRequested, &wsaData );
    if ( err != 0 ) {
      nice_debug ("Error : Could not start the winsocket engine");
      return INVALID_SOCKET;
    }
    started_wsa_engine = TRUE;
  }


  if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
Jakub Adam's avatar
Jakub Adam committed
524
    nice_debug ("Error : Could not open socket to retrieve interface list,"
Youness Alaoui's avatar
Youness Alaoui committed
525
526
527
528
529
530
        " error no : %d", WSAGetLastError ());
    return INVALID_SOCKET;
  }

  return sock;
}
531
#endif
Youness Alaoui's avatar
Youness Alaoui committed
532

533
GList * nice_interfaces_get_local_interfaces (void)
Youness Alaoui's avatar
Youness Alaoui committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
{
  ULONG size = 0;
  PMIB_IFTABLE if_table;
  GList * ret = NULL;

  GetIfTable(NULL, &size, TRUE);

  if (!size)
    return NULL;

  if_table = (PMIB_IFTABLE)g_malloc0(size);

  if (GetIfTable(if_table, &size, TRUE) == ERROR_SUCCESS) {
    DWORD i;
    for (i = 0; i < if_table->dwNumEntries; i++) {
      ret = g_list_prepend (ret, g_strdup ((gchar*)if_table->table[i].bDescr));
    }
  }

  g_free(if_table);

  return ret;
}

GList * nice_interfaces_get_local_ips (gboolean include_loopback)
{
560
561
562
563
  IP_ADAPTER_ADDRESSES *addresses = NULL, *a;
  ULONG status;
  guint iterations;
  ULONG addresses_size;
Youness Alaoui's avatar
Youness Alaoui committed
564
  DWORD pref = 0;
565
  GList *ret = NULL;
Youness Alaoui's avatar
Youness Alaoui committed
566

567
568
569
570
  /* As suggested on
   * http://msdn.microsoft.com/en-gb/library/windows/desktop/aa365915%28v=vs.85%29.aspx */
  #define MAX_TRIES 3
  #define INITIAL_BUFFER_SIZE 15000
Youness Alaoui's avatar
Youness Alaoui committed
571

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  addresses_size = INITIAL_BUFFER_SIZE;
  iterations = 0;

  do {
    g_free (addresses);
    addresses = g_malloc0 (addresses_size);

    status = GetAdaptersAddresses (AF_UNSPEC,
        GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
        GAA_FLAG_SKIP_DNS_SERVER, NULL, addresses, &addresses_size);
  } while ((status == ERROR_BUFFER_OVERFLOW) && (iterations++ < MAX_TRIES));

  nice_debug ("Queried addresses with status %lu.", status);

  #undef INITIAL_BUFFER_SIZE
  #undef MAX_TRIES

  /* Error? */
  if (status != NO_ERROR) {
    nice_debug ("Error retrieving local addresses (error code %lu).", status);
    g_free (addresses);
Youness Alaoui's avatar
Youness Alaoui committed
593
    return NULL;
594
  }
Youness Alaoui's avatar
Youness Alaoui committed
595
596
597
598
599
600
601
602

  /*
   * Get the best interface for transport to 0.0.0.0.
   * This interface should be first in list!
   */
  if (GetBestInterface (0, &pref) != NO_ERROR)
    pref = 0;

603
604
605
  /* Loop over the adapters. */
  for (a = addresses; a != NULL; a = a->Next) {
    IP_ADAPTER_UNICAST_ADDRESS *unicast;
Youness Alaoui's avatar
Youness Alaoui committed
606

607
    nice_debug ("Interface ‘%S’:", a->FriendlyName);
Youness Alaoui's avatar
Youness Alaoui committed
608

609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
    /* Various conditions for ignoring the interface. */
    if (a->Flags & IP_ADAPTER_RECEIVE_ONLY ||
        a->OperStatus == IfOperStatusDown ||
        a->OperStatus == IfOperStatusNotPresent ||
        a->OperStatus == IfOperStatusLowerLayerDown) {
      nice_debug ("Rejecting interface due to being down or read-only.");
      continue;
    }

    if (!include_loopback &&
        a->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
      nice_debug ("Rejecting loopback interface ‘%S’.", a->FriendlyName);
      continue;
    }

    /* Grab the interface’s unicast addresses. */
    for (unicast = a->FirstUnicastAddress;
         unicast != NULL; unicast = unicast->Next) {
      gchar *addr_string;

      addr_string = sockaddr_to_string (unicast->Address.lpSockaddr);
      if (addr_string == NULL) {
        nice_debug ("Failed to convert address to string for interface ‘%S’.",
            a->FriendlyName);
        continue;
Youness Alaoui's avatar
Youness Alaoui committed
634
      }
635
636
637
638
639
640
641

      nice_debug ("IP address: %s", addr_string);

      if (a->IfIndex == pref || a->Ipv6IfIndex == pref)
        ret = g_list_prepend (ret, addr_string);
      else
        ret = g_list_append (ret, addr_string);
Youness Alaoui's avatar
Youness Alaoui committed
642
643
644
    }
  }

645
  g_free (addresses);
Youness Alaoui's avatar
Youness Alaoui committed
646
647
648
649
650
651
652

  return ret;
}

/*
 * returns ip address as an utf8 string
 */
653
654
655
// Source for idx's type (Was IF_INDEX):
// http://msdn.microsoft.com/en-us/library/aa366836(v=VS.85).aspx
// (Title: MIB_IFROW structure)
Youness Alaoui's avatar
Youness Alaoui committed
656
static gchar *
657
win32_get_ip_for_interface (DWORD idx)
Youness Alaoui's avatar
Youness Alaoui committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
{
  ULONG size = 0;
  PMIB_IPADDRTABLE ip_table;
  gchar * ret = NULL;

  GetIpAddrTable (NULL, &size, TRUE);

  if (!size)
    return NULL;

  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);

  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS) {
    DWORD i;
    for (i = 0; i < ip_table->dwNumEntries; i++) {
      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
      if (ipaddr->dwIndex == idx &&
          !(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED))) {
676
        ret = g_strdup_printf ("%lu.%lu.%lu.%lu",
Youness Alaoui's avatar
Youness Alaoui committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
            (ipaddr->dwAddr      ) & 0xFF,
            (ipaddr->dwAddr >>  8) & 0xFF,
            (ipaddr->dwAddr >> 16) & 0xFF,
            (ipaddr->dwAddr >> 24) & 0xFF);
        break;
      }
    }
  }

  g_free (ip_table);
  return ret;
}

gchar * nice_interfaces_get_ip_for_interface (gchar *interface_name)
{
  ULONG size = 0;
  PMIB_IFTABLE if_table;
  gchar * ret = NULL;

  GetIfTable (NULL, &size, TRUE);

  if (!size)
    return NULL;

  if_table = (PMIB_IFTABLE)g_malloc0 (size);

  if (GetIfTable (if_table, &size, TRUE) == ERROR_SUCCESS) {
    DWORD i;
    gchar * tmp_str;
    for (i = 0; i < if_table->dwNumEntries; i++) {
      tmp_str = g_utf16_to_utf8 (
          if_table->table[i].wszName, MAX_INTERFACE_NAME_LEN,
          NULL, NULL, NULL);

711
712
      if (strlen (interface_name) == strlen (tmp_str) &&
          g_ascii_strncasecmp (interface_name, tmp_str, strlen (interface_name)) == 0) {
Youness Alaoui's avatar
Youness Alaoui committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
        ret = win32_get_ip_for_interface (if_table->table[i].dwIndex);
        g_free (tmp_str);
        break;
      }

      g_free (tmp_str);
    }
  }

  g_free (if_table);

  return ret;
}


#else /* G_OS_WIN32 */
#error Can not use this method for retreiving ip list from OS other than unix or windows
#endif /* G_OS_WIN32 */
#endif /* G_OS_UNIX */