summaryrefslogtreecommitdiff
path: root/src/transport/tcp/tcp.c
blob: ad2d398a65edf836e41824e39fa13f0ef20a4c2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
100
101
102
103
104
105
106
107
108
109
110
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
150
151
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
264
265
266
267
268
269
270
271
272
273
274
275
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "core/nng_impl.h"

// TCP transport.   Platform specific TCP operations must be
// supplied as well.

typedef struct nni_tcp_pipe	nni_tcp_pipe;
typedef struct nni_tcp_ep	nni_tcp_ep;

// nni_tcp_pipe is one end of a TCP connection.
struct nni_tcp_pipe {
	const char *		addr;
	nni_plat_tcpsock	fd;
	uint16_t		peer;
	uint16_t		proto;
	size_t			rcvmax;
};

struct nni_tcp_ep {
	char			addr[NNG_MAXADDRLEN+1];
	nni_plat_tcpsock	fd;
	int			closed;
	uint16_t		proto;
	size_t			rcvmax;
	int			ipv4only;
};

static int
nni_tcp_tran_init(void)
{
	return (0);
}


static void
nni_tcp_tran_fini(void)
{
}


static void
nni_tcp_pipe_close(void *arg)
{
	nni_tcp_pipe *pipe = arg;

	nni_plat_tcp_shutdown(&pipe->fd);
}


static void
nni_tcp_pipe_destroy(void *arg)
{
	nni_tcp_pipe *pipe = arg;

	nni_plat_tcp_fini(&pipe->fd);
	NNI_FREE_STRUCT(pipe);
}


static int
nni_tcp_pipe_send(void *arg, nni_msg *msg)
{
	nni_tcp_pipe *pipe = arg;
	uint64_t len;
	uint8_t buf[sizeof (len)];
	nni_iov iov[3];
	int rv;

	iov[0].iov_buf = buf;
	iov[0].iov_len = sizeof (buf);
	iov[1].iov_buf = nni_msg_header(msg);
	iov[1].iov_len = nni_msg_header_len(msg);
	iov[2].iov_buf = nni_msg_body(msg);
	iov[2].iov_len = nni_msg_len(msg);

	len = (uint64_t) iov[1].iov_len + (uint64_t) iov[2].iov_len;
	NNI_PUT64(buf, len);

	if ((rv = nni_plat_tcp_send(&pipe->fd, iov, 3)) == 0) {
		nni_msg_free(msg);
	}
	return (rv);
}


static int
nni_tcp_pipe_recv(void *arg, nni_msg **msgp)
{
	nni_tcp_pipe *pipe = arg;
	nni_msg *msg;
	uint64_t len;
	uint8_t buf[sizeof (len)];
	nni_iov iov[1];
	int rv;

	iov[0].iov_buf = buf;
	iov[0].iov_len = sizeof (buf);
	if ((rv = nni_plat_tcp_recv(&pipe->fd, iov, 1)) != 0) {
		return (rv);
	}
	NNI_GET64(buf, len);
	if (len > pipe->rcvmax) {
		return (NNG_EMSGSIZE);
	}

	if ((rv = nng_msg_alloc(&msg, (size_t) len)) != 0) {
		return (rv);
	}

	iov[0].iov_len = nng_msg_len(msg);
	iov[0].iov_buf = nng_msg_body(msg);

	if ((rv = nni_plat_tcp_recv(&pipe->fd, iov, 1)) == 0) {
		*msgp = msg;
	} else {
		nni_msg_free(msg);
	}
	return (rv);
}


static uint16_t
nni_tcp_pipe_peer(void *arg)
{
	nni_tcp_pipe *pipe = arg;

	return (pipe->peer);
}


static int
nni_tcp_pipe_getopt(void *arg, int option, void *buf, size_t *szp)
{
#if 0
	nni_inproc_pipe *pipe = arg;
	size_t len;

	switch (option) {
	case NNG_OPT_LOCALADDR:
	case NNG_OPT_REMOTEADDR:
		len = strlen(pipe->addr) + 1;
		if (len > *szp) {
			(void) memcpy(buf, pipe->addr, *szp);
		} else {
			(void) memcpy(buf, pipe->addr, len);
		}
		*szp = len;
		return (0);
	}
#endif
	return (NNG_ENOTSUP);
}


static int
nni_tcp_ep_init(void **epp, const char *url, nni_sock *sock)
{
	nni_tcp_ep *ep;
	int rv;

	if (strlen(url) > NNG_MAXADDRLEN-1) {
		return (NNG_EADDRINVAL);
	}
	if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
		return (NNG_ENOMEM);
	}
	ep->closed = 0;
	ep->proto = nni_sock_proto(sock);
	ep->ipv4only = 0; // XXX: FIXME
	ep->rcvmax = nni_sock_rcvmaxsz(sock);

	if ((rv = nni_plat_tcp_init(&ep->fd)) != 0) {
		NNI_FREE_STRUCT(ep);
		return (rv);
	}

	(void) snprintf(ep->addr, sizeof (ep->addr), "%s", url);

	*epp = ep;
	return (0);
}


static void
nni_tcp_ep_fini(void *arg)
{
	nni_tcp_ep *ep = arg;

	nni_plat_tcp_fini(&ep->fd);
	NNI_FREE_STRUCT(ep);
}


static void
nni_tcp_ep_close(void *arg)
{
	nni_tcp_ep *ep = arg;

	nni_plat_tcp_shutdown(&ep->fd);
}


static int
nni_parseaddr(char *pair, char **hostp, uint16_t *portp)
{
	char *host, *port, *end;
	char c;
	int val;

	if (pair[0] == '[') {
		host = pair+1;
		// IP address enclosed ... for IPv6 usually.
		if ((end = strchr(host, ']')) == NULL) {
			return (NNG_EADDRINVAL);
		}
		*end = '\0';
		port = end + 1;
		if (*port == ':') {
			port++;
		} else if (port != '\0') {
			return (NNG_EADDRINVAL);
		}
	} else {
		host = pair;
		port = strchr(host, ':');
		if (port != NULL) {
			*port = '\0';
			port++;
		}
	}
	val = 0;
	while ((c = *port) != '\0') {
		val *= 10;
		if ((c >= '0') && (c <= '9')) {
			val += (c - '0');
		} else {
			return (NNG_EADDRINVAL);
		}
		if (val > 65535) {
			return (NNG_EADDRINVAL);
		}
		port++;
	}
	if ((strlen(host) == 0) || (strcmp(host, "*") == 0)) {
		*hostp = NULL;
	} else {
		*hostp = host;
	}
	// Stash the port in big endian (network) byte order.
	NNI_PUT16((uint8_t *) portp, val);
	return (0);
}


static int
nni_tcp_negotiate(nni_tcp_pipe *pipe)
{
	int rv;
	nni_iov iov;
	uint8_t buf[8];

	// First send our header..
	buf[0] = 0;
	buf[1] = 'S';
	buf[2] = 'P';
	buf[3] = 0;     // version
	NNI_PUT16(&buf[4], pipe->proto);
	NNI_PUT16(&buf[6], 0);

	iov.iov_buf = buf;
	iov.iov_len = 8;
	if ((rv = nni_plat_tcp_send(&pipe->fd, &iov, 1)) != 0) {
		return (rv);
	}

	iov.iov_buf = buf;
	iov.iov_len = 8;
	if ((rv = nni_plat_tcp_recv(&pipe->fd, &iov, 1)) != 0) {
		return (rv);
	}

	if ((buf[0] != 0) || (buf[1] != 'S') ||
	    (buf[2] != 'P') || (buf[3] != 0) ||
	    (buf[6] != 0) || (buf[7] != 0)) {
		return (NNG_EPROTO);
	}

	NNI_GET16(&buf[4], pipe->peer);
	return (0);
}


static int
nni_tcp_ep_connect(void *arg, void **pipep)
{
	nni_tcp_ep *ep = arg;
	nni_tcp_pipe *pipe;
	char *host;
	uint16_t port;
	int flag;
	char addr[NNG_MAXADDRLEN+1];
	nni_sockaddr lcladdr;
	nni_sockaddr remaddr;
	nni_sockaddr *bindaddr;
	int rv;

	char *lclpart;
	char *rempart;

	flag = ep->ipv4only ? NNI_FLAG_IPV4ONLY : 0;
	snprintf(addr, sizeof (addr), "%s", ep->addr + strlen("tcp://"));

	if ((rempart = strchr(addr, ';')) != NULL) {
		*rempart = '\0';
		rempart++;
		lclpart = addr;

		if ((rv = nni_parseaddr(lclpart, &host, &port)) != 0) {
			return (rv);
		}
		if ((rv = nni_plat_lookup_host(host, &lcladdr, flag)) != 0) {
			return (rv);
		}
		// The port is in the same offset for both v4 and v6.
		lcladdr.s_un.s_in.sa_port = port;
	} else {
		lclpart = NULL;
		rempart = addr;
	}

	if ((rv = nni_parseaddr(rempart, &host, &port)) != 0) {
		return (rv);
	}
	if (host == NULL) {
		return (NNG_EADDRINVAL);
	}
	if ((rv = nni_plat_lookup_host(host, &remaddr, flag)) != 0) {
		return (rv);
	}

	if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_plat_tcp_init(&pipe->fd)) != 0) {
		NNI_FREE_STRUCT(pipe);
		return (rv);
	}
	pipe->proto = ep->proto;
	pipe->rcvmax = ep->rcvmax;

	// Port is in the same place for both v4 and v6.
	remaddr.s_un.s_in.sa_port = port;

	bindaddr = lclpart == NULL ? NULL : &lcladdr;
	rv = nni_plat_tcp_connect(&pipe->fd, &remaddr, bindaddr);
	if (rv != 0) {
		nni_plat_tcp_fini(&pipe->fd);
		NNI_FREE_STRUCT(pipe);
		return (rv);
	}

	if ((rv = nni_tcp_negotiate(pipe)) != 0) {
		nni_plat_tcp_shutdown(&pipe->fd);
		nni_plat_tcp_fini(&pipe->fd);
		NNI_FREE_STRUCT(pipe);
		return (rv);
	}
	*pipep = pipe;
	return (0);
}


static int
nni_tcp_ep_bind(void *arg)
{
	nni_tcp_ep *ep = arg;
	char addr[NNG_MAXADDRLEN+1];
	char *host;
	uint16_t port;
	int flag;
	int rv;
	nni_sockaddr baddr;

	flag = ep->ipv4only ? NNI_FLAG_IPV4ONLY : 0;

	// We want to strok this, so make a copy.  Skip the scheme.
	snprintf(addr, sizeof (addr), "%s", ep->addr + strlen("tcp://"));

	if ((rv = nni_parseaddr(addr, &host, &port)) != 0) {
		return (rv);
	}
	if ((rv = nni_plat_lookup_host(host, &baddr, flag)) != 0) {
		return (rv);
	}
	baddr.s_un.s_in.sa_port = port;

	if ((rv = nni_plat_tcp_listen(&ep->fd, &baddr)) != 0) {
		return (rv);
	}
	return (0);
}


static int
nni_tcp_ep_accept(void *arg, void **pipep)
{
	nni_tcp_ep *ep = arg;
	nni_tcp_pipe *pipe;
	int rv;


	if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) {
		return (NNG_ENOMEM);
	}
	pipe->proto = ep->proto;
	pipe->rcvmax = ep->rcvmax;

	if ((rv = nni_plat_tcp_init(&pipe->fd)) != 0) {
		NNI_FREE_STRUCT(pipe);
	}

	if ((rv = nni_plat_tcp_accept(&pipe->fd, &ep->fd)) != 0) {
		nni_plat_tcp_fini(&pipe->fd);
		NNI_FREE_STRUCT(pipe);
		return (rv);
	}
	if ((rv = nni_tcp_negotiate(pipe)) != 0) {
		nni_plat_tcp_shutdown(&pipe->fd);
		nni_plat_tcp_fini(&pipe->fd);
		NNI_FREE_STRUCT(pipe);
		return (rv);
	}
	*pipep = pipe;
	return (0);
}


static nni_tran_pipe nni_tcp_pipe_ops = {
	.pipe_destroy	= nni_tcp_pipe_destroy,
	.pipe_send	= nni_tcp_pipe_send,
	.pipe_recv	= nni_tcp_pipe_recv,
	.pipe_close	= nni_tcp_pipe_close,
	.pipe_peer	= nni_tcp_pipe_peer,
	.pipe_getopt	= nni_tcp_pipe_getopt,
};

static nni_tran_ep nni_tcp_ep_ops = {
	.ep_init	= nni_tcp_ep_init,
	.ep_fini	= nni_tcp_ep_fini,
	.ep_connect	= nni_tcp_ep_connect,
	.ep_bind	= nni_tcp_ep_bind,
	.ep_accept	= nni_tcp_ep_accept,
	.ep_close	= nni_tcp_ep_close,
	.ep_setopt	= NULL,
	.ep_getopt	= NULL,
};

// This is the TCP transport linkage, and should be the only global
// symbol in this entire file.
struct nni_tran nni_tcp_tran = {
	.tran_scheme	= "tcp",
	.tran_ep	= &nni_tcp_ep_ops,
	.tran_pipe	= &nni_tcp_pipe_ops,
	.tran_init	= nni_tcp_tran_init,
	.tran_fini	= nni_tcp_tran_fini,
};