ObjFW  Check-in [81674093ac]

Overview
Comment:OFDNSResolver: Obtain name servers on Nintendo 3DS
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 81674093ac6b0f7ea39911f1a51292892f8d2d4180b1f56d4337d4909398d75b
User & Date: js on 2018-08-25 15:53:58
Other Links: manifest | tags
Context
2018-08-25
18:32
OFException: Reduce backtrace size to 16 check-in: c272d187d7 user: js tags: trunk
15:53
OFDNSResolver: Obtain name servers on Nintendo 3DS check-in: 81674093ac user: js tags: trunk
2018-08-20
01:24
OFDNSResolver: Do not use gethostname() on Wii check-in: 40bf53792d user: js tags: trunk
Changes

Modified src/OFDNSResolver.m from [fc33672140] to [f53bdeefa1].

57
58
59
60
61
62
63




64
65
66
67
68
69
70
# define __USE_INLINE__
# define __NOLIBBASE__
# define __NOGLOBALIFACE__
# include <proto/exec.h>
# include <proto/bsdsocket.h>
#endif





/*
 * RFC 1035 doesn't specify if pointers to pointers are allowed, and if so how
 * many. Since it's unspecified, we have to assume that it might happen, but we
 * also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
 * immediately rejecting pointers to itself seems like a fair balance.
 */
#define MAX_ALLOWED_POINTERS 16







>
>
>
>







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# define __USE_INLINE__
# define __NOLIBBASE__
# define __NOGLOBALIFACE__
# include <proto/exec.h>
# include <proto/bsdsocket.h>
#endif

#ifdef OF_NINTENDO_3DS
# include <3ds.h>
#endif

/*
 * RFC 1035 doesn't specify if pointers to pointers are allowed, and if so how
 * many. Since it's unspecified, we have to assume that it might happen, but we
 * also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
 * immediately rejecting pointers to itself seems like a fair balance.
 */
#define MAX_ALLOWED_POINTERS 16
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
		    selector: (SEL)selector
		     context: (id)context;
@end

@interface OFDNSResolver ()
- (void)of_setDefaults;
- (void)of_obtainSystemConfig;
#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path;
# if !defined(OF_WINDOWS) && !defined(OF_AMIGAOS4)
- (void)of_parseResolvConf: (OFString *)path;
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_obtainWindowsSystemConfig;
#endif
#ifdef OF_AMIGAOS4
- (void)of_obtainAmigaOS4SystemConfig;
#endif



- (void)of_reloadSystemConfig;
- (void)of_resolveHost: (OFString *)host
	   recordClass: (of_dns_resource_record_class_t)recordClass
	    recordType: (of_dns_resource_record_type_t)recordType
	      settings: (OFDNSResolverSettings *)settings
      nameServersIndex: (size_t)nameServersIndex
    searchDomainsIndex: (size_t)searchDomainsIndex







|












>
>
>







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
		    selector: (SEL)selector
		     context: (id)context;
@end

@interface OFDNSResolver ()
- (void)of_setDefaults;
- (void)of_obtainSystemConfig;
#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_3DS)
- (void)of_parseHosts: (OFString *)path;
# if !defined(OF_WINDOWS) && !defined(OF_AMIGAOS4)
- (void)of_parseResolvConf: (OFString *)path;
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_obtainWindowsSystemConfig;
#endif
#ifdef OF_AMIGAOS4
- (void)of_obtainAmigaOS4SystemConfig;
#endif
#ifdef OF_NINTENDO_3DS
- (void)of_obtainNintendo3DSSytemConfig;
#endif
- (void)of_reloadSystemConfig;
- (void)of_resolveHost: (OFString *)host
	   recordClass: (of_dns_resource_record_class_t)recordClass
	    recordType: (of_dns_resource_record_type_t)recordType
	      settings: (OFDNSResolverSettings *)settings
      nameServersIndex: (size_t)nameServersIndex
    searchDomainsIndex: (size_t)searchDomainsIndex
797
798
799
800
801
802
803

804



805
806
807
808
809
810
811

- (void)of_setDefaults
{
	_timeout = 2;
	_maxAttempts = 3;
	_minNumberOfDotsInAbsoluteName = 1;
	_usesTCP = false;

	_configReloadInterval = 2;



}

- (void)of_obtainSystemConfig
{
	void *pool = objc_autoreleasePoolPush();
#ifdef OF_WINDOWS
	OFString *path;







>

>
>
>







804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

- (void)of_setDefaults
{
	_timeout = 2;
	_maxAttempts = 3;
	_minNumberOfDotsInAbsoluteName = 1;
	_usesTCP = false;
#ifndef OF_NINTENDO_3DS
	_configReloadInterval = 2;
#else
	_configReloadInterval = 0;
#endif
}

- (void)of_obtainSystemConfig
{
	void *pool = objc_autoreleasePoolPush();
#ifdef OF_WINDOWS
	OFString *path;
825
826
827
828
829
830
831


832
833
834
835
836
837
838
		[self of_parseHosts: path];
# endif

	[self of_obtainWindowsSystemConfig];
#elif defined(OF_AMIGAOS4)
	[self of_parseHosts: HOSTS_PATH];
	[self of_obtainAmigaOS4SystemConfig];


#elif defined(OF_HAVE_FILES)
	[self of_parseHosts: HOSTS_PATH];
# ifdef OF_OPENBSD
	[self of_parseHosts: @"/etc/resolv.conf.tail"];
# endif

	[self of_parseResolvConf: RESOLV_CONF_PATH];







>
>







836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
		[self of_parseHosts: path];
# endif

	[self of_obtainWindowsSystemConfig];
#elif defined(OF_AMIGAOS4)
	[self of_parseHosts: HOSTS_PATH];
	[self of_obtainAmigaOS4SystemConfig];
#elif defined(OF_NINTENDO_3DS)
	[self of_obtainNintendo3DSSytemConfig];
#elif defined(OF_HAVE_FILES)
	[self of_parseHosts: HOSTS_PATH];
# ifdef OF_OPENBSD
	[self of_parseHosts: @"/etc/resolv.conf.tail"];
# endif

	[self of_parseResolvConf: RESOLV_CONF_PATH];
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
	[_IPv6Socket release];
#endif
	[_queries release];

	[super dealloc];
}

#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFCharacterSet *whitespaceCharacterSet =
	    [OFCharacterSet whitespaceCharacterSet];
	OFMutableDictionary *staticHosts;
	OFFile *file;







|







904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
	[_IPv6Socket release];
#endif
	[_queries release];

	[super dealloc];
}

#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_3DS)
- (void)of_parseHosts: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFCharacterSet *whitespaceCharacterSet =
	    [OFCharacterSet whitespaceCharacterSet];
	OFMutableDictionary *staticHosts;
	OFFile *file;
1150
1151
1152
1153
1154
1155
1156











































1157
1158
1159
1160
1161
1162
1163
		[nameServers makeImmutable];
		_nameServers = [nameServers copy];
	}

	if (GetDefaultDomainName(buffer, sizeof(buffer)))
		_localDomain = [[OFString alloc] initWithCString: buffer
							encoding: encoding];











































}
#endif

- (void)of_reloadSystemConfig
{
	/*
	 * TODO: Rather than reparsing every, check what actually changed







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







1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
		[nameServers makeImmutable];
		_nameServers = [nameServers copy];
	}

	if (GetDefaultDomainName(buffer, sizeof(buffer)))
		_localDomain = [[OFString alloc] initWithCString: buffer
							encoding: encoding];
}
#endif

#ifdef OF_NINTENDO_3DS
- (void)of_obtainNintendo3DSSytemConfig
{
	OFMutableArray *nameServers = [OFMutableArray array];
	union {
		/*
		 * For some unknown reason, this needs a 336 bytes buffer and
		 * always returns 336 bytes.
		 */
		char bytes[336];
		SOCU_DNSTableEntry entries[2];
	} buffer;
	socklen_t optLen = sizeof(buffer);

	if (SOCU_GetNetworkOpt(SOL_CONFIG, NETOPT_DNS_TABLE,
	    &buffer, &optLen) != 0)
		return;

	/*
	 * We're fine if this gets smaller in a future release (unlikely), as
	 * long as two entries still fit.
	 */
	if (optLen < sizeof(buffer.entries))
		return;

	for (uint_fast8_t i = 0; i < 2; i++) {
		uint32_t ip = OF_BSWAP32_IF_LE(buffer.entries[i].ip.s_addr);

		if (ip == 0)
			continue;

		[nameServers addObject: [OFString stringWithFormat:
		    @"%u.%u.%u.%u", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
		    (ip >> 8) & 0xFF, ip & 0xFF]];
	}

	if ([nameServers count] > 0) {
		[nameServers makeImmutable];
		_nameServers = [nameServers copy];
	}
}
#endif

- (void)of_reloadSystemConfig
{
	/*
	 * TODO: Rather than reparsing every, check what actually changed