Max has uploaded this change for review. ( https://gerrit.osmocom.org/12352


Change subject: Store GSN address in libosmocore struct
......................................................................

Store GSN address in libosmocore struct

That's automated code change made using following program:
//spatch --in-place --sp-file dup.spatch -I include --dir ./ --all-includes
@@
@@
struct
- gsn_addr
+ osmo_gsn_address

@@
struct osmo_gsn_address a;
@@
(
a.
- len
+ length
|
a.
- buf
+ addr
)

Note that --all-includes is necessary because affected functions are
defined and implemented in files with different subdirectory names under
include and src correspondingly.

Change-Id: I6ed32a91483dc608c47df77869033a6e891e9e6a
---
M include/osmocom/sgsn/gtphub.h
M src/gprs/gtphub.c
M src/gprs/gtphub_ares.c
M tests/gtphub/gtphub_test.c
4 files changed, 61 insertions(+), 53 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sgsn refs/changes/52/12352/1

diff --git a/include/osmocom/sgsn/gtphub.h b/include/osmocom/sgsn/gtphub.h
index 8fd9f38..7afe83c 100644
--- a/include/osmocom/sgsn/gtphub.h
+++ b/include/osmocom/sgsn/gtphub.h
@@ -163,22 +163,25 @@
        uint8_t buf[16];
 };

-void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src);
-int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str);
+void gsn_addr_copy(struct osmo_gsn_address *gsna,
+                  const struct osmo_gsn_address *src);
+int gsn_addr_from_str(struct osmo_gsn_address *gsna,
+                     const char *numeric_addr_str);

 /* Return gsna in numeric string form, in a static buffer. */
-const char *gsn_addr_to_str(const struct gsn_addr *gsna);
+const char *gsn_addr_to_str(const struct osmo_gsn_address *gsna);

 /* note: strbuf_len doesn't need to be larger than INET6_ADDRSTRLEN + 1. */
-const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
+const char *gsn_addr_to_strb(const struct osmo_gsn_address *gsna,
                             char *strbuf, int strbuf_len);

 /* Return 1 on match, zero otherwise. */
-int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b);
+int gsn_addr_same(const struct osmo_gsn_address *a,
+                 const struct osmo_gsn_address *b);

 /* Decode sa to gsna. Return 0 on success. If port is non-NULL, the port number
  * from sa is also returned. */
-int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
+int gsn_addr_from_sockaddr(struct osmo_gsn_address *gsna, uint16_t *port,
                           const struct osmo_sockaddr *sa);

 /* expiry */
@@ -379,7 +382,7 @@
        struct llist_head entry;

        struct gtphub_peer *peer;
-       struct gsn_addr addr;
+       struct osmo_gsn_address addr;
        struct llist_head ports;
 };

@@ -411,7 +414,7 @@
 };

 struct gtphub_bind {
-       struct gsn_addr local_addr;
+       struct osmo_gsn_address local_addr;
        uint16_t local_port;
        struct osmo_fd ofd;

@@ -506,14 +509,14 @@

 struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
                                          struct gtphub_bind *bind,
-                                         const struct gsn_addr *addr,
+                                         const struct osmo_gsn_address *addr,
                                          uint16_t port);

 struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
                                             const struct osmo_sockaddr *addr);

 void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
-                         struct gsn_addr *resolved_addr,
+                         struct osmo_gsn_address *resolved_addr,
                          time_t now);

 const char *gtphub_port_str(struct gtphub_peer_port *port);
diff --git a/src/gprs/gtphub.c b/src/gprs/gtphub.c
index ca5857b..a17306e 100644
--- a/src/gprs/gtphub.c
+++ b/src/gprs/gtphub.c
@@ -161,12 +161,13 @@
        }
 }

-void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src)
+void gsn_addr_copy(struct osmo_gsn_address *gsna,
+                  const struct osmo_gsn_address *src)
 {
        *gsna = *src;
 }

-int gsn_addr_from_sockaddr(struct gsn_addr *gsna, uint16_t *port,
+int gsn_addr_from_sockaddr(struct osmo_gsn_address *gsna, uint16_t *port,
                           const struct osmo_sockaddr *sa)
 {
        char addr_str[256];
@@ -185,23 +186,24 @@
        return gsn_addr_from_str(gsna, addr_str);
 }

-int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str)
+int gsn_addr_from_str(struct osmo_gsn_address *gsna,
+                     const char *numeric_addr_str)
 {
        if ((!gsna) || (!numeric_addr_str))
                return -1;

        int af = AF_INET;
-       gsna->len = 4;
+       gsna->length = 4;
        const char *pos = numeric_addr_str;
        for (; *pos; pos++) {
                if (*pos == ':') {
                        af = AF_INET6;
-                       gsna->len = 16;
+                       gsna->length = 16;
                        break;
                }
        }

-       int rc = inet_pton(af, numeric_addr_str, gsna->buf);
+       int rc = inet_pton(af, numeric_addr_str, gsna->addr);
        if (rc != 1) {
                LOG(LOGL_ERROR, "Cannot resolve numeric address: '%s'\n",
                    numeric_addr_str);
@@ -210,18 +212,18 @@
        return 0;
 }

-const char *gsn_addr_to_str(const struct gsn_addr *gsna)
+const char *gsn_addr_to_str(const struct osmo_gsn_address *gsna)
 {
        static char buf[INET6_ADDRSTRLEN + 1];
        return gsn_addr_to_strb(gsna, buf, sizeof(buf));
 }

-const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
+const char *gsn_addr_to_strb(const struct osmo_gsn_address *gsna,
                             char *strbuf,
                             int strbuf_len)
 {
        int af;
-       switch (gsna->len) {
+       switch (gsna->length) {
        case 4:
                af = AF_INET;
                break;
@@ -232,29 +234,31 @@
                return NULL;
        }

-       const char *r = inet_ntop(af, gsna->buf, strbuf, strbuf_len);
+       const char *r = inet_ntop(af, gsna->addr, strbuf, strbuf_len);
        if (!r) {
                LOG(LOGL_ERROR, "Cannot convert gsn_addr to string:"
                    " %s: len=%d, buf=%s\n",
                    strerror(errno),
-                   (int)gsna->len,
-                   osmo_hexdump(gsna->buf, sizeof(gsna->buf)));
+                   (int)gsna->length,
+                   osmo_hexdump(gsna->addr, sizeof(gsna->addr)));
        }
        return r;
 }

-int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b)
+int gsn_addr_same(const struct osmo_gsn_address *a,
+                 const struct osmo_gsn_address *b)
 {
        if (a == b)
                return 1;
        if ((!a) || (!b))
                return 0;
-       if (a->len != b->len)
+       if (a->length != b->length)
                return 0;
-       return (memcmp(a->buf, b->buf, a->len) == 0)? 1 : 0;
+       return (memcmp(a->addr, b->addr, a->length) == 0)? 1 : 0;
 }

-static int gsn_addr_get(struct gsn_addr *gsna, const struct gtp_packet_desc *p,
+static int gsn_addr_get(struct osmo_gsn_address *gsna,
+                       const struct gtp_packet_desc *p,
                        int idx)
 {
        if (p->rc != GTP_RC_PDU_C)
@@ -263,14 +267,15 @@
        unsigned int len;
        /* gtpie.h fails to declare gtpie_gettlv()'s first arg as const. */
        if (gtpie_gettlv((union gtpie_member**)p->ie, GTPIE_GSN_ADDR, idx,
-                        &len, gsna->buf, sizeof(gsna->buf))
+                        &len, gsna->addr, sizeof(gsna->addr))
            != 0)
                return -1;
-       gsna->len = len;
+       gsna->length = len;
        return 0;
 }

-static int gsn_addr_put(const struct gsn_addr *gsna, struct gtp_packet_desc *p,
+static int gsn_addr_put(const struct osmo_gsn_address *gsna,
+                       struct gtp_packet_desc *p,
                        int idx)
 {
        if (p->rc != GTP_RC_PDU_C)
@@ -284,14 +289,14 @@

        struct gtpie_tlv *ie = &p->ie[ie_idx]->tlv;
        int ie_l = ntoh16(ie->l);
-       if (ie_l != gsna->len) {
+       if (ie_l != gsna->length) {
                LOG(LOGL_ERROR, "Not implemented:"
                    " replace an IE address of different size:"
-                   " replace %d with %d\n", (int)ie_l, (int)gsna->len);
+                   " replace %d with %d\n", (int)ie_l, (int)gsna->length);
                return -1;
        }

-       memcpy(ie->v, gsna->buf, (int)ie_l);
+       memcpy(ie->v, gsna->addr, (int)ie_l);
        return 0;
 }

@@ -574,7 +579,7 @@
        }

        for (i = 0; i < 2; i++) {
-               struct gsn_addr addr;
+               struct osmo_gsn_address addr;
                if (gsn_addr_get(&addr, res, i) == 0)
                        LOG(LOGL_DEBUG, "| addr %s\n", gsn_addr_to_str(&addr));
        }
@@ -1535,7 +1540,7 @@

        for_each_plane(plane_idx) {
                int rc;
-               struct gsn_addr use_addr;
+               struct osmo_gsn_address use_addr;
                uint16_t use_port;
                uint32_t tei_from_ie;
                int ie_idx;
@@ -1552,7 +1557,7 @@
                LOG(LOGL_DEBUG, "Read %s GSN addr %s (%d)\n",
                    gtphub_plane_idx_names[plane_idx],
                    gsn_addr_to_str(&use_addr),
-                   use_addr.len);
+                   use_addr.length);

                ie_idx = gtpie_getie(p->ie, ie_type[plane_idx], 0);
                if (ie_idx < 0) {
@@ -2069,7 +2074,7 @@
        return 0;
 }

-static int gsn_addr_to_sockaddr(struct gsn_addr *src,
+static int gsn_addr_to_sockaddr(struct osmo_gsn_address *src,
                                uint16_t port,
                                struct osmo_sockaddr *dst)
 {
@@ -2220,7 +2225,7 @@
                                return -1;
                        }
 
-                       struct gsn_addr from_gsna;
+                       struct osmo_gsn_address from_gsna;
                        uint16_t from_port;
                        if (gsn_addr_from_sockaddr(&from_gsna, &from_port, 
from_addr) != 0)
                                return -1;
@@ -2354,7 +2359,7 @@
 }

 void gtphub_resolved_ggsn(struct gtphub *hub, const char *apn_oi_str,
-                         struct gsn_addr *resolved_addr,
+                         struct osmo_gsn_address *resolved_addr,
                          time_t now)
 {
        struct gtphub_peer_port *pp;
@@ -2524,7 +2529,7 @@
        if (!addr->addr_str)
                return 0;

-       struct gsn_addr gsna;
+       struct osmo_gsn_address gsna;
        if (gsn_addr_from_str(&gsna, addr->addr_str) != 0)
                return -1;

@@ -2600,7 +2605,7 @@
 }

 static struct gtphub_peer_addr *gtphub_peer_find_addr(const struct gtphub_peer 
*peer,
-                                                     const struct gsn_addr 
*addr)
+                                                     const struct 
osmo_gsn_address *addr)
 {
        struct gtphub_peer_addr *a;
        llist_for_each_entry(a, &peer->addresses, entry) {
@@ -2623,7 +2628,7 @@
 }

 static struct gtphub_peer_addr *gtphub_addr_find(const struct gtphub_bind 
*bind,
-                                                const struct gsn_addr *addr)
+                                                const struct osmo_gsn_address 
*addr)
 {
        struct gtphub_peer *peer;
        llist_for_each_entry(peer, &bind->peers, entry) {
@@ -2635,7 +2640,7 @@
 }

 static struct gtphub_peer_port *gtphub_port_find(const struct gtphub_bind 
*bind,
-                                                const struct gsn_addr *addr,
+                                                const struct osmo_gsn_address 
*addr,
                                                 uint16_t port)
 {
        struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
@@ -2647,7 +2652,7 @@
 struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
                                             const struct osmo_sockaddr *addr)
 {
-       struct gsn_addr gsna;
+       struct osmo_gsn_address gsna;
        uint16_t port;
        if (gsn_addr_from_sockaddr(&gsna, &port, addr) != 0)
                return NULL;
@@ -2677,7 +2682,7 @@
 }

 static struct gtphub_peer_addr *gtphub_peer_add_addr(struct gtphub_peer *peer,
-                                                    const struct gsn_addr 
*addr)
+                                                    const struct 
osmo_gsn_address *addr)
 {
        struct gtphub_peer_addr *a;
        a = talloc_zero(osmo_gtphub_ctx, struct gtphub_peer_addr);
@@ -2692,7 +2697,7 @@

 static struct gtphub_peer_addr *gtphub_addr_have(struct gtphub *hub,
                                                 struct gtphub_bind *bind,
-                                                const struct gsn_addr *addr)
+                                                const struct osmo_gsn_address 
*addr)
 {
        struct gtphub_peer_addr *a = gtphub_addr_find(bind, addr);
        if (a)
@@ -2747,7 +2752,7 @@

 struct gtphub_peer_port *gtphub_port_have(struct gtphub *hub,
                                          struct gtphub_bind *bind,
-                                         const struct gsn_addr *addr,
+                                         const struct osmo_gsn_address *addr,
                                          uint16_t port)
 {
        struct gtphub_peer_addr *a = gtphub_addr_have(hub, bind, addr);
@@ -2767,7 +2772,7 @@
        struct gtphub_peer_addr *pa;
        struct gtphub_peer_port *pp;

-       struct gsn_addr gsna;
+       struct osmo_gsn_address gsna;
        uint16_t port;
        int rc = gsn_addr_from_sockaddr(&gsna, &port, addr);
        if (rc < 0)
diff --git a/src/gprs/gtphub_ares.c b/src/gprs/gtphub_ares.c
index 87dc860..be26f55 100644
--- a/src/gprs/gtphub_ares.c
+++ b/src/gprs/gtphub_ares.c
@@ -85,10 +85,10 @@
                goto remove_from_queue;
        }

-       struct gsn_addr resolved_addr;
-       if (hostent->h_length > sizeof(resolved_addr.buf)) {
+       struct osmo_gsn_address resolved_addr;
+       if (hostent->h_length > sizeof(resolved_addr.addr)) {
                LOGP(DGTPHUB, LOGL_ERROR, "Addr size too large: %d > %d\n",
-                    (int)hostent->h_length, (int)sizeof(resolved_addr.buf));
+                    (int)hostent->h_length, (int)sizeof(resolved_addr.addr));
                goto remove_from_queue;
        }

@@ -99,8 +99,8 @@
                goto remove_from_queue;
        }

-       memcpy(resolved_addr.buf, addr0, hostent->h_length);
-       resolved_addr.len = hostent->h_length;
+       memcpy(resolved_addr.addr, addr0, hostent->h_length);
+       resolved_addr.length = hostent->h_length;

        LOGP(DGTPHUB, LOGL_NOTICE, "resolved addr %s\n",
             osmo_hexdump((unsigned char*)&resolved_addr,
diff --git a/tests/gtphub/gtphub_test.c b/tests/gtphub/gtphub_test.c
index 2e48bb1..214b7d4 100644
--- a/tests/gtphub/gtphub_test.c
+++ b/tests/gtphub/gtphub_test.c
@@ -424,7 +424,7 @@
                                                         const char *imsi_str,
                                                         const char *apn_ni_str)
 {
-       struct gsn_addr resolved_gsna;
+       struct osmo_gsn_address resolved_gsna;
        uint16_t resolved_port;

        OSMO_ASSERT(gsn_addr_from_sockaddr(&resolved_gsna, &resolved_port,

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

Gerrit-Project: osmo-sgsn
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6ed32a91483dc608c47df77869033a6e891e9e6a
Gerrit-Change-Number: 12352
Gerrit-PatchSet: 1
Gerrit-Owner: Max <[email protected]>

Reply via email to