blob: b9c173189d3c79fdab0036b1b51e0b0099a4b11a [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 Maier87bd9be2017-08-22 16:35:41 +020033#define CONN_ID_BTS 0
34#define CONN_ID_NET 1
35
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036enum mgcp_trunk_type {
37 MGCP_TRUNK_VIRTUAL,
38 MGCP_TRUNK_E1,
39};
40
41struct mgcp_rtp_stream_state {
42 uint32_t ssrc;
43 uint16_t last_seq;
44 uint32_t last_timestamp;
45 uint32_t err_ts_counter;
46 int32_t last_tsdelta;
47 uint32_t last_arrival_time;
48};
49
50struct mgcp_rtp_state {
51 int initialized;
52 int patch_ssrc;
53
54 uint32_t orig_ssrc;
55
56 int seq_offset;
57
58 int32_t timestamp_offset;
59 uint32_t packet_duration;
60
61 struct mgcp_rtp_stream_state in_stream;
62 struct mgcp_rtp_stream_state out_stream;
63
64 /* jitter and packet loss calculation */
65 int stats_initialized;
66 uint16_t stats_base_seq;
67 uint16_t stats_max_seq;
68 uint32_t stats_ssrc;
69 uint32_t stats_jitter;
70 int32_t stats_transit;
71 int stats_cycles;
72 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
73};
74
75struct mgcp_rtp_codec {
76 uint32_t rate;
77 int channels;
78 uint32_t frame_duration_num;
79 uint32_t frame_duration_den;
80
81 int payload_type;
82 char *audio_name;
83 char *subtype_name;
84};
85
86struct mgcp_rtp_end {
87 /* statistics */
Philipp Maier87bd9be2017-08-22 16:35:41 +020088 unsigned int packets_rx;
89 unsigned int octets_rx;
90 unsigned int packets_tx;
91 unsigned int octets_tx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020092 unsigned int dropped_packets;
93 struct in_addr addr;
94
95 /* in network byte order */
96 int rtp_port, rtcp_port;
97
98 /* audio codec information */
99 struct mgcp_rtp_codec codec;
100 struct mgcp_rtp_codec alt_codec; /* TODO/XXX: make it generic */
101
102 /* per endpoint data */
103 int frames_per_packet;
104 uint32_t packet_duration_ms;
105 char *fmtp_extra;
106 int output_enabled;
107 int force_output_ptime;
108
109 /* RTP patching */
110 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
111 int force_aligned_timing;
112 void *rtp_process_data;
113
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200115 struct osmo_fd rtp;
116 struct osmo_fd rtcp;
117
118 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119};
120
121struct mgcp_rtp_tap {
122 int enabled;
123 struct sockaddr_in forward;
124};
125
126struct mgcp_lco {
127 char *string;
128 char *codec;
129 int pkt_period_min; /* time in ms */
130 int pkt_period_max; /* time in ms */
131};
132
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133/* Specific rtp connection type (see struct mgcp_conn_rtp) */
134enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136 MGCP_OSMUX_BSC,
137 MGCP_OSMUX_BSC_NAT,
138};
139
Philipp Maier87bd9be2017-08-22 16:35:41 +0200140#include <osmocom/mgcp/osmux.h>
141struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143/* MGCP connection (RTP) */
144struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146 /* Backpointer to conn struct */
147 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148
Philipp Maier87bd9be2017-08-22 16:35:41 +0200149 /* Specific connection type */
150 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200151
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152 /* Port status */
153 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 /* Sequence bits */
156 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 /* taps for the rtp connection */
159 struct mgcp_rtp_tap tap_in;
160 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200161
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163 struct {
164 /* Osmux state: disabled, activating, active */
165 enum osmux_state state;
166 /* Allocated Osmux circuit ID for this endpoint */
167 int allocated_cid;
168 /* Used Osmux circuit ID for this endpoint */
169 uint8_t cid;
170 /* handle to batch messages */
171 struct osmux_in_handle *in;
172 /* handle to unbatch messages */
173 struct osmux_out_handle out;
174 /* statistics */
175 struct {
176 uint32_t chunks;
177 uint32_t octets;
178 } stats;
179 } osmux;
180};
181
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182/*! Connection type, specifies which member of the union "u" in mgcp_conn
183 * contains a useful connection description (currently only RTP) */
184enum mgcp_conn_type {
185 MGCP_CONN_TYPE_RTP,
186};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188/*! MGCP connection (untyped) */
189struct mgcp_conn {
190 /*!< list head */
191 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200192
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 /*!< Backpointer to the endpoint where the conn belongs to */
194 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 /*!< type of the connection (union) */
197 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198
Philipp Maier87bd9be2017-08-22 16:35:41 +0200199 /*!< mode of the connection */
200 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 /*!< copy of the mode to restore the original setting (VTY) */
203 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205 /*!< connection id to identify the conntion */
206 uint32_t id;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 /*!< human readable name (vty, logging) */
209 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 /*!< union with connection description */
212 union {
213 struct mgcp_conn_rtp rtp;
214 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 /*!< pointer to optional private data */
217 void *priv;
218};
219
220#include <osmocom/mgcp/mgcp_conn.h>
221
222struct mgcp_endpoint_type;
223
224struct mgcp_endpoint {
225 char *callid;
226 struct mgcp_lco local_options;
227
228 struct llist_head conns;
229
230 /* backpointer */
231 struct mgcp_config *cfg;
232 struct mgcp_trunk_config *tcfg;
233
234 const struct mgcp_endpoint_type *type;
235
236 /* fields for re-transmission */
237 char *last_trans;
238 char *last_response;
239};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240
241
242#define ENDPOINT_NUMBER(endp) abs((int)(endp - endp->tcfg->endpoints))
243
244/**
245 * Internal structure while parsing a request
246 */
247struct mgcp_parse_data {
248 struct mgcp_config *cfg;
249 struct mgcp_endpoint *endp;
250 char *trans;
251 char *save;
252 int found;
253};
254
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
256 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
257 struct mgcp_conn_rtp *conn_dst);
258int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
259int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
260 unsigned int buf_size, struct mgcp_conn *conn);
261int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
262 struct mgcp_conn_rtp *conn);
263void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264
265/* For transcoding we need to manage an in and an output that are connected */
266static inline int endp_back_channel(int endpoint)
267{
268 return endpoint + 60;
269}
270
271struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
272struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
273
274void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
275 struct mgcp_rtp_end *rtp);
276uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
277 struct mgcp_rtp_end *rtp);
278
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279/* payload processing default functions */
280int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
281 char *data, int *len, int buf_size);
282
283int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
284 struct mgcp_rtp_end *dst_end,
285 struct mgcp_rtp_end *src_end);
286
287void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
288 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200289 const char**audio_name,
290 const char**fmtp_extra,
291 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292
293/* internal RTP Annex A counting */
294void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
295 const uint16_t seq, const int32_t transit,
296 const uint32_t ssrc);
297
298int mgcp_set_ip_tos(int fd, int tos);
299
300enum {
301 MGCP_DEST_NET = 0,
302 MGCP_DEST_BTS,
303};
304
305
306#define MGCP_DUMMY_LOAD 0x23
307
308
309/**
310 * SDP related information
311 */
312/* Assume audio frame length of 20ms */
313#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
314#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
315#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
316#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
317#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
318
319#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320
Philipp Maier1cb1e382017-11-02 17:16:04 +0100321void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);