2012-07-09 17:39:55 +00:00
|
|
|
/*
|
2017-11-12 16:21:03 +00:00
|
|
|
* (C) 2012-2017 by Pablo Neira Ayuso <pablo@gnumonks.org>
|
2012-07-09 17:39:55 +00:00
|
|
|
* (C) 2012 by On Waves ehf <http://www.on-waves.com>
|
2022-09-29 10:55:56 +00:00
|
|
|
* (C) 2015-2022 by sysmocom - s.f.m.c. GmbH
|
2017-11-12 16:21:03 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-07-09 17:39:55 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
2018-04-18 11:58:46 +00:00
|
|
|
#include <osmocom/core/timer_compat.h>
|
2012-07-09 17:39:55 +00:00
|
|
|
#include <osmocom/core/select.h>
|
2012-07-19 09:08:21 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
2012-07-19 11:04:39 +00:00
|
|
|
#include <osmocom/core/logging.h>
|
2012-07-09 17:39:55 +00:00
|
|
|
|
|
|
|
#include <osmocom/netif/amr.h>
|
|
|
|
#include <osmocom/netif/rtp.h>
|
|
|
|
#include <osmocom/netif/osmux.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2017-04-08 18:00:47 +00:00
|
|
|
/*! \addtogroup osmux Osmocom Multiplex Protocol
|
|
|
|
* @{
|
2017-10-27 17:57:37 +00:00
|
|
|
*
|
|
|
|
* This code implements a variety of utility functions related to the
|
|
|
|
* OSMUX user-plane multiplexing protocol, an efficient alternative to
|
|
|
|
* plain UDP/RTP streams for voice transport in back-haul of cellular
|
|
|
|
* networks.
|
|
|
|
*
|
|
|
|
* For information about the OSMUX protocol design, please see the
|
|
|
|
* OSMUX reference manual at
|
|
|
|
* http://ftp.osmocom.org/docs/latest/osmux-reference.pdf
|
2017-04-08 18:00:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file osmux.c
|
|
|
|
* \brief Osmocom multiplex protocol helpers
|
|
|
|
*/
|
|
|
|
|
2013-02-19 16:14:33 +00:00
|
|
|
static uint32_t osmux_get_payload_len(struct osmux_hdr *osmuxh)
|
|
|
|
{
|
|
|
|
return osmo_amr_bytes(osmuxh->amr_ft) * (osmuxh->ctr+1);
|
|
|
|
}
|
|
|
|
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
#define SNPRINTF_BUFFER_SIZE(ret, remain, offset) \
|
|
|
|
if (ret < 0) \
|
|
|
|
ret = 0; \
|
2013-02-19 16:14:33 +00:00
|
|
|
offset += ret; \
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
if (ret > remain) \
|
|
|
|
ret = remain; \
|
|
|
|
remain -= ret;
|
2013-02-19 16:14:33 +00:00
|
|
|
|
|
|
|
static int osmux_snprintf_header(char *buf, size_t size, struct osmux_hdr *osmuxh)
|
|
|
|
{
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
unsigned int remain = size, offset = 0;
|
2013-02-19 16:14:33 +00:00
|
|
|
int ret;
|
|
|
|
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
ret = snprintf(buf, remain, "OSMUX seq=%03u ccid=%03u "
|
2022-09-28 12:58:51 +00:00
|
|
|
"ft=%01u rtp_m=%01u ctr=%01u "
|
2013-02-19 16:14:33 +00:00
|
|
|
"amr_f=%01u amr_q=%01u "
|
2019-10-04 10:42:26 +00:00
|
|
|
"amr_ft=%02u amr_cmr=%02u",
|
2013-02-19 16:14:33 +00:00
|
|
|
osmuxh->seq, osmuxh->circuit_id,
|
2022-09-28 12:58:51 +00:00
|
|
|
osmuxh->ft, osmuxh->rtp_m, osmuxh->ctr,
|
2013-02-19 16:14:33 +00:00
|
|
|
osmuxh->amr_f, osmuxh->amr_q,
|
|
|
|
osmuxh->amr_ft, osmuxh->amr_cmr);
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2013-02-19 16:14:33 +00:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int osmux_snprintf_payload(char *buf, size_t size,
|
|
|
|
const uint8_t *payload, int payload_len)
|
|
|
|
{
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
unsigned int remain = size, offset = 0;
|
2013-02-19 16:14:33 +00:00
|
|
|
int ret, i;
|
|
|
|
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
ret = snprintf(buf + offset, remain, "[ ");
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2017-08-14 15:23:05 +00:00
|
|
|
|
2013-02-19 16:14:33 +00:00
|
|
|
for (i=0; i<payload_len; i++) {
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
ret = snprintf(buf + offset, remain, "%02x ", payload[i]);
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2013-02-19 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
ret = snprintf(buf + offset, remain, "]");
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2013-02-19 16:14:33 +00:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-10-23 15:42:10 +00:00
|
|
|
/*! Print osmux header fields and payload from msg into buffer buf.
|
|
|
|
* \param[out] buf buffer to store the output into
|
|
|
|
* \param[in] len length of buf in bytes
|
|
|
|
* \param[in] msgb message buffer containing one or more osmux frames
|
|
|
|
* \returns the number of characters printed (excluding the null byte used to end output to strings).
|
|
|
|
*
|
|
|
|
* If the output was truncated due to this limit, then the return value is the number of characters
|
|
|
|
* (excluding the terminating null byte) which would have been written to the final string if enough
|
|
|
|
* space had been available.
|
|
|
|
*/
|
2013-02-19 16:14:33 +00:00
|
|
|
int osmux_snprintf(char *buf, size_t size, struct msgb *msg)
|
|
|
|
{
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
unsigned int remain = size;
|
2017-10-23 14:27:13 +00:00
|
|
|
unsigned int msg_off = 0;
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
struct osmux_hdr *osmuxh;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
int msg_len = msg->len;
|
2017-10-23 14:27:13 +00:00
|
|
|
uint32_t payload_len;
|
osmux: fix buffer management mess in snprintf() calls
SNPRINTF_BUFFER_SIZE() looks too complex, previous version maintains two
different variables to account for the remaining space in the buffer,
one of them is always decremented based on what snprintf() returns,
which may result in underflow. These variables are swapped - not used
consistently - all over this code.
Replace this macro by a simplified version, with one single parameter to
account for remaining space. This macro also deals with two corner
cases:
1) snprintf() fails, actually never happens in practise, but
documentation indicates it may return -1, so let's catch this case
from here to stick to specs.
2) There is not enough space in the buffer, in that case, keep
increasing offset, so we know how much would have been printed, just
like snprintf() does.
Thanks to Pau Espin for reporting, and Holger for clues on this.
I have run osmux_test and, at quick glance, it looks good.
Change-Id: I5b5d6ec57a02f57c23b1ae86dbd894bad28ea797
2017-09-04 18:35:36 +00:00
|
|
|
int ret;
|
2013-02-19 16:14:33 +00:00
|
|
|
|
2017-09-12 10:44:53 +00:00
|
|
|
if (size)
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
2013-02-19 16:14:33 +00:00
|
|
|
while (msg_len > 0) {
|
2013-05-12 18:43:26 +00:00
|
|
|
if (msg_len < sizeof(struct osmux_hdr)) {
|
2018-03-07 12:13:17 +00:00
|
|
|
LOGP(DLMUX, LOGL_ERROR,
|
2013-05-12 18:43:26 +00:00
|
|
|
"No room for OSMUX header: only %d bytes\n",
|
|
|
|
msg_len);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-13 14:27:21 +00:00
|
|
|
osmuxh = (struct osmux_hdr *)((uint8_t *)msg->data + msg_off);
|
2019-10-04 10:30:32 +00:00
|
|
|
if (msg_off) {
|
|
|
|
ret = snprintf(buf + offset, remain, ", ");
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
|
|
|
}
|
2017-10-23 14:27:13 +00:00
|
|
|
ret = osmux_snprintf_header(buf + offset, remain, osmuxh);
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
|
|
|
|
|
|
|
msg_off += sizeof(struct osmux_hdr);
|
|
|
|
msg_len -= sizeof(struct osmux_hdr);
|
|
|
|
|
2017-10-23 15:29:13 +00:00
|
|
|
switch (osmuxh->ft) {
|
|
|
|
case OSMUX_FT_SIGNAL:
|
|
|
|
ret = snprintf(buf + offset, remain, "[signal]");
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2013-12-14 21:32:09 +00:00
|
|
|
return -1;
|
2017-10-23 15:29:13 +00:00
|
|
|
case OSMUX_FT_DUMMY:
|
|
|
|
case OSMUX_FT_VOICE_AMR:
|
|
|
|
if (!osmo_amr_ft_valid(osmuxh->amr_ft)) {
|
2018-03-07 12:13:17 +00:00
|
|
|
LOGP(DLMUX, LOGL_ERROR, "Bad AMR FT %d, skipping\n",
|
2017-10-23 15:29:13 +00:00
|
|
|
osmuxh->amr_ft);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-14 21:32:09 +00:00
|
|
|
|
2017-10-23 15:29:13 +00:00
|
|
|
payload_len = osmux_get_payload_len(osmuxh);
|
2013-05-12 18:43:26 +00:00
|
|
|
|
2017-10-23 15:29:13 +00:00
|
|
|
if (msg_len < payload_len) {
|
2018-03-07 12:13:17 +00:00
|
|
|
LOGP(DLMUX, LOGL_ERROR,
|
2017-10-23 15:29:13 +00:00
|
|
|
"No room for OSMUX payload: only %d bytes\n",
|
|
|
|
msg_len);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-12 18:43:26 +00:00
|
|
|
|
2017-10-23 15:29:13 +00:00
|
|
|
if (osmuxh->ft == OSMUX_FT_VOICE_AMR) {
|
2019-10-04 10:42:26 +00:00
|
|
|
ret = snprintf(buf + offset, remain, " ");
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
2017-10-23 15:29:13 +00:00
|
|
|
ret = osmux_snprintf_payload(buf + offset, remain,
|
|
|
|
osmux_get_payload(osmuxh),
|
|
|
|
payload_len);
|
|
|
|
SNPRINTF_BUFFER_SIZE(ret, remain, offset);
|
|
|
|
}
|
2013-02-19 16:14:33 +00:00
|
|
|
|
2017-10-23 15:29:13 +00:00
|
|
|
msg_off += payload_len;
|
|
|
|
msg_len -= payload_len;
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 12:13:17 +00:00
|
|
|
LOGP(DLMUX, LOGL_ERROR, "Unknown OSMUX ft value %d\n",
|
2017-10-23 15:29:13 +00:00
|
|
|
osmuxh->ft);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-19 16:14:33 +00:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2017-04-08 18:00:47 +00:00
|
|
|
|
|
|
|
/*! @} */
|