ObjFW  Check-in [8e6e7177ef]

Overview
Comment:+[OFSystemInfo networkInterfaces]: Add IPv6s
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8e6e7177ef170444f8aa6bfe79e4a8f5a2c8863b6b91003dd2a6493211cab844
User & Date: js on 2023-06-05 19:35:16
Other Links: manifest | tags
Context
2023-06-05
19:40
Fix MorphOS build check-in: 62d8cbe31f user: js tags: trunk
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
Changes

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

42
43
44
45
46
47
48







49
50
51
52
53
54
55
/**
 * @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;
#endif







>
>
>
>
>
>
>







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
 * @brief The index of a network interface.
 *
 * This maps to an @ref OFNumber.
 */
extern OFNetworkInterfaceKey OFNetworkInterfaceIndex;

/**
 * @brief The IPv6 addresses of a network interface.
 *
 * This maps to an @ref OFData of @ref OFSocketAddress.
 */
extern OFNetworkInterfaceKey OFNetworkInterfaceIPv6Addresses;

/**
 * @brief The IPv4 addresses of a network interface.
 *
 * This maps to an @ref OFData of @ref OFSocketAddress.
 */
extern OFNetworkInterfaceKey OFNetworkInterfaceIPv4Addresses;
#endif

Modified src/OFSystemInfo.m from [c827d8d06a] to [9580f1e864].

70
71
72
73
74
75
76



77
78
79
80
81
82
83
84
85
86



87
88
89
90
91
92
93
#endif

#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"
# import "OFSocket+Private.h"
#endif
#import "OFString.h"




#if defined(OF_MACOS) || defined(OF_IOS)
# ifdef HAVE_SYSDIR_H
#  include <sysdir.h>
# endif
#endif
#ifdef OF_WINDOWS
# include <windows.h>







>
>
>










>
>
>







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
#endif

#import "OFSystemInfo.h"
#import "OFApplication.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_FILES
 #import "OFFile.h"
#endif
#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"

#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

#if defined(OF_MACOS) || defined(OF_IOS)
# ifdef HAVE_SYSDIR_H
#  include <sysdir.h>
# endif
#endif
#ifdef OF_WINDOWS
# include <windows.h>
127
128
129
130
131
132
133


134
135
136
137
138
139
140
    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;







>
>







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    NSSearchPathDirectory, NSSearchPathDomainMask);
extern NSSearchPathEnumerationState NSGetNextSearchPathEnumeration(
    NSSearchPathEnumerationState, char *);
#endif

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

#if defined(OF_AMD64) || defined(OF_X86)
struct X86Regs {
	uint32_t eax, ebx, ecx, edx;
882
883
884
885
886
887
888





















































































889
890
891
892
893
894
895
	}

	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);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	}

	return true;
# else
	return false;
# endif
}

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;

		interface = [ret objectForKey: name];
		if (interface == 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;
		}

		addresses = [interface
		    objectForKey: OFNetworkInterfaceIPv6Addresses];
		if (addresses == 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: OFNetworkInterfaceIPv4Addresses]
		    makeImmutable];

	return false;
# 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);
969
970
971
972
973
974
975

976
977
978
979
980
981
982
	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;
	}








>







1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
	void *pool = objc_autoreleasePoolPush();
	OFMutableDictionary *ret = [OFMutableDictionary dictionary];
	bool success = false;
	OFEnumerator *enumerator;
	OFMutableDictionary *interface;

	success |= queryNetworkInterfaceIndices(ret);
	success |= queryNetworkInterfaceIPv6Addresses(ret);
	success |= queryNetworkInterfaceIPv4Addresses(ret);

	if (!success) {
		objc_autoreleasePoolPop(pool);
		return nil;
	}

Modified tests/OFSystemInfoTests.m from [d70a468c70] to [0a683cc5fa].

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
#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: @", "];

			[OFStdOut writeString: OFSocketAddressString(address)];
		}

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

	objc_autoreleasePoolPop(pool);
}
@end







|









>
>


>
>
>
>
>
>
>
>
>
>





|













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
#endif

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

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

		firstInterface = false;

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

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

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

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

			[OFStdOut writeString: OFSocketAddressString(address)];
		}

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

			if (i > 0 || IPv6Addresses.count > 0)
				[OFStdOut writeString: @", "];

			[OFStdOut writeString: OFSocketAddressString(address)];
		}

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

	objc_autoreleasePoolPop(pool);
}
@end