Overview
Comment: | Split out +[OFSystemInfo networkInterfaces] |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
ac1baefd27a1a0dfdb4e753f0e4b4be4 |
User & Date: | js on 2023-06-10 19:40:49 |
Other Links: | manifest | tags |
Context
2023-06-10
| ||
19:49 | +[OFSystemInfo networkInterfaces]: Index on Win32 check-in: 61907aff3a user: js tags: trunk | |
19:40 | Split out +[OFSystemInfo networkInterfaces] check-in: ac1baefd27 user: js tags: trunk | |
2023-06-08
| ||
21:56 | +[OFSystemInfo networkInterfaces]: Fix typo check-in: b1dd8e5d0e user: js tags: trunk | |
Changes
Modified src/Makefile from [3265595cf5] to [53d2c1a26b].
︙ | ︙ | |||
126 127 128 129 130 131 132 133 134 135 136 137 138 139 | OFHTTPCookieManager.m \ OFHTTPRequest.m \ OFHTTPResponse.m \ OFHTTPServer.m \ OFSequencedPacketSocket.m \ OFSocket.m \ OFStreamSocket.m \ OFTCPSocket.m \ OFTLSStream.m \ OFUDPSocket.m \ ${USE_SRCS_APPLETALK} \ ${USE_SRCS_IPX} \ ${USE_SRCS_UNIX_SOCKETS} SRCS_APPLETALK = OFDDPSocket.m | > | 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | OFHTTPCookieManager.m \ OFHTTPRequest.m \ OFHTTPResponse.m \ OFHTTPServer.m \ OFSequencedPacketSocket.m \ OFSocket.m \ OFStreamSocket.m \ OFSystemInfo+NetworkInterfaces.m \ OFTCPSocket.m \ OFTLSStream.m \ OFUDPSocket.m \ ${USE_SRCS_APPLETALK} \ ${USE_SRCS_IPX} \ ${USE_SRCS_UNIX_SOCKETS} SRCS_APPLETALK = OFDDPSocket.m |
︙ | ︙ |
Added src/OFSystemInfo+NetworkInterfaces.h version [b18248e68a].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFSystemInfo.h" OF_ASSUME_NONNULL_BEGIN /** * @brief A dictionary describing a network interface, as returned by * @ref networkInterfaces. * * Keys are of type @ref OFNetworkInterfaceKey. */ typedef OFDictionary OF_GENERIC(OFString *, id) *OFNetworkInterface; /** * @brief A key of an @ref OFNetworkInterface. * * Possible values are: * * * @ref OFNetworkInterfaceIndex */ typedef OFConstantString *OFNetworkInterfaceKey; /** * @brief The index of a network interface. * * This maps to an @ref OFNumber. */ extern OFNetworkInterfaceKey OFNetworkInterfaceIndex; /** * @brief The IPv4 addresses of a network interface. * * This maps to an @ref OFData of @ref OFSocketAddress. */ extern OFNetworkInterfaceKey OFNetworkInterfaceIPv4Addresses; #ifdef OF_HAVE_IPV6 /** * @brief The IPv6 addresses of a network interface. * * This maps to an @ref OFData of @ref OFSocketAddress. */ extern OFNetworkInterfaceKey OFNetworkInterfaceIPv6Addresses; #endif #ifdef OF_HAVE_IPX /** * @brief The IPX addresses of a network interface. * * This maps to an @ref OFData of @ref OFSocketAddress. */ extern OFNetworkInterfaceKey OFNetworkInterfaceIPXAddresses; #endif #ifdef OF_HAVE_APPLETALK /** * @brief The AppleTalk addresses of a network interface. * * This maps to an @ref OFData of @ref OFSocketAddress. */ extern OFNetworkInterfaceKey OFNetworkInterfaceAppleTalkAddresses; #endif @interface OFSystemInfo (NetworkInterfaces) #ifdef OF_HAVE_CLASS_PROPERTIES @property (class, readonly, nullable, nonatomic) OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *networkInterfaces; #endif /** * @brief Returns the available (though not necessarily configured) network * interfaces. * * @return The available network interfaces */ + (nullable OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *) networkInterfaces; @end OF_ASSUME_NONNULL_END |
Added src/OFSystemInfo+NetworkInterfaces.m version [f27da60e07].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFSystemInfo.h" OFNetworkInterfaceKey OFNetworkInterfaceIndex = @"OFNetworkInterfaceIndex"; OFNetworkInterfaceKey OFNetworkInterfaceIPv4Addresses = @"OFNetworkInterfaceIPv4Addresses"; #ifdef OF_HAVE_IPV6 OFNetworkInterfaceKey OFNetworkInterfaceIPv6Addresses = @"OFNetworkInterfaceIPv6Addresses"; #endif #ifdef OF_HAVE_IPX OFNetworkInterfaceKey OFNetworkInterfaceIPXAddresses = @"OFNetworkInterfaceIPXAddresses"; #endif #ifdef OF_HAVE_APPLETALK OFNetworkInterfaceKey OFNetworkInterfaceAppleTalkAddresses = @"OFNetworkInterfaceAppleTalkAddresses"; #endif #ifdef OF_WINDOWS # include "platform/Windows/OFSystemInfo+NetworkInterfaces.m" #else # include "platform/POSIX/OFSystemInfo+NetworkInterfaces.m" #endif |
Modified src/OFSystemInfo.h from [756bbe68de] to [1f526bb329].
︙ | ︙ | |||
17 18 19 20 21 22 23 | #import "OFString.h" OF_ASSUME_NONNULL_BEGIN @class OFDictionary OF_GENERIC(KeyType, ObjectType); @class OFIRI; | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #import "OFString.h" OF_ASSUME_NONNULL_BEGIN @class OFDictionary OF_GENERIC(KeyType, ObjectType); @class OFIRI; /** * @class OFSystemInfo OFSystemInfo.h ObjFW/OFSystemInfo.h * * @brief A class for querying information about the system. */ OF_SUBCLASSING_RESTRICTED @interface OFSystemInfo: OFObject |
︙ | ︙ | |||
119 120 121 122 123 124 125 | # endif # if defined(OF_POWERPC) || defined(OF_POWERPC64) || defined(DOXYGEN) @property (class, readonly, nonatomic) bool supportsAltiVec; # endif # ifdef OF_WINDOWS @property (class, readonly, nonatomic, getter=isWindowsNT) bool windowsNT; # endif | < < < < | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # endif # if defined(OF_POWERPC) || defined(OF_POWERPC64) || defined(DOXYGEN) @property (class, readonly, nonatomic) bool supportsAltiVec; # endif # ifdef OF_WINDOWS @property (class, readonly, nonatomic, getter=isWindowsNT) bool windowsNT; # endif #endif /** * @brief Returns the size of a page. * * @return The size of a page */ |
︙ | ︙ | |||
399 400 401 402 403 404 405 | * @note This method is only available on Windows. * * @return Whether the application is running on Windows NT */ + (bool)isWindowsNT; #endif | < < < < < < < < < < < > > > > | 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | * @note This method is only available on Windows. * * @return Whether the application is running on Windows NT */ + (bool)isWindowsNT; #endif + (instancetype)alloc OF_UNAVAILABLE; - (instancetype)init OF_UNAVAILABLE; @end OF_ASSUME_NONNULL_END #ifdef OF_HAVE_SOCKETS # import "OFSystemInfo+NetworkInterfaces.h" #endif |
Modified src/OFSystemInfo.m from [faa68bcb9a] to [50bc975723].
︙ | ︙ | |||
26 27 28 29 30 31 32 | #ifdef HAVE_SYS_UTSNAME_H # include <sys/utsname.h> #endif #if defined(OF_MACOS) || defined(OF_IOS) || defined(OF_NETBSD) # include <sys/sysctl.h> #endif | < < < < < < < < < < < < < < < < | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #ifdef HAVE_SYS_UTSNAME_H # include <sys/utsname.h> #endif #if defined(OF_MACOS) || defined(OF_IOS) || defined(OF_NETBSD) # include <sys/sysctl.h> #endif #ifdef OF_AMIGAOS # define Class IntuitionClass # include <exec/execbase.h> # include <proto/exec.h> # undef Class #endif |
︙ | ︙ | |||
77 78 79 80 81 82 83 | #ifdef OF_HAVE_FILES #import "OFFile.h" #endif #import "OFIRI.h" #import "OFLocale.h" #import "OFNumber.h" #import "OFOnce.h" | < < < < < < < < < < | 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #ifdef OF_HAVE_FILES #import "OFFile.h" #endif #import "OFIRI.h" #import "OFLocale.h" #import "OFNumber.h" #import "OFOnce.h" #import "OFString.h" #import "OFInvalidFormatException.h" #import "OFOpenItemFailedException.h" #if defined(OF_MACOS) || defined(OF_IOS) # ifdef HAVE_SYSDIR_H # include <sysdir.h> # endif #endif #ifdef OF_HAIKU # include <FindDirectory.h> #endif #ifdef OF_QNX # include <sys/syspage.h> #endif |
︙ | ︙ | |||
134 135 136 137 138 139 140 | extern NSSearchPathEnumerationState NSStartSearchPathEnumeration( NSSearchPathDirectory, NSSearchPathDomainMask); extern NSSearchPathEnumerationState NSGetNextSearchPathEnumeration( NSSearchPathEnumerationState, char *); #endif | < < < < < < < < < < < < < < < < < < | 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | extern NSSearchPathEnumerationState NSStartSearchPathEnumeration( NSSearchPathDirectory, NSSearchPathDomainMask); extern NSSearchPathEnumerationState NSGetNextSearchPathEnumeration( NSSearchPathEnumerationState, char *); #endif #if defined(OF_AMD64) || defined(OF_X86) struct X86Regs { uint32_t eax, ebx, ecx, edx; }; #endif static size_t pageSize = 4096; |
︙ | ︙ | |||
863 864 865 866 867 868 869 | #endif #ifdef OF_WINDOWS + (bool)isWindowsNT { return !(GetVersion() & 0x80000000); } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | #endif #ifdef OF_WINDOWS + (bool)isWindowsNT { return !(GetVersion() & 0x80000000); } #endif - (instancetype)init { OF_INVALID_INIT_METHOD } @end |
Added src/platform/POSIX/OFSystemInfo+NetworkInterfaces.m version [6f9554552c].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #ifdef HAVE_NET_IF_H # include <net/if.h> #endif #ifdef HAVE_NET_IF_TYPES_H # include <net/if_types.h> #endif #ifdef HAVE_NET_IF_DL_H # include <net/if_dl.h> #endif #ifdef HAVE_NETPACKET_PACKET_H # include <netpacket/packet.h> #endif #ifdef HAVE_SYS_IOCTL_H # include <sys/ioctl.h> #endif #import "OFSystemInfo.h" #import "OFSystemInfo+NetworkInterfaces.h" #import "OFData.h" #import "OFDictionary.h" #ifdef OF_HAVE_FILES #import "OFFile.h" #endif #import "OFLocale.h" #import "OFNumber.h" #import "OFSocket.h" #import "OFSocket+Private.h" #import "OFString.h" #import "OFInvalidFormatException.h" #import "OFOpenItemFailedException.h" @implementation OFSystemInfo (NetworkInterfaces) static bool queryNetworkInterfaceIndices(OFMutableDictionary *ret) { #ifdef HAVE_IF_NAMEINDEX OFStringEncoding encoding = [OFLocale encoding]; struct if_nameindex *nameindex = if_nameindex(); if (nameindex == NULL) return false; @try { for (size_t i = 0; nameindex[i].if_index != 0; i++) { OFString *name = [OFString stringWithCString: nameindex[i].if_name encoding: encoding]; OFNumber *index = [OFNumber numberWithUnsignedInt: nameindex[i].if_index]; OFMutableDictionary *interface = [ret objectForKey: name]; if (interface == nil) { interface = [OFMutableDictionary dictionary]; [ret setObject: interface forKey: name]; } [interface setObject: index forKey: OFNetworkInterfaceIndex]; } } @finally { if_freenameindex(nameindex); } return true; #else return false; #endif } #if defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) static bool queryNetworkInterfaceAddresses(OFMutableDictionary *ret, OFNetworkInterfaceKey key, OFSocketAddressFamily addressFamily, int family, size_t sockaddrSize) { OFStringEncoding encoding = [OFLocale encoding]; int sock = socket(family, SOCK_DGRAM, 0); struct ifconf ifc; struct ifreq *ifrs; OFMutableDictionary *interface; OFEnumerator *enumerator; if (sock < 0) return false; ifrs = malloc(128 * sizeof(struct ifreq)); if (ifrs == NULL) { closesocket(sock); return false; } @try { char *buffer; memset(&ifc, 0, sizeof(ifc)); ifc.ifc_buf = (void *)ifrs; ifc.ifc_len = 128 * sizeof(struct ifreq); if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) return false; buffer = ifc.ifc_buf; while (buffer < (char *)ifc.ifc_buf + ifc.ifc_len) { struct ifreq *current = (struct ifreq *)(void *)buffer; OFString *name; OFMutableData *addresses; OFSocketAddress address; if (current->ifr_addr.sa_family != family) goto next; name = [OFString stringWithCString: current->ifr_name encoding: encoding]; if ((interface = [ret objectForKey: name]) == nil) { interface = [OFMutableDictionary dictionary]; [ret setObject: interface forKey: name]; } addresses = [interface objectForKey: key]; if (addresses == nil) { addresses = [OFMutableData dataWithItemSize: sizeof(OFSocketAddress)]; [interface setObject: addresses forKey: key]; } memset(&address, 0, sizeof(address)); address.family = addressFamily; memcpy(&address.sockaddr.in, ¤t->ifr_addr, sockaddrSize); [addresses addItem: &address]; next: # ifdef _SIZEOF_ADDR_IFREQ buffer += _SIZEOF_ADDR_IFREQ(*current); # else buffer += sizeof(struct ifreq); # endif } } @finally { free(ifrs); closesocket(sock); } enumerator = [ret objectEnumerator]; while ((interface = [enumerator nextObject]) != nil) [[interface objectForKey: key] makeImmutable]; return true; } #endif static bool queryNetworkInterfaceIPv4Addresses(OFMutableDictionary *ret) { #if defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) return queryNetworkInterfaceAddresses(ret, OFNetworkInterfaceIPv4Addresses, OFSocketAddressFamilyIPv4, AF_INET, sizeof(struct sockaddr_in)); #else return false; #endif } #ifdef OF_HAVE_IPV6 static bool queryNetworkInterfaceIPv6Addresses(OFMutableDictionary *ret) { # if defined(OF_LINUX) && defined(OF_HAVE_FILES) OFFile *file; OFString *line; OFMutableDictionary *interface; OFEnumerator *enumerator; @try { file = [OFFile fileWithPath: @"/proc/net/if_inet6" mode: @"r"]; } @catch (OFOpenItemFailedException *e) { return false; } while ((line = [file readLine]) != nil) { OFArray *components = [line componentsSeparatedByString: @" " options: OFStringSkipEmptyComponents]; OFString *addressString, *name; OFSocketAddress address; OFMutableData *addresses; if (components.count < 6) continue; addressString = [components objectAtIndex: 0]; name = [components objectAtIndex: 5]; if (addressString.length != 32) continue; if ((interface = [ret objectForKey: name]) == nil) { interface = [OFMutableDictionary dictionary]; [ret setObject: interface forKey: name]; } memset(&address, 0, sizeof(address)); address.family = OFSocketAddressFamilyIPv6; for (size_t i = 0; i < 32; i += 2) { unsigned long long byte; @try { byte = [[addressString substringWithRange: OFMakeRange(i, 2)] unsignedLongLongValueWithBase: 16]; } @catch (OFInvalidFormatException *e) { goto next_line; } if (byte > 0xFF) goto next_line; address.sockaddr.in6.sin6_addr.s6_addr[i / 2] = (unsigned char)byte; } if ((addresses = [interface objectForKey: OFNetworkInterfaceIPv6Addresses]) == nil) { addresses = [OFMutableData dataWithItemSize: sizeof(OFSocketAddress)]; [interface setObject: addresses forKey: OFNetworkInterfaceIPv6Addresses]; } [addresses addItem: &address]; next_line: continue; } enumerator = [ret objectEnumerator]; while ((interface = [enumerator nextObject]) != nil) [[interface objectForKey: OFNetworkInterfaceIPv6Addresses] makeImmutable]; return false; # elif defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) return queryNetworkInterfaceAddresses(ret, OFNetworkInterfaceIPv6Addresses, OFSocketAddressFamilyIPv6, AF_INET6, sizeof(struct sockaddr_in6)); # else return false; # endif } #endif #ifdef OF_HAVE_IPX static bool queryNetworkInterfaceIPXAddresses(OFMutableDictionary *ret) { # if defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) return queryNetworkInterfaceAddresses(ret, OFNetworkInterfaceIPXAddresses, OFSocketAddressFamilyIPX, AF_IPX, sizeof(struct sockaddr_ipx)); # else return false; # endif } #endif #ifdef OF_HAVE_APPLETALK static bool queryNetworkInterfaceAppleTalkAddresses(OFMutableDictionary *ret) { # if defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) return queryNetworkInterfaceAddresses(ret, OFNetworkInterfaceAppleTalkAddresses, OFSocketAddressFamilyAppleTalk, AF_APPLETALK, sizeof(struct sockaddr_at)); # else return false; # endif } #endif + (OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *)networkInterfaces { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *ret = [OFMutableDictionary dictionary]; bool success = false; OFEnumerator *enumerator; OFMutableDictionary *interface; success |= queryNetworkInterfaceIndices(ret); success |= queryNetworkInterfaceIPv4Addresses(ret); #ifdef OF_HAVE_IPV6 success |= queryNetworkInterfaceIPv6Addresses(ret); #endif #ifdef OF_HAVE_IPX success |= queryNetworkInterfaceIPXAddresses(ret); #endif #ifdef OF_HAVE_APPLETALK success |= queryNetworkInterfaceAppleTalkAddresses(ret); #endif if (!success) { objc_autoreleasePoolPop(pool); return nil; } enumerator = [ret objectEnumerator]; while ((interface = [enumerator nextObject]) != nil) [interface makeImmutable]; [ret makeImmutable]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } @end |
Added src/platform/Windows/OFSystemInfo+NetworkInterfaces.m version [8e067f5c85].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFSystemInfo.h" #import "OFSystemInfo+NetworkInterfaces.h" #import "OFData.h" #import "OFDictionary.h" #import "OFLocale.h" #import "OFSocket.h" #import "OFString.h" #include <windows.h> #define interface struct #include <iphlpapi.h> #undef interface @implementation OFSystemInfo (NetworkInterfaces) + (OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *)networkInterfaces { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *ret = [OFMutableDictionary dictionary]; OFStringEncoding encoding = [OFLocale encoding]; ULONG adapterInfoSize; PIP_ADAPTER_INFO adapterInfo; OFEnumerator *enumerator; OFMutableDictionary *interface; adapterInfoSize = sizeof(IP_ADAPTER_INFO); if ((adapterInfo = malloc(adapterInfoSize)) == NULL) { objc_autoreleasePoolPop(pool); return nil; } @try { ULONG error = GetAdaptersInfo(adapterInfo, &adapterInfoSize); if (error == ERROR_BUFFER_OVERFLOW) { PIP_ADAPTER_INFO newAdapterInfo = realloc( adapterInfo, adapterInfoSize); if (newAdapterInfo == NULL) { objc_autoreleasePoolPop(pool); return nil; } adapterInfo = newAdapterInfo; error = GetAdaptersInfo(adapterInfo, &adapterInfoSize); } if (error != ERROR_SUCCESS) { objc_autoreleasePoolPop(pool); return nil; } for (PIP_ADAPTER_INFO iter = adapterInfo; iter != NULL; iter = iter->Next) { OFString *name, *IPString; OFMutableData *addresses; OFSocketAddress address; name = [OFString stringWithCString: iter->AdapterName encoding: encoding]; if ((interface = [ret objectForKey: name]) == nil) { interface = [OFMutableDictionary dictionary]; [ret setObject: interface forKey: name]; } IPString = [OFString stringWithCString: iter->IpAddressList.IpAddress .String encoding: encoding]; if ([IPString isEqual: @"0.0.0.0"]) continue; if ((addresses = [interface objectForKey: OFNetworkInterfaceIPv4Addresses]) == nil) { addresses = [OFMutableData dataWithItemSize: sizeof(OFSocketAddress)]; [interface setObject: addresses forKey: OFNetworkInterfaceIPv4Addresses]; } address = OFSocketAddressParseIPv4(IPString, 0); [addresses addItem: &address]; } } @finally { free(adapterInfo); } enumerator = [ret objectEnumerator]; while ((interface = [enumerator nextObject]) != nil) [interface makeImmutable]; [ret makeImmutable]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } @end |