aboutsummaryrefslogtreecommitdiffstats
path: root/src/datagram.c
blob: 4bc7bede9b732574706c110742e1c2853aba6bb5 (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
/* (C) 2011 by Pablo Neira Ayuso <pablo@gnumonks.org>
 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved.
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#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 <netinet/in.h>

#include <osmocom/core/linuxlist.h>
#include <osmocom/core/select.h>
#include <osmocom/core/utils.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/datagram.h>

/*! \addtogroup datagram Osmocom Datagram Socket
 *  @{
 *
 *  This code is intended to abstract any use of datagram type sockets,
 *  such as UDP.  It offers both transmitter and receiver side
 *  implementations, fully integrated with the libosmocore select loop
 *  abstraction.
 */

/*! \file datagram.c
 *  \brief Osmocom datagram socket helpers
 */


/*
 * Client side.
 */

#define OSMO_DGRAM_CLI_F_RECONF	(1 << 0)

struct osmo_dgram_tx {
	struct osmo_fd			ofd;
	struct llist_head		tx_queue;
	char				*addr;
	uint16_t			port;
	char				*local_addr;
	uint16_t			local_port;
	int (*write_cb)(struct osmo_dgram_tx *conn);
	void				*data;
	unsigned int			flags;
};

/*! \brief Close an Osmocom Datagram Transmitter
 *  \param[in] conn Osmocom Datagram Transmitter to be closed
 *  We unregister the socket fd from the osmocom select() loop
 *  abstraction and close the socket */
void osmo_dgram_tx_close(struct osmo_dgram_tx *conn)
{
	if (conn->ofd.fd == -1)
		return;
	osmo_fd_unregister(&conn->ofd);
	close(conn->ofd.fd);
	conn->ofd.fd = -1;
}

static int osmo_dgram_tx_write(struct osmo_dgram_tx *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 &= ~OSMO_FD_WRITE;
		return 0;
	}
	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 (%s)\n",
			strerror(errno));
	}
	msgb_free(msg);
	return 0;
}

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

	if (what & OSMO_FD_WRITE) {
		LOGP(DLINP, LOGL_DEBUG, "write\n");
		osmo_dgram_tx_write(conn);
	}
        return 0;
}

/*! \brief Create an Osmocom datagram transmitter
 *  \param[in] ctx talloc context from which to allocate memory
 *  This function allocates a new \ref osmo_dgram_tx and initializes
 *  it with default values
 *  \returns Osmocom Datagram Transmitter; NULL on error */
struct osmo_dgram_tx *osmo_dgram_tx_create(void *ctx)
{
	struct osmo_dgram_tx *conn;

	conn = talloc_zero(ctx, struct osmo_dgram_tx);
	if (!conn)
		return NULL;

	osmo_fd_setup(&conn->ofd, -1, OSMO_FD_READ, osmo_dgram_tx_fd_cb, conn, 0);
	INIT_LLIST_HEAD(&conn->tx_queue);

	return conn;
}


/*! \brief Set the remote address to which we transmit
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] addr Remote IP address */
void
osmo_dgram_tx_set_addr(struct osmo_dgram_tx *conn,
				const char *addr)
{
	osmo_talloc_replace_string(conn, &conn->addr, addr);
	conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
}

/*! \brief Set the remote port to which we transmit
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] port Remote Port Number */
void
osmo_dgram_tx_set_port(struct osmo_dgram_tx *conn,
				uint16_t port)
{
	conn->port = port;
	conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
}

/*! \brief Set the local address from which we transmit
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] addr Local IP address */
void
osmo_dgram_tx_set_local_addr(struct osmo_dgram_tx *conn, const char *addr)
{
	osmo_talloc_replace_string(conn, &conn->local_addr, addr);
	conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
}

/*! \brief Set the local port from which we transmit
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] port Local Port Number */
void
osmo_dgram_tx_set_local_port(struct osmo_dgram_tx *conn, uint16_t port)
{
	conn->local_port = port;
	conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
}

/*! \brief Set application private data of the datagram transmitter
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] data User-specific data (available in call-back functions) */
void
osmo_dgram_tx_set_data(struct osmo_dgram_tx *conn, void *data)
{
	conn->data = data;
}

/*! \brief Destroy a Osmocom datagram transmitter
 *  \param[in] conn Datagram Transmitter to destroy */
void osmo_dgram_tx_destroy(struct osmo_dgram_tx *conn)
{
	osmo_dgram_tx_close(conn);
	talloc_free(conn);
}

/*! \brief Open connection of an Osmocom datagram transmitter
 *  \param[in] conn Stream Client to connect
 *  \returns 0 on success; negative in case of error */
int osmo_dgram_tx_open(struct osmo_dgram_tx *conn)
{
	int ret;

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

	conn->flags &= ~OSMO_DGRAM_CLI_F_RECONF;

	ret = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
			      conn->local_addr, conn->local_port, conn->addr, conn->port,
			      OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
	if (ret < 0)
		return ret;

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

/*! \brief Enqueue data to be sent via an Osmocom datagram transmitter
 *  \param[in] conn Datagram Transmitter through which we want to send
 *  \param[in] msg Message buffer to enqueue in transmit queue */
void osmo_dgram_tx_send(struct osmo_dgram_tx *conn,
				 struct msgb *msg)
{
	msgb_enqueue(&conn->tx_queue, msg);
	conn->ofd.when |= OSMO_FD_WRITE;
}

/*
 * Server side.
 */

#define OSMO_DGRAM_RX_F_RECONF	(1 << 0)

struct osmo_dgram_rx {
        struct osmo_fd                  ofd;
        char                            *addr;
        uint16_t                        port;
	int (*cb)(struct osmo_dgram_rx *conn);
        void                            *data;
	unsigned int			flags;
};

/*! \brief Receive data via Osmocom datagram receiver
 *  \param[in] conn Datagram Receiver from which to receive
 *  \param msg pre-allocate message buffer to which received data is appended
 *  \returns number of bytes read (might be 0 for empty UDP packet), negative on error. */
int osmo_dgram_rx_recv(struct osmo_dgram_rx *conn,
				struct msgb *msg)
{
	int ret;

	ret = recv(conn->ofd.fd, msg->data, msg->data_len, 0);
	if (ret < 0) {
		LOGP(DLINP, LOGL_ERROR, "error receiving data from tx: %s\n", strerror(errno));
		return ret;
	}
	msgb_put(msg, ret);
	LOGP(DLINP, LOGL_DEBUG, "received %d bytes from tx\n", ret);
	return ret;
}

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

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

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

	LOGP(DLINP, LOGL_DEBUG, "read\n");
	if (what & OSMO_FD_READ)
		osmo_dgram_rx_read(conn);

	return 0;
}

/*! \brief Create an Osmocom datagram receiver
 *  \param[in] ctx talloc context from which to allocate memory
 *  This function allocates a new \ref osmo_dgram_rx and initializes
 *  it with default values
 *  \returns Datagram Receiver; NULL on error */
struct osmo_dgram_rx *osmo_dgram_rx_create(void *ctx)
{
	struct osmo_dgram_rx *conn;

	conn = talloc_zero(ctx, struct osmo_dgram_rx);
	if (!conn)
		return NULL;

	osmo_fd_setup(&conn->ofd, -1, OSMO_FD_READ, osmo_dgram_rx_cb, conn, 0);

	return conn;
}

/*! \brief Set the local address to which we bind
 *  \param[in] conn Datagram Receiver to modify
 *  \param[in] addr Local IP address */
void osmo_dgram_rx_set_addr(struct osmo_dgram_rx *conn,
				     const char *addr)
{
	osmo_talloc_replace_string(conn, &conn->addr, addr);
	conn->flags |= OSMO_DGRAM_RX_F_RECONF;
}

/*! \brief Set the local port to which we bind
 *  \param[in] conn Datagram Receiver to modify
 *  \param[in] port Local port number */
void osmo_dgram_rx_set_port(struct osmo_dgram_rx *conn,
				     uint16_t port)
{
	conn->port = port;
	conn->flags |= OSMO_DGRAM_RX_F_RECONF;
}

/*! \brief Set the read() call-back of the datagram receiver
 *  \param[in] conn Datagram Receiver to modify
 *  \param[in] read_cb Call-back function executed after read() */
void osmo_dgram_rx_set_read_cb(struct osmo_dgram_rx *conn,
	int (*read_cb)(struct osmo_dgram_rx *conn))
{
	conn->cb = read_cb;
}

/*! \brief Destroy the datagram receiver. Releases Memory.
 *  \param[in] conn Datagram Receiver */
void osmo_dgram_rx_destroy(struct osmo_dgram_rx *conn)
{
	osmo_dgram_rx_close(conn);
	talloc_free(conn);
}

/*! \brief Open the datagram receiver.  This actually initializes the
 *  underlying socket and binds it to the configured ip/port
 *  \param[in] conn Datagram Receiver to open */
int osmo_dgram_rx_open(struct osmo_dgram_rx *conn)
{
	int ret;

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

	conn->flags &= ~OSMO_DGRAM_RX_F_RECONF;

	ret = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
			     conn->addr, conn->port, OSMO_SOCK_F_BIND);
	if (ret < 0)
		return ret;

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


/*! \brief Close the datagram receiver and unregister from select loop
 *  Does not destroy the datagram receiver, merely closes it!
 *  \param[in] conn Stream Server Link to close */
void osmo_dgram_rx_close(struct osmo_dgram_rx *conn)
{
	if (conn->ofd.fd == -1)
		return;
	osmo_fd_unregister(&conn->ofd);
	close(conn->ofd.fd);
	conn->ofd.fd = -1;
}

/*
 * Client+Server (bidirectional communications).
 */

struct osmo_dgram {
	struct osmo_dgram_rx	*rx;
	struct osmo_dgram_tx	*tx;
	int (*read_cb)(struct osmo_dgram *conn);
	void				*data;
};

static int
dgram_rx_cb(struct osmo_dgram_rx *rx)
{
	struct osmo_dgram *conn = rx->data;

	if (conn->read_cb)
		return conn->read_cb(conn);

	return 0;
}


/*! \brief Create an Osmocom datagram transceiver (bidirectional)
 *  \param[in] ctx talloc context from which to allocate memory
 *  This function allocates a new \ref osmo_dgram and initializes
 *  it with default values.  Internally, the Transceiver is based on a
 *  tuple of transmitter (\ref osmo_dgram_tx) and receiver (\ref osmo_dgram_rx)
 *  \returns Osmocom Datagram Transceiver; NULL on error */
struct osmo_dgram *osmo_dgram_create(void *ctx)
{
	struct osmo_dgram *conn;

	conn = talloc_zero(ctx, struct osmo_dgram);
	if (!conn)
		return NULL;

	conn->rx= osmo_dgram_rx_create(ctx);
	if (conn->rx == NULL)
		return NULL;

	osmo_dgram_rx_set_read_cb(conn->rx, dgram_rx_cb);
	conn->rx->data = conn;

	conn->tx = osmo_dgram_tx_create(ctx);
	if (conn->tx == NULL) {
		osmo_dgram_rx_destroy(conn->rx);
		return NULL;
	}

	return conn;
}

/*! \brief Destroy a Osmocom datagram transceiver
 *  \param[in] conn Datagram Transceiver to destroy */
void osmo_dgram_destroy(struct osmo_dgram *conn)
{
	osmo_dgram_rx_destroy(conn->rx);
	osmo_dgram_tx_destroy(conn->tx);
}

/*! \brief Set the local address to which we bind
 *  \param[in] conn Datagram Transceiver to modify
 *  \param[in] addr Local IP address */
void
osmo_dgram_set_local_addr(struct osmo_dgram *conn, const char *addr)
{
	osmo_dgram_rx_set_addr(conn->rx, addr);
}

/*! \brief Set the remote address to which we transmit/connect
 *  \param[in] conn Datagram Transceiver to modify
 *  \param[in] addr Remote IP address */
void
osmo_dgram_set_remote_addr(struct osmo_dgram *conn, const char *addr)
{
	osmo_dgram_tx_set_addr(conn->tx, addr);
}

/*! \brief Set the local port to which we bind
 *  \param[in] conn Datagram Transceiver to modify
 *  \param[in] port Local Port Number */
void
osmo_dgram_set_local_port(struct osmo_dgram *conn, uint16_t port)
{
	osmo_dgram_rx_set_port(conn->rx, port);
}

/*! \brief Set the remote port to which we transmit
 *  \param[in] conn Datagram Transceiver to modify
 *  \param[in] port Remote Port Number */
void
osmo_dgram_set_remote_port(struct osmo_dgram *conn, uint16_t port)
{
	osmo_dgram_tx_set_port(conn->tx, port);
}

/*! \brief Set the read() call-back of the datagram receiver
 *  \param[in] conn Datagram Receiver to modify
 *  \param[in] read_cb Call-back function executed after read() */
void osmo_dgram_set_read_cb(struct osmo_dgram *conn,
			    int (*read_cb)(struct osmo_dgram *conn))
{
	conn->read_cb = read_cb;
}

/*! \brief Set application private data of the datagram transmitter
 *  \param[in] conn Datagram Transmitter to modify
 *  \param[in] data User-specific data (available in call-back functions) */
void osmo_dgram_set_data(struct osmo_dgram *conn, void *data)
{
	conn->data = data;
}

/*! \brief Get application private data of the datagram transceiver
 *  \param[in] conn Datagram Transceiver
 *  \returns Application private data, as set by \ref osmo_dgram_set_data() */
void *osmo_dgram_get_data(struct osmo_dgram *conn)
{
	return conn->data;
}

/*! \brief Open the datagram transceiver.  This actually initializes the
 * underlying sockets and binds/connects them to the configured ips/ports
 *  \param[in] conn Datagram Transceiver to open */
int osmo_dgram_open(struct osmo_dgram *conn)
{
	int ret;

	ret = osmo_dgram_rx_open(conn->rx);
	if (ret < 0)
		return ret;

	ret = osmo_dgram_tx_open(conn->tx);
	if (ret < 0) {
		osmo_dgram_rx_close(conn->rx);
		return ret;
	}
	return ret;
}

/*! \brief Close an Osmocom Datagram Transceiver
 *  \param[in] conn Osmocom Datagram Transceiver to be closed
 *  We unregister the socket fds from the osmocom select() loop
 *  and close them. */
void osmo_dgram_close(struct osmo_dgram *conn)
{
	osmo_dgram_rx_close(conn->rx);
	osmo_dgram_tx_close(conn->tx);
}

/*! \brief Enqueue data to be sent via an Osmocom datagram transceiver
 *  \param[in] conn Datagram Transceiver through which we want to send
 *  \param[in] msg Message buffer to enqueue in transmit queue */
void osmo_dgram_send(struct osmo_dgram *conn, struct msgb *msg)
{
	osmo_dgram_tx_send(conn->tx, msg);
}

/*! \brief Receive data via Osmocom datagram transceiver
 *  \param[in] conn Datagram Transceiver from which to receive
 *  \param msg pre-allocate message buffer to which received data is appended
 *  \returns number of bytes read, negative on error. */
int osmo_dgram_recv(struct osmo_dgram *conn, struct msgb *msg)
{
	return osmo_dgram_rx_recv(conn->rx, msg);
}

/*! @} */