@@ -51,11 +51,9 @@ | |||||
//============================================================================== | //============================================================================== | ||||
static String getMacAddressList() | static String getMacAddressList() | ||||
{ | { | ||||
Array<MACAddress> macAddresses; | |||||
MACAddress::findAllAddresses (macAddresses); | |||||
String addressList; | String addressList; | ||||
for (auto& addr : macAddresses) | |||||
for (auto& addr : MACAddress::getAllAddresses()) | |||||
addressList << addr.toString() << newLine; | addressList << addr.toString() << newLine; | ||||
return addressList; | return addressList; | ||||
@@ -75,12 +73,9 @@ static String getFileSystemRoots() | |||||
static String getIPAddressList() | static String getIPAddressList() | ||||
{ | { | ||||
Array<IPAddress> addresses; | |||||
IPAddress::findAllAddresses (addresses); | |||||
String addressList; | String addressList; | ||||
for (auto& addr : addresses) | |||||
for (auto& addr : IPAddress::getAllAddresses()) | |||||
addressList << " " << addr.toString() << newLine; | addressList << " " << addr.toString() << newLine; | ||||
return addressList; | return addressList; | ||||
@@ -28,16 +28,9 @@ | |||||
namespace | namespace | ||||
{ | { | ||||
String getIPAddress() | |||||
{ | |||||
Array<IPAddress> addresses; | |||||
IPAddress::findAllAddresses (addresses); | |||||
return addresses[1].toString(); | |||||
} | |||||
String getBroadcastIPAddress() | 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. | 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 ("[]")) | if (adr.containsAnyOf ("[]")) | ||||
return adr.fromFirstOccurrenceOf ("[", false, true).upToLastOccurrenceOf ("]", false, true); | 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.upToLastOccurrenceOf (":", false, true); | ||||
return adr; | return adr; | ||||
@@ -199,30 +200,26 @@ int IPAddress::compare (const IPAddress& other) const noexcept | |||||
return 1; | 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) | 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; | 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) | String IPAddress::getFormattedAddress (const String& unformattedAddress) | ||||
{ | { | ||||
@@ -304,10 +301,8 @@ bool IPAddress::isIPv4MappedAddress (const IPAddress& mappedAddress) | |||||
return false; | return false; | ||||
for (int i = 0; i < 10; ++i) | for (int i = 0; i < 10; ++i) | ||||
{ | |||||
if (mappedAddress.address[i] != 0) | if (mappedAddress.address[i] != 0) | ||||
return false; | return false; | ||||
} | |||||
if (mappedAddress.address[10] != 255 || mappedAddress.address[11] != 255) | if (mappedAddress.address[10] != 255 || mappedAddress.address[11] != 255) | ||||
return false; | return false; | ||||
@@ -337,6 +332,23 @@ IPAddress IPAddress::convertIPv4AddressToIPv4Mapped (const IPAddress& addressToM | |||||
static_cast<uint16> ((addressToMap.address[2] << 8) | addressToMap.address[3]) }; | 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) | #if (! JUCE_WINDOWS) && (! JUCE_ANDROID) | ||||
static void addAddress (const sockaddr_in* addr_in, Array<IPAddress>& result) | 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) | void IPAddress::findAllAddresses (Array<IPAddress>& result, bool includeIPv6) | ||||
{ | { | ||||
struct ifaddrs *ifaddr, *ifa; | |||||
struct ifaddrs* ifaddr = nullptr; | |||||
if (getifaddrs (&ifaddr) == -1) | if (getifaddrs (&ifaddr) == -1) | ||||
return; | 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) | if (ifa->ifa_addr == nullptr) | ||||
continue; | continue; | ||||
@@ -33,9 +33,6 @@ class JUCE_API IPAddress final | |||||
{ | { | ||||
public: | 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) */ | /** Returns an IP address meaning "any", equivalent to 0.0.0.0 (IPv4) or ::, (IPv6) */ | ||||
static IPAddress any() noexcept; | 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) */ | /** 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; | 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) */ | /** Creates a null address - 0.0.0.0 (IPv4) or ::, (IPv6) */ | ||||
IPAddress() noexcept; | IPAddress() noexcept; | ||||
@@ -85,6 +85,13 @@ int64 MACAddress::toInt64() const noexcept | |||||
return n; | return n; | ||||
} | } | ||||
Array<MACAddress> MACAddress::getAllAddresses() | |||||
{ | |||||
Array<MACAddress> addresses; | |||||
findAllAddresses (addresses); | |||||
return addresses; | |||||
} | |||||
bool MACAddress::isNull() const noexcept { return toInt64() == 0; } | bool MACAddress::isNull() const noexcept { return toInt64() == 0; } | ||||
bool MACAddress::operator== (const MACAddress& other) const noexcept { return memcmp (address, other.address, sizeof (address)) == 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: | 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. */ | /** Populates a list of the MAC addresses of all the available network cards. */ | ||||
static void findAllAddresses (Array<MACAddress>& results); | static void findAllAddresses (Array<MACAddress>& results); | ||||
@@ -590,11 +590,9 @@ bool StreamingSocket::isLocal() const noexcept | |||||
if (! isConnected()) | if (! isConnected()) | ||||
return false; | return false; | ||||
Array<IPAddress> localAddresses; | |||||
IPAddress::findAllAddresses (localAddresses); | |||||
IPAddress currentIP (SocketHelpers::getConnectedAddress (handle)); | IPAddress currentIP (SocketHelpers::getConnectedAddress (handle)); | ||||
for (auto& a : localAddresses) | |||||
for (auto& a : IPAddress::getAllAddresses()) | |||||
if (a == currentIP) | if (a == currentIP) | ||||
return true; | return true; | ||||
@@ -68,7 +68,7 @@ public: | |||||
network address otherwise this function will fail. | network address otherwise this function will fail. | ||||
@returns true on success; false may indicate that another socket is already bound | @returns true on success; false may indicate that another socket is already bound | ||||
on the same port | on the same port | ||||
@see bindToPort(int localPortNumber), IPAddress::findAllAddresses | |||||
@see bindToPort(int localPortNumber), IPAddress::getAllAddresses | |||||
*/ | */ | ||||
bool bindToPort (int localPortNumber, const String& localAddress); | bool bindToPort (int localPortNumber, const String& localAddress); | ||||
@@ -236,7 +236,7 @@ public: | |||||
network address otherwise this function will fail. | network address otherwise this function will fail. | ||||
@returns true on success; false may indicate that another socket is already bound | @returns true on success; false may indicate that another socket is already bound | ||||
on the same port | on the same port | ||||
@see bindToPort(int localPortNumber), IPAddress::findAllAddresses | |||||
@see bindToPort(int localPortNumber), IPAddress::getAllAddresses | |||||
*/ | */ | ||||
bool bindToPort (int localPortNumber, const String& localAddress); | bool bindToPort (int localPortNumber, const String& localAddress); | ||||
@@ -73,13 +73,11 @@ StringArray SystemStats::getDeviceIdentifiers() | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
Array<MACAddress> addresses; | |||||
MACAddress::findAllAddresses (addresses); | |||||
for (auto& address : addresses) | |||||
for (auto& address : MACAddress::getAllAddresses()) | |||||
ids.add (address.toString()); | ids.add (address.toString()); | ||||
} | } | ||||
jassert (ids.size() > 0); // Failed to create any IDs! | |||||
jassert (! ids.isEmpty()); // Failed to create any IDs! | |||||
return ids; | return ids; | ||||
} | } | ||||
@@ -195,7 +195,7 @@ static var machineNumberAllowed (StringArray numbersFromKeyFile, | |||||
for (int i = 0; i < localMachineNumbers.size(); ++i) | for (int i = 0; i < localMachineNumbers.size(); ++i) | ||||
{ | { | ||||
String localNumber (localMachineNumbers[i].trim()); | |||||
auto localNumber = localMachineNumbers[i].trim(); | |||||
if (localNumber.isNotEmpty()) | if (localNumber.isNotEmpty()) | ||||
{ | { | ||||
@@ -287,7 +287,7 @@ char OnlineUnlockStatus::MachineIDUtilities::getPlatformPrefix() | |||||
String OnlineUnlockStatus::MachineIDUtilities::getEncodedIDString (const String& input) | 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()) | return platform + MD5 ((input + "salt_1" + platform).toUTF8()) | ||||
.toHexString().substring (0, 9).toUpperCase(); | .toHexString().substring (0, 9).toUpperCase(); | ||||
@@ -295,7 +295,7 @@ String OnlineUnlockStatus::MachineIDUtilities::getEncodedIDString (const String& | |||||
bool OnlineUnlockStatus::MachineIDUtilities::addFileIDToList (StringArray& ids, const File& f) | 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))); | ids.add (getEncodedIDString (String::toHexString ((int64) num))); | ||||
return true; | return true; | ||||
@@ -306,16 +306,14 @@ bool OnlineUnlockStatus::MachineIDUtilities::addFileIDToList (StringArray& ids, | |||||
void OnlineUnlockStatus::MachineIDUtilities::addMACAddressesToList (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() | StringArray OnlineUnlockStatus::MachineIDUtilities::getLocalMachineIDs() | ||||
{ | { | ||||
auto identifiers = SystemStats::getDeviceIdentifiers(); | auto identifiers = SystemStats::getDeviceIdentifiers(); | ||||
for (auto& identifier : identifiers) | for (auto& identifier : identifiers) | ||||
identifier = getEncodedIDString (identifier); | identifier = getEncodedIDString (identifier); | ||||
@@ -398,7 +396,7 @@ OnlineUnlockStatus::UnlockResult OnlineUnlockStatus::handleXmlReply (XmlElement | |||||
{ | { | ||||
UnlockResult r; | UnlockResult r; | ||||
if (const XmlElement* keyNode = xml.getChildByName ("KEY")) | |||||
if (auto keyNode = xml.getChildByName ("KEY")) | |||||
{ | { | ||||
const String keyText (keyNode->getAllSubText().trim()); | const String keyText (keyNode->getAllSubText().trim()); | ||||
r.succeeded = keyText.length() > 10 && applyKeyFile (keyText); | 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! | // This method will block while it contacts the server, so you must run it on a background thread! | ||||
jassert (! MessageManager::getInstance()->isThisTheMessageThread()); | jassert (! MessageManager::getInstance()->isThisTheMessageThread()); | ||||
String reply (readReplyFromWebserver (email, password)); | |||||
auto reply = readReplyFromWebserver (email, password); | |||||
DBG ("Reply from server: " << reply); | DBG ("Reply from server: " << reply); | ||||
@@ -481,8 +479,8 @@ String KeyGeneration::generateKeyFile (const String& appName, | |||||
const String& machineNumbers, | const String& machineNumbers, | ||||
const RSAKey& privateKey) | 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); | return KeyFileUtils::createKeyFile (comment, xml, privateKey); | ||||
} | } | ||||
@@ -494,10 +492,10 @@ String KeyGeneration::generateExpiringKeyFile (const String& appName, | |||||
const Time expiryTime, | const Time expiryTime, | ||||
const RSAKey& privateKey) | 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())); | 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); | comment << newLine << "Expires: " << expiryTime.toString (true, true); | ||||
return KeyFileUtils::createKeyFile (comment, xml, privateKey); | return KeyFileUtils::createKeyFile (comment, xml, privateKey); | ||||