-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkUtils.h
More file actions
144 lines (121 loc) · 4.5 KB
/
NetworkUtils.h
File metadata and controls
144 lines (121 loc) · 4.5 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Super Timecode Converter
// Copyright (c) 2026 Fiverecords -- MIT License
// https://github.com/fiverecords/SuperTimecodeConverter
#pragma once
#include <JuceHeader.h>
#include <vector>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#else
#include <ifaddrs.h>
#include <net/if.h>
#include <arpa/inet.h>
#endif
struct NetworkInterface
{
juce::String name;
juce::String ip;
juce::String broadcast;
juce::String subnet;
};
//==============================================================================
// Enumerate active (non-loopback) IPv4 network interfaces
//==============================================================================
inline juce::Array<NetworkInterface> getNetworkInterfaces()
{
juce::Array<NetworkInterface> interfaces;
#ifdef _WIN32
ULONG bufSize = 15000;
std::vector<uint8_t> buffer(bufSize);
PIP_ADAPTER_ADDRESSES addresses = nullptr;
ULONG result = 0;
for (int attempts = 0; attempts < 3; attempts++)
{
buffer.resize(bufSize);
addresses = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
result = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, addresses, &bufSize);
if (result == ERROR_BUFFER_OVERFLOW)
{
addresses = nullptr;
continue;
}
break;
}
if (result != NO_ERROR || addresses == nullptr)
return interfaces;
for (auto* adapter = addresses; adapter != nullptr; adapter = adapter->Next)
{
if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
continue;
if (adapter->OperStatus != IfOperStatusUp)
continue;
for (auto* unicast = adapter->FirstUnicastAddress; unicast != nullptr; unicast = unicast->Next)
{
if (unicast->Address.lpSockaddr->sa_family == AF_INET)
{
auto* addr = (sockaddr_in*)unicast->Address.lpSockaddr;
char ipStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr->sin_addr, ipStr, sizeof(ipStr));
ULONG prefixLength = unicast->OnLinkPrefixLength;
uint32_t ip = ntohl(addr->sin_addr.s_addr);
uint32_t mask = (prefixLength == 0) ? 0u : (prefixLength >= 32) ? ~0u : (~0u << (32 - prefixLength));
uint32_t broadcast = ip | ~mask;
char broadcastStr[INET_ADDRSTRLEN];
struct in_addr broadcastAddr;
broadcastAddr.s_addr = htonl(broadcast);
inet_ntop(AF_INET, &broadcastAddr, broadcastStr, sizeof(broadcastStr));
char maskStr[INET_ADDRSTRLEN];
struct in_addr maskAddr;
maskAddr.s_addr = htonl(mask);
inet_ntop(AF_INET, &maskAddr, maskStr, sizeof(maskStr));
NetworkInterface ni;
ni.name = juce::String(adapter->FriendlyName);
ni.ip = juce::String(ipStr);
ni.broadcast = juce::String(broadcastStr);
ni.subnet = juce::String(maskStr);
interfaces.add(ni);
}
}
}
#else
struct ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1)
return interfaces;
for (auto* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == nullptr || ifa->ifa_addr->sa_family != AF_INET)
continue;
if (ifa->ifa_flags & IFF_LOOPBACK)
continue;
if (!(ifa->ifa_flags & IFF_UP))
continue;
auto* addr = (sockaddr_in*)ifa->ifa_addr;
char ipStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr->sin_addr, ipStr, sizeof(ipStr));
char broadcastStr[INET_ADDRSTRLEN] = "255.255.255.255";
if (ifa->ifa_broadaddr)
{
auto* baddr = (sockaddr_in*)ifa->ifa_broadaddr;
inet_ntop(AF_INET, &baddr->sin_addr, broadcastStr, sizeof(broadcastStr));
}
char maskStr[INET_ADDRSTRLEN] = "255.255.255.0";
if (ifa->ifa_netmask)
{
auto* maddr = (sockaddr_in*)ifa->ifa_netmask;
inet_ntop(AF_INET, &maddr->sin_addr, maskStr, sizeof(maskStr));
}
NetworkInterface ni;
ni.name = juce::String(ifa->ifa_name);
ni.ip = juce::String(ipStr);
ni.broadcast = juce::String(broadcastStr);
ni.subnet = juce::String(maskStr);
interfaces.add(ni);
}
freeifaddrs(ifaddr);
#endif
return interfaces;
}