blob: 8d82b149862599d45c34c3a27c8bde1144fc79d9 [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>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030
31#define CI_UNUSED 0
32
Philipp Maier01d24a32017-11-21 17:26:09 +010033/* FIXME: This this is only needed to compile the currently
34 * broken OSMUX support. Remove when fixed */
35#define CONN_ID_BTS "0"
36#define CONN_ID_NET "1"
Philipp Maier87bd9be2017-08-22 16:35:41 +020037
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020038enum mgcp_trunk_type {
39 MGCP_TRUNK_VIRTUAL,
40 MGCP_TRUNK_E1,
41};
42
43struct mgcp_rtp_stream_state {
44 uint32_t ssrc;
45 uint16_t last_seq;
46 uint32_t last_timestamp;
47 uint32_t err_ts_counter;
48 int32_t last_tsdelta;
49 uint32_t last_arrival_time;
50};
51
52struct mgcp_rtp_state {
Harald Welte33381352017-12-25 09:44:26 +010053 /* has this state structure been initialized? */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020054 int initialized;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020055
Harald Welte33381352017-12-25 09:44:26 +010056 struct {
57 /* are we patching the SSRC value? */
58 int patch_ssrc;
59 /* original SSRC (to which we shall patch any different SSRC) */
60 uint32_t orig_ssrc;
61 /* offset to apply on the sequence number */
62 int seq_offset;
63 /* offset to apply on the timestamp number */
64 int32_t timestamp_offset;
65 } patch;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020066
Harald Welte33381352017-12-25 09:44:26 +010067 /* duration of a packet (FIXME: in which unit?) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020068 uint32_t packet_duration;
69
70 struct mgcp_rtp_stream_state in_stream;
71 struct mgcp_rtp_stream_state out_stream;
72
73 /* jitter and packet loss calculation */
74 int stats_initialized;
75 uint16_t stats_base_seq;
76 uint16_t stats_max_seq;
77 uint32_t stats_ssrc;
78 uint32_t stats_jitter;
79 int32_t stats_transit;
80 int stats_cycles;
81 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
82};
83
84struct mgcp_rtp_codec {
85 uint32_t rate;
86 int channels;
87 uint32_t frame_duration_num;
88 uint32_t frame_duration_den;
89
90 int payload_type;
91 char *audio_name;
92 char *subtype_name;
93};
94
95struct mgcp_rtp_end {
96 /* statistics */
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 unsigned int packets_rx;
98 unsigned int octets_rx;
99 unsigned int packets_tx;
100 unsigned int octets_tx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200101 unsigned int dropped_packets;
102 struct in_addr addr;
103
104 /* in network byte order */
105 int rtp_port, rtcp_port;
106
107 /* audio codec information */
108 struct mgcp_rtp_codec codec;
109 struct mgcp_rtp_codec alt_codec; /* TODO/XXX: make it generic */
110
111 /* per endpoint data */
112 int frames_per_packet;
113 uint32_t packet_duration_ms;
114 char *fmtp_extra;
115 int output_enabled;
116 int force_output_ptime;
117
118 /* RTP patching */
119 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
120 int force_aligned_timing;
121 void *rtp_process_data;
122
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200124 struct osmo_fd rtp;
125 struct osmo_fd rtcp;
126
127 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128};
129
130struct mgcp_rtp_tap {
131 int enabled;
132 struct sockaddr_in forward;
133};
134
135struct mgcp_lco {
136 char *string;
137 char *codec;
138 int pkt_period_min; /* time in ms */
139 int pkt_period_max; /* time in ms */
140};
141
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142/* Specific rtp connection type (see struct mgcp_conn_rtp) */
143enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145 MGCP_OSMUX_BSC,
146 MGCP_OSMUX_BSC_NAT,
147};
148
Philipp Maier87bd9be2017-08-22 16:35:41 +0200149#include <osmocom/mgcp/osmux.h>
150struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200151
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152/* MGCP connection (RTP) */
153struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 /* Backpointer to conn struct */
156 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 /* Specific connection type */
159 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 /* Port status */
162 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 /* Sequence bits */
165 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200166
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 /* taps for the rtp connection */
168 struct mgcp_rtp_tap tap_in;
169 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200172 struct {
173 /* Osmux state: disabled, activating, active */
174 enum osmux_state state;
175 /* Allocated Osmux circuit ID for this endpoint */
176 int allocated_cid;
177 /* Used Osmux circuit ID for this endpoint */
178 uint8_t cid;
179 /* handle to batch messages */
180 struct osmux_in_handle *in;
181 /* handle to unbatch messages */
182 struct osmux_out_handle out;
183 /* statistics */
184 struct {
185 uint32_t chunks;
186 uint32_t octets;
187 } stats;
188 } osmux;
189};
190
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191/*! Connection type, specifies which member of the union "u" in mgcp_conn
192 * contains a useful connection description (currently only RTP) */
193enum mgcp_conn_type {
194 MGCP_CONN_TYPE_RTP,
195};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197/*! MGCP connection (untyped) */
198struct mgcp_conn {
199 /*!< list head */
200 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 /*!< Backpointer to the endpoint where the conn belongs to */
203 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205 /*!< type of the connection (union) */
206 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 /*!< mode of the connection */
209 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 /*!< copy of the mode to restore the original setting (VTY) */
212 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 /*!< connection id to identify the conntion */
Philipp Maier01d24a32017-11-21 17:26:09 +0100215 char id[MGCP_CONN_ID_LENGTH];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 /*!< human readable name (vty, logging) */
218 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 /*!< union with connection description */
221 union {
222 struct mgcp_conn_rtp rtp;
223 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 /*!< pointer to optional private data */
226 void *priv;
227};
228
229#include <osmocom/mgcp/mgcp_conn.h>
230
231struct mgcp_endpoint_type;
232
233struct mgcp_endpoint {
234 char *callid;
235 struct mgcp_lco local_options;
236
237 struct llist_head conns;
238
239 /* backpointer */
240 struct mgcp_config *cfg;
241 struct mgcp_trunk_config *tcfg;
242
243 const struct mgcp_endpoint_type *type;
244
245 /* fields for re-transmission */
246 char *last_trans;
247 char *last_response;
248};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249
250
251#define ENDPOINT_NUMBER(endp) abs((int)(endp - endp->tcfg->endpoints))
252
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;
261 int found;
262};
263
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
265 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
266 struct mgcp_conn_rtp *conn_dst);
267int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
268int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
269 unsigned int buf_size, struct mgcp_conn *conn);
270int 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);