Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 1 | /* (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 Welte | 21c14fc | 2018-07-23 17:54:41 +0200 | [diff] [blame] | 26 | #include "logging.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 27 | #include "gsup_server.h" |
| 28 | |
| 29 | struct gsup_route { |
| 30 | struct llist_head list; |
| 31 | |
| 32 | uint8_t *addr; |
| 33 | struct osmo_gsup_conn *conn; |
| 34 | }; |
| 35 | |
| 36 | /* find a route for the given address */ |
| 37 | struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs, |
| 38 | const uint8_t *addr, size_t addrlen) |
| 39 | { |
| 40 | struct gsup_route *gr; |
| 41 | |
| 42 | llist_for_each_entry(gr, &gs->routes, list) { |
| 43 | if (talloc_total_size(gr->addr) == addrlen && |
| 44 | !memcmp(gr->addr, addr, addrlen)) |
| 45 | return gr->conn; |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | /* add a new route for the given address to the given conn */ |
| 51 | int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen) |
| 52 | { |
| 53 | struct gsup_route *gr; |
| 54 | |
| 55 | /* Check if we already have a route for this address */ |
| 56 | if (gsup_route_find(conn->server, addr, addrlen)) |
| 57 | return -EEXIST; |
| 58 | |
| 59 | /* allocate new route and populate it */ |
| 60 | gr = talloc_zero(conn->server, struct gsup_route); |
| 61 | if (!gr) |
| 62 | return -ENOMEM; |
| 63 | |
Harald Welte | 21c14fc | 2018-07-23 17:54:41 +0200 | [diff] [blame] | 64 | LOGP(DMAIN, LOGL_INFO, "Adding GSUP route for %s\n", addr); |
| 65 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 66 | gr->addr = talloc_memdup(gr, addr, addrlen); |
| 67 | gr->conn = conn; |
| 68 | llist_add_tail(&gr->list, &conn->server->routes); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* delete all routes for the given connection */ |
| 74 | int gsup_route_del_conn(struct osmo_gsup_conn *conn) |
| 75 | { |
| 76 | struct gsup_route *gr, *gr2; |
| 77 | unsigned int num_deleted = 0; |
| 78 | |
| 79 | llist_for_each_entry_safe(gr, gr2, &conn->server->routes, list) { |
| 80 | if (gr->conn == conn) { |
Harald Welte | 21c14fc | 2018-07-23 17:54:41 +0200 | [diff] [blame] | 81 | LOGP(DMAIN, LOGL_INFO, "Removing GSUP route for %s (GSUP disconnect)\n", |
| 82 | gr->addr); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 83 | llist_del(&gr->list); |
| 84 | talloc_free(gr); |
| 85 | num_deleted++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return num_deleted; |
| 90 | } |