ObjFW  Check-in [f7abfde081]

Overview
Comment:OFTCPSocketSOCKS5Connector: Move state enum to .m
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f7abfde0811fcb30bb34e8406400f54942c584bb493593a2edabffe8d27f53bf
User & Date: js on 2021-04-30 21:15:25
Other Links: manifest | tags
Context
2021-04-30
21:23
Make OFHashSeed private check-in: 39863b3503 user: js tags: trunk
21:15
OFTCPSocketSOCKS5Connector: Move state enum to .m check-in: f7abfde081 user: js tags: trunk
21:11
OFPlugin: Document how to create a plugin check-in: eaea08f31d user: js tags: trunk
Changes

Modified src/OFTCPSocketSOCKS5Connector.h from [444395c847] to [a3decd0834].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
25
26
27
28
29
30
31








32
33
34
35
36
37
38
39







-
-
-
-
-
-
-
-
+







	OFString *_host;
	uint16_t _port;
	id <OFTCPSocketDelegate> _Nullable _delegate;
#ifdef OF_HAVE_BLOCKS
	OFTCPSocketAsyncConnectBlock _Nullable _block;
#endif
	id _Nullable _exception;
	enum {
		OFSOCKS5StateSendAuthentication = 1,
		OFSOCKS5StateReadVersion,
		OFSOCKS5StateSendRequest,
		OFSOCKS5StateReadResponse,
		OFSOCKS5StateReadAddress,
		OFSOCKS5StateReadAddressLength,
	} _SOCKS5State;
	uint_least8_t _SOCKS5State;
	/* Longest read is domain name (max 255 bytes) + port */
	unsigned char _buffer[257];
	OFMutableData *_Nullable _request;
}

- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host

Modified src/OFTCPSocketSOCKS5Connector.m from [e52c5629f6] to [cef61681e6].

20
21
22
23
24
25
26









27
28
29
30
31
32
33
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42







+
+
+
+
+
+
+
+
+








#import "OFTCPSocketSOCKS5Connector.h"
#import "OFData.h"
#import "OFRunLoop.h"
#import "OFString.h"

#import "OFConnectionFailedException.h"

enum {
	stateSendAuthentication = 1,
	stateReadVersion,
	stateSendRequest,
	stateReadResponse,
	stateReadAddress,
	stateReadAddressLength,
};

@implementation OFTCPSocketSOCKS5Connector
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		      delegate: (id <OFTCPSocketDelegate>)delegate
#ifdef OF_HAVE_BLOCKS
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
111
112
113
114
115
116
117

118
119
120
121
122
123
124
125







-
+







		_exception = [exception retain];
		[self didConnect];
		return;
	}

	data = [OFData dataWithItems: "\x05\x01\x00" count: 3];

	_SOCKS5State = OFSOCKS5StateSendAuthentication;
	_SOCKS5State = stateSendAuthentication;
	[_socket asyncWriteData: data
		    runLoopMode: [OFRunLoop currentRunLoop].currentMode];
}

-      (bool)stream: (OFStream *)sock
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length
127
128
129
130
131
132
133
134

135
136
137
138
139
140
141
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150







-
+







		[self didConnect];
		return false;
	}

	runLoopMode = [OFRunLoop currentRunLoop].currentMode;

	switch (_SOCKS5State) {
	case OFSOCKS5StateReadVersion:
	case stateReadVersion:
		SOCKSVersion = buffer;

		if (SOCKSVersion[0] != 5 || SOCKSVersion[1] != 0) {
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port
				  socket: self
153
154
155
156
157
158
159
160

161
162
163

164
165
166
167
168
169
170
162
163
164
165
166
167
168

169
170
171

172
173
174
175
176
177
178
179







-
+


-
+







		[_request addItem: &hostLength];
		[_request addItems: _host.UTF8String count: hostLength];

		port[0] = _port >> 8;
		port[1] = _port & 0xFF;
		[_request addItems: port count: 2];

		_SOCKS5State = OFSOCKS5StateSendRequest;
		_SOCKS5State = stateSendRequest;
		[_socket asyncWriteData: _request runLoopMode: runLoopMode];
		return false;
	case OFSOCKS5StateReadResponse:
	case stateReadResponse:
		response = buffer;

		if (response[0] != 5 || response[2] != 0) {
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port
				  socket: self
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
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







-
+





-
+





-
+















-
+


-
+


-
+







			[self didConnect];
			return false;
		}

		/* Skip the rest of the response */
		switch (response[3]) {
		case 1: /* IPv4 */
			_SOCKS5State = OFSOCKS5StateReadAddress;
			_SOCKS5State = stateReadAddress;
			[_socket asyncReadIntoBuffer: _buffer
					 exactLength: 4 + 2
					 runLoopMode: runLoopMode];
			return false;
		case 3: /* Domain name */
			_SOCKS5State = OFSOCKS5StateReadAddressLength;
			_SOCKS5State = stateReadAddressLength;
			[_socket asyncReadIntoBuffer: _buffer
					 exactLength: 1
					 runLoopMode: runLoopMode];
			return false;
		case 4: /* IPv6 */
			_SOCKS5State = OFSOCKS5StateReadAddress;
			_SOCKS5State = stateReadAddress;
			[_socket asyncReadIntoBuffer: _buffer
					 exactLength: 16 + 2
					 runLoopMode: runLoopMode];
			return false;
		default:
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port
				  socket: self
				   errNo: EPROTONOSUPPORT];
			[self didConnect];
			return false;
		}

		return false;
	case OFSOCKS5StateReadAddress:
	case stateReadAddress:
		[self didConnect];
		return false;
	case OFSOCKS5StateReadAddressLength:
	case stateReadAddressLength:
		addressLength = buffer;

		_SOCKS5State = OFSOCKS5StateReadAddress;
		_SOCKS5State = stateReadAddress;
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: addressLength[0] + 2
				 runLoopMode: runLoopMode];
		return false;
	default:
		assert(0);
		return false;
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
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







-
-
+
+




-
+



-
+










		[self didConnect];
		return nil;
	}

	runLoopMode = [OFRunLoop currentRunLoop].currentMode;

	switch (_SOCKS5State) {
	case OFSOCKS5StateSendAuthentication:
		_SOCKS5State = OFSOCKS5StateReadVersion;
	case stateSendAuthentication:
		_SOCKS5State = stateReadVersion;
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: 2
				 runLoopMode: runLoopMode];
		return nil;
	case OFSOCKS5StateSendRequest:
	case stateSendRequest:
		[_request release];
		_request = nil;

		_SOCKS5State = OFSOCKS5StateReadResponse;
		_SOCKS5State = stateReadResponse;
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: 4
				 runLoopMode: runLoopMode];
		return nil;
	default:
		assert(0);
		return nil;
	}
}
@end