summaryrefslogtreecommitdiffstats
path: root/src/common/win32/ifaddrs.cc
blob: d7de4a5ff8ae2892872a54c88d990aef40fb0540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
109
#include <errno.h>
#include <winsock2.h>
#include <wincrypt.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <ifaddrs.h>
#include <stdio.h>

#include "include/compat.h"

int getifaddrs(struct ifaddrs **ifap)
{
  int ret = 0;

  DWORD size, res = 0;
  res = GetAdaptersAddresses(
    AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
    NULL, NULL, &size);
  if (res != ERROR_BUFFER_OVERFLOW) {
    errno = ENOMEM;
    return -1;
  }

  PIP_ADAPTER_ADDRESSES adapter_addrs = (PIP_ADAPTER_ADDRESSES)malloc(size);
  res = GetAdaptersAddresses(
    AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX,
    NULL, adapter_addrs, &size);
  if (res != ERROR_SUCCESS) {
    errno = ENOMEM;
    return -1;
  }

  struct ifaddrs *out_list_head = NULL;
  struct ifaddrs *out_list_curr;

  for (PIP_ADAPTER_ADDRESSES curr_addrs = adapter_addrs;
       curr_addrs != NULL;
       curr_addrs = curr_addrs->Next) {
    if (curr_addrs->OperStatus != 1)
      continue;

    for (PIP_ADAPTER_UNICAST_ADDRESS unicast_addrs = curr_addrs->FirstUnicastAddress;
         unicast_addrs != NULL;
         unicast_addrs = unicast_addrs->Next) {
      SOCKADDR* unicast_sockaddr = unicast_addrs->Address.lpSockaddr;
      if (unicast_sockaddr->sa_family != AF_INET &&
          unicast_sockaddr->sa_family != AF_INET6)
        continue;
      out_list_curr = calloc(sizeof(*out_list_curr), 1);
      if (!out_list_curr) {
        errno = ENOMEM;
        ret = -1;
        goto out;
      }

      out_list_curr->ifa_next = out_list_head;
      out_list_head = out_list_curr;

      out_list_curr->ifa_flags = IFF_UP;
      if (curr_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
        out_list_curr->ifa_flags |= IFF_LOOPBACK;

      out_list_curr->ifa_addr = (struct sockaddr *) &out_list_curr->in_addrs;
      out_list_curr->ifa_netmask = (struct sockaddr *) &out_list_curr->in_netmasks;
      out_list_curr->ifa_name = out_list_curr->ad_name;

      if (unicast_sockaddr->sa_family == AF_INET) {
        ULONG subnet_mask = 0;
        if (ConvertLengthToIpv4Mask(unicast_addrs->OnLinkPrefixLength, &subnet_mask)) {
          errno = ENODATA;
          ret = -1;
          goto out;
        }
        struct sockaddr_in *addr4 = (struct sockaddr_in *) &out_list_curr->in_addrs;
        struct sockaddr_in *netmask4 = (struct sockaddr_in *) &out_list_curr->in_netmasks;
        netmask4->sin_family = unicast_sockaddr->sa_family;
        addr4->sin_family = unicast_sockaddr->sa_family;
        netmask4->sin_addr.S_un.S_addr = subnet_mask;
        addr4->sin_addr = ((struct sockaddr_in*) unicast_sockaddr)->sin_addr;
      } else {
        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &out_list_curr->in_addrs;
        (*addr6) = *(struct sockaddr_in6 *) unicast_sockaddr;
      }
      out_list_curr->speed = curr_addrs->TransmitLinkSpeed;
      // TODO maybe use friendly name instead of adapter GUID
      sprintf_s(out_list_curr->ad_name,
                sizeof(out_list_curr->ad_name),
                curr_addrs->AdapterName);
    }
  }
  ret = 0;
out:
  free(adapter_addrs);
  if (ret && out_list_head)
    free(out_list_head);
  else if (ifap)
    *ifap = out_list_head;

  return ret;
}

void freeifaddrs(struct ifaddrs *ifa)
{
  while (ifa) {
    struct ifaddrs *next = ifa->ifa_next;
    free(ifa);
    ifa = next;
  }
}