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