Fixed dead pointer problem when handling interfaces
authorAndreas Eversberg <jolly@eversberg.eu>
Fri, 20 Jan 2012 09:05:41 +0000 (10:05 +0100)
committerAndreas Eversberg <jolly@eversberg.eu>
Fri, 20 Jan 2012 09:05:41 +0000 (10:05 +0100)
In order to get the pointer to the currently existing interface, a
new function is used, to resolve interface by name.

gsm.cpp
gsm.h
gsm_bs.cpp
gsm_ms.cpp
interface.c
interface.h
sip.cpp

diff --git a/gsm.cpp b/gsm.cpp
index 3f778b4..8e5a2d6 100644 (file)
--- a/gsm.cpp
+++ b/gsm.cpp
@@ -263,11 +263,7 @@ void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int
        char msgtext[64];
        struct interface *interface = interface_first;
 
-       while (interface) {
-               if (!strcmp(interface->name, interface_name))
-                       break;
-               interface = interface->next;
-       }
+       interface = getinterfacebyname(interface_name);
        if (!interface)
                return;
 
diff --git a/gsm.h b/gsm.h
index 254797e..724a4c2 100644 (file)
--- a/gsm.h
+++ b/gsm.h
@@ -15,7 +15,7 @@ enum {
 };
 
 struct lcr_gsm {
-       struct interface *interface;    /* interface this instance is associated to */
+       char interface_name[64]; /* name of interface this instance is associated to */
        struct lcr_gsm  *gsm_ms_next;   /* list of MS instances, in case of MS */
        char            name[16];       /* name of MS instance, in case of MS */
        int             type;           /* LCR_GSM_TYPE_*/
index 9608be6..7748f7a 100644 (file)
@@ -212,13 +212,9 @@ void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
        class Endpoint *epoint;
        struct lcr_msg *message;
        struct gsm_mncc *mode, *proceeding, *frame;
-       struct interface *interface = interface_first;
+       struct interface *interface;
 
-       while (interface) {
-               if (!strcmp(interface->name, p_g_interface_name))
-                       break;
-               interface = interface->next;
-       }
+       interface = getinterfacebyname(p_g_interface_name);
        if (!interface) {
                PERROR("Cannot find interface %s.\n", p_g_interface_name);
                return;
@@ -376,7 +372,6 @@ void Pgsm_bs::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
  */
 int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
 {
-       struct interface *interface = lcr_gsm->interface;
        struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
        unsigned int callref = mncc->callref;
        class Port *port;
@@ -431,17 +426,13 @@ int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
        }
 
        if (!port) {
+               struct interface *interface;
+
                if (msg_type != MNCC_SETUP_IND)
                        return(0);
-#if 0
-               /* find gsm port */
-               mISDNport = mISDNport_first;
-               while(mISDNport) {
-                       if (mISDNport->gsm_bs)
-                               break;
-                       mISDNport = mISDNport->next;
-               }
-               if (!mISDNport) {
+
+               interface = getinterfacebyname(lcr_gsm->interface_name);
+               if (!interface) {
                        struct gsm_mncc *rej;
 
                        rej = create_mncc(MNCC_REJ_REQ, callref);
@@ -453,11 +444,11 @@ int message_bsc(struct lcr_gsm *lcr_gsm, int msg_type, void *arg)
                        add_trace("cause", "coding", "%d", rej->cause.coding);
                        add_trace("cause", "location", "%d", rej->cause.location);
                        add_trace("cause", "value", "%d", rej->cause.value);
+                       add_trace("reason", NULL, "interface %s not found", lcr_gsm->interface_name);
                        end_trace();
                        send_and_free_mncc(lcr_gsm, rej->msg_type, rej);
                        return 0;
                }
-#endif
                /* creating port object, transparent until setup with hdlc */
                SPRINT(name, "%s-%d-in", interface->name, 0);
                if (!(pgsm_bs = new Pgsm_bs(PORT_TYPE_GSM_BS_IN, name, NULL, interface)))
@@ -766,7 +757,7 @@ int gsm_bs_init(struct interface *interface)
        /* create gsm instance */
        gsm_bs = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
 
-       gsm_bs->interface = interface;
+       SCPY(gsm_bs->interface_name, interface->name);
        gsm_bs->type = LCR_GSM_TYPE_NETWORK;
        gsm_bs->sun.sun_family = AF_UNIX;
        SCPY(gsm_bs->sun.sun_path, "/tmp/bsc_mncc");
index ae99930..cfb1bf6 100644 (file)
@@ -68,13 +68,9 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
        class Endpoint *epoint;
        struct lcr_msg *message;
        struct gsm_mncc *mode, *proceeding, *frame;
-       struct interface *interface = interface_first;
+       struct interface *interface;
 
-       while (interface) {
-               if (!strcmp(interface->name, p_g_interface_name))
-                       break;
-               interface = interface->next;
-       }
+       interface = getinterfacebyname(p_g_interface_name);
        if (!interface) {
                PERROR("Cannot find interface %s.\n", p_g_interface_name);
                return;
@@ -325,7 +321,6 @@ void Pgsm_ms::setup_ind(unsigned int msg_type, unsigned int callref, struct gsm_
  */
 int message_ms(struct lcr_gsm *gsm_ms, int msg_type, void *arg)
 {
-       struct interface *interface = gsm_ms->interface;
        struct gsm_mncc *mncc = (struct gsm_mncc *)arg;
        unsigned int callref = mncc->callref;
        class Port *port;
@@ -357,17 +352,13 @@ int message_ms(struct lcr_gsm *gsm_ms, int msg_type, void *arg)
        }
 
        if (!port) {
+               struct interface *interface;
+
                if (msg_type != MNCC_SETUP_IND)
                        return(0);
-#if 0
-               /* find gsm ms port */
-               mISDNport = mISDNport_first;
-               while(mISDNport) {
-                       if (mISDNport->gsm_ms && !strcmp(mISDNport->ifport->gsm_ms_name, gsm_ms->name))
-                               break;
-                       mISDNport = mISDNport->next;
-               }
-               if (!mISDNport) {
+
+               interface = getinterfacebyname(gsm_ms->interface_name);
+               if (!interface) {
                        struct gsm_mncc *rej;
 
                        rej = create_mncc(MNCC_REJ_REQ, callref);
@@ -379,11 +370,11 @@ int message_ms(struct lcr_gsm *gsm_ms, int msg_type, void *arg)
                        add_trace("cause", "coding", "%d", rej->cause.coding);
                        add_trace("cause", "location", "%d", rej->cause.location);
                        add_trace("cause", "value", "%d", rej->cause.value);
+                       add_trace("reason", NULL, "interface %s not found", gsm_ms->interface_name);
                        end_trace();
                        send_and_free_mncc(gsm_ms, rej->msg_type, rej);
                        return 0;
                }
-#endif
                /* creating port object, transparent until setup with hdlc */
                SPRINT(name, "%s-%d-in", interface->name, 0);
                if (!(pgsm_ms = new Pgsm_ms(PORT_TYPE_GSM_MS_IN, name, NULL, interface)))
@@ -730,7 +721,7 @@ int gsm_ms_new(struct interface *interface)
        /* create gsm instance */
        gsm_ms = (struct lcr_gsm *)MALLOC(sizeof(struct lcr_gsm));
 
-       gsm_ms->interface = interface;
+       SCPY(gsm_ms->interface_name, interface->name);
        gsm_ms->type = LCR_GSM_TYPE_MS;
        SCPY(gsm_ms->name, interface->gsm_ms_name);
        gsm_ms->sun.sun_family = AF_UNIX;
index 89aa582..ca569be 100644 (file)
@@ -1699,11 +1699,7 @@ void do_screen(int out, char *id, int idsize, int *type, int *present, const cha
        char suffix[64];
        struct interface *interface = interface_first;
 
-       while (interface) {
-               if (!strcmp(interface->name, interface_name))
-                       break;
-               interface = interface->next;
-       }
+       interface = getinterfacebyname(interface_name);
        if (!interface)
                return;
 
@@ -1830,3 +1826,16 @@ void do_screen(int out, char *id, int idsize, int *type, int *present, const cha
        }
 }
 
+struct interface *getinterfacebyname(const char *name)
+{
+       struct interface *interface = interface_first;
+
+       while (interface) {
+               if (!strcmp(interface->name, name))
+                       return interface;
+               interface = interface->next;
+       }
+
+       return NULL;
+}
+
index 4640187..2a46c59 100644 (file)
@@ -141,4 +141,5 @@ void relink_interfaces(void);
 void load_port(struct interface_port *ifport);
 void doc_interface(void);
 void do_screen(int out, char *id, int idsize, int *type, int *present, const char *interface_name);
+struct interface *getinterfacebyname(const char *name);
 
diff --git a/sip.cpp b/sip.cpp
index 94e9760..20e4ece 100644 (file)
--- a/sip.cpp
+++ b/sip.cpp
@@ -1161,13 +1161,9 @@ void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *mag
        class Endpoint *epoint;
        struct lcr_msg *message;
        uint8_t payload_type;
-       struct interface *interface = interface_first;
+       struct interface *interface;
 
-       while (interface) {
-               if (!strcmp(interface->name, inst->interface_name))
-                       break;
-               interface = interface->next;
-       }
+       interface = getinterfacebyname(inst->interface_name);
        if (!interface) {
                PERROR("Cannot find interface %s.\n", inst->interface_name);
                return;