blob: e9aed78df46ff330cbb766c40d9186cb724c0072 [file] [log] [blame]
Harald Weltee687be52016-05-03 18:49:27 +02001/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20
21#include <errno.h>
22
23#include <osmocom/core/linuxlist.h>
24#include <osmocom/core/talloc.h>
25
26#include "gsup_server.h"
27
28struct gsup_route {
29 struct llist_head list;
30
31 uint8_t *addr;
32 struct osmo_gsup_conn *conn;
33};
34
35/* find a route for the given address */
36struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs,
37 const uint8_t *addr, size_t addrlen)
38{
39 struct gsup_route *gr;
40
41 llist_for_each_entry(gr, &gs->routes, list) {
42 if (talloc_total_size(gr->addr) == addrlen &&
43 !memcmp(gr->addr, addr, addrlen))
44 return gr->conn;
45 }
46 return NULL;
47}
48
49/* add a new route for the given address to the given conn */
50int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen)
51{
52 struct gsup_route *gr;
53
54 /* Check if we already have a route for this address */
55 if (gsup_route_find(conn->server, addr, addrlen))
56 return -EEXIST;
57
58 /* allocate new route and populate it */
59 gr = talloc_zero(conn->server, struct gsup_route);
60 if (!gr)
61 return -ENOMEM;
62
63 gr->addr = talloc_memdup(gr, addr, addrlen);
64 gr->conn = conn;
65 llist_add_tail(&gr->list, &conn->server->routes);
66
67 return 0;
68}
69
70/* delete all routes for the given connection */
71int gsup_route_del_conn(struct osmo_gsup_conn *conn)
72{
73 struct gsup_route *gr, *gr2;
74 unsigned int num_deleted = 0;
75
76 llist_for_each_entry_safe(gr, gr2, &conn->server->routes, list) {
77 if (gr->conn == conn) {
78 llist_del(&gr->list);
79 talloc_free(gr);
80 num_deleted++;
81 }
82 }
83
84 return num_deleted;
85}