From 1486ac019172a46cd2672638576e79cf538bafdb Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 20 Jul 2022 16:32:01 +0200 Subject: [PATCH] Split cbc_peer to its own .c and .h files Change-Id: I41c890d81e76ac1a1c89d42af70a1f0631e9724e --- include/osmocom/cbc/Makefile.am | 1 + include/osmocom/cbc/cbc_data.h | 40 +--------- include/osmocom/cbc/cbc_peer.h | 46 ++++++++++++ include/osmocom/cbc/cbsp_server.h | 1 + include/osmocom/cbc/sbcap_server.h | 1 + src/Makefile.am | 1 + src/cbc_data.c | 84 --------------------- src/cbc_peer.c | 115 +++++++++++++++++++++++++++++ src/cbc_vty.c | 1 + src/cbsp_server.c | 1 + src/cbsp_server_fsm.c | 1 + src/message_handling.c | 1 + src/sbcap_server.c | 1 + src/sbcap_server_fsm.c | 1 + src/smscb_peer_fsm.c | 1 + 15 files changed, 174 insertions(+), 122 deletions(-) create mode 100644 include/osmocom/cbc/cbc_peer.h create mode 100644 src/cbc_peer.c diff --git a/include/osmocom/cbc/Makefile.am b/include/osmocom/cbc/Makefile.am index d1cc46e..1ceb6a4 100644 --- a/include/osmocom/cbc/Makefile.am +++ b/include/osmocom/cbc/Makefile.am @@ -1,5 +1,6 @@ noinst_HEADERS = \ cbc_data.h \ + cbc_peer.h \ cbsp_server.h \ charset.h \ internal.h \ diff --git a/include/osmocom/cbc/cbc_data.h b/include/osmocom/cbc/cbc_data.h index a90ff66..09b0ebf 100644 --- a/include/osmocom/cbc/cbc_data.h +++ b/include/osmocom/cbc/cbc_data.h @@ -7,40 +7,9 @@ #include #include -struct osmo_cbsp_cbc_client; -struct osmo_sabp_cbc_client; struct rest_it_op; -#define CBC_MAX_ADDRS 8 - -/********************************************************************************* - * CBC Peer - *********************************************************************************/ - -enum cbc_peer_protocol { - CBC_PEER_PROTO_CBSP, - CBC_PEER_PROTO_SABP, - CBC_PEER_PROTO_SBcAP -}; - -struct cbc_peer { - struct llist_head list; /* linked to cbc.peers */ - const char *name; - - char *remote_host[CBC_MAX_ADDRS]; /* remote IP address in string format */ - unsigned int num_remote_host; /* number of addresses present in remote_host */ - int remote_port; /* remote port number or -1 for random */ - bool unknown_dynamic_peer; /* dynamic/unknown peer; not saved in VTY */ - - enum cbc_peer_protocol proto; - union { - struct osmo_cbsp_cbc_client *cbsp; - struct osmo_sabp_cbc_client *sabp; - struct osmo_sbcap_cbc_client *sbcap; - } client; -}; - -extern const struct value_string cbc_peer_proto_name[]; +#define CBC_MAX_LOC_ADDRS 8 enum cbc_cell_id_type { CBC_CELL_ID_NONE, @@ -176,7 +145,7 @@ struct cbc { int local_port; } cbsp; struct { - char *local_host[CBC_MAX_ADDRS]; + char *local_host[CBC_MAX_LOC_ADDRS]; unsigned int num_local_host; int local_port; } sbcap; @@ -202,8 +171,3 @@ int cbc_message_del_peer(struct cbc_message *cbcmsg, struct cbc_peer *peer); int cbc_message_add_peer(struct cbc_message *cbcmsg, struct cbc_peer *peer); struct cbc_message_peer *smscb_peer_fsm_alloc(struct cbc_peer *peer, struct cbc_message *cbcmsg); struct cbc_message_peer *cbc_message_peer_get(struct cbc_message *cbcmsg, struct cbc_peer *peer); -struct cbc_peer *cbc_peer_by_name(const char *name); -struct cbc_peer *cbc_peer_by_addr_proto(const char *remote_host, uint16_t remote_port, - enum cbc_peer_protocol proto); -struct cbc_peer *cbc_peer_create(const char *name, enum cbc_peer_protocol proto); -void cbc_peer_remove(struct cbc_peer *peer); diff --git a/include/osmocom/cbc/cbc_peer.h b/include/osmocom/cbc/cbc_peer.h new file mode 100644 index 0000000..adfa32d --- /dev/null +++ b/include/osmocom/cbc/cbc_peer.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include + +struct osmo_cbsp_cbc_client; +struct osmo_sabp_cbc_client; +struct osmo_sbcap_cbc_client; + +#define CBC_MAX_REM_ADDRS 8 + +/********************************************************************************* + * CBC Peer + *********************************************************************************/ + +enum cbc_peer_protocol { + CBC_PEER_PROTO_CBSP, + CBC_PEER_PROTO_SABP, + CBC_PEER_PROTO_SBcAP +}; + +struct cbc_peer { + struct llist_head list; /* linked to cbc.peers */ + const char *name; + + char *remote_host[CBC_MAX_REM_ADDRS]; /* remote IP address in string format */ + unsigned int num_remote_host; /* number of addresses present in remote_host */ + int remote_port; /* remote port number or -1 for random */ + bool unknown_dynamic_peer; /* dynamic/unknown peer; not saved in VTY */ + + enum cbc_peer_protocol proto; + union { + struct osmo_cbsp_cbc_client *cbsp; + struct osmo_sabp_cbc_client *sabp; + struct osmo_sbcap_cbc_client *sbcap; + } client; +}; + +extern const struct value_string cbc_peer_proto_name[]; + +struct cbc_peer *cbc_peer_create(const char *name, enum cbc_peer_protocol proto); +void cbc_peer_remove(struct cbc_peer *peer); + +struct cbc_peer *cbc_peer_by_name(const char *name); +struct cbc_peer *cbc_peer_by_addr_proto(const char *remote_host, uint16_t remote_port, + enum cbc_peer_protocol proto); diff --git a/include/osmocom/cbc/cbsp_server.h b/include/osmocom/cbc/cbsp_server.h index 1576864..e3caf0c 100644 --- a/include/osmocom/cbc/cbsp_server.h +++ b/include/osmocom/cbc/cbsp_server.h @@ -10,6 +10,7 @@ struct osmo_cbsp_cbc_client; struct osmo_fsm_inst; +struct cbc_peer; /* a CBC server */ struct osmo_cbsp_cbc { diff --git a/include/osmocom/cbc/sbcap_server.h b/include/osmocom/cbc/sbcap_server.h index 7b4b7e1..ebc9f2a 100644 --- a/include/osmocom/cbc/sbcap_server.h +++ b/include/osmocom/cbc/sbcap_server.h @@ -13,6 +13,7 @@ typedef struct SBcAP_SBC_AP_PDU SBcAP_SBC_AP_PDU_t; struct osmo_sbcap_cbc_client; struct osmo_fsm_inst; +struct cbc_peer; /* a CBC server */ struct osmo_sbcap_cbc { diff --git a/src/Makefile.am b/src/Makefile.am index 5805fd7..f7c9516 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,6 +12,7 @@ bin_PROGRAMS = osmo-cbc osmo_cbc_SOURCES = \ cbc_main.c \ cbc_data.c \ + cbc_peer.c \ cbc_vty.c \ cbsp_server.c \ cbsp_server_fsm.c \ diff --git a/src/cbc_data.c b/src/cbc_data.c index 92da369..4226cfc 100644 --- a/src/cbc_data.c +++ b/src/cbc_data.c @@ -31,13 +31,6 @@ #include #include -const struct value_string cbc_peer_proto_name[] = { - { CBC_PEER_PROTO_CBSP, "CBSP" }, - { CBC_PEER_PROTO_SABP, "SABP" }, - { CBC_PEER_PROTO_SBcAP, "SBc-AP" }, - { 0, NULL } -}; - /* remove a peer from the message */ int cbc_message_del_peer(struct cbc_message *cbcmsg, struct cbc_peer *peer) { @@ -79,80 +72,3 @@ int cbc_message_add_peer(struct cbc_message *cbcmsg, struct cbc_peer *peer) return 0; } #endif - - -/* look-up of cbc_peer by name */ -struct cbc_peer *cbc_peer_by_name(const char *name) -{ - struct cbc_peer *peer; - - llist_for_each_entry(peer, &g_cbc->peers, list) { - if (peer->name && !strcmp(name, peer->name)) - return peer; - } - return NULL; -} - -/* look-up of cbc_peer by tuple of (remote host, protocol) */ -struct cbc_peer *cbc_peer_by_addr_proto(const char *remote_host, uint16_t remote_port, - enum cbc_peer_protocol proto) -{ - struct cbc_peer *peer; - - llist_for_each_entry(peer, &g_cbc->peers, list) { - unsigned int i; - for (i = 0; i < peer->num_remote_host; i++) { - if (peer->proto != proto) - continue; - if (!strcasecmp(remote_host, peer->remote_host[i])) { - if (peer->remote_port == -1) - return peer; - else if (remote_port == peer->remote_port) - return peer; - } - } - } - return NULL; -} - -/* create a new cbc_peer */ -struct cbc_peer *cbc_peer_create(const char *name, enum cbc_peer_protocol proto) -{ - struct cbc_peer *peer; - if (name && cbc_peer_by_name(name)) - return NULL; - - peer = talloc_zero(g_cbc, struct cbc_peer); - if (!peer) - return NULL; - - peer->proto = proto; - peer->name = talloc_strdup(peer, name); - llist_add_tail(&peer->list, &g_cbc->peers); - - return peer; -} - -/* remove a cbc_peer */ -void cbc_peer_remove(struct cbc_peer *peer) -{ - struct cbc_message *cbcmsg; - - /* close any existing client connection */ - switch (peer->proto) { - case CBC_PEER_PROTO_CBSP: - if (peer->client.cbsp) - cbsp_cbc_client_close(peer->client.cbsp); - break; - default: - OSMO_ASSERT(0); - } - - /* iterate over messages; remove client from all message_peers */ - llist_for_each_entry(cbcmsg, &g_cbc->messages, list) { - cbc_message_del_peer(cbcmsg, peer); - } - - llist_del(&peer->list); - talloc_free(peer); -} diff --git a/src/cbc_peer.c b/src/cbc_peer.c new file mode 100644 index 0000000..3e6d778 --- /dev/null +++ b/src/cbc_peer.c @@ -0,0 +1,115 @@ +/* Osmocom CBC (Cell Broacast Centre) */ + +/* (C) 2019 by Harald Welte + * All Rights Reserved + * + * SPDX-License-Identifier: AGPL-3.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +#include +#include + +#include +#include +#include + +#include +#include + +const struct value_string cbc_peer_proto_name[] = { + { CBC_PEER_PROTO_CBSP, "CBSP" }, + { CBC_PEER_PROTO_SABP, "SABP" }, + { CBC_PEER_PROTO_SBcAP, "SBc-AP" }, + { 0, NULL } +}; + +/* create a new cbc_peer */ +struct cbc_peer *cbc_peer_create(const char *name, enum cbc_peer_protocol proto) +{ + struct cbc_peer *peer; + if (name && cbc_peer_by_name(name)) + return NULL; + + peer = talloc_zero(g_cbc, struct cbc_peer); + if (!peer) + return NULL; + + peer->proto = proto; + peer->name = talloc_strdup(peer, name); + llist_add_tail(&peer->list, &g_cbc->peers); + + return peer; +} + +/* remove a cbc_peer */ +void cbc_peer_remove(struct cbc_peer *peer) +{ + struct cbc_message *cbcmsg; + + /* close any existing client connection */ + switch (peer->proto) { + case CBC_PEER_PROTO_CBSP: + if (peer->client.cbsp) + cbsp_cbc_client_close(peer->client.cbsp); + break; + default: + OSMO_ASSERT(0); + } + + /* iterate over messages; remove client from all message_peers */ + llist_for_each_entry(cbcmsg, &g_cbc->messages, list) { + cbc_message_del_peer(cbcmsg, peer); + } + + llist_del(&peer->list); + talloc_free(peer); +} + +/* look-up of cbc_peer by name */ +struct cbc_peer *cbc_peer_by_name(const char *name) +{ + struct cbc_peer *peer; + + llist_for_each_entry(peer, &g_cbc->peers, list) { + if (peer->name && !strcmp(name, peer->name)) + return peer; + } + return NULL; +} + +/* look-up of cbc_peer by tuple of (remote host, protocol) */ +struct cbc_peer *cbc_peer_by_addr_proto(const char *remote_host, uint16_t remote_port, + enum cbc_peer_protocol proto) +{ + struct cbc_peer *peer; + + llist_for_each_entry(peer, &g_cbc->peers, list) { + unsigned int i; + for (i = 0; i < peer->num_remote_host; i++) { + if (peer->proto != proto) + continue; + if (!strcasecmp(remote_host, peer->remote_host[i])) { + if (peer->remote_port == -1) + return peer; + else if (remote_port == peer->remote_port) + return peer; + } + } + } + return NULL; +} diff --git a/src/cbc_vty.c b/src/cbc_vty.c index 916adc2..030f9e6 100644 --- a/src/cbc_vty.c +++ b/src/cbc_vty.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include diff --git a/src/cbsp_server.c b/src/cbsp_server.c index 4e47baf..58206e8 100644 --- a/src/cbsp_server.c +++ b/src/cbsp_server.c @@ -33,6 +33,7 @@ #include #include +#include #if 0 struct osmo_cbsp_bsc { diff --git a/src/cbsp_server_fsm.c b/src/cbsp_server_fsm.c index 903e08a..8062364 100644 --- a/src/cbsp_server_fsm.c +++ b/src/cbsp_server_fsm.c @@ -22,6 +22,7 @@ #include +#include #include #include diff --git a/src/message_handling.c b/src/message_handling.c index 3344a75..23c0ace 100644 --- a/src/message_handling.c +++ b/src/message_handling.c @@ -33,6 +33,7 @@ #include #include #include +#include /* convert cbc_message to osmo_cbsp_cell_list */ static int cbcmsg_to_cbsp_cell_list(const void *ctx, struct osmo_cbsp_cell_list *list, diff --git a/src/sbcap_server.c b/src/sbcap_server.c index 159fa89..2f78322 100644 --- a/src/sbcap_server.c +++ b/src/sbcap_server.c @@ -37,6 +37,7 @@ #include #include +#include const char *sbcap_cbc_client_name(const struct osmo_sbcap_cbc_client *client) { diff --git a/src/sbcap_server_fsm.c b/src/sbcap_server_fsm.c index 11bbf28..43c5ddb 100644 --- a/src/sbcap_server_fsm.c +++ b/src/sbcap_server_fsm.c @@ -27,6 +27,7 @@ #include #include +#include #define S(x) (1 << (x)) diff --git a/src/smscb_peer_fsm.c b/src/smscb_peer_fsm.c index c72a8b4..9c840fa 100644 --- a/src/smscb_peer_fsm.c +++ b/src/smscb_peer_fsm.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include