diff options
author | Max <msuraev@sysmocom.de> | 2022-08-15 17:13:57 +0700 |
---|---|---|
committer | Max <msuraev@sysmocom.de> | 2022-08-22 18:36:51 +0700 |
commit | 196ddcbb05e82762a34eb596b5a3bec3b36e962c (patch) | |
tree | b4431649ba12745a7c7d2a72fd1eeded04d71983 | |
parent | 28a8e3e8362dacf98d1132fb4924e9230b24ef9f (diff) |
SIGTRAN: add function to check connection existence
Add convenience helper to check if particular connection ID exists and use it to
properly report errors when attempting to send messages over non-existent connections.
Change-Id: Iffedf55b4c292ee6b2f97bcdeef6dc13c050ce01
-rw-r--r-- | examples/sccp_test_vty.c | 11 | ||||
-rw-r--r-- | include/osmocom/sigtran/sccp_helpers.h | 2 | ||||
-rw-r--r-- | src/sccp_helpers.c | 22 | ||||
-rw-r--r-- | src/sccp_scoc.c | 7 |
4 files changed, 36 insertions, 6 deletions
diff --git a/examples/sccp_test_vty.c b/examples/sccp_test_vty.c index 044ecf2..3f644e2 100644 --- a/examples/sccp_test_vty.c +++ b/examples/sccp_test_vty.c @@ -41,11 +41,16 @@ DEFUN(scu_conn_req, scu_conn_req_cmd, "Connection ID\n") { struct osmo_sccp_user *scu = vty->index; - int conn_id = atoi(argv[0]); + int rc, conn_id = atoi(argv[0]); const char *data = argv[1]; - osmo_sccp_tx_conn_req(scu, conn_id, &g_calling_addr, &g_called_addr, - (const uint8_t *)data, data ? strlen(data)+1 : 0); + rc = osmo_sccp_tx_conn_req(scu, conn_id, &g_calling_addr, &g_called_addr, + (const uint8_t *)data, data ? strlen(data) + 1 : 0); + if (rc < 0) { + vty_out(vty, "Error while sending N-CONNECT.req: %s%s", strerror(-rc), VTY_NEWLINE); + return CMD_WARNING; + } + return CMD_SUCCESS; } diff --git a/include/osmocom/sigtran/sccp_helpers.h b/include/osmocom/sigtran/sccp_helpers.h index dc4e115..a575169 100644 --- a/include/osmocom/sigtran/sccp_helpers.h +++ b/include/osmocom/sigtran/sccp_helpers.h @@ -67,5 +67,7 @@ int osmo_sccp_addr_to_id_buf(char *buf, size_t buf_len, const struct osmo_ss7_in const struct osmo_sccp_addr *addr); char *osmo_sccp_addr_to_id_c(void *ctx, const struct osmo_ss7_instance *ss7, const struct osmo_sccp_addr *addr); +bool osmo_sccp_conn_id_exists(const struct osmo_sccp_instance *inst, uint32_t id); + char *osmo_sccp_addr_name(const struct osmo_ss7_instance *ss7, const struct osmo_sccp_addr *addr); char *osmo_sccp_inst_addr_name(const struct osmo_sccp_instance *sccp, const struct osmo_sccp_addr *addr); diff --git a/src/sccp_helpers.c b/src/sccp_helpers.c index 2dc762e..266c869 100644 --- a/src/sccp_helpers.c +++ b/src/sccp_helpers.c @@ -21,6 +21,7 @@ * */ +#include <errno.h> #include <string.h> #include <stdbool.h> @@ -152,9 +153,15 @@ int osmo_sccp_tx_conn_req_msg(struct osmo_sccp_user *scu, uint32_t conn_id, int osmo_sccp_tx_data(struct osmo_sccp_user *scu, uint32_t conn_id, const uint8_t *data, unsigned int len) { - struct msgb *msg = scu_msgb_alloc(__func__); + struct msgb *msg; struct osmo_scu_prim *prim; + if (!osmo_sccp_conn_id_exists(scu->inst, conn_id)) { + LOGP(DLSCCP, LOGL_ERROR, "N-DATA.req TX error: unable to find connection ID (local_ref) %u\n", conn_id); + return -ENOTCONN; + } + + msg = scu_msgb_alloc(__func__); prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_DATA, @@ -183,10 +190,16 @@ int osmo_sccp_tx_disconn(struct osmo_sccp_user *scu, uint32_t conn_id, const struct osmo_sccp_addr *resp_addr, uint32_t cause) { - struct msgb *msg = scu_msgb_alloc(__func__); + struct msgb *msg; struct osmo_scu_prim *prim; struct osmo_scu_disconn_param *param; + if (!osmo_sccp_conn_id_exists(scu->inst, conn_id)) { + LOGP(DLSCCP, LOGL_ERROR, "N-DISCONNECT.req TX error: unable to find connection ID (local_ref) %u\n", conn_id); + return -ENOTCONN; + } + + msg = scu_msgb_alloc(__func__); prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_DISCONNECT, @@ -210,6 +223,11 @@ int osmo_sccp_tx_conn_resp_msg(struct osmo_sccp_user *scu, uint32_t conn_id, struct osmo_scu_prim *prim; struct osmo_scu_connect_param *param; + if (!osmo_sccp_conn_id_exists(scu->inst, conn_id)) { + LOGP(DLSCCP, LOGL_ERROR, "N-CONNECT.resp TX error: unable to find connection ID (local_ref) %u\n", conn_id); + return -ENOTCONN; + } + msg->l2h = msg->data; prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim)); diff --git a/src/sccp_scoc.c b/src/sccp_scoc.c index b85eeb9..3b1ca02 100644 --- a/src/sccp_scoc.c +++ b/src/sccp_scoc.c @@ -446,7 +446,7 @@ static void conn_stop_connect_timer(struct sccp_connection *conn) static void conn_destroy(struct sccp_connection *conn); -static struct sccp_connection *conn_find_by_id(struct osmo_sccp_instance *inst, uint32_t id) +static struct sccp_connection *conn_find_by_id(const struct osmo_sccp_instance *inst, uint32_t id) { struct sccp_connection *conn; @@ -457,6 +457,11 @@ static struct sccp_connection *conn_find_by_id(struct osmo_sccp_instance *inst, return NULL; } +bool osmo_sccp_conn_id_exists(const struct osmo_sccp_instance *inst, uint32_t id) +{ + return conn_find_by_id(inst, id) ? true : false; +} + #define INIT_TIMER(x, fn, priv) do { (x)->cb = fn; (x)->data = priv; } while (0) /* allocate + init a SCCP Connection with given ID */ |