Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 26 | #include <inttypes.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 27 | #include <osmocom/core/select.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp.h> |
| 29 | #include <osmocom/core/linuxlist.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 30 | |
| 31 | #define CI_UNUSED 0 |
| 32 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 33 | /* 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 38 | enum mgcp_trunk_type { |
| 39 | MGCP_TRUNK_VIRTUAL, |
| 40 | MGCP_TRUNK_E1, |
| 41 | }; |
| 42 | |
| 43 | struct 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 | |
| 52 | struct 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 | |
| 77 | struct 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 | |
| 88 | struct mgcp_rtp_end { |
| 89 | /* statistics */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 90 | unsigned int packets_rx; |
| 91 | unsigned int octets_rx; |
| 92 | unsigned int packets_tx; |
| 93 | unsigned int octets_tx; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 94 | 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 116 | /* Each end has a separate socket for RTP and RTCP */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 117 | struct osmo_fd rtp; |
| 118 | struct osmo_fd rtcp; |
| 119 | |
| 120 | int local_port; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct mgcp_rtp_tap { |
| 124 | int enabled; |
| 125 | struct sockaddr_in forward; |
| 126 | }; |
| 127 | |
| 128 | struct 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 135 | /* Specific rtp connection type (see struct mgcp_conn_rtp) */ |
| 136 | enum mgcp_conn_rtp_type { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 137 | MGCP_RTP_DEFAULT = 0, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 138 | MGCP_OSMUX_BSC, |
| 139 | MGCP_OSMUX_BSC_NAT, |
| 140 | }; |
| 141 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 142 | #include <osmocom/mgcp/osmux.h> |
| 143 | struct mgcp_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 144 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | /* MGCP connection (RTP) */ |
| 146 | struct mgcp_conn_rtp { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 148 | /* Backpointer to conn struct */ |
| 149 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 150 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 151 | /* Specific connection type */ |
| 152 | enum mgcp_conn_rtp_type type; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 153 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 154 | /* Port status */ |
| 155 | struct mgcp_rtp_end end; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 157 | /* Sequence bits */ |
| 158 | struct mgcp_rtp_state state; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 159 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 160 | /* taps for the rtp connection */ |
| 161 | struct mgcp_rtp_tap tap_in; |
| 162 | struct mgcp_rtp_tap tap_out; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 163 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 164 | /* Osmux states (optional) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 165 | 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 184 | /*! Connection type, specifies which member of the union "u" in mgcp_conn |
| 185 | * contains a useful connection description (currently only RTP) */ |
| 186 | enum mgcp_conn_type { |
| 187 | MGCP_CONN_TYPE_RTP, |
| 188 | }; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 189 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 190 | /*! MGCP connection (untyped) */ |
| 191 | struct mgcp_conn { |
| 192 | /*!< list head */ |
| 193 | struct llist_head entry; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 194 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 195 | /*!< Backpointer to the endpoint where the conn belongs to */ |
| 196 | struct mgcp_endpoint *endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 197 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 198 | /*!< type of the connection (union) */ |
| 199 | enum mgcp_conn_type type; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 201 | /*!< mode of the connection */ |
| 202 | enum mgcp_connection_mode mode; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 203 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 204 | /*!< copy of the mode to restore the original setting (VTY) */ |
| 205 | enum mgcp_connection_mode mode_orig; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 206 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 207 | /*!< connection id to identify the conntion */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 208 | char id[MGCP_CONN_ID_LENGTH]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 209 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 210 | /*!< human readable name (vty, logging) */ |
| 211 | char name[256]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 212 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 213 | /*!< union with connection description */ |
| 214 | union { |
| 215 | struct mgcp_conn_rtp rtp; |
| 216 | } u; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 217 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 218 | /*!< pointer to optional private data */ |
| 219 | void *priv; |
| 220 | }; |
| 221 | |
| 222 | #include <osmocom/mgcp/mgcp_conn.h> |
| 223 | |
| 224 | struct mgcp_endpoint_type; |
| 225 | |
| 226 | struct 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 Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 242 | |
| 243 | |
| 244 | #define ENDPOINT_NUMBER(endp) abs((int)(endp - endp->tcfg->endpoints)) |
| 245 | |
| 246 | /** |
| 247 | * Internal structure while parsing a request |
| 248 | */ |
| 249 | struct 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 257 | int 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); |
| 260 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn); |
| 261 | int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf, |
| 262 | unsigned int buf_size, struct mgcp_conn *conn); |
| 263 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 264 | struct mgcp_conn_rtp *conn); |
| 265 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 266 | |
| 267 | /* For transcoding we need to manage an in and an output that are connected */ |
| 268 | static inline int endp_back_channel(int endpoint) |
| 269 | { |
| 270 | return endpoint + 60; |
| 271 | } |
| 272 | |
| 273 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index); |
| 274 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index); |
| 275 | |
| 276 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 277 | struct mgcp_rtp_end *rtp); |
| 278 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 279 | struct mgcp_rtp_end *rtp); |
| 280 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 281 | /* payload processing default functions */ |
| 282 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end, |
| 283 | char *data, int *len, int buf_size); |
| 284 | |
| 285 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 286 | struct mgcp_rtp_end *dst_end, |
| 287 | struct mgcp_rtp_end *src_end); |
| 288 | |
| 289 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 290 | int *payload_type, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 291 | const char**audio_name, |
| 292 | const char**fmtp_extra, |
| 293 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 294 | |
| 295 | /* internal RTP Annex A counting */ |
| 296 | void 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 | |
| 300 | int mgcp_set_ip_tos(int fd, int tos); |
| 301 | |
| 302 | enum { |
| 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 Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 323 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn); |