File: WindowsInterfacePicker.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (119 lines) | stat: -rw-r--r-- 3,980 bytes parent folder | download | duplicates (4)
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
110
111
112
113
114
115
116
117
118
119
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * WindowsInterfacePicker.cpp
 * Chooses an interface to listen on
 * Copyright (C) 2005 Simon Newton
 */

typedef int socklen_t;
#include <ola/win/CleanWinSock2.h>
#include <Lm.h>
#include <iphlpapi.h>
#include <unistd.h>
#include <vector>

#include "ola/network/IPV4Address.h"
#include "common/network/WindowsInterfacePicker.h"
#include "ola/Logging.h"

namespace ola {
namespace network {

using std::vector;

/*
 * Return a vector of interfaces on the system.
 */
vector<Interface> WindowsInterfacePicker::GetInterfaces(
    bool include_loopback) const {
  vector<Interface> interfaces;

  PIP_ADAPTER_INFO pAdapter = NULL;
  PIP_ADAPTER_INFO pAdapterInfo;
  IP_ADDR_STRING *ipAddress;
  ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
  uint32_t net, mask;

  if (include_loopback) {
    OLA_WARN << "Loopback interface inclusion requested. Loopback might not "
             << "exist on Windows";
  }

  while (1) {
    pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new uint8_t[ulOutBufLen]);
    if (!pAdapterInfo) {
      OLA_WARN << "Error allocating memory needed for GetAdaptersInfo";
      return interfaces;
    }

    // if ulOutBufLen isn't long enough it will be set to the size of the
    // buffer required
    DWORD status = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
    if (status == NO_ERROR) {
      break;
    }

    delete[] pAdapterInfo;
    if (status != ERROR_BUFFER_OVERFLOW) {
      OLA_WARN << "GetAdaptersInfo failed with error: "
               << static_cast<int>(status);
      return interfaces;
    }
  }

  for (pAdapter = pAdapterInfo;
       pAdapter && pAdapter < pAdapterInfo + ulOutBufLen;
       pAdapter = pAdapter->Next) {
    // Since Vista, wireless interfaces return a different type
    // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa366062(v=vs.85).aspx
    if ((pAdapter->Type != MIB_IF_TYPE_ETHERNET) &&
        (pAdapter->Type != IF_TYPE_IEEE80211)) {
      OLA_INFO << "Skipping " << pAdapter->AdapterName
               << " (" << pAdapter->Description << ")"
               << " as it's not MIB_IF_TYPE_ETHERNET"
               << " or IF_TYPE_IEEE80211, got "
               << pAdapter->Type << " instead";
      continue;
    }

    for (ipAddress = &pAdapter->IpAddressList; ipAddress;
         ipAddress = ipAddress->Next) {
      net = inet_addr(ipAddress->IpAddress.String);
      // Windows doesn't seem to have the notion of an interface being 'up'
      // so we check if this interface has an address assigned.
      if (net) {
        Interface iface;
        iface.name = pAdapter->AdapterName;  // IFNAME_SIZE
        iface.index = pAdapter->Index;
        uint8_t macaddr[MACAddress::LENGTH];
        memcpy(macaddr, pAdapter->Address, MACAddress::LENGTH);
        iface.hw_address = MACAddress(macaddr);
        iface.ip_address = IPV4Address(net);

        mask = inet_addr(ipAddress->IpMask.String);
        iface.subnet_mask = IPV4Address(mask);
        iface.bcast_address = IPV4Address((iface.ip_address.AsInt() & mask) |
                                          (0xFFFFFFFF ^ mask));

        interfaces.push_back(iface);
      }
    }
  }
  delete[] pAdapterInfo;
  return interfaces;
}
}  // namespace network
}  // namespace ola