aboutsummaryrefslogtreecommitdiffstats
path: root/src/datagram.c
blob: 8f74a79907a7259afb1cc54c469b5b7f0992acc3 (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
#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/linuxlist.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/datagram.h>

/*
 * Client side.
 */

#define OSMO_DGRAM_CLIENT_F_RECONFIG	(1 << 0)

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

void osmo_dgram_client_conn_close(struct osmo_dgram_client_conn *conn)
{
	osmo_fd_unregister(&conn->ofd);
	close(conn->ofd.fd);
}

static int osmo_dgram_client_write(struct osmo_dgram_client_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 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_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
{
	struct osmo_dgram_client_conn *conn = ofd->data;

	if (what & BSC_FD_WRITE) {
		LOGP(DLINP, LOGL_DEBUG, "connected write\n");
		osmo_dgram_client_write(conn);
	}
        return 0;
}

struct osmo_dgram_client_conn *osmo_dgram_client_conn_create(void *ctx)
{
	struct osmo_dgram_client_conn *conn;

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

	conn->ofd.fd = -1;
	conn->ofd.when |= BSC_FD_READ;
	conn->ofd.priv_nr = 0;	/* XXX */
	conn->ofd.cb = osmo_dgram_client_fd_cb;
	conn->ofd.data = conn;
	INIT_LLIST_HEAD(&conn->tx_queue);

	return conn;
}

void
osmo_dgram_client_conn_set_addr(struct osmo_dgram_client_conn *conn,
				const char *addr)
{
	if (conn->addr != NULL)
		talloc_free((void *)conn->addr);

	conn->addr = talloc_strdup(conn, addr);
	conn->flags |= OSMO_DGRAM_CLIENT_F_RECONFIG;
}

void
osmo_dgram_client_conn_set_port(struct osmo_dgram_client_conn *conn,
				uint16_t port)
{
	conn->port = port;
	conn->flags |= OSMO_DGRAM_CLIENT_F_RECONFIG;
}

void
osmo_dgram_client_conn_set_data(struct osmo_dgram_client_conn *conn, void *data)
{
	conn->data = data;
}

void osmo_dgram_client_conn_destroy(struct osmo_dgram_client_conn *conn)
{
	talloc_free(conn);
}

int osmo_dgram_client_conn_open(struct osmo_dgram_client_conn *conn)
{
	int ret;

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

	conn->flags &= ~OSMO_DGRAM_CLIENT_F_RECONFIG;

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

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

/*
 * Server side.
 */

#define OSMO_DGRAM_SERVER_F_RECONFIG	(1 << 0)

struct osmo_dgram_server_conn {
        struct osmo_fd                  ofd;
        const char                      *addr;
        uint16_t                        port;
	int (*cb)(struct osmo_dgram_server_conn *conn, struct msgb *msg);
        void                            *data;
	unsigned int			flags;
};

static void osmo_dgram_server_conn_read(struct osmo_dgram_server_conn *conn)
{
	struct msgb *msg;
	int ret;

	LOGP(DLINP, LOGL_DEBUG, "message received\n");

	msg = msgb_alloc(1200, "LAPD/client");
	if (!msg) {
		LOGP(DLINP, LOGL_ERROR, "cannot allocate room for message\n");
		return;
	}
	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");
		}
		osmo_dgram_server_conn_destroy(conn);
		return;
	} else if (ret == 0) {
		LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
		osmo_dgram_server_conn_destroy(conn);
		return;
	}
	msgb_put(msg, ret);
	LOGP(DLINP, LOGL_DEBUG, "received %d bytes from client\n", ret);
	if (conn->cb)
		conn->cb(conn, msg);

	return;
}

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

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

	return 0;
}

struct osmo_dgram_server_conn *osmo_dgram_server_conn_create(void *ctx)
{
	struct osmo_dgram_server_conn *conn;

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

	conn->ofd.fd = -1;
	conn->ofd.when |= BSC_FD_READ;
	conn->ofd.cb = osmo_dgram_server_conn_cb;
	conn->ofd.data = conn;

	return conn;
}

void osmo_dgram_server_conn_set_addr(struct osmo_dgram_server_conn *conn,
				     const char *addr)
{
	if (conn->addr != NULL)
		talloc_free((void *)conn->addr);

	conn->addr = talloc_strdup(conn, addr);
	conn->flags |= OSMO_DGRAM_SERVER_F_RECONFIG;
}

void osmo_dgram_server_conn_set_port(struct osmo_dgram_server_conn *conn,
				     uint16_t port)
{
	conn->port = port;
	conn->flags |= OSMO_DGRAM_SERVER_F_RECONFIG;
}

void osmo_dgram_server_conn_set_read_cb(struct osmo_dgram_server_conn *conn,
	int (*read_cb)(struct osmo_dgram_server_conn *conn, struct msgb *msg))
{
	conn->cb = read_cb;
}

void osmo_dgram_server_conn_destroy(struct osmo_dgram_server_conn *conn)
{
	talloc_free(conn);
}

int osmo_dgram_server_conn_open(struct osmo_dgram_server_conn *conn)
{
	int ret;

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

	conn->flags &= ~OSMO_DGRAM_SERVER_F_RECONFIG;

	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);
		return -EIO;
	}
	return 0;
}

void osmo_dgram_server_conn_close(struct osmo_dgram_server_conn *conn)
{
	osmo_fd_unregister(&conn->ofd);
	close(conn->ofd.fd);
}

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

struct osmo_dgram_conn {
	struct osmo_dgram_server_conn	*server;
	struct osmo_dgram_client_conn	*client;
	int (*read_cb)(struct osmo_dgram_conn *conn, struct msgb *msg);
	void				*data;
};

static int
dgram_server_conn_cb(struct osmo_dgram_server_conn *server, struct msgb *msg)
{
	struct osmo_dgram_conn *conn = server->data;

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

	return 0;
}

struct osmo_dgram_conn *osmo_dgram_conn_create(void *ctx)
{
	struct osmo_dgram_conn *conn;

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

	conn->server = osmo_dgram_server_conn_create(ctx);
	if (conn->server == NULL)
		return NULL;

	osmo_dgram_server_conn_set_read_cb(conn->server, dgram_server_conn_cb);
	conn->server->data = conn;

	conn->client = osmo_dgram_client_conn_create(ctx);
	if (conn->client == NULL) {
		osmo_dgram_server_conn_destroy(conn->server);
		return NULL;
	}

	return conn;
}

void osmo_dgram_conn_destroy(struct osmo_dgram_conn *conn)
{
	osmo_dgram_server_conn_destroy(conn->server);
	osmo_dgram_client_conn_destroy(conn->client);
}

void
osmo_dgram_conn_set_local_addr(struct osmo_dgram_conn *conn, const char *addr)
{
	osmo_dgram_server_conn_set_addr(conn->server, addr);
}

void
osmo_dgram_conn_set_remote_addr(struct osmo_dgram_conn *conn, const char *addr)
{
	osmo_dgram_client_conn_set_addr(conn->client, addr);
}

void
osmo_dgram_conn_set_local_port(struct osmo_dgram_conn *conn, uint16_t port)
{
	osmo_dgram_server_conn_set_port(conn->server, port);
}

void
osmo_dgram_conn_set_remote_port(struct osmo_dgram_conn *conn, uint16_t port)
{
	osmo_dgram_client_conn_set_port(conn->client, port);
}

void osmo_dgram_conn_set_read_cb(struct osmo_dgram_conn *conn,
	int (*read_cb)(struct osmo_dgram_conn *conn, struct msgb *msg))
{
	conn->read_cb = read_cb;
}

void
osmo_dgram_conn_set_data(struct osmo_dgram_client_conn *conn, void *data)
{
	conn->data = data;
}

int osmo_dgram_conn_open(struct osmo_dgram_conn *conn)
{
	int ret;

	ret = osmo_dgram_server_conn_open(conn->server);
	if (ret < 0)
		return ret;

	ret = osmo_dgram_client_conn_open(conn->client);
	if (ret < 0) {
		osmo_dgram_server_conn_close(conn->server);
		return ret;
	}
	return ret;
}

void osmo_dgram_conn_close(struct osmo_dgram_conn *conn)
{
	osmo_dgram_server_conn_close(conn->server);
	osmo_dgram_client_conn_close(conn->client);
}

void osmo_dgram_conn_send(struct osmo_dgram_conn *conn, struct msgb *msg)
{
	osmo_dgram_client_conn_send(conn->client, msg);
}