laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/22566 )


Change subject: ns2: Move to one common/shared ns2_bind_alloc()
......................................................................

ns2: Move to one common/shared ns2_bind_alloc()

Avoid code duplication between three different drivers by sharing
the "core" of the bind initialization in a new, shared ns2_bind_alloc().

Change-Id: I535fc68e94fcd695de827dd922706adc1c5a2cb7
---
M src/gb/gprs_ns2.c
M src/gb/gprs_ns2_fr.c
M src/gb/gprs_ns2_frgre.c
M src/gb/gprs_ns2_internal.h
M src/gb/gprs_ns2_udp.c
5 files changed, 63 insertions(+), 74 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/66/22566/1

diff --git a/src/gb/gprs_ns2.c b/src/gb/gprs_ns2.c
index eccbe13..7308e75 100644
--- a/src/gb/gprs_ns2.c
+++ b/src/gb/gprs_ns2.c
@@ -1351,4 +1351,36 @@
        return transfer_cap;
 }

+/*! common allocation + low-level initialization of a bind. Called by 
vc-drivers */
+int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
+                  struct gprs_ns2_vc_bind **result)
+{
+       struct gprs_ns2_vc_bind *bind;
+
+       if (!name)
+               return -EINVAL;
+
+       if (gprs_ns2_bind_by_name(nsi, name))
+               return -EALREADY;
+
+       bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
+       if (!bind)
+               return -ENOSPC;
+
+       bind->name = talloc_strdup(bind, name);
+       if (!bind->name) {
+               talloc_free(bind);
+               return -ENOSPC;
+       }
+
+       bind->nsi = nsi;
+       INIT_LLIST_HEAD(&bind->nsvc);
+       llist_add(&bind->list, &nsi->binding);
+
+       if (result)
+               *result = bind;
+
+       return 0;
+}
+
 /*! @} */
diff --git a/src/gb/gprs_ns2_fr.c b/src/gb/gprs_ns2_fr.c
index 9de2b06..953f4ca 100644
--- a/src/gb/gprs_ns2_fr.c
+++ b/src/gb/gprs_ns2_fr.c
@@ -719,21 +719,15 @@
        struct osmo_fr_link *fr_link;
        int rc = 0;

-       if (!name)
+       if (strlen(netif) > IFNAMSIZ)
                return -EINVAL;

        if (gprs_ns2_bind_by_name(nsi, name))
                return -EALREADY;

-       bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
-       if (!bind)
-               return -ENOSPC;
-
-       bind->name = talloc_strdup(bind, name);
-       if (!bind->name) {
-               rc = -ENOSPC;
-               goto err_bind;
-       }
+       rc = ns2_bind_alloc(nsi, name, &bind);
+       if (rc < 0)
+               return rc;

        bind->driver = &vc_driver_fr;
        bind->ll = GPRS_NS2_LL_FR;
@@ -742,27 +736,19 @@
        bind->send_vc = fr_vc_sendmsg;
        bind->free_vc = free_vc;
        bind->dump_vty = dump_vty;
-       bind->nsi = nsi;
        priv = bind->priv = talloc_zero(bind, struct priv_bind);
        if (!priv) {
                rc = -ENOSPC;
-               goto err_name;
+               goto err_bind;
        }

-       if (strlen(netif) > IFNAMSIZ) {
-               rc = -EINVAL;
-               goto err_priv;
-       }
        OSMO_STRLCPY_ARRAY(priv->netif, netif);

-       if (result)
-               *result = bind;
-
        /* FIXME: move fd handling into socket.c */
        fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
        if (!fr_link) {
                rc = -EINVAL;
-               goto err_priv;
+               goto err_bind;
        }

        fr_link->tx_cb = fr_tx_cb;
@@ -793,9 +779,6 @@
        if (rc < 0)
                goto err_fd;

-       INIT_LLIST_HEAD(&bind->nsvc);
-       llist_add(&bind->list, &nsi->binding);
-
 #ifdef ENABLE_LIBMNL
        if (!nsi->linkmon_mnl)
                nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, 
RTMGRP_LINK, linkmon_mnl_cb, nsi);
@@ -806,18 +789,17 @@
                linkmon_initial_dump(nsi->linkmon_mnl);
 #endif

+       if (result)
+               *result = bind;
+
        return rc;

 err_fd:
        close(priv->backlog.ofd.fd);
 err_fr:
        osmo_fr_link_free(fr_link);
-err_priv:
-       talloc_free(priv);
-err_name:
-       talloc_free((char *)bind->name);
 err_bind:
-       talloc_free(bind);
+       gprs_ns2_free_bind(bind);

        return rc;
 }
diff --git a/src/gb/gprs_ns2_frgre.c b/src/gb/gprs_ns2_frgre.c
index 5047af0..863f8f2 100644
--- a/src/gb/gprs_ns2_frgre.c
+++ b/src/gb/gprs_ns2_frgre.c
@@ -537,29 +537,22 @@
                        int dscp,
                        struct gprs_ns2_vc_bind **result)
 {
-       struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct 
gprs_ns2_vc_bind);
+       struct gprs_ns2_vc_bind *bind;
        struct priv_bind *priv;
        int rc;

-       if (!name)
-               return -ENOSPC;
-
-       if (gprs_ns2_bind_by_name(nsi, name))
-               return -EALREADY;
-
-       if (!bind)
-               return -ENOSPC;
-
-       if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != 
AF_INET6) {
-               talloc_free(bind);
+       if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != 
AF_INET6)
                return -EINVAL;
+
+       bind = gprs_ns2_bind_by_name(nsi, name);
+       if (bind) {
+               *result = bind;
+               return -EALREADY;
        }

-       bind->name = talloc_strdup(bind, name);
-       if (!bind->name) {
-               talloc_free(bind);
-               return -ENOSPC;
-       }
+       rc = ns2_bind_alloc(nsi, name, &bind);
+       if (rc < 0)
+               return rc;

        bind->driver = &vc_driver_frgre;
        bind->ll = GPRS_NS2_LL_FR_GRE;
@@ -571,7 +564,7 @@

        priv = bind->priv = talloc_zero(bind, struct priv_bind);
        if (!priv) {
-               talloc_free(bind);
+               gprs_ns2_free_bind(bind);
                return -ENOSPC;
        }
        priv->fd.cb = frgre_fd_cb;
@@ -579,14 +572,11 @@
        priv->addr = *local;
        INIT_LLIST_HEAD(&bind->nsvc);

-       llist_add(&bind->list, &nsi->binding);
-
        rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE,
                                 local, NULL,
                                 OSMO_SOCK_F_BIND);
        if (rc < 0) {
-               talloc_free(priv);
-               talloc_free(bind);
+               gprs_ns2_free_bind(bind);
                return rc;
        }

diff --git a/src/gb/gprs_ns2_internal.h b/src/gb/gprs_ns2_internal.h
index 0b264b6..1ab2770 100644
--- a/src/gb/gprs_ns2_internal.h
+++ b/src/gb/gprs_ns2_internal.h
@@ -272,6 +272,9 @@
                                 enum gprs_ns2_vc_mode vc_mode,
                                 const char *id);

+int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
+                  struct gprs_ns2_vc_bind **result);
+
 struct msgb *ns2_msgb_alloc(void);

 void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse);
diff --git a/src/gb/gprs_ns2_udp.c b/src/gb/gprs_ns2_udp.c
index 43ece13..b4bcd01 100644
--- a/src/gb/gprs_ns2_udp.c
+++ b/src/gb/gprs_ns2_udp.c
@@ -315,32 +315,18 @@
        struct priv_bind *priv;
        int rc;

-       if (!name)
+       if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != 
AF_INET6)
                return -EINVAL;

-       if (gprs_ns2_bind_by_name(nsi, name))
-               return -EALREADY;
-
        bind = gprs_ns2_ip_bind_by_sockaddr(nsi, local);
        if (bind) {
                *result = bind;
                return -EBUSY;
        }

-       bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
-       if (!bind)
-               return -ENOSPC;
-
-       bind->name = talloc_strdup(bind, name);
-       if (!bind->name) {
-               talloc_free(bind);
-               return -ENOSPC;
-       }
-
-       if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != 
AF_INET6) {
-               talloc_free(bind);
-               return -EINVAL;
-       }
+       rc = ns2_bind_alloc(nsi, name, &bind);
+       if (rc < 0)
+               return rc;

        bind->driver = &vc_driver_ip;
        bind->ll = GPRS_NS2_LL_UDP;
@@ -351,24 +337,21 @@
        bind->send_vc = nsip_vc_sendmsg;
        bind->free_vc = free_vc;
        bind->dump_vty = dump_vty;
-       bind->nsi = nsi;

        priv = bind->priv = talloc_zero(bind, struct priv_bind);
        if (!priv) {
-               talloc_free(bind);
+               gprs_ns2_free_bind(bind);
                return -ENOSPC;
        }
        priv->fd.cb = nsip_fd_cb;
        priv->fd.data = bind;
        priv->addr = *local;
-       INIT_LLIST_HEAD(&bind->nsvc);

        rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP,
                                 local, NULL,
                                 OSMO_SOCK_F_BIND);
        if (rc < 0) {
-               talloc_free(priv);
-               talloc_free(bind);
+               gprs_ns2_free_bind(bind);
                return rc;
        }

@@ -382,7 +365,6 @@
                                dscp, rc, errno);
        }

-       llist_add(&bind->list, &nsi->binding);
        if (result)
                *result = bind;


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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I535fc68e94fcd695de827dd922706adc1c5a2cb7
Gerrit-Change-Number: 22566
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <lafo...@osmocom.org>
Gerrit-MessageType: newchange

Reply via email to