blob: 11bafe74e204d72208095514bf78e50deeacb7c2 [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 */
Harald Welte49e3d5a2017-12-25 09:47:57 +010074 struct {
75 int initialized;
76 uint16_t base_seq;
77 uint16_t max_seq;
78 uint32_t ssrc;
79 uint32_t jitter;
80 int32_t transit;
81 int cycles;
82 } stats;
83
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020084 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
85};
86
87struct mgcp_rtp_codec {
88 uint32_t rate;
89 int channels;
90 uint32_t frame_duration_num;
91 uint32_t frame_duration_den;
92
93 int payload_type;
94 char *audio_name;
95 char *subtype_name;
96};
97
98struct mgcp_rtp_end {
99 /* statistics */
Harald Weltea0ac30f2017-12-25 09:52:30 +0100100 struct {
101 unsigned int packets_rx;
102 unsigned int octets_rx;
103 unsigned int packets_tx;
104 unsigned int octets_tx;
105 unsigned int dropped_packets;
106 } stats;
107
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108 struct in_addr addr;
109
110 /* in network byte order */
111 int rtp_port, rtcp_port;
112
113 /* audio codec information */
114 struct mgcp_rtp_codec codec;
115 struct mgcp_rtp_codec alt_codec; /* TODO/XXX: make it generic */
116
117 /* per endpoint data */
118 int frames_per_packet;
119 uint32_t packet_duration_ms;
120 char *fmtp_extra;
121 int output_enabled;
122 int force_output_ptime;
123
124 /* RTP patching */
125 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
126 int force_aligned_timing;
127 void *rtp_process_data;
128
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 struct osmo_fd rtp;
131 struct osmo_fd rtcp;
132
133 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134};
135
136struct mgcp_rtp_tap {
137 int enabled;
138 struct sockaddr_in forward;
139};
140
141struct mgcp_lco {
142 char *string;
143 char *codec;
144 int pkt_period_min; /* time in ms */
145 int pkt_period_max; /* time in ms */
146};
147
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148/* Specific rtp connection type (see struct mgcp_conn_rtp) */
149enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200151 MGCP_OSMUX_BSC,
152 MGCP_OSMUX_BSC_NAT,
153};
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155#include <osmocom/mgcp/osmux.h>
156struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158/* MGCP connection (RTP) */
159struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 /* Backpointer to conn struct */
162 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 /* Specific connection type */
165 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200166
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 /* Port status */
168 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200169
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 /* Sequence bits */
171 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200172
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 /* taps for the rtp connection */
174 struct mgcp_rtp_tap tap_in;
175 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200178 struct {
179 /* Osmux state: disabled, activating, active */
180 enum osmux_state state;
181 /* Allocated Osmux circuit ID for this endpoint */
182 int allocated_cid;
183 /* Used Osmux circuit ID for this endpoint */
184 uint8_t cid;
185 /* handle to batch messages */
186 struct osmux_in_handle *in;
187 /* handle to unbatch messages */
188 struct osmux_out_handle out;
189 /* statistics */
190 struct {
191 uint32_t chunks;
192 uint32_t octets;
193 } stats;
194 } osmux;
195};
196
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197/*! Connection type, specifies which member of the union "u" in mgcp_conn
198 * contains a useful connection description (currently only RTP) */
199enum mgcp_conn_type {
200 MGCP_CONN_TYPE_RTP,
201};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203/*! MGCP connection (untyped) */
204struct mgcp_conn {
205 /*!< list head */
206 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 /*!< Backpointer to the endpoint where the conn belongs to */
209 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 /*!< type of the connection (union) */
212 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 /*!< mode of the connection */
215 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 /*!< copy of the mode to restore the original setting (VTY) */
218 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 /*!< connection id to identify the conntion */
Philipp Maier01d24a32017-11-21 17:26:09 +0100221 char id[MGCP_CONN_ID_LENGTH];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222
Philipp Maier87bd9be2017-08-22 16:35:41 +0200223 /*!< human readable name (vty, logging) */
224 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225
Philipp Maier87bd9be2017-08-22 16:35:41 +0200226 /*!< union with connection description */
227 union {
228 struct mgcp_conn_rtp rtp;
229 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200230
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 /*!< pointer to optional private data */
232 void *priv;
233};
234
235#include <osmocom/mgcp/mgcp_conn.h>
236
237struct mgcp_endpoint_type;
238
239struct mgcp_endpoint {
240 char *callid;
241 struct mgcp_lco local_options;
242
243 struct llist_head conns;
244
245 /* backpointer */
246 struct mgcp_config *cfg;
247 struct mgcp_trunk_config *tcfg;
248
249 const struct mgcp_endpoint_type *type;
250
251 /* fields for re-transmission */
252 char *last_trans;
253 char *last_response;
254};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255
256
257#define ENDPOINT_NUMBER(endp) abs((int)(endp - endp->tcfg->endpoints))
258
259/**
260 * Internal structure while parsing a request
261 */
262struct mgcp_parse_data {
263 struct mgcp_config *cfg;
264 struct mgcp_endpoint *endp;
265 char *trans;
266 char *save;
267 int found;
268};
269
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
271 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
272 struct mgcp_conn_rtp *conn_dst);
273int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
274int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
275 unsigned int buf_size, struct mgcp_conn *conn);
276int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
277 struct mgcp_conn_rtp *conn);
278void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279
280/* For transcoding we need to manage an in and an output that are connected */
281static inline int endp_back_channel(int endpoint)
282{
283 return endpoint + 60;
284}
285
286struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
287struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
288
289void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
290 struct mgcp_rtp_end *rtp);
291uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
292 struct mgcp_rtp_end *rtp);
293
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294/* payload processing default functions */
295int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
296 char *data, int *len, int buf_size);
297
298int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
299 struct mgcp_rtp_end *dst_end,
300 struct mgcp_rtp_end *src_end);
301
302void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
303 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304 const char**audio_name,
305 const char**fmtp_extra,
306 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307
308/* internal RTP Annex A counting */
309void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
310 const uint16_t seq, const int32_t transit,
311 const uint32_t ssrc);
312
313int mgcp_set_ip_tos(int fd, int tos);
314
315enum {
316 MGCP_DEST_NET = 0,
317 MGCP_DEST_BTS,
318};
319
320
321#define MGCP_DUMMY_LOAD 0x23
322
323
324/**
325 * SDP related information
326 */
327/* Assume audio frame length of 20ms */
328#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
329#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
330#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
331#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
332#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
333
334#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335
Philipp Maier1cb1e382017-11-02 17:16:04 +0100336void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);