mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-02-03 03:30:06 +00:00
Added some methods IPAddress::getAllAddresses(), IPAddress::getLocalAddress(), MACAddress::getAllAddresses()
This commit is contained in:
parent
d9fa1c4602
commit
ccbaf89f0e
10 changed files with 77 additions and 61 deletions
|
|
@ -51,11 +51,9 @@
|
|||
//==============================================================================
|
||||
static String getMacAddressList()
|
||||
{
|
||||
Array<MACAddress> macAddresses;
|
||||
MACAddress::findAllAddresses (macAddresses);
|
||||
|
||||
String addressList;
|
||||
for (auto& addr : macAddresses)
|
||||
|
||||
for (auto& addr : MACAddress::getAllAddresses())
|
||||
addressList << addr.toString() << newLine;
|
||||
|
||||
return addressList;
|
||||
|
|
@ -75,12 +73,9 @@ static String getFileSystemRoots()
|
|||
|
||||
static String getIPAddressList()
|
||||
{
|
||||
Array<IPAddress> addresses;
|
||||
IPAddress::findAllAddresses (addresses);
|
||||
|
||||
String addressList;
|
||||
|
||||
for (auto& addr : addresses)
|
||||
for (auto& addr : IPAddress::getAllAddresses())
|
||||
addressList << " " << addr.toString() << newLine;
|
||||
|
||||
return addressList;
|
||||
|
|
|
|||
|
|
@ -28,16 +28,9 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
String getIPAddress()
|
||||
{
|
||||
Array<IPAddress> addresses;
|
||||
IPAddress::findAllAddresses (addresses);
|
||||
return addresses[1].toString();
|
||||
}
|
||||
|
||||
String getBroadcastIPAddress()
|
||||
{
|
||||
return getIPAddress().upToLastOccurrenceOf (".", false, false) + ".255";
|
||||
return IPAddress::getLocalAddress().toString().upToLastOccurrenceOf (".", false, false) + ".255";
|
||||
}
|
||||
|
||||
static const int masterPortNumber = 9001; // the UDP port the master sends on / the clients receive.
|
||||
|
|
|
|||
|
|
@ -89,7 +89,8 @@ static String removePort (const String& adr)
|
|||
{
|
||||
if (adr.containsAnyOf ("[]"))
|
||||
return adr.fromFirstOccurrenceOf ("[", false, true).upToLastOccurrenceOf ("]", false, true);
|
||||
else if (adr.indexOf (":") == adr.lastIndexOf (":"))
|
||||
|
||||
if (adr.indexOf (":") == adr.lastIndexOf (":"))
|
||||
return adr.upToLastOccurrenceOf (":", false, true);
|
||||
|
||||
return adr;
|
||||
|
|
@ -199,30 +200,26 @@ int IPAddress::compare (const IPAddress& other) const noexcept
|
|||
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isIPv4MappedAddress (other))
|
||||
return compare (convertIPv4MappedAddressToIPv4 (other));
|
||||
|
||||
return -1;
|
||||
}
|
||||
if (isIPv4MappedAddress (other))
|
||||
return compare (convertIPv4MappedAddressToIPv4 (other));
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (isIPv6 ? 16 : 4); ++i)
|
||||
{
|
||||
if (address[i] > other.address[i])
|
||||
return 1;
|
||||
else if (address[i] < other.address[i])
|
||||
return -1;
|
||||
if (address[i] > other.address[i]) return 1;
|
||||
if (address[i] < other.address[i]) return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
IPAddress IPAddress::any() noexcept { return IPAddress(); }
|
||||
IPAddress IPAddress::broadcast() noexcept { return IPAddress (255, 255, 255, 255); }
|
||||
IPAddress IPAddress::local (bool IPv6) noexcept { return IPv6 ? IPAddress (0, 0, 0, 0, 0, 0, 0, 1)
|
||||
: IPAddress (127, 0, 0, 1); }
|
||||
IPAddress IPAddress::any() noexcept { return IPAddress(); }
|
||||
IPAddress IPAddress::broadcast() noexcept { return IPAddress (255, 255, 255, 255); }
|
||||
IPAddress IPAddress::local (bool IPv6) noexcept { return IPv6 ? IPAddress (0, 0, 0, 0, 0, 0, 0, 1)
|
||||
: IPAddress (127, 0, 0, 1); }
|
||||
|
||||
String IPAddress::getFormattedAddress (const String& unformattedAddress)
|
||||
{
|
||||
|
|
@ -304,10 +301,8 @@ bool IPAddress::isIPv4MappedAddress (const IPAddress& mappedAddress)
|
|||
return false;
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
if (mappedAddress.address[i] != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mappedAddress.address[10] != 255 || mappedAddress.address[11] != 255)
|
||||
return false;
|
||||
|
|
@ -337,6 +332,23 @@ IPAddress IPAddress::convertIPv4AddressToIPv4Mapped (const IPAddress& addressToM
|
|||
static_cast<uint16> ((addressToMap.address[2] << 8) | addressToMap.address[3]) };
|
||||
}
|
||||
|
||||
IPAddress IPAddress::getLocalAddress (bool includeIPv6)
|
||||
{
|
||||
auto addresses = getAllAddresses (includeIPv6);
|
||||
|
||||
for (auto& a : addresses)
|
||||
if (a != local())
|
||||
return a;
|
||||
|
||||
return local();
|
||||
}
|
||||
|
||||
Array<IPAddress> IPAddress::getAllAddresses (bool includeIPv6)
|
||||
{
|
||||
Array<IPAddress> addresses;
|
||||
findAllAddresses (addresses, includeIPv6);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
#if (! JUCE_WINDOWS) && (! JUCE_ANDROID)
|
||||
static void addAddress (const sockaddr_in* addr_in, Array<IPAddress>& result)
|
||||
|
|
@ -373,12 +385,12 @@ static void addAddress (const sockaddr_in6* addr_in, Array<IPAddress>& result)
|
|||
|
||||
void IPAddress::findAllAddresses (Array<IPAddress>& result, bool includeIPv6)
|
||||
{
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
struct ifaddrs* ifaddr = nullptr;
|
||||
|
||||
if (getifaddrs (&ifaddr) == -1)
|
||||
return;
|
||||
|
||||
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
|
||||
for (auto* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
|
||||
{
|
||||
if (ifa->ifa_addr == nullptr)
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -33,9 +33,6 @@ class JUCE_API IPAddress final
|
|||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Populates a list of all the IP addresses that this machine is using. */
|
||||
static void findAllAddresses (Array<IPAddress>& results, bool includeIPv6 = false);
|
||||
|
||||
/** Returns an IP address meaning "any", equivalent to 0.0.0.0 (IPv4) or ::, (IPv6) */
|
||||
static IPAddress any() noexcept;
|
||||
|
||||
|
|
@ -45,6 +42,21 @@ public:
|
|||
/** Returns an IPv4 or IPv6 address meaning "localhost", equivalent to 127.0.0.1 (IPv4) or ::1 (IPv6) */
|
||||
static IPAddress local (bool IPv6 = false) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Populates a list of all the IP addresses that this machine is using. */
|
||||
static void findAllAddresses (Array<IPAddress>& results, bool includeIPv6 = false);
|
||||
|
||||
/** Populates a list of all the IP addresses that this machine is using. */
|
||||
static Array<IPAddress> getAllAddresses (bool includeIPv6 = false);
|
||||
|
||||
/** Returns the first 'real' address for the local machine.
|
||||
Unlike local(), this will attempt to find the machine's actual assigned
|
||||
address rather than "127.0.0.1". If there are multiple network cards, this
|
||||
may return any of their addresses. If it doesn't find any, then it'll return
|
||||
local() as a fallback.
|
||||
*/
|
||||
static IPAddress getLocalAddress (bool includeIPv6 = false);
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a null address - 0.0.0.0 (IPv4) or ::, (IPv6) */
|
||||
IPAddress() noexcept;
|
||||
|
|
|
|||
|
|
@ -85,6 +85,13 @@ int64 MACAddress::toInt64() const noexcept
|
|||
return n;
|
||||
}
|
||||
|
||||
Array<MACAddress> MACAddress::getAllAddresses()
|
||||
{
|
||||
Array<MACAddress> addresses;
|
||||
findAllAddresses (addresses);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
bool MACAddress::isNull() const noexcept { return toInt64() == 0; }
|
||||
|
||||
bool MACAddress::operator== (const MACAddress& other) const noexcept { return memcmp (address, other.address, sizeof (address)) == 0; }
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ class JUCE_API MACAddress final
|
|||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Returns a list of the MAC addresses of all the available network cards. */
|
||||
static Array<MACAddress> getAllAddresses();
|
||||
|
||||
/** Populates a list of the MAC addresses of all the available network cards. */
|
||||
static void findAllAddresses (Array<MACAddress>& results);
|
||||
|
||||
|
|
|
|||
|
|
@ -590,11 +590,9 @@ bool StreamingSocket::isLocal() const noexcept
|
|||
if (! isConnected())
|
||||
return false;
|
||||
|
||||
Array<IPAddress> localAddresses;
|
||||
IPAddress::findAllAddresses (localAddresses);
|
||||
IPAddress currentIP (SocketHelpers::getConnectedAddress (handle));
|
||||
|
||||
for (auto& a : localAddresses)
|
||||
for (auto& a : IPAddress::getAllAddresses())
|
||||
if (a == currentIP)
|
||||
return true;
|
||||
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ public:
|
|||
network address otherwise this function will fail.
|
||||
@returns true on success; false may indicate that another socket is already bound
|
||||
on the same port
|
||||
@see bindToPort(int localPortNumber), IPAddress::findAllAddresses
|
||||
@see bindToPort(int localPortNumber), IPAddress::getAllAddresses
|
||||
*/
|
||||
bool bindToPort (int localPortNumber, const String& localAddress);
|
||||
|
||||
|
|
@ -236,7 +236,7 @@ public:
|
|||
network address otherwise this function will fail.
|
||||
@returns true on success; false may indicate that another socket is already bound
|
||||
on the same port
|
||||
@see bindToPort(int localPortNumber), IPAddress::findAllAddresses
|
||||
@see bindToPort(int localPortNumber), IPAddress::getAllAddresses
|
||||
*/
|
||||
bool bindToPort (int localPortNumber, const String& localAddress);
|
||||
|
||||
|
|
|
|||
|
|
@ -73,13 +73,11 @@ StringArray SystemStats::getDeviceIdentifiers()
|
|||
}
|
||||
else
|
||||
{
|
||||
Array<MACAddress> addresses;
|
||||
MACAddress::findAllAddresses (addresses);
|
||||
for (auto& address : addresses)
|
||||
for (auto& address : MACAddress::getAllAddresses())
|
||||
ids.add (address.toString());
|
||||
}
|
||||
|
||||
jassert (ids.size() > 0); // Failed to create any IDs!
|
||||
jassert (! ids.isEmpty()); // Failed to create any IDs!
|
||||
return ids;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -195,7 +195,7 @@ static var machineNumberAllowed (StringArray numbersFromKeyFile,
|
|||
|
||||
for (int i = 0; i < localMachineNumbers.size(); ++i)
|
||||
{
|
||||
String localNumber (localMachineNumbers[i].trim());
|
||||
auto localNumber = localMachineNumbers[i].trim();
|
||||
|
||||
if (localNumber.isNotEmpty())
|
||||
{
|
||||
|
|
@ -287,7 +287,7 @@ char OnlineUnlockStatus::MachineIDUtilities::getPlatformPrefix()
|
|||
|
||||
String OnlineUnlockStatus::MachineIDUtilities::getEncodedIDString (const String& input)
|
||||
{
|
||||
const String platform (String::charToString (static_cast<juce_wchar> (getPlatformPrefix())));
|
||||
auto platform = String::charToString (static_cast<juce_wchar> (getPlatformPrefix()));
|
||||
|
||||
return platform + MD5 ((input + "salt_1" + platform).toUTF8())
|
||||
.toHexString().substring (0, 9).toUpperCase();
|
||||
|
|
@ -295,7 +295,7 @@ String OnlineUnlockStatus::MachineIDUtilities::getEncodedIDString (const String&
|
|||
|
||||
bool OnlineUnlockStatus::MachineIDUtilities::addFileIDToList (StringArray& ids, const File& f)
|
||||
{
|
||||
if (uint64 num = f.getFileIdentifier())
|
||||
if (auto num = f.getFileIdentifier())
|
||||
{
|
||||
ids.add (getEncodedIDString (String::toHexString ((int64) num)));
|
||||
return true;
|
||||
|
|
@ -306,16 +306,14 @@ bool OnlineUnlockStatus::MachineIDUtilities::addFileIDToList (StringArray& ids,
|
|||
|
||||
void OnlineUnlockStatus::MachineIDUtilities::addMACAddressesToList (StringArray& ids)
|
||||
{
|
||||
Array<MACAddress> addresses;
|
||||
MACAddress::findAllAddresses (addresses);
|
||||
|
||||
for (int i = 0; i < addresses.size(); ++i)
|
||||
ids.add (getEncodedIDString (addresses.getReference(i).toString()));
|
||||
for (auto& address : MACAddress::getAllAddresses())
|
||||
ids.add (getEncodedIDString (address.toString()));
|
||||
}
|
||||
|
||||
StringArray OnlineUnlockStatus::MachineIDUtilities::getLocalMachineIDs()
|
||||
{
|
||||
auto identifiers = SystemStats::getDeviceIdentifiers();
|
||||
|
||||
for (auto& identifier : identifiers)
|
||||
identifier = getEncodedIDString (identifier);
|
||||
|
||||
|
|
@ -398,7 +396,7 @@ OnlineUnlockStatus::UnlockResult OnlineUnlockStatus::handleXmlReply (XmlElement
|
|||
{
|
||||
UnlockResult r;
|
||||
|
||||
if (const XmlElement* keyNode = xml.getChildByName ("KEY"))
|
||||
if (auto keyNode = xml.getChildByName ("KEY"))
|
||||
{
|
||||
const String keyText (keyNode->getAllSubText().trim());
|
||||
r.succeeded = keyText.length() > 10 && applyKeyFile (keyText);
|
||||
|
|
@ -460,7 +458,7 @@ OnlineUnlockStatus::UnlockResult OnlineUnlockStatus::attemptWebserverUnlock (con
|
|||
// This method will block while it contacts the server, so you must run it on a background thread!
|
||||
jassert (! MessageManager::getInstance()->isThisTheMessageThread());
|
||||
|
||||
String reply (readReplyFromWebserver (email, password));
|
||||
auto reply = readReplyFromWebserver (email, password);
|
||||
|
||||
DBG ("Reply from server: " << reply);
|
||||
|
||||
|
|
@ -481,8 +479,8 @@ String KeyGeneration::generateKeyFile (const String& appName,
|
|||
const String& machineNumbers,
|
||||
const RSAKey& privateKey)
|
||||
{
|
||||
XmlElement xml (KeyFileUtils::createKeyFileContent (appName, userEmail, userName, machineNumbers, "mach"));
|
||||
const String comment (KeyFileUtils::createKeyFileComment (appName, userEmail, userName, machineNumbers));
|
||||
auto xml = KeyFileUtils::createKeyFileContent (appName, userEmail, userName, machineNumbers, "mach");
|
||||
auto comment = KeyFileUtils::createKeyFileComment (appName, userEmail, userName, machineNumbers);
|
||||
|
||||
return KeyFileUtils::createKeyFile (comment, xml, privateKey);
|
||||
}
|
||||
|
|
@ -494,10 +492,10 @@ String KeyGeneration::generateExpiringKeyFile (const String& appName,
|
|||
const Time expiryTime,
|
||||
const RSAKey& privateKey)
|
||||
{
|
||||
XmlElement xml (KeyFileUtils::createKeyFileContent (appName, userEmail, userName, machineNumbers, "expiring_mach"));
|
||||
auto xml = KeyFileUtils::createKeyFileContent (appName, userEmail, userName, machineNumbers, "expiring_mach");
|
||||
xml.setAttribute ("expiryTime", String::toHexString (expiryTime.toMilliseconds()));
|
||||
|
||||
String comment (KeyFileUtils::createKeyFileComment (appName, userEmail, userName, machineNumbers));
|
||||
auto comment = KeyFileUtils::createKeyFileComment (appName, userEmail, userName, machineNumbers);
|
||||
comment << newLine << "Expires: " << expiryTime.toString (true, true);
|
||||
|
||||
return KeyFileUtils::createKeyFile (comment, xml, privateKey);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue