blob: 0da2c56b96bb1cd54cdccfa6d41f3a25249e83d0 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* MGCP Private Data */
2
3/*
4 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2012 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#pragma once
24
25#include <string.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020026#include <inttypes.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020027#include <osmocom/core/select.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020028#include <osmocom/mgcp/mgcp.h>
29#include <osmocom/core/linuxlist.h>
Philipp Maierc430d192018-03-16 12:11:18 +010030#include <osmocom/core/counter.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020031
32#define CI_UNUSED 0
33
Philipp Maier01d24a32017-11-21 17:26:09 +010034/* FIXME: This this is only needed to compile the currently
35 * broken OSMUX support. Remove when fixed */
36#define CONN_ID_BTS "0"
37#define CONN_ID_NET "1"
Philipp Maier87bd9be2017-08-22 16:35:41 +020038
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020039enum mgcp_trunk_type {
40 MGCP_TRUNK_VIRTUAL,
41 MGCP_TRUNK_E1,
42};
43
44struct mgcp_rtp_stream_state {
45 uint32_t ssrc;
46 uint16_t last_seq;
47 uint32_t last_timestamp;
48 uint32_t err_ts_counter;
49 int32_t last_tsdelta;
50 uint32_t last_arrival_time;
51};
52
53struct mgcp_rtp_state {
Harald Welte33381352017-12-25 09:44:26 +010054 /* has this state structure been initialized? */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020055 int initialized;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056
Harald Welte33381352017-12-25 09:44:26 +010057 struct {
58 /* are we patching the SSRC value? */
59 int patch_ssrc;
60 /* original SSRC (to which we shall patch any different SSRC) */
61 uint32_t orig_ssrc;
62 /* offset to apply on the sequence number */
63 int seq_offset;
64 /* offset to apply on the timestamp number */
65 int32_t timestamp_offset;
66 } patch;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020067
Harald Welte33381352017-12-25 09:44:26 +010068 /* duration of a packet (FIXME: in which unit?) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020069 uint32_t packet_duration;
70
71 struct mgcp_rtp_stream_state in_stream;
72 struct mgcp_rtp_stream_state out_stream;
73
74 /* jitter and packet loss calculation */
Harald Welte49e3d5a2017-12-25 09:47:57 +010075 struct {
76 int initialized;
77 uint16_t base_seq;
78 uint16_t max_seq;
79 uint32_t ssrc;
80 uint32_t jitter;
81 int32_t transit;
82 int cycles;
83 } stats;
84
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
86};
87
88struct mgcp_rtp_codec {
89 uint32_t rate;
90 int channels;
91 uint32_t frame_duration_num;
92 uint32_t frame_duration_den;
93
94 int payload_type;
95 char *audio_name;
96 char *subtype_name;
97};
98
Harald Welte3f35a372017-12-26 15:48:46 +010099/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200100struct mgcp_rtp_end {
101 /* statistics */
Harald Weltea0ac30f2017-12-25 09:52:30 +0100102 struct {
103 unsigned int packets_rx;
104 unsigned int octets_rx;
105 unsigned int packets_tx;
106 unsigned int octets_tx;
107 unsigned int dropped_packets;
108 } stats;
109
Harald Welte3f35a372017-12-26 15:48:46 +0100110 /* local IP address of the RTP socket */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111 struct in_addr addr;
112
113 /* in network byte order */
114 int rtp_port, rtcp_port;
115
116 /* audio codec information */
117 struct mgcp_rtp_codec codec;
118 struct mgcp_rtp_codec alt_codec; /* TODO/XXX: make it generic */
119
120 /* per endpoint data */
121 int frames_per_packet;
122 uint32_t packet_duration_ms;
123 char *fmtp_extra;
Harald Welte3f35a372017-12-26 15:48:46 +0100124 /* are we transmitting packets (1) or dropping (0) outbound packets */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125 int output_enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100126 /* FIXME: This parameter can be set + printed, but is nowhere used! */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127 int force_output_ptime;
128
129 /* RTP patching */
130 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
Harald Welte3f35a372017-12-26 15:48:46 +0100131 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132 int force_aligned_timing;
Harald Welte3f35a372017-12-26 15:48:46 +0100133 /* FIXME: not used anymore, used to be [external] transcoding related */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 void *rtp_process_data;
135
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137 struct osmo_fd rtp;
138 struct osmo_fd rtcp;
139
Harald Welte3f35a372017-12-26 15:48:46 +0100140 /* local UDP port number of the RTP socket; RTCP is +1 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200141 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142};
143
144struct mgcp_rtp_tap {
Harald Welte3f35a372017-12-26 15:48:46 +0100145 /* is this tap active (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146 int enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100147 /* IP/port to which we're forwarding the tapped data */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148 struct sockaddr_in forward;
149};
150
151struct mgcp_lco {
152 char *string;
153 char *codec;
154 int pkt_period_min; /* time in ms */
155 int pkt_period_max; /* time in ms */
156};
157
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158/* Specific rtp connection type (see struct mgcp_conn_rtp) */
159enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200161 MGCP_OSMUX_BSC,
162 MGCP_OSMUX_BSC_NAT,
163};
164
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165#include <osmocom/mgcp/osmux.h>
166struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200167
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168/* MGCP connection (RTP) */
169struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 /* Backpointer to conn struct */
172 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200173
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 /* Specific connection type */
175 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 /* Port status */
178 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200179
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180 /* Sequence bits */
181 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182
Harald Welte3f35a372017-12-26 15:48:46 +0100183 /* taps for the rtp connection; one per direction */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184 struct mgcp_rtp_tap tap_in;
185 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200186
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188 struct {
189 /* Osmux state: disabled, activating, active */
190 enum osmux_state state;
191 /* Allocated Osmux circuit ID for this endpoint */
192 int allocated_cid;
193 /* Used Osmux circuit ID for this endpoint */
194 uint8_t cid;
195 /* handle to batch messages */
196 struct osmux_in_handle *in;
197 /* handle to unbatch messages */
198 struct osmux_out_handle out;
199 /* statistics */
200 struct {
201 uint32_t chunks;
202 uint32_t octets;
203 } stats;
204 } osmux;
205};
206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207/*! Connection type, specifies which member of the union "u" in mgcp_conn
208 * contains a useful connection description (currently only RTP) */
209enum mgcp_conn_type {
210 MGCP_CONN_TYPE_RTP,
211};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213/*! MGCP connection (untyped) */
214struct mgcp_conn {
215 /*!< list head */
216 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 /*!< Backpointer to the endpoint where the conn belongs to */
219 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220
Philipp Maier87bd9be2017-08-22 16:35:41 +0200221 /*!< type of the connection (union) */
222 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200223
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224 /*!< mode of the connection */
225 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 /*!< copy of the mode to restore the original setting (VTY) */
228 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
Harald Welte1d1b98f2017-12-25 10:03:40 +0100230 /*!< connection id to identify the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100231 char id[MGCP_CONN_ID_LENGTH];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 /*!< human readable name (vty, logging) */
234 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 /*!< union with connection description */
237 union {
238 struct mgcp_conn_rtp rtp;
239 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 /*!< pointer to optional private data */
242 void *priv;
243};
244
245#include <osmocom/mgcp/mgcp_conn.h>
246
247struct mgcp_endpoint_type;
248
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249
250
Philipp Maierfdd603c2018-02-01 13:31:15 +0100251
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252
253/**
254 * Internal structure while parsing a request
255 */
256struct mgcp_parse_data {
257 struct mgcp_config *cfg;
258 struct mgcp_endpoint *endp;
259 char *trans;
260 char *save;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261};
262
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
264 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
265 struct mgcp_conn_rtp *conn_dst);
266int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
267int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
268 unsigned int buf_size, struct mgcp_conn *conn);
Philipp Maierdf5d2192018-01-24 11:39:32 +0100269void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
271 struct mgcp_conn_rtp *conn);
272void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273
274/* For transcoding we need to manage an in and an output that are connected */
275static inline int endp_back_channel(int endpoint)
276{
277 return endpoint + 60;
278}
279
280struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
281struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
282
283void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
284 struct mgcp_rtp_end *rtp);
285uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
286 struct mgcp_rtp_end *rtp);
287
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288/* payload processing default functions */
289int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
290 char *data, int *len, int buf_size);
291
292int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
293 struct mgcp_rtp_end *dst_end,
294 struct mgcp_rtp_end *src_end);
295
296void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
297 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298 const char**audio_name,
299 const char**fmtp_extra,
300 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301
302/* internal RTP Annex A counting */
303void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
304 const uint16_t seq, const int32_t transit,
305 const uint32_t ssrc);
306
307int mgcp_set_ip_tos(int fd, int tos);
308
309enum {
310 MGCP_DEST_NET = 0,
311 MGCP_DEST_BTS,
312};
313
314
315#define MGCP_DUMMY_LOAD 0x23
316
317
318/**
319 * SDP related information
320 */
321/* Assume audio frame length of 20ms */
322#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
323#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
324#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
325#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
326#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
327
328#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier1cb1e382017-11-02 17:16:04 +0100330void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);