iu: iu_helpers: add functions to decode ip/port from rab-ass

add ranap_transp_assoc_decode() to decode the port information from
an  RANAP_IuTransportAssociation_t field.

add ranap_transp_layer_addr_decode() to decode the ip-address from
an RANAP_TransportLayerAddress_t field.

Change-Id: I3c1a0455c5f25cae41ee19229d6daf299e023062
diff --git a/src/iu_helpers.c b/src/iu_helpers.c
index 2f44e93..5e78293 100644
--- a/src/iu_helpers.c
+++ b/src/iu_helpers.c
@@ -20,8 +20,12 @@
 
 #include <stdint.h>
 #include <string.h>
-
+#include <errno.h>
+#include <arpa/inet.h>
+#include "asn1helpers.h"
 #include <osmocom/core/utils.h>
+#include <osmocom/ranap/RANAP_IuTransportAssociation.h>
+#include <osmocom/ranap/RANAP_TransportLayerAddress.h>
 
 /* decode a BCD-string as used inside ASN.1 encoded Iu interface protocols */
 int ranap_bcd_decode(char *out, size_t out_len, const uint8_t *in, size_t in_len)
@@ -70,3 +74,50 @@
 	}
 	return i;
 }
+
+/* decode a network port as used inside ASN.1 encoded Iu interface protocols */
+int ranap_transp_assoc_decode(uint16_t *port, const RANAP_IuTransportAssociation_t *transp_assoc)
+{
+	uint32_t result;
+
+	if (!transp_assoc)
+		return -EINVAL;
+
+	result = asn1bitstr_to_u32((BIT_STRING_t *) & transp_assoc->choice.bindingID);
+
+	/* The lower 16 bits should be zero, otherwise the decoding may
+	 * have yielded some odd result */
+	if (result & 0xFFFF)
+		return -EINVAL;
+
+	*port = (uint16_t) ((result >> 16) & 0xFFFF);
+
+	if (*port == 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+/* decode a network address as used inside ASN.1 encoded Iu interface protocols */
+int ranap_transp_layer_addr_decode(char *addr, unsigned int addr_len,
+				   const RANAP_TransportLayerAddress_t *trasp_layer_addr)
+{
+	unsigned char *buf;
+	int len;
+	const char *rc;
+
+	buf = trasp_layer_addr->buf;
+	len = trasp_layer_addr->size;
+
+	if (buf[0] == 0x35 && len > 7)
+		rc = inet_ntop(AF_INET, buf + 3, addr, addr_len);
+	else if (len > 3)
+		rc = inet_ntop(AF_INET, buf, addr, addr_len);
+	else
+		return -EINVAL;
+
+	if (!rc)
+		return -EINVAL;
+
+	return 0;
+}