summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <zecke@selfish.org>2011-01-12 11:20:32 +0100
committerHolger Hans Peter Freyther <zecke@selfish.org>2011-01-12 12:50:09 +0100
commitf3b7effbc169b9bdf91828c1557dcab06aaabb44 (patch)
tree0dd3df9342ce6394f9a989e5962bca34dd91d105
parentb8962797a5bdbfbf69c294db455654edde8d0e70 (diff)
m2ua: Add parsing and creation of m2ua packages
-rw-r--r--configure.ac1
-rw-r--r--include/m2ua/Makefile.am2
-rw-r--r--include/m2ua/m2ua_msg.h54
-rw-r--r--src/Makefile.am3
-rw-r--r--src/m2ua_msg.c155
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/m2ua/Makefile.am6
-rw-r--r--tests/m2ua/m2ua_test.c54
8 files changed, 274 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 0b44dae..9886070 100644
--- a/configure.ac
+++ b/configure.ac
@@ -38,5 +38,6 @@ AC_OUTPUT(
tests/Makefile
tests/sccp/Makefile
tests/mtp/Makefile
+ tests/m2ua/Makefile
Makefile)
diff --git a/include/m2ua/Makefile.am b/include/m2ua/Makefile.am
index f52d610..d740a6c 100644
--- a/include/m2ua/Makefile.am
+++ b/include/m2ua/Makefile.am
@@ -1,2 +1,2 @@
-m2ua_HEADERS = m2ua_types.h
+m2ua_HEADERS = m2ua_types.h m2ua_msg.h
m2uadir = $(includedir)/osmocom/m2ua
diff --git a/include/m2ua/m2ua_msg.h b/include/m2ua/m2ua_msg.h
new file mode 100644
index 0000000..49fe8cc
--- /dev/null
+++ b/include/m2ua/m2ua_msg.h
@@ -0,0 +1,54 @@
+/* Routines for generating and parsing messages */
+/* (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ *
+ * 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/>.
+ *
+ */
+
+
+#ifndef m2ua_msg_h
+#define m2ua_msg_h
+
+#include "m2ua_types.h"
+
+#include <osmocore/linuxlist.h>
+
+struct msgb;
+
+struct m2ua_msg {
+ struct m2ua_common_hdr hdr;
+
+ struct llist_head headers;
+};
+
+struct m2ua_msg_part {
+ struct llist_head entry;
+
+ uint16_t tag;
+ uint16_t len;
+ uint8_t *dat;
+
+ /* TODO: keep small data in the struct for perf reasons */
+};
+
+
+struct m2ua_msg *m2ua_msg_alloc(void);
+void m2ua_msg_free(struct m2ua_msg *msg);
+
+int m2ua_msg_add_data(struct m2ua_msg *msg, uint16_t tag, uint16_t len, uint8_t *dat);
+
+struct m2ua_msg *m2ua_from_msg(uint16_t len, uint8_t *data);
+struct msgb *m2ua_to_msg(struct m2ua_msg *msg);
+
+#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index 63d11a4..c7451ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,7 +2,8 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)
AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS)
sccpdir = $(libdir)
-sccp_LIBRARIES = libsccp.a libmtp.a
+sccp_LIBRARIES = libsccp.a libmtp.a libm2ua.a
libsccp_a_SOURCES = sccp.c
libmtp_a_SOURCES = mtp_pcap.c
+libm2ua_a_SOURCES = m2ua_msg.c
diff --git a/src/m2ua_msg.c b/src/m2ua_msg.c
new file mode 100644
index 0000000..c4115b6
--- /dev/null
+++ b/src/m2ua_msg.c
@@ -0,0 +1,155 @@
+/* Routines for generating and parsing messages */
+/* (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ *
+ * 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 <m2ua/m2ua_msg.h>
+
+#include <osmocore/msgb.h>
+#include <osmocore/logging.h>
+#include <osmocore/talloc.h>
+
+#include <arpa/inet.h>
+
+#include <string.h>
+
+static void *tall_m2ua;
+static int DM2UA = -1;
+
+struct m2ua_msg *m2ua_msg_alloc(void)
+{
+ struct m2ua_msg *msg;
+
+ msg = talloc_zero(tall_m2ua, struct m2ua_msg);
+ if (!msg) {
+ LOGP(DM2UA, LOGL_ERROR, "Failed to allocate.\n");
+ return NULL;
+ }
+
+ INIT_LLIST_HEAD(&msg->headers);
+ return msg;
+}
+
+void m2ua_msg_free(struct m2ua_msg *msg)
+{
+ talloc_free(msg);
+}
+
+int m2ua_msg_add_data(struct m2ua_msg *msg, uint16_t tag,
+ uint16_t len, uint8_t *dat)
+{
+ struct m2ua_msg_part *part;
+
+ part = talloc_zero(msg, struct m2ua_msg_part);
+ if (!part)
+ return -1;
+
+ part->tag = tag;
+ part->len = len;
+
+ /* do we have any data? */
+ if (part->len != 0) {
+ part->dat = talloc_memdup(part, dat, len);
+ if (!part->dat) {
+ talloc_free(part);
+ return -1;
+ }
+ }
+
+ llist_add_tail(&part->entry, &msg->headers);
+ return 0;
+}
+
+struct m2ua_msg *m2ua_from_msg(uint16_t len, uint8_t *data)
+{
+ struct m2ua_parameter_hdr *par;
+ struct m2ua_common_hdr *hdr;
+ struct m2ua_msg *msg;
+ uint16_t pos, par_len;
+ int rc;
+
+ msg = m2ua_msg_alloc();
+ if (!msg)
+ return NULL;
+
+ if (len < sizeof(*hdr))
+ goto fail;
+
+ hdr = (struct m2ua_common_hdr *) data;
+ if (hdr->version != M2UA_VERSION)
+ goto fail;
+ if (ntohl(hdr->msg_length) > len)
+ goto fail;
+
+ msg->hdr = *hdr;
+ pos = sizeof(*hdr);
+
+ while (pos + sizeof(*par) < len) {
+ par = (struct m2ua_parameter_hdr *) &data[pos];
+ par_len = ntohs(par->len);
+
+ if (pos + par_len > len || par_len < 4)
+ goto fail;
+
+ rc = m2ua_msg_add_data(msg, ntohs(par->tag),
+ par_len - 4, par->data);
+ if (rc != 0)
+ goto fail;
+
+ pos += par_len;
+ }
+
+ /* TODO: parse */
+ return msg;
+
+fail:
+ LOGP(DM2UA, LOGL_ERROR, "Failed to parse.\n");
+ m2ua_msg_free(msg);
+ return NULL;
+}
+
+struct msgb *m2ua_to_msg(struct m2ua_msg *m2ua)
+{
+ struct m2ua_msg_part *part;
+ struct m2ua_common_hdr *hdr;
+ struct msgb *msg;
+ msg = msgb_alloc_headroom(512, 2048, "m2ua msg");
+ if (!msg) {
+ LOGP(DM2UA, LOGL_ERROR, "Failed to allocate.\n");
+ return NULL;
+ }
+
+ msg->l2h = msgb_put(msg, sizeof(*hdr));
+ hdr = (struct m2ua_common_hdr *) msg->l2h;
+ *hdr = m2ua->hdr;
+
+ /* make sure that is right */
+ hdr->version = M2UA_VERSION;
+ hdr->spare = 0;
+
+ llist_for_each_entry(part, &m2ua->headers, entry) {
+ msgb_put_u16(msg, part->tag);
+ msgb_put_u16(msg, part->len + 4);
+ if (part->dat) {
+ uint8_t *dat = msgb_put(msg, part->len);
+ memcpy(dat, part->dat, part->len);
+ }
+ }
+
+ /* update the size of the data */
+ hdr->msg_length = htonl(msgb_l2len(msg));
+ return msg;
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bf7855d..882d1e2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1 +1 @@
-SUBDIRS = sccp mtp
+SUBDIRS = sccp mtp m2ua
diff --git a/tests/m2ua/Makefile.am b/tests/m2ua/Makefile.am
new file mode 100644
index 0000000..8efb04d
--- /dev/null
+++ b/tests/m2ua/Makefile.am
@@ -0,0 +1,6 @@
+INCLUDES = $(all_includes) -I$(top_srcdir)/include -Wall
+AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS)
+
+noinst_PROGRAMS = m2ua_test
+m2ua_test_SOURCES = m2ua_test.c
+m2ua_test_LDADD = $(LIBOSMOCORE_LIBS) $(top_builddir)/src/libm2ua.a
diff --git a/tests/m2ua/m2ua_test.c b/tests/m2ua/m2ua_test.c
new file mode 100644
index 0000000..cd6f9ad
--- /dev/null
+++ b/tests/m2ua/m2ua_test.c
@@ -0,0 +1,54 @@
+/* (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ *
+ * 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 <m2ua/m2ua_msg.h>
+
+#include <osmocore/utils.h>
+#include <osmocore/msgb.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define FAIL(msg) \
+ do { \
+ fprintf(stderr, "FAILURE: %s on line %d\n", msg, __LINE__); \
+ abort(); \
+ } while(0);
+
+static uint8_t asp_up[] = {
+ 0x01, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x10,
+ 0x00, 0x11, 0x00, 0x08, 0xac, 0x10, 0x01, 0x51,
+};
+
+int main(int argc, char **argv)
+{
+ struct m2ua_msg *m2u = m2ua_from_msg(ARRAY_SIZE(asp_up), asp_up);
+ struct msgb *msg = m2ua_to_msg(m2u);
+
+ if (msg->len != ARRAY_SIZE(asp_up)) {
+ printf("Got %d wanted %d\n", msg->len, ARRAY_SIZE(asp_up));
+ FAIL("Wrong size");
+ }
+
+ if (memcmp(msg->data, asp_up, msg->len) != 0) {
+ printf("Got '%s'\n", hexdump(msg->data, msg->len));
+ FAIL("Wrong memory");
+ }
+
+ return 0;
+}