aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-socket.c
blob: f1eb561f9bcd84ba7ce726ec07c3759b7bfc46f8 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright (C) 2015-2019 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <endian.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "afb-fdev.h"
#include "afb-socket.h"

#include "fdev.h"
#include "verbose.h"

/******************************************************************************/
#define BACKLOG  5

/******************************************************************************/

/**
 * known types
 */
enum type {
	/** type internet */
	Type_Inet,

	/** type systemd */
	Type_Systemd,

	/** type virtual socket of L4 */
	Type_L4,

	/** type Unix */
	Type_Unix
};

/**
 * Structure for known entries
 */
struct entry
{
	/** the known prefix */
	const char *prefix;

	/** the type of the entry */
	unsigned type: 2;

	/** should not set SO_REUSEADDR for servers */
	unsigned noreuseaddr: 1;

	/** should not call listen for servers */
	unsigned nolisten: 1;
};

/**
 * The known entries with the default one at the first place
 */
static struct entry entries[] = {
	{
		.prefix = "tcp:",
		.type = Type_Inet
	},
	{
		.prefix = "sd:",
		.type = Type_Systemd,
		.noreuseaddr = 1,
		.nolisten = 1
	},
	{
		.prefix = "l4vsock:",
		.type = Type_L4
	},
	{
		.prefix = "unix:",
		.type = Type_Unix
	}
};

/**
 * It is possible to set explicit api name instead of using the
 * default one.
 */
static const char as_api[] = "?as-api=";

/******************************************************************************/

/**
 * open a unix domain socket for client or server
 *
 * @param spec the specification of the path (prefix with @ for abstract)
 * @param server 0 for client, server otherwise
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
static int open_unix(const char *spec, int server)
{
	int fd, rc, abstract;
	struct sockaddr_un addr;
	size_t length;

	abstract = spec[0] == '@';

	/* check the length */
	length = strlen(spec);
	if (length >= 108) {
		errno = ENAMETOOLONG;
		return -1;
	}

	/* remove the file on need */
	if (server && !abstract)
		unlink(spec);

	/* create a  socket */
	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0)
		return fd;

	/* prepare address  */
	memset(&addr, 0, sizeof addr);
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, spec);
	if (abstract)
		addr.sun_path[0] = 0; /* implement abstract sockets */

	if (server) {
		rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
	} else {
		rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
	}
	if (rc < 0) {
		close(fd);
		return rc;
	}
	return fd;
}

/**
 * open a tcp socket for client or server
 *
 * @param spec the specification of the host:port/...
 * @param server 0 for client, server otherwise
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
static int open_tcp(const char *spec, int server, int reuseaddr)
{
	int rc, fd;
	const char *service, *host, *tail;
	struct addrinfo hint, *rai, *iai;

	/* scan the uri */
	tail = strchrnul(spec, '/');
	service = strchr(spec, ':');
	if (tail == NULL || service == NULL || tail < service) {
		errno = EINVAL;
		return -1;
	}
	host = strndupa(spec, service++ - spec);
	service = strndupa(service, tail - service);

	/* get addr */
	memset(&hint, 0, sizeof hint);
	hint.ai_family = AF_INET;
	hint.ai_socktype = SOCK_STREAM;
	if (server) {
		hint.ai_flags = AI_PASSIVE;
		if (host[0] == 0 || (host[0] == '*' && host[1] == 0))
			host = NULL;
	}
	rc = getaddrinfo(host, service, &hint, &rai);
	if (rc != 0) {
		errno = EINVAL;
		return -1;
	}

	/* get the socket */
	iai = rai;
	while (iai != NULL) {
		fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
		if (fd >= 0) {
			if (server) {
				if (reuseaddr) {
					rc = 1;
					setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
				}
				rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
			} else {
				rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
			}
			if (rc == 0) {
				freeaddrinfo(rai);
				return fd;
			}
			close(fd);
		}
		iai = iai->ai_next;
	}
	freeaddrinfo(rai);
	return -1;
}

/******************************************************************************/
#if WITH_SYSTEMD

#include "systemd.h"

/**
 * open a systemd socket for server
 *
 * @param spec the specification of the systemd name
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
static int open_systemd(const char *spec)
{
	return systemd_fds_for(spec);
}
#endif

/******************************************************************************/
#if WITH_L4VSOCK

struct sockaddr_l4
{
  unsigned short sl4_family;
  unsigned short port;
  char name[8];
  char _pad[4];
};

enum address_families_l4
{
  AF_VIO_SOCK = 50,  /* virtio-based sockets, must match the number in Linux */
  PF_VIO_SOCK = AF_VIO_SOCK,
};

#define DEFAULT_L4VSOCK_PORT 7777

/**
 * open a L4 VSOCK socket for client or server
 *
 * @param spec the specification of the path (prefix with @ for abstract)
 * @param server 0 for client, server otherwise
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
static int open_l4(const char *spec, int server)
{
	int fd, rc;
	struct sockaddr_l4 addr;
	const char *port, *slash;
	unsigned short portnum;
	size_t length;

	/* scan the spec */
	port = strchr(spec, ':');
	slash = strchr(spec, '/');
	if (port && slash && slash < port) {
		errno = EINVAL;
		return -1;
	}
	if (port) {
		rc = atoi(port + 1);
		if (rc <= 0 && rc > UINT16_MAX) {
			errno = EINVAL;
			return -1;
		}
		portnum = (unsigned short)rc;
		length = port - spec;
	} else {
		portnum = DEFAULT_L4VSOCK_PORT;
		length = slash ? slash - spec : strlen(spec);
	}

	/* check the length */
	if (length >= sizeof addr.name) {
		errno = ENAMETOOLONG;
		return -1;
	}

	/* create a  socket */
	fd = socket(PF_VIO_SOCK, SOCK_STREAM, 0);
	if (fd < 0)
		return fd;

	/* prepare address  */
	memset(&addr, 0, sizeof addr);
	addr.sl4_family = AF_VIO_SOCK;
	addr.port = portnum;
	memcpy(addr.name, spec, length);

	if (server) {
		rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
	} else {
		rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
	}
	if (rc < 0) {
		close(fd);
		return rc;
	}
	return fd;
}

#endif

/******************************************************************************/

/**
 * Get the entry of the uri by searching to its prefix
 *
 * @param uri the searched uri
 * @param offset where to store the prefix length
 *
 * @return the found entry or the default one
 */
static struct entry *get_entry(const char *uri, int *offset)
{
	int l, i = (int)(sizeof entries / sizeof * entries);

	for (;;) {
		if (!i) {
			l = 0;
			break;
		}
		i--;
		l = (int)strlen(entries[i].prefix);
		if (!strncmp(uri, entries[i].prefix, l))
			break;
	}

	*offset = l;
	return &entries[i];
}

/**
 * open socket for client or server
 *
 * @param uri the specification of the socket
 * @param server 0 for client, server otherwise
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
static int open_uri(const char *uri, int server)
{
	int fd, offset;
	struct entry *e;
	const char *api;

	/* search for the entry */
	e = get_entry(uri, &offset);

	/* get the names */
	uri += offset;
	api = strstr(uri, as_api);
	if (api)
		uri = strndupa(uri, api - uri);

	/* open the socket */
	switch (e->type) {
	case Type_Unix:
		fd = open_unix(uri, server);
		break;
	case Type_Inet:
		fd = open_tcp(uri, server, !e->noreuseaddr);
		break;
#if WITH_SYSTEMD
	case Type_Systemd:
		if (server)
			fd = open_systemd(uri);
		else {
			errno = EINVAL;
			fd = -1;
		}
		break;
#endif
#if WITH_L4VSOCK
	case Type_L4:
		fd = open_l4(uri, server);
		break;
#endif
	default:
		errno = EAFNOSUPPORT;
		fd = -1;
		break;
	}
	if (fd < 0)
		return -1;

	/* set it up */
	fcntl(fd, F_SETFD, FD_CLOEXEC);
	fcntl(fd, F_SETFL, O_NONBLOCK);
	if (server) {
		if (!e->nolisten)
			listen(fd, BACKLOG);
	}
	return fd;
}

/**
 * open socket for client or server
 *
 * @param uri the specification of the socket
 * @param server 0 for client, server otherwise
 *
 * @return the file descriptor number of the socket or -1 in case of error
 */
int afb_socket_open(const char *uri, int server)
{
	int fd = open_uri(uri, server);
	if (fd < 0)
		ERROR("can't open %s socket for %s", server ? "server" : "client", uri);
	return fd;
}

/**
 * open socket for client or server
 *
 * @param uri the specification of the socket
 * @param server 0 for client, server otherwise
 *
 * @return the fdev of the socket or NULL in case of error
 */
struct fdev *afb_socket_open_fdev(const char *uri, int server)
{
	struct fdev *fdev;
	int fd;

	fd = afb_socket_open(uri, server);
	if (fd < 0)
		fdev = NULL;
	else {
		fdev = afb_fdev_create(fd);
		if (!fdev) {
			close(fd);
			ERROR("can't make %s socket for %s", server ? "server" : "client", uri);
		}
	}
	return fdev;
}

/**
 * Get the api name of the uri
 *
 * @param uri the specification of the socket
 *
 * @return the api name or NULL if none can be deduced
 */
const char *afb_socket_api(const char *uri)
{
	int offset;
	const char *api;
	struct entry *entry;

	entry = get_entry(uri, &offset);
	uri += offset;
	uri += (entry->type == Type_Unix && *uri == '@');
	api = strstr(uri, as_api);
	if (api)
		api += sizeof as_api - 1;
	else {
		api = strrchr(uri, '/');
		if (api)
			api++;
		else
			api = uri;
		if (strchr(api, ':'))
			api = NULL;
	}
	return api;
}