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