[PATCH] libosmocore[master]: gsm0808: Add utils for AoIP Transport Layer Address

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/.

dexter gerrit-no-reply at lists.osmocom.org
Thu Mar 30 16:44:52 UTC 2017


Review at  https://gerrit.osmocom.org/2176

gsm0808: Add utils for AoIP Transport Layer Address

The planned support for true A over IP requires the encoding and
decoding of a so called "AoIP Transport Layer Address" element.

This commt adds parsing functionality and tests for the element
mentioned above, however, it is not yet actively used.

Change-Id: I57933b0a06a3f54ec2a41e6ecb6ced9fbbc89332
---
A include/osmocom/gsm/gsm0808_utils.h
M src/gsm/Makefile.am
A src/gsm/gsm0808_utils.c
M src/gsm/libosmogsm.map
M tests/gsm0808/gsm0808_test.c
5 files changed, 217 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/76/2176/1

diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
new file mode 100644
index 0000000..d4d8618
--- /dev/null
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -0,0 +1,29 @@
+/* (C) 2016 by Sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Philipp Maier
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+#include <sys/socket.h>
+
+/* Encode AoIP transport address element */
+struct msgb *gsm0808_enc_aoip_trasp_addr(struct sockaddr_storage *ss);
+
+/* Decode AoIP transport address element */
+struct sockaddr_storage *gsm0808_dec_aoip_trasp_addr(const void *ctx,
+						     struct msgb *msg);
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index 3a4a0cd..e64c9e7 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -25,7 +25,7 @@
 			auth_milenage.c milenage/aes-encblock.c gea.c \
 			milenage/aes-internal.c milenage/aes-internal-enc.c \
 			milenage/milenage.c gan.c ipa.c gsm0341.c apn.c \
-			gsup.c gprs_gea.c gsm0503_conv.c oap.c
+			gsup.c gprs_gea.c gsm0503_conv.c oap.c gsm0808_utils.c
 libgsmint_la_LDFLAGS = -no-undefined
 libgsmint_la_LIBADD = $(top_builddir)/src/libosmocore.la
 
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
new file mode 100644
index 0000000..d1897cf4
--- /dev/null
+++ b/src/gsm/gsm0808_utils.c
@@ -0,0 +1,115 @@
+/* (C) 2016 by Sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Philipp Maier
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/msgb.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define ELEMENT_MSGB_MAXLEN 256
+
+#define IP_V4_ADDR_LEN 4
+#define IP_V6_ADDR_LEN 16
+#define IP_PORT_LEN 2
+
+/* Encode AoIP transport address element */
+struct msgb *gsm0808_enc_aoip_trasp_addr(struct sockaddr_storage *ss)
+{
+	/* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
+	struct sockaddr_in *sin;
+	struct sockaddr_in6 *sin6;
+	uint16_t port = 0;
+
+	uint8_t *ptr;
+	struct msgb *msg;
+
+	OSMO_ASSERT(ss);
+	OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
+
+	msg = msgb_alloc(ELEMENT_MSGB_MAXLEN, "AoIP Transport Layer Address");
+	if (!msg)
+		return NULL;
+
+	switch (ss->ss_family) {
+	case AF_INET:
+		sin = (struct sockaddr_in *)ss;
+		port = ntohs(sin->sin_port);
+		ptr = msgb_put(msg, IP_V4_ADDR_LEN);
+		memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
+		break;
+	case AF_INET6:
+		sin6 = (struct sockaddr_in6 *)ss;
+		port = ntohs(sin6->sin6_port);
+		ptr = msgb_put(msg, IP_V6_ADDR_LEN);
+		memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
+		break;
+	}
+
+	msgb_put_u16(msg, port);
+	return msg;
+}
+
+/* Decode AoIP transport address element */
+struct sockaddr_storage *gsm0808_dec_aoip_trasp_addr(const void *ctx,
+						     struct msgb *msg)
+{
+	/* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
+	struct sockaddr_storage *ss;
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+	uint8_t *ptr;
+
+	if (!msg)
+		return NULL;
+
+	switch (msg->len) {
+
+	case IP_V4_ADDR_LEN + IP_PORT_LEN:
+		memset(&sin, 0, sizeof(sin));
+		sin.sin_family = AF_INET;
+		sin.sin_port = htons(msgb_get_u16(msg));
+		ptr = msgb_get(msg, IP_V4_ADDR_LEN);
+		memcpy(&sin.sin_addr.s_addr, ptr, IP_V4_ADDR_LEN);
+		ss = talloc_zero(ctx, struct sockaddr_storage);
+		if (!ss)
+			return NULL;
+		memcpy(ss, &sin, sizeof(sin));
+		break;
+	case IP_V6_ADDR_LEN + IP_PORT_LEN:
+		memset(&sin6, 0, sizeof(sin6));
+		sin6.sin6_family = AF_INET6;
+		sin6.sin6_port = htons(msgb_get_u16(msg));
+		ptr = msgb_get(msg, IP_V6_ADDR_LEN);
+		memcpy(sin6.sin6_addr.s6_addr, ptr, IP_V6_ADDR_LEN);
+		ss = talloc_zero(ctx, struct sockaddr_storage);
+		if (!ss)
+			return NULL;
+		memcpy(ss, &sin6, sizeof(sin6));
+		break;
+	default:
+		/* Malformed element */
+		return NULL;
+		break;
+	}
+
+	return ss;
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 5649e71..3ad847d 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -137,6 +137,8 @@
 gsm0808_create_reset;
 gsm0808_create_sapi_reject;
 gsm0808_prepend_dtap_header;
+gsm0808_enc_aoip_trasp_addr;
+gsm0808_dec_aoip_trasp_addr;
 
 gsm0858_rsl_ul_meas_enc;
 
diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c
index 98502b7..99605d8 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -19,9 +19,14 @@
  */
 
 #include <osmocom/gsm/gsm0808.h>
+#include <osmocom/gsm/gsm0808_utils.h>
+#include <osmocom/gsm/protocol/gsm_08_08.h>
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 
 #define VERIFY(msg, data, len) 						\
 	if (msgb_l3len(msg) != len) {					\
@@ -247,8 +252,67 @@
 	msgb_free(in_msg);
 }
 
+static void test_enc_dec_aoip_trasp_addr_v4(const void *ctx)
+{
+	struct sockaddr_storage enc_addr;
+	struct sockaddr_storage *dec_addr;
+	struct sockaddr_in enc_addr_in;
+	struct msgb *msg;
+
+	memset(&enc_addr_in, 0, sizeof(enc_addr_in));
+	enc_addr_in.sin_family = AF_INET;
+	enc_addr_in.sin_port = htons(1234);
+	inet_aton("255.0.255.255", &enc_addr_in.sin_addr);
+
+	memset(&enc_addr, 0, sizeof(enc_addr));
+	memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
+
+	msg = gsm0808_enc_aoip_trasp_addr(&enc_addr);
+	OSMO_ASSERT(msg);
+	dec_addr = gsm0808_dec_aoip_trasp_addr(ctx, msg);
+	OSMO_ASSERT(dec_addr);
+	OSMO_ASSERT(msg->len == 0);
+
+	OSMO_ASSERT(memcmp(&enc_addr, dec_addr, sizeof(enc_addr)) == 0);
+
+	talloc_free(dec_addr);
+	msgb_free(msg);
+}
+
+static void test_enc_dec_aoip_trasp_addr_v6(const void *ctx)
+{
+	struct sockaddr_storage enc_addr;
+	struct sockaddr_storage *dec_addr;
+	struct sockaddr_in6 enc_addr_in;
+	struct msgb *msg;
+
+	memset(&enc_addr_in, 0, sizeof(enc_addr_in));
+	enc_addr_in.sin6_family = AF_INET6;
+	enc_addr_in.sin6_port = htons(4567);
+	inet_pton(AF_INET6, "2001:0db8:85a3:08d3:1319:8a2e:0370:7344",
+		  &enc_addr_in.sin6_addr);
+
+	memset(&enc_addr, 0, sizeof(enc_addr));
+	memcpy(&enc_addr, &enc_addr_in, sizeof(enc_addr_in));
+
+	msg = gsm0808_enc_aoip_trasp_addr(&enc_addr);
+	OSMO_ASSERT(msg);
+	dec_addr = gsm0808_dec_aoip_trasp_addr(ctx, msg);
+	OSMO_ASSERT(dec_addr);
+	OSMO_ASSERT(msg->len == 0);
+
+	OSMO_ASSERT(memcmp(&enc_addr, dec_addr, sizeof(enc_addr)) == 0);
+
+	talloc_free(dec_addr);
+	msgb_free(msg);
+}
+
 int main(int argc, char **argv)
 {
+	void *ctx;
+
+	ctx = talloc_named_const(NULL, 0, "gsm0808_ctx");
+
 	printf("Testing generation of GSM0808 messages\n");
 	test_create_layer3();
 	test_create_reset();
@@ -263,7 +327,13 @@
 	test_create_clear_rqst();
 	test_create_dtap();
 	test_prepend_dtap();
+	test_enc_dec_aoip_trasp_addr_v4(ctx);
+	test_enc_dec_aoip_trasp_addr_v6(ctx);
 
 	printf("Done\n");
+
+	talloc_report_full(ctx, stderr);
+	OSMO_ASSERT(talloc_total_blocks(ctx) == 1);
+
 	return EXIT_SUCCESS;
 }

-- 
To view, visit https://gerrit.osmocom.org/2176
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I57933b0a06a3f54ec2a41e6ecb6ced9fbbc89332
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>



More information about the gerrit-log mailing list