aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream.c
blob: c87f999dd94a19ecea9b91a59bf059deaaf9ea29 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>
#include <osmocom/gsm/tlv.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/socket.h>

#include <osmocom/netif/stream.h>

/*
 * Client side.
 */

enum osmo_stream_client_conn_state {
        STREAM_CLIENT_LINK_STATE_NONE         = 0,
        STREAM_CLIENT_LINK_STATE_CONNECTING   = 1,
        STREAM_CLIENT_LINK_STATE_CONNECTED    = 2,
        STREAM_CLIENT_LINK_STATE_MAX
};

#define OSMO_STREAM_CLIENT_F_RECONFIG	(1 << 0)

struct osmo_stream_client_conn {
	struct osmo_fd			ofd;
	struct llist_head		tx_queue;
	struct osmo_timer_list		timer;
	enum osmo_stream_client_conn_state	state;
	const char			*addr;
	uint16_t			port;
	int (*connect_cb)(struct osmo_stream_client_conn *link);
	int (*read_cb)(struct osmo_stream_client_conn *link);
	int (*write_cb)(struct osmo_stream_client_conn *link);
	void				*data;
	int				flags;
	int				reconnect_timeout;
};

void osmo_stream_client_conn_close(struct osmo_stream_client_conn *link);

static void osmo_stream_client_reconnect(struct osmo_stream_client_conn *link)
{
	if (link->reconnect_timeout < 0) {
		LOGP(DLINP, LOGL_DEBUG, "not reconnecting, disabled.\n");
		return;
	}
	LOGP(DLINP, LOGL_DEBUG, "connection closed\n");
	osmo_stream_client_conn_close(link);
	LOGP(DLINP, LOGL_DEBUG, "retrying in %d seconds...\n",
		link->reconnect_timeout);
	osmo_timer_schedule(&link->timer, link->reconnect_timeout, 0);
	link->state = STREAM_CLIENT_LINK_STATE_CONNECTING;
}

void osmo_stream_client_conn_close(struct osmo_stream_client_conn *link)
{
	osmo_fd_unregister(&link->ofd);
	close(link->ofd.fd);
}

static void osmo_stream_client_read(struct osmo_stream_client_conn *link)
{
	LOGP(DLINP, LOGL_DEBUG, "message received\n");

	if (link->read_cb)
		link->read_cb(link);
}

static int osmo_stream_client_write(struct osmo_stream_client_conn *link)
{
	struct msgb *msg;
	struct llist_head *lh;
	int ret;

	LOGP(DLINP, LOGL_DEBUG, "sending data\n");

	if (llist_empty(&link->tx_queue)) {
		link->ofd.when &= ~BSC_FD_WRITE;
		return 0;
	}
	lh = link->tx_queue.next;
	llist_del(lh);
	msg = llist_entry(lh, struct msgb, list);

	if (link->state == STREAM_CLIENT_LINK_STATE_CONNECTING) {
		LOGP(DLINP, LOGL_ERROR, "not connected, dropping data!\n");
		return 0;
	}

	ret = send(link->ofd.fd, msg->data, msg->len, 0);
	if (ret < 0) {
		if (errno == EPIPE || errno == ENOTCONN) {
			osmo_stream_client_reconnect(link);
		}
		LOGP(DLINP, LOGL_ERROR, "error to send\n");
	}
	msgb_free(msg);
	return 0;
}

static int osmo_stream_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
{
	struct osmo_stream_client_conn *link = ofd->data;
	int error, ret;
	socklen_t len = sizeof(error);

	switch(link->state) {
	case STREAM_CLIENT_LINK_STATE_CONNECTING:
		ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
		if (ret >= 0 && error > 0) {
			osmo_stream_client_reconnect(link);
			return 0;
		}
		ofd->when &= ~BSC_FD_WRITE;
		LOGP(DLINP, LOGL_DEBUG, "connection done.\n");
		link->state = STREAM_CLIENT_LINK_STATE_CONNECTED;
		if (link->connect_cb)
			link->connect_cb(link);
		break;
	case STREAM_CLIENT_LINK_STATE_CONNECTED:
		if (what & BSC_FD_READ) {
			LOGP(DLINP, LOGL_DEBUG, "connected read\n");
			osmo_stream_client_read(link);
		}
		if (what & BSC_FD_WRITE) {
			LOGP(DLINP, LOGL_DEBUG, "connected write\n");
			osmo_stream_client_write(link);
		}
		break;
	default:
		break;
	}
        return 0;
}

static void link_timer_cb(void *data);

struct osmo_stream_client_conn *osmo_stream_client_conn_create(void *ctx)
{
	struct osmo_stream_client_conn *link;

	link = talloc_zero(ctx, struct osmo_stream_client_conn);
	if (!link)
		return NULL;

	link->ofd.fd = -1;
	link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
	link->ofd.priv_nr = 0;	/* XXX */
	link->ofd.cb = osmo_stream_client_fd_cb;
	link->ofd.data = link;
	link->state = STREAM_CLIENT_LINK_STATE_CONNECTING;
	link->timer.cb = link_timer_cb;
	link->timer.data = link;
	link->reconnect_timeout = 5;	/* default is 5 seconds. */
	INIT_LLIST_HEAD(&link->tx_queue);

	return link;
}

void
osmo_stream_client_conn_set_addr(struct osmo_stream_client_conn *link,
				 const char *addr)
{
	link->addr = talloc_strdup(link, addr);
	link->flags |= OSMO_STREAM_CLIENT_F_RECONFIG;
}

void
osmo_stream_client_conn_set_port(struct osmo_stream_client_conn *link,
				 uint16_t port)
{
	link->port = port;
	link->flags |= OSMO_STREAM_CLIENT_F_RECONFIG;
}

void osmo_stream_client_conn_set_reconnect_timeout(
	struct osmo_stream_client_conn *link, int timeout)
{
	link->reconnect_timeout = timeout;
}

void
osmo_stream_client_conn_set_data(struct osmo_stream_client_conn *link,
				 void *data)
{
	link->data = data;
}

void *osmo_stream_client_conn_get_data(struct osmo_stream_client_conn *link)
{
	return link->data;
}

struct osmo_fd *
osmo_stream_client_conn_get_ofd(struct osmo_stream_client_conn *link)
{
	return &link->ofd;
}

void
osmo_stream_client_conn_set_connect_cb(struct osmo_stream_client_conn *link,
	int (*connect_cb)(struct osmo_stream_client_conn *link))
{
	link->connect_cb = connect_cb;
}

void
osmo_stream_client_conn_set_read_cb(struct osmo_stream_client_conn *link,
	int (*read_cb)(struct osmo_stream_client_conn *link))
{
	link->read_cb = read_cb;
}

void osmo_stream_client_conn_destroy(struct osmo_stream_client_conn *link)
{
	talloc_free(link);
}

int osmo_stream_client_conn_open(struct osmo_stream_client_conn *link)
{
	int ret;

	/* we are reconfiguring this socket, close existing first. */
	if ((link->flags & OSMO_STREAM_CLIENT_F_RECONFIG) && link->ofd.fd >= 0)
		osmo_stream_client_conn_close(link);

	link->flags &= ~OSMO_STREAM_CLIENT_F_RECONFIG;

	ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
			     link->addr, link->port,
			     OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
	if (ret < 0) {
		if (errno != EINPROGRESS)
			return ret;
	}
	link->ofd.fd = ret;
	if (osmo_fd_register(&link->ofd) < 0) {
		close(ret);
		return -EIO;
	}
	return 0;
}

static void link_timer_cb(void *data)
{
	struct osmo_stream_client_conn *link = data;

	LOGP(DLINP, LOGL_DEBUG, "reconnecting.\n");

	switch(link->state) {
	case STREAM_CLIENT_LINK_STATE_CONNECTING:
		osmo_stream_client_conn_open(link);
	        break;
	default:
		break;
	}
}

void osmo_stream_client_conn_send(struct osmo_stream_client_conn *link,
				  struct msgb *msg)
{
	msgb_enqueue(&link->tx_queue, msg);
	link->ofd.when |= BSC_FD_WRITE;
}

int osmo_stream_client_conn_recv(struct osmo_stream_client_conn *link,
				 struct msgb *msg)
{
	int ret;

	ret = recv(link->ofd.fd, msg->data, msg->data_len, 0);
	if (ret < 0) {
		if (errno == EPIPE || errno == ECONNRESET) {
			LOGP(DLINP, LOGL_ERROR,
				"lost connection with server\n");
		}
		osmo_stream_client_reconnect(link);
		return ret;
	} else if (ret == 0) {
		LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
		osmo_stream_client_reconnect(link);
		return ret;
	}
	msgb_put(msg, ret);
	LOGP(DLINP, LOGL_DEBUG, "received %d bytes from server\n", ret);
	return ret;
}

/*
 * Server side.
 */

#define OSMO_STREAM_SERVER_F_RECONFIG	(1 << 0)

struct osmo_stream_server_link {
        struct osmo_fd                  ofd;
        const char                      *addr;
        uint16_t                        port;
        int (*accept_cb)(struct osmo_stream_server_link *link, int fd);
        void                            *data;
	int				flags;
};

static int osmo_stream_server_fd_cb(struct osmo_fd *ofd, unsigned int what)
{
	int ret;
	struct sockaddr_in sa;
	socklen_t sa_len = sizeof(sa);
	struct osmo_stream_server_link *link = ofd->data;

	ret = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
	if (ret < 0) {
		LOGP(DLINP, LOGL_ERROR, "failed to accept from origin "
			"peer, reason=`%s'\n", strerror(errno));
		return ret;
	}
	LOGP(DLINP, LOGL_DEBUG, "accept()ed new link from %s to port %u\n",
		inet_ntoa(sa.sin_addr), link->port);

	if (link->accept_cb)
		link->accept_cb(link, ret);

	return 0;
}

struct osmo_stream_server_link *osmo_stream_server_link_create(void *ctx)
{
	struct osmo_stream_server_link *link;

	link = talloc_zero(ctx, struct osmo_stream_server_link);
	if (!link)
		return NULL;

	link->ofd.fd = -1;
	link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
	link->ofd.cb = osmo_stream_server_fd_cb;
	link->ofd.data = link;

	return link;
}

void osmo_stream_server_link_set_addr(struct osmo_stream_server_link *link,
				      const char *addr)
{
	link->addr = talloc_strdup(link, addr);
	link->flags |= OSMO_STREAM_SERVER_F_RECONFIG;
}

void osmo_stream_server_link_set_port(struct osmo_stream_server_link *link,
				      uint16_t port)
{
	link->port = port;
	link->flags |= OSMO_STREAM_SERVER_F_RECONFIG;
}

void
osmo_stream_server_link_set_data(struct osmo_stream_server_link *link,
				 void *data)
{
	link->data = data;
}

void *osmo_stream_server_link_get_data(struct osmo_stream_server_link *link)
{
	return link->data;
}

struct osmo_fd *
osmo_stream_server_link_get_ofd(struct osmo_stream_server_link *link)
{
	return &link->ofd;
}

void osmo_stream_server_link_set_accept_cb(struct osmo_stream_server_link *link,
	int (*accept_cb)(struct osmo_stream_server_link *link, int fd))

{
	link->accept_cb = accept_cb;
}

void osmo_stream_server_link_destroy(struct osmo_stream_server_link *link)
{
	talloc_free(link);
}

int osmo_stream_server_link_open(struct osmo_stream_server_link *link)
{
	int ret;

	/* we are reconfiguring this socket, close existing first. */
	if ((link->flags & OSMO_STREAM_SERVER_F_RECONFIG) && link->ofd.fd >= 0)
		osmo_stream_server_link_close(link);

	link->flags &= ~OSMO_STREAM_SERVER_F_RECONFIG;

	ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
			     link->addr, link->port, OSMO_SOCK_F_BIND);
	if (ret < 0)
		return ret;

	link->ofd.fd = ret;
	if (osmo_fd_register(&link->ofd) < 0) {
		close(ret);
		return -EIO;
	}
	return 0;
}

void osmo_stream_server_link_close(struct osmo_stream_server_link *link)
{
	osmo_fd_unregister(&link->ofd);
	close(link->ofd.fd);
}

struct osmo_stream_server_conn {
	struct osmo_stream_server_link	*server;
        struct osmo_fd                  ofd;
        struct llist_head               tx_queue;
        int (*closed_cb)(struct osmo_stream_server_conn *peer);
        int (*cb)(struct osmo_stream_server_conn *peer);
        void                            *data;
};

static void osmo_stream_server_conn_read(struct osmo_stream_server_conn *conn)
{
	LOGP(DLINP, LOGL_DEBUG, "message received\n");

	if (conn->cb)
		conn->cb(conn);

	return;
}

static void osmo_stream_server_conn_write(struct osmo_stream_server_conn *conn)
{
	struct msgb *msg;
	struct llist_head *lh;
	int ret;

	LOGP(DLINP, LOGL_DEBUG, "sending data\n");

	if (llist_empty(&conn->tx_queue)) {
		conn->ofd.when &= ~BSC_FD_WRITE;
		return;
	}
	lh = conn->tx_queue.next;
	llist_del(lh);
	msg = llist_entry(lh, struct msgb, list);

	ret = send(conn->ofd.fd, msg->data, msg->len, 0);
	if (ret < 0) {
		LOGP(DLINP, LOGL_ERROR, "error to send\n");
	}
	msgb_free(msg);
}

static int osmo_stream_server_conn_cb(struct osmo_fd *ofd, unsigned int what)
{
	struct osmo_stream_server_conn *conn = ofd->data;

	LOGP(DLINP, LOGL_DEBUG, "connected read/write\n");
	if (what & BSC_FD_READ)
		osmo_stream_server_conn_read(conn);
	if (what & BSC_FD_WRITE)
		osmo_stream_server_conn_write(conn);

	return 0;
}

struct osmo_stream_server_conn *
osmo_stream_server_conn_create(void *ctx, struct osmo_stream_server_link *link,
	int fd,
	int (*cb)(struct osmo_stream_server_conn *conn),
	int (*closed_cb)(struct osmo_stream_server_conn *conn), void *data)
{
	struct osmo_stream_server_conn *conn;

	conn = talloc_zero(ctx, struct osmo_stream_server_conn);
	if (conn == NULL) {
		LOGP(DLINP, LOGL_ERROR, "cannot allocate new peer in server, "
			"reason=`%s'\n", strerror(errno));
		return NULL;
	}
	conn->server = link;
	conn->ofd.fd = fd;
	conn->ofd.data = conn;
	conn->ofd.cb = osmo_stream_server_conn_cb;
	conn->ofd.when = BSC_FD_READ;
	conn->cb = cb;
	conn->closed_cb = closed_cb;
	conn->data = data;
	INIT_LLIST_HEAD(&conn->tx_queue);

	if (osmo_fd_register(&conn->ofd) < 0) {
		LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
		talloc_free(conn);
		return NULL;
	}
	return conn;
}

void *osmo_stream_server_conn_get_data(struct osmo_stream_server_conn *link)
{
	return link->data;
}

struct osmo_fd *
osmo_stream_server_conn_get_ofd(struct osmo_stream_server_conn *link)
{
	return &link->ofd;
}

void osmo_stream_server_conn_destroy(struct osmo_stream_server_conn *conn)
{
	close(conn->ofd.fd);
	osmo_fd_unregister(&conn->ofd);
	if (conn->closed_cb)
		conn->closed_cb(conn);
	talloc_free(conn);
}

void osmo_stream_server_conn_send(struct osmo_stream_server_conn *conn,
				  struct msgb *msg)
{
	msgb_enqueue(&conn->tx_queue, msg);
	conn->ofd.when |= BSC_FD_WRITE;
}

int osmo_stream_server_conn_recv(struct osmo_stream_server_conn *conn,
				 struct msgb *msg)
{
	int ret;

	ret = recv(conn->ofd.fd, msg->data, msg->data_len, 0);
	if (ret < 0) {
		if (errno == EPIPE || errno == ECONNRESET) {
			LOGP(DLINP, LOGL_ERROR,
				"lost connection with server\n");
		}
		return ret;
	} else if (ret == 0) {
		LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
		return ret;
	}
	msgb_put(msg, ret);
	LOGP(DLINP, LOGL_DEBUG, "received %d bytes from client\n", ret);
	return ret;
}