ObjFW  Diff

Differences From Artifact [91568c0543]:

To Artifact [29fca6defb]:


26
27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
26
27
28
29
30
31
32


33
34
35
36
37
38
39
40
41







-
-
+
+







#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 HAVE_IFADDRS_H
# include <ifaddrs.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
69
70
71
72
73
74
75

76
77
78
79
80
81
82
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83







+







#import "OFSystemInfo.h"
#import "OFApplication.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFOnce.h"
#ifdef OF_HAVE_SOCKETS
# import "OFSocket.h"
#endif
#import "OFString.h"

#if defined(OF_MACOS) || defined(OF_IOS)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
127
128
129
130
131
132
133







134
135
136
137
138
139
140







-
-
-
-
-
-
-








#if defined(OF_AMD64) || defined(OF_X86)
struct X86Regs {
	uint32_t eax, ebx, ecx, edx;
};
#endif

#ifdef OF_SYSTEM_INFO_HAS_NETWORK_INTERFACES
OFConstantString *const OFNetworkInterfaceAddresses =
    @"OFNetworkInterfaceAddresses";
OFConstantString *const OFNetworkInterfaceEthernetAddress =
    @"OFNetworkInterfaceEthernetAddress";
#endif

static size_t pageSize = 4096;
static size_t numberOfCPUs = 1;
static OFString *operatingSystemName = nil;
static OFString *operatingSystemVersion = nil;

static void
initOperatingSystemName(void)
840
841
842
843
844
845
846
847
848


849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886

887
888
889

890
891
892
893
894
895

896
897


898
899

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916


917
918
919

920
921
922
923
924
925
926
927

928
929
930
931

932
933
934
935
936
937

938
939
940
941
942
943
944
945
946

947
948
949

950
951
952
953
954

955
956
957
958
959
960

961
962
963
964
965
966


967
968
969

970
971

972
973
974



975
976
977
978
979
980
981
982
834
835
836
837
838
839
840


841
842

843
















844




















845



846




847

848
849

850
851
852

853

















854
855



856








857




858






859









860



861





862

863




864






865
866



867


868



869
870
871
872
873
874
875
876
877
878
879







-
-
+
+
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-

-
+

-
+
+

-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-

-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
-
+
-
-
+
-
-
-
+
+
+








#ifdef OF_WINDOWS
+ (bool)isWindowsNT
{
	return !(GetVersion() & 0x80000000);
}
#endif

#ifdef OF_SYSTEM_INFO_HAS_NETWORK_INTERFACES
static OFSocketAddress
#ifdef OF_HAVE_SOCKETS
+ (OFArray OF_GENERIC(OFString *) *)networkInterfaces
wrapSockaddr(struct sockaddr *sa)
{
	OFSocketAddress address;

	switch (sa->sa_family) {
	case AF_INET:
		address.family = OFSocketAddressFamilyIPv4;
		memcpy(&address.sockaddr.ipx, sa, sizeof(struct sockaddr_in));
		address.length = (socklen_t)sizeof(struct sockaddr_in);
		break;
# ifdef AF_INET6
	case AF_INET6:
		address.family = OFSocketAddressFamilyIPv6;
		memcpy(&address.sockaddr.ipx, sa, sizeof(struct sockaddr_in6));
		address.length = (socklen_t)sizeof(struct sockaddr_in6);
		break;
# endif
# ifdef AF_IPX
# ifdef HAVE_IF_NAMEINDEX
	case AF_IPX:
		address.family = OFSocketAddressFamilyIPX;
		memcpy(&address.sockaddr.ipx, sa, sizeof(struct sockaddr_ipx));
		address.length = (socklen_t)sizeof(struct sockaddr_ipx);
		break;
# endif
# ifdef AF_APPLETALK
	case AF_APPLETALK:
		address.family = OFSocketAddressFamilyAppleTalk;
		memcpy(&address.sockaddr.at, sa, sizeof(struct sockaddr_at));
		address.length = (socklen_t)sizeof(struct sockaddr_at);
		break;
# endif
	default:
		address.family = OFSocketAddressFamilyUnknown;
		memcpy(&address.sockaddr, sa, sizeof(struct sockaddr));
		address.length = sizeof(struct sockaddr);
		break;
	}

	OFMutableArray *ret = [OFMutableArray array];
	return address;
}

	void *pool = objc_autoreleasePoolPush();
+ (OFDictionary OF_GENERIC(OFString *, OFDictionary
    OF_GENERIC(OFNetworkInterfaceInfoKey, id) *) *)networkInterfaces
{
	OFMutableDictionary *interfaces = [OFMutableDictionary dictionary];
	OFStringEncoding encoding = [OFLocale encoding];
	struct ifaddrs *ifaddrs;
	struct if_nameindex *nameindex = if_nameindex();

	if (getifaddrs(&ifaddrs) != 0)
	if (nameindex == NULL) {
		objc_autoreleasePoolPop(pool);
		return nil;

	}
	@try {
		for (struct ifaddrs *iter = ifaddrs; iter != NULL;
		    iter = iter->ifa_next) {
			OFString *interfaceName =
			    [OFString stringWithCString: iter->ifa_name
					       encoding: encoding];
			OFMutableDictionary *interface;
			OFMutableData *addresses;
			OFSocketAddress address;

			interface = [interfaces objectForKey: interfaceName];
			if (interface == nil) {
				interface = [OFMutableDictionary dictionary];
				[interfaces setObject: interface
					       forKey: interfaceName];
			}


	@try {
			if (iter->ifa_addr == NULL)
				continue;

		for (size_t i = 0; nameindex[i].if_index != 0; i++)
# if defined(HAVE_STRUCT_SOCKADDR_LL) && defined(AF_PACKET)
			if (iter->ifa_addr->sa_family == AF_PACKET) {
				const OFNetworkInterfaceInfoKey key =
				    OFNetworkInterfaceEthernetAddress;
				struct sockaddr_ll *sll = (struct sockaddr_ll *)
				    (void *)iter->ifa_addr;
				OFData *addr;

			[ret addObject: [OFString
				/* ARP hardware address type 1 is Ethernet. */
				if (sll->sll_hatype != 1)
					continue;

			    stringWithCString: nameindex[i].if_name
				addr = [OFData dataWithItems: sll->sll_addr
						       count: sll->sll_halen];
				[interface setObject: addr forKey: key];
				continue;
			}
# endif
				     encoding: encoding]];
# if defined(HAVE_STRUCT_SOCKADDR_DL) && defined(AF_LINK) && \
    defined(IFT_ETHER) && defined(LLADDR)
			if (iter->ifa_addr->sa_family == AF_LINK) {
				const OFNetworkInterfaceInfoKey key =
				    OFNetworkInterfaceEthernetAddress;
				struct sockaddr_dl *sdl = (struct sockaddr_dl *)
				    (void *)iter->ifa_addr;
				OFData *addr;

	} @finally {
				if (sdl->sdl_type != IFT_ETHER)
					continue;

		if_freenameindex(nameindex);
				addr = [OFData dataWithItems: LLADDR(sdl)
						       count: sdl->sdl_alen];
				[interface setObject: addr forKey: key];
				continue;
			}
	}
# endif

			addresses = [interface
			    objectForKey: OFNetworkInterfaceAddresses];
			if (addresses == nil) {
				addresses = [OFMutableData
	[ret makeImmutable];
				    dataWithItemSize: sizeof(OFSocketAddress)];
				[interface
				    setObject: addresses
				       forKey: OFNetworkInterfaceAddresses];
			}


	objc_autoreleasePoolPop(pool);
			address = wrapSockaddr(iter->ifa_addr);
			[addresses addItem: &address];
		}

	} @finally {
		freeifaddrs(ifaddrs);
	return ret;
	}

	return interfaces;
# else
	return nil;
# endif
}
#endif

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}
@end