ObjFW  Check-in [a15a0854be]

Overview
Comment:+[OFSystemInfo networkInterfaces]: Add IPv4s
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a15a0854bec26e5126ca461062fbe350312b3ecd00f9281020e8a4a0e160cc1b
User & Date: js on 2023-06-05 00:50:51
Other Links: manifest | tags
Context
2023-06-05
19:35
+[OFSystemInfo networkInterfaces]: Add IPv6s check-in: 8e6e7177ef user: js tags: trunk
00:50
+[OFSystemInfo networkInterfaces]: Add IPv4s check-in: a15a0854be user: js tags: trunk
2023-06-04
15:33
Improve API for +[OFSystemInfo networkInterfaces] check-in: ef03831928 user: js tags: trunk
Changes

Modified src/OFSystemInfo.h from [757e7e248c] to [6ed8b22df6].

38
39
40
41
42
43
44
45

46
47







48
49
50
51
52
53
54
38
39
40
41
42
43
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61







-
+


+
+
+
+
+
+
+







 *   * @ref OFNetworkInterfaceIndex
 */
typedef OFConstantString *OFNetworkInterfaceKey;

/**
 * @brief The index of a network interface.
 *
 * This maps to a @ref OFNumber.
 * 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;
#endif

/**
 * @class OFSystemInfo OFSystemInfo.h ObjFW/OFSystemInfo.h
 *
 * @brief A class for querying information about the system.
 */

Modified src/OFSystemInfo.m from [8a22d63aae] to [c827d8d06a].

38
39
40
41
42
43
44



45
46
47
48
49
50
51
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54







+
+
+







#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

#ifdef OF_AMIGAOS
# define Class IntuitionClass
# include <exec/execbase.h>
# include <proto/exec.h>
# undef Class
#endif
73
74
75
76
77
78
79

80
81
82
83
84
85
86
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90







+







#import "OFDictionary.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFOnce.h"
#ifdef OF_HAVE_SOCKETS
# import "OFSocket.h"
# import "OFSocket+Private.h"
#endif
#import "OFString.h"

#if defined(OF_MACOS) || defined(OF_IOS)
# ifdef HAVE_SYSDIR_H
#  include <sysdir.h>
# endif
123
124
125
126
127
128
129


130
131
132
133
134
135
136
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142







+
+







    NSSearchPathDirectory, NSSearchPathDomainMask);
extern NSSearchPathEnumerationState NSGetNextSearchPathEnumeration(
    NSSearchPathEnumerationState, char *);
#endif

#ifdef OF_HAVE_SOCKETS
OFNetworkInterfaceKey OFNetworkInterfaceIndex = @"OFNetworkInterfaceIndex";
OFNetworkInterfaceKey OFNetworkInterfaceIPv4Addresses =
    @"OFNetworkInterfaceIPv4Addresses";
#endif

#if defined(OF_AMD64) || defined(OF_X86)
struct X86Regs {
	uint32_t eax, ebx, ecx, edx;
};
#endif
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
880

881
882
883
884
885
886
887
888
889
890
891
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
983
984
985
986
987
988
989
990
991

992



993
994
995
996
997
998
999
1000







+
-
+


-
-



-
+
-
-
+
-








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

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

+



-
+
-
-
-








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

#ifdef OF_HAVE_SOCKETS
static bool
+ (OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *)networkInterfaces
queryNetworkInterfaceIndices(OFMutableDictionary *ret)
{
# ifdef HAVE_IF_NAMEINDEX
	OFMutableDictionary *ret = [OFMutableDictionary dictionary];
	void *pool = objc_autoreleasePoolPush();
	OFStringEncoding encoding = [OFLocale encoding];
	struct if_nameindex *nameindex = if_nameindex();

	if (nameindex == NULL) {
	if (nameindex == NULL)
		objc_autoreleasePoolPop(pool);
		return nil;
		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];
			OFDictionary *interface = [OFDictionary
			    dictionaryWithObject: index
					  forKey: OFNetworkInterfaceIndex];

			[ret setObject: interface forKey: name];
		}
			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
}

static bool
queryNetworkInterfaceIPv4Addresses(OFMutableDictionary *ret)
{
# if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
	OFStringEncoding encoding = [OFLocale encoding];
	int sock = socket(AF_INET, 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 {
		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;

		for (size_t i = 0; i < ifc.ifc_len / sizeof(struct ifreq);
		    i++) {
			OFString *name;
			OFMutableData *addresses;
			OFSocketAddress address;

			if (ifrs[i].ifr_addr.sa_family != AF_INET)
				continue;

			name = [OFString stringWithCString: ifrs[i].ifr_name
						  encoding: encoding];
			interface = [ret objectForKey: name];
			if (interface == nil) {
				interface = [OFMutableDictionary dictionary];
				[ret setObject: interface forKey: name];
			}

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

			memset(&address, 0, sizeof(address));
			address.family = OFSocketAddressFamilyIPv4;
			memcpy(&address.sockaddr.in, &ifrs[i].ifr_addr,
			    sizeof(struct sockaddr_in));

			[addresses addItem: &address];
		}
	} @finally {
		free(ifrs);
		closesocket(sock);
	}
		if_freenameindex(nameindex);
	}


	enumerator = [ret objectEnumerator];
	while ((interface = [enumerator nextObject]) != nil)
		[[interface objectForKey: OFNetworkInterfaceIPv4Addresses]
		    makeImmutable];

	return true;
# else
	return false;
# 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);

	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;
	return [ret autorelease];
# else
	return nil;
# endif
}
#endif

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}
@end

Modified tests/OFSystemInfoTests.m from [4c99d3ac50] to [d70a468c70].

111
112
113
114
115
116
117



118
119
120
121
122











123






124
125
126
127
128
129
130
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







+
+
+





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







	    [OFSystemInfo supportsAltiVec]];
#endif

#ifdef OF_HAVE_SOCKETS
	networkInterfaces = [OFSystemInfo networkInterfaces];
	[OFStdOut writeString: @"[OFSystemInfo] Network interfaces: "];
	for (OFString *name in networkInterfaces) {
		OFNetworkInterface interface;
		OFData *IPv4Addresses;

		if (!firstInterface)
			[OFStdOut writeString: @"; "];

		firstInterface = false;

		[OFStdOut writeFormat: @"%@(", name];

		interface = [networkInterfaces objectForKey: name];
		IPv4Addresses = [interface
		    objectForKey: OFNetworkInterfaceIPv4Addresses];

		for (size_t i = 0; i < IPv4Addresses.count; i++) {
			const OFSocketAddress *address =
			    [IPv4Addresses itemAtIndex: i];

			if (i > 0)
		[OFStdOut writeString: name];
				[OFStdOut writeString: @", "];

			[OFStdOut writeString: OFSocketAddressString(address)];
		}

		[OFStdOut writeString: @")"];
	}
	[OFStdOut writeString: @"\n"];
#endif

	objc_autoreleasePoolPop(pool);
}
@end