blob: c3f9ba1c8e4d5ce3f9356559d1cc658a336a4ddd [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 {
53 int initialized;
54 int patch_ssrc;
55
56 uint32_t orig_ssrc;
57
58 int seq_offset;
59
60 int32_t timestamp_offset;
61 uint32_t packet_duration;
62
63 struct mgcp_rtp_stream_state in_stream;
64 struct mgcp_rtp_stream_state out_stream;
65
66 /* jitter and packet loss calculation */
67 int stats_initialized;
68 uint16_t stats_base_seq;
69 uint16_t stats_max_seq;
70 uint32_t stats_ssrc;
71 uint32_t stats_jitter;
72 int32_t stats_transit;
73 int stats_cycles;
74 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
75};
76
77struct mgcp_rtp_codec {
78 uint32_t rate;
79 int channels;
80 uint32_t frame_duration_num;
81 uint32_t frame_duration_den;
82
83 int payload_type;
84 char *audio_name;
85 char *subtype_name;
86};
87
88struct mgcp_rtp_end {
89 /* statistics */
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 unsigned int packets_rx;
91 unsigned int octets_rx;
92 unsigned int packets_tx;
93 unsigned int octets_tx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020094 unsigned int dropped_packets;
95 struct in_addr addr;
96
97 /* in network byte order */
98 int rtp_port, rtcp_port;
99
100 /* audio codec information */
101 struct mgcp_rtp_codec codec;
102 struct mgcp_rtp_codec alt_codec; /* TODO/XXX: make it generic */
103
104 /* per endpoint data */
105 int frames_per_packet;
106 uint32_t packet_duration_ms;
107 char *fmtp_extra;
108 int output_enabled;
109 int force_output_ptime;
110
111 /* RTP patching */
112 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
113 int force_aligned_timing;
114 void *rtp_process_data;
115
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200117 struct osmo_fd rtp;
118 struct osmo_fd rtcp;
119
120 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121};
122
123struct mgcp_rtp_tap {
124 int enabled;
125 struct sockaddr_in forward;
126};
127
128struct mgcp_lco {
129 char *string;
130 char *codec;
131 int pkt_period_min; /* time in ms */
132 int pkt_period_max; /* time in ms */
133};
134
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135/* Specific rtp connection type (see struct mgcp_conn_rtp) */
136enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200138 MGCP_OSMUX_BSC,
139 MGCP_OSMUX_BSC_NAT,
140};
141
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142#include <osmocom/mgcp/osmux.h>
143struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145/* MGCP connection (RTP) */
146struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200147
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148 /* Backpointer to conn struct */
149 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151 /* Specific connection type */
152 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153
Philipp Maier87bd9be2017-08-22 16:35:41 +0200154 /* Port status */
155 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157 /* Sequence bits */
158 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 /* taps for the rtp connection */
161 struct mgcp_rtp_tap tap_in;
162 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165 struct {
166 /* Osmux state: disabled, activating, active */
167 enum osmux_state state;
168 /* Allocated Osmux circuit ID for this endpoint */
169 int allocated_cid;
170 /* Used Osmux circuit ID for this endpoint */
171 uint8_t cid;
172 /* handle to batch messages */
173 struct osmux_in_handle *in;
174 /* handle to unbatch messages */
175 struct osmux_out_handle out;
176 /* statistics */
177 struct {
178 uint32_t chunks;
179 uint32_t octets;
180 } stats;
181 } osmux;
182};
183
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184/*! Connection type, specifies which member of the union "u" in mgcp_conn
185 * contains a useful connection description (currently only RTP) */
186enum mgcp_conn_type {
187 MGCP_CONN_TYPE_RTP,
188};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190/*! MGCP connection (untyped) */
191struct mgcp_conn {
192 /*!< list head */
193 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195 /*!< Backpointer to the endpoint where the conn belongs to */
196 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198 /*!< type of the connection (union) */
199 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 /*!< mode of the connection */
202 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204 /*!< copy of the mode to restore the original setting (VTY) */
205 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 /*!< connection id to identify the conntion */
Philipp Maier01d24a32017-11-21 17:26:09 +0100208 char id[MGCP_CONN_ID_LENGTH];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210 /*!< human readable name (vty, logging) */
211 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213 /*!< union with connection description */
214 union {
215 struct mgcp_conn_rtp rtp;
216 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 /*!< pointer to optional private data */
219 void *priv;
220};
221
222#include <osmocom/mgcp/mgcp_conn.h>
223
224struct mgcp_endpoint_type;
225
226struct mgcp_endpoint {
227 char *callid;
228 struct mgcp_lco local_options;
229
230 struct llist_head conns;
231
232 /* backpointer */
233 struct mgcp_config *cfg;
234 struct mgcp_trunk_config *tcfg;
235
236 const struct mgcp_endpoint_type *type;
237
238 /* fields for re-transmission */
239 char *last_trans;
240 char *last_response;
241};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242
243
244#define ENDPOINT_NUMBER(endp) abs((int)(endp - endp->tcfg->endpoints))
245
246/**
247 * Internal structure while parsing a request
248 */
249struct mgcp_parse_data {
250 struct mgcp_config *cfg;
251 struct mgcp_endpoint *endp;
252 char *trans;
253 char *save;
254 int found;
255};
256
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
258 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
259 struct mgcp_conn_rtp *conn_dst);
260int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
261int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
262 unsigned int buf_size, struct mgcp_conn *conn);
263int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
264 struct mgcp_conn_rtp *conn);
265void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266
267/* For transcoding we need to manage an in and an output that are connected */
268static inline int endp_back_channel(int endpoint)
269{
270 return endpoint + 60;
271}
272
273struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
274struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
275
276void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
277 struct mgcp_rtp_end *rtp);
278uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
279 struct mgcp_rtp_end *rtp);
280
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281/* payload processing default functions */
282int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
283 char *data, int *len, int buf_size);
284
285int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
286 struct mgcp_rtp_end *dst_end,
287 struct mgcp_rtp_end *src_end);
288
289void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
290 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 const char**audio_name,
292 const char**fmtp_extra,
293 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294
295/* internal RTP Annex A counting */
296void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
297 const uint16_t seq, const int32_t transit,
298 const uint32_t ssrc);
299
300int mgcp_set_ip_tos(int fd, int tos);
301
302enum {
303 MGCP_DEST_NET = 0,
304 MGCP_DEST_BTS,
305};
306
307
308#define MGCP_DUMMY_LOAD 0x23
309
310
311/**
312 * SDP related information
313 */
314/* Assume audio frame length of 20ms */
315#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
316#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
317#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
318#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
319#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
320
321#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322
Philipp Maier1cb1e382017-11-02 17:16:04 +0100323void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);