aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2015-08-30 12:28:29 +0200
committerHarald Welte <laforge@gnumonks.org>2015-08-30 12:28:29 +0200
commit30afef3073e6f9d0c00275b7c41009eb64661f67 (patch)
treeeea2959ffd3b0977e18b12a546ce8f2604c1678b
parentb3dae30e5ee3d9109b07023327df1c6c00b06806 (diff)
move asn.1 helper functions to asn1helpers.[ch]
-rw-r--r--src/Makefile2
-rw-r--r--src/asn1helpers.c19
-rw-r--r--src/asn1helpers.h29
-rw-r--r--src/hnbgw_hnbap.c28
4 files changed, 52 insertions, 26 deletions
diff --git a/src/Makefile b/src/Makefile
index 18ec5bb..8ce4186 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -5,7 +5,7 @@ LDFLAGS=`pkg-config --libs libosmocore libosmovty libosmogsm` -lsctp -L$(LIBFFAS
all: hnbgw
-hnbgw: hnbgw.o hnbgw_hnbap.o hnbap.o
+hnbgw: hnbgw.o hnbgw_hnbap.o hnbap.o asn1helpers.o
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c
diff --git a/src/asn1helpers.c b/src/asn1helpers.c
new file mode 100644
index 0000000..fe886e3
--- /dev/null
+++ b/src/asn1helpers.c
@@ -0,0 +1,19 @@
+
+#include <string.h>
+
+#include <asn1defs.h>
+
+#include "asn1helpers.h"
+
+int asn1_strncpy(char *out, const ASN1String *in, size_t n)
+{
+ size_t cpylen = n;
+
+ if (in->len < cpylen)
+ cpylen = in->len;
+
+ strncpy(out, (char *)in->buf, cpylen);
+ out[n-1] = '\0';
+
+ return cpylen;
+}
diff --git a/src/asn1helpers.h b/src/asn1helpers.h
new file mode 100644
index 0000000..3861d13
--- /dev/null
+++ b/src/asn1helpers.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <asn1defs.h>
+
+static inline uint16_t asn1str_to_u16(ASN1String *as)
+{
+ if (as->len < 2)
+ return 0;
+ else
+ return *(uint16_t *)as->buf;
+}
+
+static inline uint8_t asn1str_to_u8(ASN1String *as)
+{
+ if (as->len < 1)
+ return 0;
+ else
+ return *(uint8_t *)as->buf;
+}
+
+static inline uint8_t asn1bitstr_to_u32(ASN1BitString *as)
+{
+ if (as->len < 25)
+ return 0;
+ else
+ return *(uint32_t *)as->buf;
+}
+
+int asn1_strncpy(char *out, const ASN1String *in, size_t n);
diff --git a/src/hnbgw_hnbap.c b/src/hnbgw_hnbap.c
index fd3a328..8d9e1c8 100644
--- a/src/hnbgw_hnbap.c
+++ b/src/hnbgw_hnbap.c
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <string.h>
+#include "asn1helpers.h"
+
#include "hnbap.h"
#include "hnbgw.h"
#include "hnbap_const.h"
@@ -43,30 +45,6 @@ static void *find_ie(const struct ProtocolIE_Container_1 *cont, ProtocolIE_ID id
return NULL;
}
-static inline uint16_t asn1str_to_u16(ASN1String *as)
-{
- if (as->len < 2)
- return 0;
- else
- return *(uint16_t *)as->buf;
-}
-
-static inline uint8_t asn1str_to_u8(ASN1String *as)
-{
- if (as->len < 1)
- return 0;
- else
- return *(uint8_t *)as->buf;
-}
-
-static inline uint8_t asn1bitstr_to_u32(ASN1BitString *as)
-{
- if (as->len < 25)
- return 0;
- else
- return *(uint32_t *)as->buf;
-}
-
static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, struct HNBRegisterRequest *req)
{
HNB_Identity *identity =
@@ -86,7 +64,7 @@ static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, struct HNBRegister
return -1;
/* copy all identity parameters from the message to ctx */
- strncpy(ctx->identity_info, (const char *) identity->hNB_Identity_Info.buf,
+ asn1_strncpy(ctx->identity_info, &identity->hNB_Identity_Info,
sizeof(ctx->identity_info));
ctx->id.lac = asn1str_to_u16(lac);
ctx->id.sac = asn1str_to_u16(sac);