/*
* (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
* All Rights Reserved
*
* SPDX-License-Identifier: GPL-2.0+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/byteswap.h>
#include <string.h>
#include <errno.h>
#include <osmocom/gsm/protocol/gsm_08_08.h>
#include <osmocom/gsm/gsm48.h>
#include <osmocom/gsm/gsm0808_utils.h>
#define IP_V4_ADDR_LEN 4
#define IP_V6_ADDR_LEN 16
#define IP_PORT_LEN 2
#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
#define CHANNEL_TYPE_ELEMENT_MINLEN 3
#define ENCRYPT_INFO_ELEMENT_MINLEN 1
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#include <netinet/in.h>
/*! \addtogroup gsm0808
* @{
* \file gsm0808_utils.c
*/
/*! Encode TS 08.08 AoIP Cause IE
* \param[out] msg Message Buffer to which to append IE
* \param[in] cause Cause code to be used in IE
* \returns number of bytes added to \a msg */
uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause)
{
/* See also 3GPP TS 48.008 3.2.2.5 Cause */
uint8_t *old_tail;
bool extended;
old_tail = msg->tail;
extended = gsm0808_cause_ext(cause >> 8);
msgb_put_u8(msg, GSM0808_IE_CAUSE);
if (extended) {
msgb_put_u8(msg, 2);
msgb_put_u16(msg, cause);
} else {
msgb_put_u8(msg, 1);
msgb_put_u8(msg, (uint8_t) (cause & 0xFF));
}
return (uint8_t) (msg->tail - old_tail);
}
/*! Encode TS 08.08 AoIP transport address IE
* \param[out] msg Message Buffer to which to append IE
* \param[in] ss Socket Address to be used in IE
* \returns number of bytes added to \a msg */
uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
const 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;
uint8_t *old_tail;
uint8_t *tlv_len;
OSMO_ASSERT(msg);
OSMO_ASSERT(ss);
OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
tlv_len = msgb_put(msg,1);
old_tail = msg->tail;
switch (ss->ss_family) {
case AF_INET:
sin = (struct sockaddr_in *)ss;
port = osmo_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 = osmo_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);
*tlv_len = (uint8_t) (msg->tail - old_tail);
return *tlv_len + 2;
}
/*! Decode TS 08.08 AoIP transport address IE
* \param[out] ss Caller-provided memory where decoded socket addr is stored
* \param[in] elem pointer to IE value
* \param[in] len length of \a elem in bytes
* \returns number of bytes parsed */
int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
const uint8_t *elem, uint8_t len)
{
/* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
const uint8_t *old_elem = elem;
OSMO_ASSERT(ss);
if (!elem)
return -EINVAL;
if (len == 0)
return -EINVAL;
memset(ss, 0, sizeof(*ss));
switch (len) {
case IP_V4_ADDR_LEN + IP_PORT_LEN:
memset(&sin, 0, sizeof(sin));
sin.sin_family = A
|