This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Max gerrit-no-reply at lists.osmocom.orgMax has uploaded this change for review. ( https://gerrit.osmocom.org/12230
Change subject: gtphub: remove code duplication
......................................................................
gtphub: remove code duplication
Use libosmocore struct for GSN address.
Change-Id: I75645fd5cd1016bd9a08096e3ee4101b2560cefd
---
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, 54 insertions(+), 62 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-sgsn refs/changes/30/12230/1
diff --git a/include/osmocom/sgsn/gtphub.h b/include/osmocom/sgsn/gtphub.h
index 8fd9f38..93d647d 100644
--- a/include/osmocom/sgsn/gtphub.h
+++ b/include/osmocom/sgsn/gtphub.h
@@ -155,30 +155,22 @@
extern const char* const gtphub_side_idx_names[GTPH_SIDE_N];
-/* A host address in the form that is expected in the 7.7.32 GSN Address IE.
- * len is either 4 (IPv4) or 16 (IPv6), any other value is invalid. If no
- * address is set, len shall be 0. */
-struct gsn_addr {
- uint16_t len;
- 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 +371,7 @@
struct llist_head entry;
struct gtphub_peer *peer;
- struct gsn_addr addr;
+ struct osmo_gsn_address addr;
struct llist_head ports;
};
@@ -411,7 +403,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 +498,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..d35c076 100644
--- a/src/gprs/gtphub.c
+++ b/src/gprs/gtphub.c
@@ -161,12 +161,12 @@
}
}
-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 +185,23 @@
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 +210,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 +232,29 @@
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:"
+ LOG(LOGL_ERROR, "Cannot convert osmo_gsn_address 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 +263,14 @@
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 +284,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 +574,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 +1535,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 +1552,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 +2069,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 +2220,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 +2354,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 +2524,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 +2600,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 +2623,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 +2635,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 +2647,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 +2677,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 +2692,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 +2747,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 +2767,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/12230
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: I75645fd5cd1016bd9a08096e3ee4101b2560cefd
Gerrit-Change-Number: 12230
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181210/482c6875/attachment.htm>