OpenCPN Partial API docs
AdapterInfo.cpp
1 #include "AdapterInfo.h"
2 #include <iostream>
3 #include <winsock2.h>
4 #include <iphlpapi.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 
8 #pragma comment(lib, "iphlpapi.lib")
9 #pragma comment(lib, "ws2_32.lib")
10 
11  AdapterInfo::AdapterInfo() {
12  QueryAdapterInfo();
13  }
14 
15  void AdapterInfo::QueryAdapterInfo() {
16  PIP_ADAPTER_INFO pAdapterInfo;
17  PIP_ADAPTER_INFO pAdapter = NULL;
18  DWORD dwRetVal = 0;
19 
20  ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
21  pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
22  if (pAdapterInfo == NULL) {
23  std::cerr << "Error allocating memory needed to call GetAdaptersinfo\n";
24  return;
25  }
26 
27  // Make an initial call to GetAdaptersInfo to get the necessary size into ulOutBufLen
28  if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
29  free(pAdapterInfo);
30  pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
31  if (pAdapterInfo == NULL) {
32  std::cerr << "Error allocating memory needed to call GetAdaptersinfo\n";
33  return;
34  }
35  }
36 
37  if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
38  pAdapter = pAdapterInfo;
39  while (pAdapter) {
40 
41  if (pAdapter->GatewayList.IpAddress.String[0] != '0') { // Check if gateway is defined
42  IPAddress = pAdapter->IpAddressList.IpAddress.String;
43  NetMask = pAdapter->IpAddressList.IpMask.String;
44  GateWay = pAdapter->GatewayList.IpAddress.String;
45  break;
46  }
47 
48  pAdapter = pAdapter->Next;
49  }
50  }
51  else {
52  std::cerr << "GetAdaptersInfo failed with error: " << dwRetVal << "\n";
53  }
54 
55  if (pAdapterInfo) {
56  free(pAdapterInfo);
57  }
58 
59  }
60 
61  std::string AdapterInfo::longToIp(unsigned long ip) {
62  return std::to_string((ip >> 24) & 0xFF) + "." +
63  std::to_string((ip >> 16) & 0xFF) + "." +
64  std::to_string((ip >> 8) & 0xFF) + "." +
65  std::to_string(ip & 0xFF);
66  }
67 
68  unsigned long AdapterInfo::ipToLong(const std::string& ip) {
69  unsigned long ipBytes[4] = { 0 };
70  int count = sscanf_s(ip.c_str(), "%lu.%lu.%lu.%lu", &ipBytes[3], &ipBytes[2], &ipBytes[1], &ipBytes[0]);
71  if (count != 4) {
72  std::cerr << "Error parsing IP address." << std::endl;
73  return 0;
74  }
75  return (ipBytes[3] << 24) | (ipBytes[2] << 16) | (ipBytes[1] << 8) | ipBytes[0];
76  }
77 
78 std::string AdapterInfo::CalculateBroadcastAddress(const std::string& ip, const std::string& netmask) {
79  unsigned long ipLong = ipToLong(ip);
80  unsigned long netmaskLong = ipToLong(netmask);
81  unsigned long wildcardMask = ~netmaskLong;
82  unsigned long broadcastAddress = ipLong | wildcardMask;
83  return longToIp(broadcastAddress);
84  }
85 
86 std::string AdapterInfo::GetBroadcastAddress() {
87  return CalculateBroadcastAddress(IPAddress, NetMask);
88 
89  }
90 
91  std::string AdapterInfo::GetIPAddress() {
92  return IPAddress;
93  }
94 
95  std::string AdapterInfo::GetNetMask() {
96  return NetMask;
97  }
98 
99  std::string AdapterInfo::GetGateWay() {
100  return GateWay;
101  }