blob: 4fedd38f181c08e1299281a7037b9e2395318f53 [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
Harald Welte21c14fc2018-07-23 17:54:41 +020026#include "logging.h"
Harald Weltee687be52016-05-03 18:49:27 +020027#include "gsup_server.h"
28
29struct gsup_route {
30 struct llist_head list;
31
32 uint8_t *addr;
33 struct osmo_gsup_conn *conn;
34};
35
Oliver Smithf7d32512019-04-08 15:00:43 +020036/*! Find a route for the given address.
37 * \param[in] gs gsup server
38 * \param[in] addr IPA name of the client (SGSN, MSC/VLR). Although this is passed like a blob, together with the
39 * length, it must be nul-terminated! This is for legacy reasons, see the discussion here:
40 * https://gerrit.osmocom.org/#/c/osmo-hlr/+/13048/
41 * \param[in] addrlen length of addr, *including the nul-byte* (strlen(addr) + 1).
42 */
Harald Weltee687be52016-05-03 18:49:27 +020043struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs,
44 const uint8_t *addr, size_t addrlen)
45{
46 struct gsup_route *gr;
47
48 llist_for_each_entry(gr, &gs->routes, list) {
49 if (talloc_total_size(gr->addr) == addrlen &&
50 !memcmp(gr->addr, addr, addrlen))
51 return gr->conn;
52 }
53 return NULL;
54}
55
56/* add a new route for the given address to the given conn */
57int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen)
58{
59 struct gsup_route *gr;
60
61 /* Check if we already have a route for this address */
62 if (gsup_route_find(conn->server, addr, addrlen))
63 return -EEXIST;
64
65 /* allocate new route and populate it */
66 gr = talloc_zero(conn->server, struct gsup_route);
67 if (!gr)
68 return -ENOMEM;
69
Max44a21802019-02-14 11:28:21 +010070 LOGP(DMAIN, LOGL_INFO, "Adding GSUP route for %s via %s:%u\n", addr, conn->conn->addr, conn->conn->port);
Harald Welte21c14fc2018-07-23 17:54:41 +020071
Harald Weltee687be52016-05-03 18:49:27 +020072 gr->addr = talloc_memdup(gr, addr, addrlen);
73 gr->conn = conn;
74 llist_add_tail(&gr->list, &conn->server->routes);
75
76 return 0;
77}
78
79/* delete all routes for the given connection */
80int gsup_route_del_conn(struct osmo_gsup_conn *conn)
81{
82 struct gsup_route *gr, *gr2;
83 unsigned int num_deleted = 0;
84
85 llist_for_each_entry_safe(gr, gr2, &conn->server->routes, list) {
86 if (gr->conn == conn) {
Harald Welte21c14fc2018-07-23 17:54:41 +020087 LOGP(DMAIN, LOGL_INFO, "Removing GSUP route for %s (GSUP disconnect)\n",
88 gr->addr);
Harald Weltee687be52016-05-03 18:49:27 +020089 llist_del(&gr->list);
90 talloc_free(gr);
91 num_deleted++;
92 }
93 }
94
95 return num_deleted;
96}