JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_IPAddress_posix.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26namespace
27{
28 struct InterfaceInfo
29 {
30 IPAddress interfaceAddress, broadcastAddress;
31 };
32
33 inline bool operator== (const InterfaceInfo& lhs, const InterfaceInfo& rhs)
34 {
35 return lhs.interfaceAddress == rhs.interfaceAddress
36 && lhs.broadcastAddress == rhs.broadcastAddress;
37 }
38
39 #if ! JUCE_WASM
40 static IPAddress makeAddress (const sockaddr_in6* addr_in)
41 {
42 if (addr_in == nullptr)
43 return {};
44
45 auto addr = addr_in->sin6_addr;
46
47 IPAddressByteUnion temp;
48 uint16 arr[8];
49
50 for (int i = 0; i < 8; ++i) // Swap bytes from network to host order
51 {
52 temp.split[0] = addr.s6_addr[i * 2 + 1];
53 temp.split[1] = addr.s6_addr[i * 2];
54
55 arr[i] = temp.combined;
56 }
57
58 return IPAddress (arr);
59 }
60
61 static IPAddress makeAddress (const sockaddr_in* addr_in)
62 {
63 if (addr_in->sin_addr.s_addr == INADDR_NONE)
64 return {};
65
66 return IPAddress (ntohl (addr_in->sin_addr.s_addr));
67 }
68
69 bool populateInterfaceInfo (struct ifaddrs* ifa, InterfaceInfo& interfaceInfo)
70 {
71 if (ifa->ifa_addr != nullptr)
72 {
73 if (ifa->ifa_addr->sa_family == AF_INET)
74 {
77
78 if (interfaceAddressInfo->sin_addr.s_addr != INADDR_NONE)
79 {
82 return true;
83 }
84 }
85 else if (ifa->ifa_addr->sa_family == AF_INET6)
86 {
88 interfaceInfo.broadcastAddress = makeAddress (unalignedPointerCast<sockaddr_in6*> (ifa->ifa_dstaddr));
89 return true;
90 }
91 }
92
93 return false;
94 }
95 #endif
96
98 {
99 Array<InterfaceInfo> interfaces;
100
101 #if JUCE_WASM
102 // TODO
103 #else
104 struct ifaddrs* ifaddr = nullptr;
105
106 if (getifaddrs (&ifaddr) != -1)
107 {
108 for (auto* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
109 {
110 InterfaceInfo i;
111
112 if (populateInterfaceInfo (ifa, i))
113 interfaces.addIfNotAlreadyThere (i);
114 }
115
117 }
118 #endif
119
120 return interfaces;
121 }
122}
123
125{
126 for (auto& i : getAllInterfaceInfo())
127 if (includeIPv6 || ! i.interfaceAddress.isIPv6)
128 result.addIfNotAlreadyThere (i.interfaceAddress);
129}
130
132{
133 for (auto& i : getAllInterfaceInfo())
134 if (i.interfaceAddress == interfaceAddress)
135 return i.broadcastAddress;
136
137 return {};
138}
139
140} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
bool addIfNotAlreadyThere(ParameterType newElement)
Appends a new element at the end of the array as long as the array doesn't already contain it.
Definition juce_Array.h:522
Represents an IP address.
static IPAddress getInterfaceBroadcastAddress(const IPAddress &interfaceAddress)
If the IPAdress is the address of an interface on the machine, returns the associated broadcast addre...
static void findAllAddresses(Array< IPAddress > &results, bool includeIPv6=false)
Populates a list of all the IP addresses that this machine is using.
JUCE Namespace.
unsigned short uint16
A platform-independent 16-bit unsigned integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88