From cf641969d0b0549a1d0108935194e7d3b18ea2d0 Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Tue, 26 Oct 2010 18:25:22 +0100 Subject: [PATCH] Created a new class: MACAddress, which replaces the old MAC address detection functions with a cleaner implementation. --- Builds/Linux/Makefile | 6 + Builds/MacOSX/Juce.xcodeproj/project.pbxproj | 6 + Builds/VisualStudio2005/Juce.vcproj | 2 + Builds/VisualStudio2008/Juce.vcproj | 2 + Builds/VisualStudio2008_DLL/Juce.vcproj | 2 + Builds/VisualStudio2010/Juce.vcxproj | 2 + Builds/VisualStudio2010/Juce.vcxproj.filters | 6 + Builds/iPhone/Juce.xcodeproj/project.pbxproj | 6 + Juce.jucer | 4 + amalgamation/juce_amalgamated_template.cpp | 1 + .../juce demo/Source/ApplicationStartup.cpp | 14 +- juce_amalgamated.cpp | 282 ++++++++---------- juce_amalgamated.h | 106 ++++--- src/core/juce_SystemStats.cpp | 20 -- src/core/juce_SystemStats.h | 43 +-- src/core/juce_Uuid.cpp | 9 +- src/io/network/juce_MACAddress.cpp | 88 ++++++ src/io/network/juce_MACAddress.h | 90 ++++++ src/juce_core_includes.h | 3 + src/native/juce_linux_NativeCode.cpp | 1 + src/native/juce_mac_NativeCode.mm | 1 + src/native/juce_win32_NativeCode.cpp | 1 + src/native/linux/juce_linux_Network.cpp | 31 +- src/native/mac/juce_mac_Network.mm | 30 +- src/native/windows/juce_win32_Network.cpp | 134 ++++----- 25 files changed, 516 insertions(+), 374 deletions(-) create mode 100644 src/io/network/juce_MACAddress.cpp create mode 100644 src/io/network/juce_MACAddress.h diff --git a/Builds/Linux/Makefile b/Builds/Linux/Makefile index e5f1551fb2..b40eb105a9 100644 --- a/Builds/Linux/Makefile +++ b/Builds/Linux/Makefile @@ -276,6 +276,7 @@ OBJECTS := \ $(OBJDIR)/juce_NamedPipe_b3a205e0.o \ $(OBJDIR)/juce_TemporaryFile_935f3fc6.o \ $(OBJDIR)/juce_ZipFile_16fbb396.o \ + $(OBJDIR)/juce_MACAddress_b81987ed.o \ $(OBJDIR)/juce_Socket_cf39c63b.o \ $(OBJDIR)/juce_URL_d6b7cff1.o \ $(OBJDIR)/juce_BufferedInputStream_c71b2b68.o \ @@ -1548,6 +1549,11 @@ $(OBJDIR)/juce_ZipFile_16fbb396.o: ../../src/io/files/juce_ZipFile.cpp @echo "Compiling juce_ZipFile.cpp" @$(CXX) $(CXXFLAGS) -o "$@" -c "$<" +$(OBJDIR)/juce_MACAddress_b81987ed.o: ../../src/io/network/juce_MACAddress.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling juce_MACAddress.cpp" + @$(CXX) $(CXXFLAGS) -o "$@" -c "$<" + $(OBJDIR)/juce_Socket_cf39c63b.o: ../../src/io/network/juce_Socket.cpp -@mkdir -p $(OBJDIR) @echo "Compiling juce_Socket.cpp" diff --git a/Builds/MacOSX/Juce.xcodeproj/project.pbxproj b/Builds/MacOSX/Juce.xcodeproj/project.pbxproj index ca9f2ff5a4..a15c9f6f77 100644 --- a/Builds/MacOSX/Juce.xcodeproj/project.pbxproj +++ b/Builds/MacOSX/Juce.xcodeproj/project.pbxproj @@ -245,6 +245,7 @@ BBCA4C01CEF1EDA75625D6DC = { isa = PBXBuildFile; fileRef = D9B3C04F8EB4228DD59002E7; }; 136FB9588C834E6F4A1EEBFB = { isa = PBXBuildFile; fileRef = 17C3AF03FF7AE88AE0C73311; }; 274415D1C0AE9BF56FAAE791 = { isa = PBXBuildFile; fileRef = 4035C867821E9B5887AA25FB; }; + CBAD975785BD26A0DA9417B6 = { isa = PBXBuildFile; fileRef = EFA58F646B69B227AEF14140; }; 2A8F78D412AC7EC0C2998A1A = { isa = PBXBuildFile; fileRef = 06B06C420341B005DFF951D2; }; 2DD969525DEF4AD408E9BB59 = { isa = PBXBuildFile; fileRef = AD7A1C2C82EE714EBDD52EFC; }; 059B694C912BE81023D5240D = { isa = PBXBuildFile; fileRef = D4211E697F43821CDBCE4F24; }; @@ -888,6 +889,8 @@ 75D6F1C676E178DEA8B86849 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_TemporaryFile.h; path = ../../src/io/files/juce_TemporaryFile.h; sourceTree = SOURCE_ROOT; }; 4035C867821E9B5887AA25FB = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_ZipFile.cpp; path = ../../src/io/files/juce_ZipFile.cpp; sourceTree = SOURCE_ROOT; }; 4D005659935C7DE99C2C01E2 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_ZipFile.h; path = ../../src/io/files/juce_ZipFile.h; sourceTree = SOURCE_ROOT; }; + EFA58F646B69B227AEF14140 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_MACAddress.cpp; path = ../../src/io/network/juce_MACAddress.cpp; sourceTree = SOURCE_ROOT; }; + 7241B187AA9B0217838B6052 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_MACAddress.h; path = ../../src/io/network/juce_MACAddress.h; sourceTree = SOURCE_ROOT; }; 06B06C420341B005DFF951D2 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_Socket.cpp; path = ../../src/io/network/juce_Socket.cpp; sourceTree = SOURCE_ROOT; }; 286206E754AC14DEAF88FD43 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_Socket.h; path = ../../src/io/network/juce_Socket.h; sourceTree = SOURCE_ROOT; }; AD7A1C2C82EE714EBDD52EFC = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_URL.cpp; path = ../../src/io/network/juce_URL.cpp; sourceTree = SOURCE_ROOT; }; @@ -1663,6 +1666,8 @@ 4035C867821E9B5887AA25FB, 4D005659935C7DE99C2C01E2 ); name = files; sourceTree = ""; }; 13FBF71BD76A08C8971C6351 = { isa = PBXGroup; children = ( + EFA58F646B69B227AEF14140, + 7241B187AA9B0217838B6052, 06B06C420341B005DFF951D2, 286206E754AC14DEAF88FD43, AD7A1C2C82EE714EBDD52EFC, @@ -2165,6 +2170,7 @@ BBCA4C01CEF1EDA75625D6DC, 136FB9588C834E6F4A1EEBFB, 274415D1C0AE9BF56FAAE791, + CBAD975785BD26A0DA9417B6, 2A8F78D412AC7EC0C2998A1A, 2DD969525DEF4AD408E9BB59, 059B694C912BE81023D5240D, diff --git a/Builds/VisualStudio2005/Juce.vcproj b/Builds/VisualStudio2005/Juce.vcproj index 91309b0806..8454b730c9 100644 --- a/Builds/VisualStudio2005/Juce.vcproj +++ b/Builds/VisualStudio2005/Juce.vcproj @@ -807,6 +807,8 @@ + + diff --git a/Builds/VisualStudio2008/Juce.vcproj b/Builds/VisualStudio2008/Juce.vcproj index 93b63483cc..408d6dcbd1 100644 --- a/Builds/VisualStudio2008/Juce.vcproj +++ b/Builds/VisualStudio2008/Juce.vcproj @@ -807,6 +807,8 @@ + + diff --git a/Builds/VisualStudio2008_DLL/Juce.vcproj b/Builds/VisualStudio2008_DLL/Juce.vcproj index af91502958..1e0c471ccc 100644 --- a/Builds/VisualStudio2008_DLL/Juce.vcproj +++ b/Builds/VisualStudio2008_DLL/Juce.vcproj @@ -809,6 +809,8 @@ + + diff --git a/Builds/VisualStudio2010/Juce.vcxproj b/Builds/VisualStudio2010/Juce.vcxproj index d476cf4950..9c753e4dd2 100644 --- a/Builds/VisualStudio2010/Juce.vcxproj +++ b/Builds/VisualStudio2010/Juce.vcxproj @@ -359,6 +359,7 @@ + @@ -727,6 +728,7 @@ + diff --git a/Builds/VisualStudio2010/Juce.vcxproj.filters b/Builds/VisualStudio2010/Juce.vcxproj.filters index 36bd373e9b..cb6dde432f 100644 --- a/Builds/VisualStudio2010/Juce.vcxproj.filters +++ b/Builds/VisualStudio2010/Juce.vcxproj.filters @@ -925,6 +925,9 @@ Juce\Source\io\files + + Juce\Source\io\network + Juce\Source\io\network @@ -2103,6 +2106,9 @@ Juce\Source\io\files + + Juce\Source\io\network + Juce\Source\io\network diff --git a/Builds/iPhone/Juce.xcodeproj/project.pbxproj b/Builds/iPhone/Juce.xcodeproj/project.pbxproj index bfdbdbafaf..8edaa5342c 100644 --- a/Builds/iPhone/Juce.xcodeproj/project.pbxproj +++ b/Builds/iPhone/Juce.xcodeproj/project.pbxproj @@ -245,6 +245,7 @@ BBCA4C01CEF1EDA75625D6DC = { isa = PBXBuildFile; fileRef = D9B3C04F8EB4228DD59002E7; }; 136FB9588C834E6F4A1EEBFB = { isa = PBXBuildFile; fileRef = 17C3AF03FF7AE88AE0C73311; }; 274415D1C0AE9BF56FAAE791 = { isa = PBXBuildFile; fileRef = 4035C867821E9B5887AA25FB; }; + CBAD975785BD26A0DA9417B6 = { isa = PBXBuildFile; fileRef = EFA58F646B69B227AEF14140; }; 2A8F78D412AC7EC0C2998A1A = { isa = PBXBuildFile; fileRef = 06B06C420341B005DFF951D2; }; 2DD969525DEF4AD408E9BB59 = { isa = PBXBuildFile; fileRef = AD7A1C2C82EE714EBDD52EFC; }; 059B694C912BE81023D5240D = { isa = PBXBuildFile; fileRef = D4211E697F43821CDBCE4F24; }; @@ -888,6 +889,8 @@ 75D6F1C676E178DEA8B86849 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_TemporaryFile.h; path = ../../src/io/files/juce_TemporaryFile.h; sourceTree = SOURCE_ROOT; }; 4035C867821E9B5887AA25FB = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_ZipFile.cpp; path = ../../src/io/files/juce_ZipFile.cpp; sourceTree = SOURCE_ROOT; }; 4D005659935C7DE99C2C01E2 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_ZipFile.h; path = ../../src/io/files/juce_ZipFile.h; sourceTree = SOURCE_ROOT; }; + EFA58F646B69B227AEF14140 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_MACAddress.cpp; path = ../../src/io/network/juce_MACAddress.cpp; sourceTree = SOURCE_ROOT; }; + 7241B187AA9B0217838B6052 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_MACAddress.h; path = ../../src/io/network/juce_MACAddress.h; sourceTree = SOURCE_ROOT; }; 06B06C420341B005DFF951D2 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_Socket.cpp; path = ../../src/io/network/juce_Socket.cpp; sourceTree = SOURCE_ROOT; }; 286206E754AC14DEAF88FD43 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = juce_Socket.h; path = ../../src/io/network/juce_Socket.h; sourceTree = SOURCE_ROOT; }; AD7A1C2C82EE714EBDD52EFC = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = juce_URL.cpp; path = ../../src/io/network/juce_URL.cpp; sourceTree = SOURCE_ROOT; }; @@ -1663,6 +1666,8 @@ 4035C867821E9B5887AA25FB, 4D005659935C7DE99C2C01E2 ); name = files; sourceTree = ""; }; 13FBF71BD76A08C8971C6351 = { isa = PBXGroup; children = ( + EFA58F646B69B227AEF14140, + 7241B187AA9B0217838B6052, 06B06C420341B005DFF951D2, 286206E754AC14DEAF88FD43, AD7A1C2C82EE714EBDD52EFC, @@ -2165,6 +2170,7 @@ BBCA4C01CEF1EDA75625D6DC, 136FB9588C834E6F4A1EEBFB, 274415D1C0AE9BF56FAAE791, + CBAD975785BD26A0DA9417B6, 2A8F78D412AC7EC0C2998A1A, 2DD969525DEF4AD408E9BB59, 059B694C912BE81023D5240D, diff --git a/Juce.jucer b/Juce.jucer index 8d25866618..17ed25708a 100644 --- a/Juce.jucer +++ b/Juce.jucer @@ -1201,6 +1201,10 @@ file="src/io/files/juce_ZipFile.h"/> + + macAddresses; + MACAddress::findAllAddresses (macAddresses); + + StringArray addressStrings; + for (int i = 0; i < macAddresses.size(); ++i) + addressStrings.add (macAddresses[i].toString()); + + return addressStrings.joinIntoString (", "); + } }; diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index 67119f5edb..17318a5b14 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -1682,26 +1682,6 @@ const String SystemStats::getJUCEVersion() + "." + String (JUCE_BUILDNUMBER); } -const StringArray SystemStats::getMACAddressStrings() -{ - int64 macAddresses [16]; - const int numAddresses = getMACAddresses (macAddresses, numElementsInArray (macAddresses), false); - - StringArray s; - - for (int i = 0; i < numAddresses; ++i) - { - s.add (String::toHexString (0xff & (int) (macAddresses [i] >> 40)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 32)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 24)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 16)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 8)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 0)).paddedLeft ('0', 2)); - } - - return s; -} - #ifdef JUCE_DLL void* juce_Malloc (const int size) @@ -9738,6 +9718,66 @@ END_JUCE_NAMESPACE /*** End of inlined file: juce_URL.cpp ***/ +/*** Start of inlined file: juce_MACAddress.cpp ***/ +BEGIN_JUCE_NAMESPACE + +MACAddress::MACAddress() + : asInt64 (0) +{ +} + +MACAddress::MACAddress (const MACAddress& other) + : asInt64 (other.asInt64) +{ +} + +MACAddress& MACAddress::operator= (const MACAddress& other) +{ + asInt64 = other.asInt64; + return *this; +} + +MACAddress::MACAddress (const uint8 bytes[6]) + : asInt64 (0) +{ + memcpy (asBytes, bytes, sizeof (asBytes)); +} + +const String MACAddress::toString() const +{ + String s; + s.preallocateStorage (18); + + for (int i = 0; i < numElementsInArray (asBytes); ++i) + { + s << String::toHexString ((int) asBytes[i]).paddedLeft ('0', 2); + + if (i < numElementsInArray (asBytes) - 1) + s << '-'; + } + + return s; +} + +int64 MACAddress::toInt64() const throw() +{ + int64 n = 0; + + for (int i = numElementsInArray (asBytes); --i >= 0;) + n = (n << 8) | asBytes[i]; + + return n; +} + +bool MACAddress::isNull() const throw() { return asInt64 == 0; } + +bool MACAddress::operator== (const MACAddress& other) const throw() { return asInt64 == other.asInt64; } +bool MACAddress::operator!= (const MACAddress& other) const throw() { return asInt64 != other.asInt64; } + +END_JUCE_NAMESPACE +/*** End of inlined file: juce_MACAddress.cpp ***/ + + /*** Start of inlined file: juce_BufferedInputStream.cpp ***/ BEGIN_JUCE_NAMESPACE @@ -10335,7 +10375,12 @@ Uuid::Uuid() if (! hasCheckedMacAddresses) { hasCheckedMacAddresses = true; - SystemStats::getMACAddresses (macAddresses, 2); + + Array result; + MACAddress::findAllAddresses (result); + + for (int i = 0; i < numElementsInArray (macAddresses); ++i) + macAddresses[i] = result[i].toInt64(); } value.asInt64[0] = macAddresses[0]; @@ -239855,121 +239900,91 @@ void juce_closeInternetFile (void* handle) } } -static int getMACAddressViaGetAdaptersInfo (int64* addresses, int maxNum, const bool littleEndian) throw() +namespace MACAddressHelpers { - int numFound = 0; - - DynamicLibraryLoader dll ("iphlpapi.dll"); - DynamicLibraryImport (GetAdaptersInfo, getAdaptersInfo, DWORD, dll, (PIP_ADAPTER_INFO, PULONG)) - - if (getAdaptersInfo != 0) + void getViaGetAdaptersInfo (Array& result) { - ULONG len = sizeof (IP_ADAPTER_INFO); - MemoryBlock mb; - PIP_ADAPTER_INFO adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); - - if (getAdaptersInfo (adapterInfo, &len) == ERROR_BUFFER_OVERFLOW) - { - mb.setSize (len); - adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); - } + DynamicLibraryLoader dll ("iphlpapi.dll"); + DynamicLibraryImport (GetAdaptersInfo, getAdaptersInfo, DWORD, dll, (PIP_ADAPTER_INFO, PULONG)) - if (getAdaptersInfo (adapterInfo, &len) == NO_ERROR) + if (getAdaptersInfo != 0) { - PIP_ADAPTER_INFO adapter = adapterInfo; + ULONG len = sizeof (IP_ADAPTER_INFO); + MemoryBlock mb; + PIP_ADAPTER_INFO adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); - while (adapter != 0) + if (getAdaptersInfo (adapterInfo, &len) == ERROR_BUFFER_OVERFLOW) { - int64 mac = 0; - for (unsigned int i = 0; i < adapter->AddressLength; ++i) - mac = (mac << 8) | adapter->Address[i]; - - if (littleEndian) - mac = (int64) ByteOrder::swap ((uint64) mac); - - if (numFound < maxNum && mac != 0) - addresses [numFound++] = mac; + mb.setSize (len); + adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); + } - adapter = adapter->Next; + if (getAdaptersInfo (adapterInfo, &len) == NO_ERROR) + { + for (PIP_ADAPTER_INFO adapter = adapterInfo; adapter != 0; adapter = adapter->Next) + { + if (adapter->AddressLength >= 6) + result.addIfNotAlreadyThere (MACAddress (adapter->Address)); + } } } } - return numFound; -} - -static int getMACAddressesViaNetBios (int64* addresses, int maxNum, const bool littleEndian) throw() -{ - int numFound = 0; - - DynamicLibraryLoader dll ("netapi32.dll"); - DynamicLibraryImport (Netbios, NetbiosCall, UCHAR, dll, (PNCB)) - - if (NetbiosCall != 0) + void getViaNetBios (Array& result) { - NCB ncb; - zerostruct (ncb); + DynamicLibraryLoader dll ("netapi32.dll"); + DynamicLibraryImport (Netbios, NetbiosCall, UCHAR, dll, (PNCB)) - struct ASTAT + if (NetbiosCall != 0) { - ADAPTER_STATUS adapt; - NAME_BUFFER NameBuff [30]; - }; + NCB ncb; + zerostruct (ncb); - ASTAT astat; - zeromem (&astat, sizeof (astat)); // (can't use zerostruct here in VC6) + struct ASTAT + { + ADAPTER_STATUS adapt; + NAME_BUFFER NameBuff [30]; + }; - LANA_ENUM enums; - zerostruct (enums); + ASTAT astat; + zeromem (&astat, sizeof (astat)); // (can't use zerostruct here in VC6) - ncb.ncb_command = NCBENUM; - ncb.ncb_buffer = (unsigned char*) &enums; - ncb.ncb_length = sizeof (LANA_ENUM); - NetbiosCall (&ncb); + LANA_ENUM enums; + zerostruct (enums); - for (int i = 0; i < enums.length; ++i) - { - zerostruct (ncb); - ncb.ncb_command = NCBRESET; - ncb.ncb_lana_num = enums.lana[i]; + ncb.ncb_command = NCBENUM; + ncb.ncb_buffer = (unsigned char*) &enums; + ncb.ncb_length = sizeof (LANA_ENUM); + NetbiosCall (&ncb); - if (NetbiosCall (&ncb) == 0) + for (int i = 0; i < enums.length; ++i) { zerostruct (ncb); - memcpy (ncb.ncb_callname, "* ", NCBNAMSZ); - ncb.ncb_command = NCBASTAT; + ncb.ncb_command = NCBRESET; ncb.ncb_lana_num = enums.lana[i]; - ncb.ncb_buffer = (unsigned char*) &astat; - ncb.ncb_length = sizeof (ASTAT); - if (NetbiosCall (&ncb) == 0) { - if (astat.adapt.adapter_type == 0xfe) - { - uint64 mac = 0; - for (int i = 6; --i >= 0;) - mac = (mac << 8) | astat.adapt.adapter_address [littleEndian ? i : (5 - i)]; + zerostruct (ncb); + memcpy (ncb.ncb_callname, "* ", NCBNAMSZ); + ncb.ncb_command = NCBASTAT; + ncb.ncb_lana_num = enums.lana[i]; - if (numFound < maxNum && mac != 0) - addresses [numFound++] = mac; - } + ncb.ncb_buffer = (unsigned char*) &astat; + ncb.ncb_length = sizeof (ASTAT); + + if (NetbiosCall (&ncb) == 0 && astat.adapt.adapter_type == 0xfe) + result.addIfNotAlreadyThere (MACAddress (astat.adapt.adapter_address)); } } } } - - return numFound; } -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - int numFound = getMACAddressViaGetAdaptersInfo (addresses, maxNum, littleEndian); - - if (numFound == 0) - numFound = getMACAddressesViaNetBios (addresses, maxNum, littleEndian); - - return numFound; + MACAddressHelpers::getViaGetAdaptersInfo (result); + MACAddressHelpers::getViaNetBios (result); } bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress, @@ -256110,10 +256125,8 @@ int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOut // compiled on its own). #if JUCE_INCLUDED_FILE -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - int numResults = 0; - const int s = socket (AF_INET, SOCK_DGRAM, 0); if (s != -1) { @@ -256130,22 +256143,14 @@ int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littl if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0 && (ifr.ifr_flags & IFF_LOOPBACK) == 0 - && ioctl (s, SIOCGIFHWADDR, &ifr) == 0 - && numResults < maxNum) + && ioctl (s, SIOCGIFHWADDR, &ifr) == 0) { - int64 a = 0; - for (int j = 6; --j >= 0;) - a = (a << 8) | (uint8) ifr.ifr_hwaddr.sa_data [littleEndian ? j : (5 - j)]; - - *addresses++ = a; - ++numResults; + result.addIfNotAlreadyThere (MACAddress ((const uint8*) ifr.ifr_hwaddr.sa_data)); } } close (s); } - - return numResults; } bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress, @@ -256251,11 +256256,8 @@ public: return false; } - const MemoryBlock requestHeader (createRequestHeader (hostName, hostPort, - proxyName, proxyPort, - hostPath, url, - headers, postData, - isPost)); + const MemoryBlock requestHeader (createRequestHeader (hostName, hostPort, proxyName, proxyPort, + hostPath, url, headers, postData, isPost)); size_t totalHeaderSent = 0; while (totalHeaderSent < requestHeader.getSize()) @@ -256268,10 +256270,8 @@ public: const int numToSend = jmin (1024, (int) (requestHeader.getSize() - totalHeaderSent)); - if (send (socketHandle, - ((const char*) requestHeader.getData()) + totalHeaderSent, - numToSend, 0) - != numToSend) + if (send (socketHandle, ((const char*) requestHeader.getData()) + totalHeaderSent, numToSend, 0) + != numToSend) { closeSocket(); return false; @@ -256290,8 +256290,6 @@ public: if (responseHeader.isNotEmpty()) { - //DBG (responseHeader); - headerLines.clear(); headerLines.addLines (responseHeader); @@ -264109,46 +264107,30 @@ void PlatformUtilities::fpuReset() // compiled on its own). #if JUCE_INCLUDED_FILE -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - #ifndef IFT_ETHER - #define IFT_ETHER 6 - #endif - ifaddrs* addrs = 0; - int numResults = 0; if (getifaddrs (&addrs) == 0) { - const ifaddrs* cursor = addrs; - - while (cursor != 0 && numResults < maxNum) + for (const ifaddrs* cursor = addrs; cursor != 0; cursor = cursor->ifa_next) { sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr; if (sto->ss_family == AF_LINK) { const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr; - if (sadd->sdl_type == IFT_ETHER) - { - const uint8* const addr = ((const uint8*) sadd->sdl_data) + sadd->sdl_nlen; + #ifndef IFT_ETHER + #define IFT_ETHER 6 + #endif - uint64 a = 0; - for (int i = 6; --i >= 0;) - a = (a << 8) | addr [littleEndian ? i : (5 - i)]; - - *addresses++ = (int64) a; - ++numResults; - } + if (sadd->sdl_type == IFT_ETHER) + result.addIfNotAlreadyThere (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen)); } - - cursor = cursor->ifa_next; } freeifaddrs (addrs); } - - return numResults; } bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress, diff --git a/juce_amalgamated.h b/juce_amalgamated.h index 2cceaeb46a..71800d6fdb 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -15112,20 +15112,19 @@ public: static const String getCpuVendor(); /** Checks whether Intel MMX instructions are available. */ - static bool hasMMX() throw() { return cpuFlags.hasMMX; } + static bool hasMMX() throw() { return cpuFlags.hasMMX; } /** Checks whether Intel SSE instructions are available. */ - static bool hasSSE() throw() { return cpuFlags.hasSSE; } + static bool hasSSE() throw() { return cpuFlags.hasSSE; } /** Checks whether Intel SSE2 instructions are available. */ - static bool hasSSE2() throw() { return cpuFlags.hasSSE2; } + static bool hasSSE2() throw() { return cpuFlags.hasSSE2; } /** Checks whether AMD 3DNOW instructions are available. */ - static bool has3DNow() throw() { return cpuFlags.has3DNow; } + static bool has3DNow() throw() { return cpuFlags.has3DNow; } - /** Returns the number of CPUs. - */ - static int getNumCpus() throw() { return cpuFlags.numCpus; } + /** Returns the number of CPUs. */ + static int getNumCpus() throw() { return cpuFlags.numCpus; } /** Finds out how much RAM is in the machine. @@ -15140,34 +15139,6 @@ public: */ static int getPageSize(); - /** Returns a list of MAC addresses found on this machine. - - @param addresses an array into which the MAC addresses should be copied - @param maxNum the number of elements in this array - @param littleEndian the endianness of the numbers to return. If this is true, - the least-significant byte of each number is the first byte - of the mac address. If false, the least significant byte is - the last number. Note that the default values of this parameter - are different on Mac/PC to avoid breaking old software that was - written before this parameter was added (when the two systems - defaulted to using different endiannesses). In newer - software you probably want to specify an explicit value - for this. - @returns the number of MAC addresses that were found - */ - static int getMACAddresses (int64* addresses, int maxNum, -#if JUCE_MAC - bool littleEndian = true); -#else - bool littleEndian = false); -#endif - - /** Returns a list of MAC addresses found on this machine. - - @returns an array of strings containing the MAC addresses that were found - */ - static const StringArray getMACAddressStrings(); - // not-for-public-use platform-specific method gets called at startup to initialise things. static void initialiseStats(); @@ -16418,6 +16389,71 @@ private: /*** End of inlined file: juce_ZipFile.h ***/ +#endif +#ifndef __JUCE_MACADDRESS_JUCEHEADER__ + +/*** Start of inlined file: juce_MACAddress.h ***/ +#ifndef __JUCE_MACADDRESS_JUCEHEADER__ +#define __JUCE_MACADDRESS_JUCEHEADER__ + +/** + A wrapper for a streaming (TCP) socket. + + This allows low-level use of sockets; for an easier-to-use messaging layer on top of + sockets, you could also try the InterprocessConnection class. + + @see DatagramSocket, InterprocessConnection, InterprocessConnectionServer +*/ +class JUCE_API MACAddress +{ +public: + + /** Populates a list of the MAC addresses of all the available network cards. */ + static void findAllAddresses (Array& results); + + /** Creates a null address (00-00-00-00-00-00). */ + MACAddress(); + + /** Creates a copy of another address. */ + MACAddress (const MACAddress& other); + + /** Creates a copy of another address. */ + MACAddress& operator= (const MACAddress& other); + + /** Creates an address from 6 bytes. */ + explicit MACAddress (const uint8 bytes[6]); + + /** Returns a pointer to the 6 bytes that make up this address. */ + const uint8* getBytes() const throw() { return asBytes; } + + /** Returns a dash-separated string in the form "11-22-33-44-55-66" */ + const String toString() const; + + /** Returns the address in the lower 6 bytes of an int64. + + This uses a little-endian arrangement, with the first byte of the address being + stored in the least-significant byte of the result value. + */ + int64 toInt64() const throw(); + + /** Returns true if this address is null (00-00-00-00-00-00). */ + bool isNull() const throw(); + + bool operator== (const MACAddress& other) const throw(); + bool operator!= (const MACAddress& other) const throw(); + +private: + union + { + uint64 asInt64; + uint8 asBytes[6]; + }; +}; + +#endif // __JUCE_MACADDRESS_JUCEHEADER__ +/*** End of inlined file: juce_MACAddress.h ***/ + + #endif #ifndef __JUCE_SOCKET_JUCEHEADER__ diff --git a/src/core/juce_SystemStats.cpp b/src/core/juce_SystemStats.cpp index aca7bcd13d..41c416afcb 100644 --- a/src/core/juce_SystemStats.cpp +++ b/src/core/juce_SystemStats.cpp @@ -43,26 +43,6 @@ const String SystemStats::getJUCEVersion() + "." + String (JUCE_BUILDNUMBER); } -const StringArray SystemStats::getMACAddressStrings() -{ - int64 macAddresses [16]; - const int numAddresses = getMACAddresses (macAddresses, numElementsInArray (macAddresses), false); - - StringArray s; - - for (int i = 0; i < numAddresses; ++i) - { - s.add (String::toHexString (0xff & (int) (macAddresses [i] >> 40)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 32)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 24)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 16)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 8)).paddedLeft ('0', 2) - + "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 0)).paddedLeft ('0', 2)); - } - - return s; -} - //============================================================================== #ifdef JUCE_DLL diff --git a/src/core/juce_SystemStats.h b/src/core/juce_SystemStats.h index 9318febae1..63141bf0b4 100644 --- a/src/core/juce_SystemStats.h +++ b/src/core/juce_SystemStats.h @@ -117,20 +117,19 @@ public: static const String getCpuVendor(); /** Checks whether Intel MMX instructions are available. */ - static bool hasMMX() throw() { return cpuFlags.hasMMX; } + static bool hasMMX() throw() { return cpuFlags.hasMMX; } /** Checks whether Intel SSE instructions are available. */ - static bool hasSSE() throw() { return cpuFlags.hasSSE; } + static bool hasSSE() throw() { return cpuFlags.hasSSE; } /** Checks whether Intel SSE2 instructions are available. */ - static bool hasSSE2() throw() { return cpuFlags.hasSSE2; } + static bool hasSSE2() throw() { return cpuFlags.hasSSE2; } /** Checks whether AMD 3DNOW instructions are available. */ - static bool has3DNow() throw() { return cpuFlags.has3DNow; } + static bool has3DNow() throw() { return cpuFlags.has3DNow; } - /** Returns the number of CPUs. - */ - static int getNumCpus() throw() { return cpuFlags.numCpus; } + /** Returns the number of CPUs. */ + static int getNumCpus() throw() { return cpuFlags.numCpus; } //============================================================================== /** Finds out how much RAM is in the machine. @@ -146,36 +145,6 @@ public: */ static int getPageSize(); - //============================================================================== - /** Returns a list of MAC addresses found on this machine. - - @param addresses an array into which the MAC addresses should be copied - @param maxNum the number of elements in this array - @param littleEndian the endianness of the numbers to return. If this is true, - the least-significant byte of each number is the first byte - of the mac address. If false, the least significant byte is - the last number. Note that the default values of this parameter - are different on Mac/PC to avoid breaking old software that was - written before this parameter was added (when the two systems - defaulted to using different endiannesses). In newer - software you probably want to specify an explicit value - for this. - @returns the number of MAC addresses that were found - */ - static int getMACAddresses (int64* addresses, int maxNum, -#if JUCE_MAC - bool littleEndian = true); -#else - bool littleEndian = false); -#endif - - /** Returns a list of MAC addresses found on this machine. - - @returns an array of strings containing the MAC addresses that were found - */ - static const StringArray getMACAddressStrings(); - - //============================================================================== // not-for-public-use platform-specific method gets called at startup to initialise things. static void initialiseStats(); diff --git a/src/core/juce_Uuid.cpp b/src/core/juce_Uuid.cpp index fde98bfafb..4f58caa431 100644 --- a/src/core/juce_Uuid.cpp +++ b/src/core/juce_Uuid.cpp @@ -30,7 +30,7 @@ BEGIN_JUCE_NAMESPACE #include "juce_Uuid.h" #include "juce_Random.h" #include "juce_Time.h" -#include "juce_SystemStats.h" +#include "../io/network/juce_MACAddress.h" #include "../containers/juce_MemoryBlock.h" @@ -46,7 +46,12 @@ Uuid::Uuid() if (! hasCheckedMacAddresses) { hasCheckedMacAddresses = true; - SystemStats::getMACAddresses (macAddresses, 2); + + Array result; + MACAddress::findAllAddresses (result); + + for (int i = 0; i < numElementsInArray (macAddresses); ++i) + macAddresses[i] = result[i].toInt64(); } value.asInt64[0] = macAddresses[0]; diff --git a/src/io/network/juce_MACAddress.cpp b/src/io/network/juce_MACAddress.cpp new file mode 100644 index 0000000000..ac6ce5d1d4 --- /dev/null +++ b/src/io/network/juce_MACAddress.cpp @@ -0,0 +1,88 @@ +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-10 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +#include "../../core/juce_StandardHeader.h" + +BEGIN_JUCE_NAMESPACE + +#include "juce_MACAddress.h" + + +//============================================================================== +MACAddress::MACAddress() + : asInt64 (0) +{ +} + +MACAddress::MACAddress (const MACAddress& other) + : asInt64 (other.asInt64) +{ +} + +MACAddress& MACAddress::operator= (const MACAddress& other) +{ + asInt64 = other.asInt64; + return *this; +} + +MACAddress::MACAddress (const uint8 bytes[6]) + : asInt64 (0) +{ + memcpy (asBytes, bytes, sizeof (asBytes)); +} + +const String MACAddress::toString() const +{ + String s; + s.preallocateStorage (18); + + for (int i = 0; i < numElementsInArray (asBytes); ++i) + { + s << String::toHexString ((int) asBytes[i]).paddedLeft ('0', 2); + + if (i < numElementsInArray (asBytes) - 1) + s << '-'; + } + + return s; +} + +int64 MACAddress::toInt64() const throw() +{ + int64 n = 0; + + for (int i = numElementsInArray (asBytes); --i >= 0;) + n = (n << 8) | asBytes[i]; + + return n; +} + +bool MACAddress::isNull() const throw() { return asInt64 == 0; } + +bool MACAddress::operator== (const MACAddress& other) const throw() { return asInt64 == other.asInt64; } +bool MACAddress::operator!= (const MACAddress& other) const throw() { return asInt64 != other.asInt64; } + + +END_JUCE_NAMESPACE diff --git a/src/io/network/juce_MACAddress.h b/src/io/network/juce_MACAddress.h new file mode 100644 index 0000000000..d0c4728c41 --- /dev/null +++ b/src/io/network/juce_MACAddress.h @@ -0,0 +1,90 @@ +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-10 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +#ifndef __JUCE_MACADDRESS_JUCEHEADER__ +#define __JUCE_MACADDRESS_JUCEHEADER__ + +#include "../../containers/juce_Array.h" + + +//============================================================================== +/** + A wrapper for a streaming (TCP) socket. + + This allows low-level use of sockets; for an easier-to-use messaging layer on top of + sockets, you could also try the InterprocessConnection class. + + @see DatagramSocket, InterprocessConnection, InterprocessConnectionServer +*/ +class JUCE_API MACAddress +{ +public: + //============================================================================== + /** Populates a list of the MAC addresses of all the available network cards. */ + static void findAllAddresses (Array& results); + + //============================================================================== + /** Creates a null address (00-00-00-00-00-00). */ + MACAddress(); + + /** Creates a copy of another address. */ + MACAddress (const MACAddress& other); + + /** Creates a copy of another address. */ + MACAddress& operator= (const MACAddress& other); + + /** Creates an address from 6 bytes. */ + explicit MACAddress (const uint8 bytes[6]); + + /** Returns a pointer to the 6 bytes that make up this address. */ + const uint8* getBytes() const throw() { return asBytes; } + + /** Returns a dash-separated string in the form "11-22-33-44-55-66" */ + const String toString() const; + + /** Returns the address in the lower 6 bytes of an int64. + + This uses a little-endian arrangement, with the first byte of the address being + stored in the least-significant byte of the result value. + */ + int64 toInt64() const throw(); + + /** Returns true if this address is null (00-00-00-00-00-00). */ + bool isNull() const throw(); + + bool operator== (const MACAddress& other) const throw(); + bool operator!= (const MACAddress& other) const throw(); + + //============================================================================== +private: + union + { + uint64 asInt64; + uint8 asBytes[6]; + }; +}; + + +#endif // __JUCE_MACADDRESS_JUCEHEADER__ diff --git a/src/juce_core_includes.h b/src/juce_core_includes.h index 05e11be9a4..7147bf72c4 100644 --- a/src/juce_core_includes.h +++ b/src/juce_core_includes.h @@ -182,6 +182,9 @@ #ifndef __JUCE_ZIPFILE_JUCEHEADER__ #include "io/files/juce_ZipFile.h" #endif +#ifndef __JUCE_MACADDRESS_JUCEHEADER__ + #include "io/network/juce_MACAddress.h" +#endif #ifndef __JUCE_SOCKET_JUCEHEADER__ #include "io/network/juce_Socket.h" #endif diff --git a/src/native/juce_linux_NativeCode.cpp b/src/native/juce_linux_NativeCode.cpp index abb57f1f31..5ca65e9750 100644 --- a/src/native/juce_linux_NativeCode.cpp +++ b/src/native/juce_linux_NativeCode.cpp @@ -61,6 +61,7 @@ BEGIN_JUCE_NAMESPACE #include "../io/files/juce_FileOutputStream.h" #include "../io/files/juce_DirectoryIterator.h" #include "../io/network/juce_URL.h" +#include "../io/network/juce_MACAddress.h" #include "../io/streams/juce_MemoryInputStream.h" #include "../io/streams/juce_MemoryOutputStream.h" #include "../events/juce_MessageManager.h" diff --git a/src/native/juce_mac_NativeCode.mm b/src/native/juce_mac_NativeCode.mm index 9d0efded75..dec8152ca0 100644 --- a/src/native/juce_mac_NativeCode.mm +++ b/src/native/juce_mac_NativeCode.mm @@ -50,6 +50,7 @@ BEGIN_JUCE_NAMESPACE #include "../io/files/juce_NamedPipe.h" #include "../io/files/juce_DirectoryIterator.h" #include "../io/network/juce_URL.h" +#include "../io/network/juce_MACAddress.h" #include "../io/streams/juce_MemoryInputStream.h" #include "../io/streams/juce_BufferedInputStream.h" #include "../core/juce_PlatformUtilities.h" diff --git a/src/native/juce_win32_NativeCode.cpp b/src/native/juce_win32_NativeCode.cpp index 4b59973c5e..947f84754b 100644 --- a/src/native/juce_win32_NativeCode.cpp +++ b/src/native/juce_win32_NativeCode.cpp @@ -51,6 +51,7 @@ BEGIN_JUCE_NAMESPACE #include "../io/files/juce_NamedPipe.h" #include "../io/files/juce_DirectoryIterator.h" #include "../io/network/juce_URL.h" +#include "../io/network/juce_MACAddress.h" #include "../core/juce_PlatformUtilities.h" #include "../text/juce_LocalisedStrings.h" #include "../utilities/juce_DeletedAtShutdown.h" diff --git a/src/native/linux/juce_linux_Network.cpp b/src/native/linux/juce_linux_Network.cpp index 955f03e307..136ec0f452 100644 --- a/src/native/linux/juce_linux_Network.cpp +++ b/src/native/linux/juce_linux_Network.cpp @@ -29,10 +29,8 @@ //============================================================================== -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - int numResults = 0; - const int s = socket (AF_INET, SOCK_DGRAM, 0); if (s != -1) { @@ -49,22 +47,14 @@ int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littl if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0 && (ifr.ifr_flags & IFF_LOOPBACK) == 0 - && ioctl (s, SIOCGIFHWADDR, &ifr) == 0 - && numResults < maxNum) + && ioctl (s, SIOCGIFHWADDR, &ifr) == 0) { - int64 a = 0; - for (int j = 6; --j >= 0;) - a = (a << 8) | (uint8) ifr.ifr_hwaddr.sa_data [littleEndian ? j : (5 - j)]; - - *addresses++ = a; - ++numResults; + result.addIfNotAlreadyThere (MACAddress ((const uint8*) ifr.ifr_hwaddr.sa_data)); } } close (s); } - - return numResults; } @@ -173,11 +163,8 @@ public: return false; } - const MemoryBlock requestHeader (createRequestHeader (hostName, hostPort, - proxyName, proxyPort, - hostPath, url, - headers, postData, - isPost)); + const MemoryBlock requestHeader (createRequestHeader (hostName, hostPort, proxyName, proxyPort, + hostPath, url, headers, postData, isPost)); size_t totalHeaderSent = 0; while (totalHeaderSent < requestHeader.getSize()) @@ -190,10 +177,8 @@ public: const int numToSend = jmin (1024, (int) (requestHeader.getSize() - totalHeaderSent)); - if (send (socketHandle, - ((const char*) requestHeader.getData()) + totalHeaderSent, - numToSend, 0) - != numToSend) + if (send (socketHandle, ((const char*) requestHeader.getData()) + totalHeaderSent, numToSend, 0) + != numToSend) { closeSocket(); return false; @@ -212,8 +197,6 @@ public: if (responseHeader.isNotEmpty()) { - //DBG (responseHeader); - headerLines.clear(); headerLines.addLines (responseHeader); diff --git a/src/native/mac/juce_mac_Network.mm b/src/native/mac/juce_mac_Network.mm index 6405933562..24c1d0ea47 100644 --- a/src/native/mac/juce_mac_Network.mm +++ b/src/native/mac/juce_mac_Network.mm @@ -28,46 +28,30 @@ #if JUCE_INCLUDED_FILE //============================================================================== -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - #ifndef IFT_ETHER - #define IFT_ETHER 6 - #endif - ifaddrs* addrs = 0; - int numResults = 0; if (getifaddrs (&addrs) == 0) { - const ifaddrs* cursor = addrs; - - while (cursor != 0 && numResults < maxNum) + for (const ifaddrs* cursor = addrs; cursor != 0; cursor = cursor->ifa_next) { sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr; if (sto->ss_family == AF_LINK) { const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr; - if (sadd->sdl_type == IFT_ETHER) - { - const uint8* const addr = ((const uint8*) sadd->sdl_data) + sadd->sdl_nlen; - - uint64 a = 0; - for (int i = 6; --i >= 0;) - a = (a << 8) | addr [littleEndian ? i : (5 - i)]; + #ifndef IFT_ETHER + #define IFT_ETHER 6 + #endif - *addresses++ = (int64) a; - ++numResults; - } + if (sadd->sdl_type == IFT_ETHER) + result.addIfNotAlreadyThere (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen)); } - - cursor = cursor->ifa_next; } freeifaddrs (addrs); } - - return numResults; } //============================================================================== diff --git a/src/native/windows/juce_win32_Network.cpp b/src/native/windows/juce_win32_Network.cpp index 7b90a943a9..c71f89f23c 100644 --- a/src/native/windows/juce_win32_Network.cpp +++ b/src/native/windows/juce_win32_Network.cpp @@ -325,121 +325,91 @@ void juce_closeInternetFile (void* handle) } //============================================================================== -static int getMACAddressViaGetAdaptersInfo (int64* addresses, int maxNum, const bool littleEndian) throw() +namespace MACAddressHelpers { - int numFound = 0; - - DynamicLibraryLoader dll ("iphlpapi.dll"); - DynamicLibraryImport (GetAdaptersInfo, getAdaptersInfo, DWORD, dll, (PIP_ADAPTER_INFO, PULONG)) - - if (getAdaptersInfo != 0) + void getViaGetAdaptersInfo (Array& result) { - ULONG len = sizeof (IP_ADAPTER_INFO); - MemoryBlock mb; - PIP_ADAPTER_INFO adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); + DynamicLibraryLoader dll ("iphlpapi.dll"); + DynamicLibraryImport (GetAdaptersInfo, getAdaptersInfo, DWORD, dll, (PIP_ADAPTER_INFO, PULONG)) - if (getAdaptersInfo (adapterInfo, &len) == ERROR_BUFFER_OVERFLOW) + if (getAdaptersInfo != 0) { - mb.setSize (len); - adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); - } + ULONG len = sizeof (IP_ADAPTER_INFO); + MemoryBlock mb; + PIP_ADAPTER_INFO adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); - if (getAdaptersInfo (adapterInfo, &len) == NO_ERROR) - { - PIP_ADAPTER_INFO adapter = adapterInfo; - - while (adapter != 0) + if (getAdaptersInfo (adapterInfo, &len) == ERROR_BUFFER_OVERFLOW) { - int64 mac = 0; - for (unsigned int i = 0; i < adapter->AddressLength; ++i) - mac = (mac << 8) | adapter->Address[i]; - - if (littleEndian) - mac = (int64) ByteOrder::swap ((uint64) mac); - - if (numFound < maxNum && mac != 0) - addresses [numFound++] = mac; + mb.setSize (len); + adapterInfo = (PIP_ADAPTER_INFO) mb.getData(); + } - adapter = adapter->Next; + if (getAdaptersInfo (adapterInfo, &len) == NO_ERROR) + { + for (PIP_ADAPTER_INFO adapter = adapterInfo; adapter != 0; adapter = adapter->Next) + { + if (adapter->AddressLength >= 6) + result.addIfNotAlreadyThere (MACAddress (adapter->Address)); + } } } } - return numFound; -} - -static int getMACAddressesViaNetBios (int64* addresses, int maxNum, const bool littleEndian) throw() -{ - int numFound = 0; - - DynamicLibraryLoader dll ("netapi32.dll"); - DynamicLibraryImport (Netbios, NetbiosCall, UCHAR, dll, (PNCB)) - - if (NetbiosCall != 0) + void getViaNetBios (Array& result) { - NCB ncb; - zerostruct (ncb); + DynamicLibraryLoader dll ("netapi32.dll"); + DynamicLibraryImport (Netbios, NetbiosCall, UCHAR, dll, (PNCB)) - struct ASTAT + if (NetbiosCall != 0) { - ADAPTER_STATUS adapt; - NAME_BUFFER NameBuff [30]; - }; + NCB ncb; + zerostruct (ncb); - ASTAT astat; - zeromem (&astat, sizeof (astat)); // (can't use zerostruct here in VC6) + struct ASTAT + { + ADAPTER_STATUS adapt; + NAME_BUFFER NameBuff [30]; + }; - LANA_ENUM enums; - zerostruct (enums); + ASTAT astat; + zeromem (&astat, sizeof (astat)); // (can't use zerostruct here in VC6) - ncb.ncb_command = NCBENUM; - ncb.ncb_buffer = (unsigned char*) &enums; - ncb.ncb_length = sizeof (LANA_ENUM); - NetbiosCall (&ncb); + LANA_ENUM enums; + zerostruct (enums); - for (int i = 0; i < enums.length; ++i) - { - zerostruct (ncb); - ncb.ncb_command = NCBRESET; - ncb.ncb_lana_num = enums.lana[i]; + ncb.ncb_command = NCBENUM; + ncb.ncb_buffer = (unsigned char*) &enums; + ncb.ncb_length = sizeof (LANA_ENUM); + NetbiosCall (&ncb); - if (NetbiosCall (&ncb) == 0) + for (int i = 0; i < enums.length; ++i) { zerostruct (ncb); - memcpy (ncb.ncb_callname, "* ", NCBNAMSZ); - ncb.ncb_command = NCBASTAT; + ncb.ncb_command = NCBRESET; ncb.ncb_lana_num = enums.lana[i]; - ncb.ncb_buffer = (unsigned char*) &astat; - ncb.ncb_length = sizeof (ASTAT); - if (NetbiosCall (&ncb) == 0) { - if (astat.adapt.adapter_type == 0xfe) - { - uint64 mac = 0; - for (int i = 6; --i >= 0;) - mac = (mac << 8) | astat.adapt.adapter_address [littleEndian ? i : (5 - i)]; + zerostruct (ncb); + memcpy (ncb.ncb_callname, "* ", NCBNAMSZ); + ncb.ncb_command = NCBASTAT; + ncb.ncb_lana_num = enums.lana[i]; - if (numFound < maxNum && mac != 0) - addresses [numFound++] = mac; - } + ncb.ncb_buffer = (unsigned char*) &astat; + ncb.ncb_length = sizeof (ASTAT); + + if (NetbiosCall (&ncb) == 0 && astat.adapt.adapter_type == 0xfe) + result.addIfNotAlreadyThere (MACAddress (astat.adapt.adapter_address)); } } } } - - return numFound; } -int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian) +void MACAddress::findAllAddresses (Array& result) { - int numFound = getMACAddressViaGetAdaptersInfo (addresses, maxNum, littleEndian); - - if (numFound == 0) - numFound = getMACAddressesViaNetBios (addresses, maxNum, littleEndian); - - return numFound; + MACAddressHelpers::getViaGetAdaptersInfo (result); + MACAddressHelpers::getViaNetBios (result); } //==============================================================================