laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/libosmo-sccp/+/29087 )

Change subject: SIGTRAN: add function to check connection existence
......................................................................

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
---
M examples/sccp_test_vty.c
M include/osmocom/sigtran/sccp_helpers.h
M src/sccp_helpers.c
M src/sccp_scoc.c
4 files changed, 36 insertions(+), 6 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



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 @@
        "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 @@
                              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_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 @@
                         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 @@
        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_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 @@
        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 */

--
To view, visit https://gerrit.osmocom.org/c/libosmo-sccp/+/29087
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-sccp
Gerrit-Branch: master
Gerrit-Change-Id: Iffedf55b4c292ee6b2f97bcdeef6dc13c050ce01
Gerrit-Change-Number: 29087
Gerrit-PatchSet: 13
Gerrit-Owner: msuraev <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: neels <[email protected]>
Gerrit-CC: fixeria <[email protected]>
Gerrit-MessageType: merged

Reply via email to