diff --git a/examples/Utilities/SystemInfoDemo.h b/examples/Utilities/SystemInfoDemo.h index ffe34619cd..3c2338efb2 100644 --- a/examples/Utilities/SystemInfoDemo.h +++ b/examples/Utilities/SystemInfoDemo.h @@ -51,11 +51,9 @@ //============================================================================== static String getMacAddressList() { - Array 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 addresses; - IPAddress::findAllAddresses (addresses); - String addressList; - for (auto& addr : addresses) + for (auto& addr : IPAddress::getAllAddresses()) addressList << " " << addr.toString() << newLine; return addressList; diff --git a/extras/NetworkGraphicsDemo/Source/Main.cpp b/extras/NetworkGraphicsDemo/Source/Main.cpp index 3a352b6ff6..40c0597b3b 100644 --- a/extras/NetworkGraphicsDemo/Source/Main.cpp +++ b/extras/NetworkGraphicsDemo/Source/Main.cpp @@ -28,16 +28,9 @@ namespace { - String getIPAddress() - { - Array 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. diff --git a/modules/juce_core/network/juce_IPAddress.cpp b/modules/juce_core/network/juce_IPAddress.cpp index bb26b4b57c..9e021c24ff 100644 --- a/modules/juce_core/network/juce_IPAddress.cpp +++ b/modules/juce_core/network/juce_IPAddress.cpp @@ -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 ((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::getAllAddresses (bool includeIPv6) +{ + Array addresses; + findAllAddresses (addresses, includeIPv6); + return addresses; +} #if (! JUCE_WINDOWS) && (! JUCE_ANDROID) static void addAddress (const sockaddr_in* addr_in, Array& result) @@ -373,12 +385,12 @@ static void addAddress (const sockaddr_in6* addr_in, Array& result) void IPAddress::findAllAddresses (Array& 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; diff --git a/modules/juce_core/network/juce_IPAddress.h b/modules/juce_core/network/juce_IPAddress.h index b44ff36aef..b77bbb9275 100644 --- a/modules/juce_core/network/juce_IPAddress.h +++ b/modules/juce_core/network/juce_IPAddress.h @@ -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& 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& results, bool includeIPv6 = false); + + /** Populates a list of all the IP addresses that this machine is using. */ + static Array 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; diff --git a/modules/juce_core/network/juce_MACAddress.cpp b/modules/juce_core/network/juce_MACAddress.cpp index a2c7e1fb55..cf7033529b 100644 --- a/modules/juce_core/network/juce_MACAddress.cpp +++ b/modules/juce_core/network/juce_MACAddress.cpp @@ -85,6 +85,13 @@ int64 MACAddress::toInt64() const noexcept return n; } +Array MACAddress::getAllAddresses() +{ + Array 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; } diff --git a/modules/juce_core/network/juce_MACAddress.h b/modules/juce_core/network/juce_MACAddress.h index ae0661ebc3..d174ba169a 100644 --- a/modules/juce_core/network/juce_MACAddress.h +++ b/modules/juce_core/network/juce_MACAddress.h @@ -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 getAllAddresses(); + /** Populates a list of the MAC addresses of all the available network cards. */ static void findAllAddresses (Array& results); diff --git a/modules/juce_core/network/juce_Socket.cpp b/modules/juce_core/network/juce_Socket.cpp index b800fe8e2b..a36ec0d875 100644 --- a/modules/juce_core/network/juce_Socket.cpp +++ b/modules/juce_core/network/juce_Socket.cpp @@ -590,11 +590,9 @@ bool StreamingSocket::isLocal() const noexcept if (! isConnected()) return false; - Array localAddresses; - IPAddress::findAllAddresses (localAddresses); IPAddress currentIP (SocketHelpers::getConnectedAddress (handle)); - for (auto& a : localAddresses) + for (auto& a : IPAddress::getAllAddresses()) if (a == currentIP) return true; diff --git a/modules/juce_core/network/juce_Socket.h b/modules/juce_core/network/juce_Socket.h index a1420d0ace..524554ee55 100644 --- a/modules/juce_core/network/juce_Socket.h +++ b/modules/juce_core/network/juce_Socket.h @@ -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); diff --git a/modules/juce_core/system/juce_SystemStats.cpp b/modules/juce_core/system/juce_SystemStats.cpp index c073331b97..cbc7aa1ebc 100644 --- a/modules/juce_core/system/juce_SystemStats.cpp +++ b/modules/juce_core/system/juce_SystemStats.cpp @@ -73,13 +73,11 @@ StringArray SystemStats::getDeviceIdentifiers() } else { - Array 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; } diff --git a/modules/juce_product_unlocking/marketplace/juce_OnlineUnlockStatus.cpp b/modules/juce_product_unlocking/marketplace/juce_OnlineUnlockStatus.cpp index fc6a5c52df..f866f4bcf7 100644 --- a/modules/juce_product_unlocking/marketplace/juce_OnlineUnlockStatus.cpp +++ b/modules/juce_product_unlocking/marketplace/juce_OnlineUnlockStatus.cpp @@ -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 (getPlatformPrefix()))); + auto platform = String::charToString (static_cast (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 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);