Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <time.h> |
| 29 | #include <limits.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 30 | #include <sys/socket.h> |
| 31 | #include <arpa/inet.h> |
| 32 | |
| 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/select.h> |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 35 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 36 | #include <osmocom/netif/rtp.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 39 | #include <osmocom/mgcp/mgcp_internal.h> |
| 40 | #include <osmocom/mgcp/mgcp_stat.h> |
| 41 | #include <osmocom/mgcp/osmux.h> |
| 42 | #include <osmocom/mgcp/mgcp_conn.h> |
| 43 | #include <osmocom/mgcp/mgcp_ep.h> |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 44 | #include <osmocom/mgcp/debug.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 45 | |
| 46 | #define RTP_SEQ_MOD (1 << 16) |
| 47 | #define RTP_MAX_DROPOUT 3000 |
| 48 | #define RTP_MAX_MISORDER 100 |
| 49 | #define RTP_BUF_SIZE 4096 |
| 50 | |
| 51 | enum { |
| 52 | MGCP_PROTO_RTP, |
| 53 | MGCP_PROTO_RTCP, |
| 54 | }; |
| 55 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 56 | /*! Determine the local rtp bind IP-address. |
| 57 | * \param[out] addr caller provided memory to store the resulting IP-Address |
| 58 | * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters |
| 59 | * |
| 60 | * The local bind IP-address is automatically selected by probing the |
| 61 | * IP-Address of the interface that is pointing towards the remote IP-Address, |
| 62 | * if no remote IP-Address is known yet, the statically configured |
| 63 | * IP-Addresses are used as fallback. */ |
| 64 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn) |
| 65 | { |
| 66 | |
| 67 | struct mgcp_endpoint *endp; |
| 68 | int rc; |
| 69 | endp = conn->conn->endp; |
| 70 | |
| 71 | /* Try probing the local IP-Address */ |
| 72 | if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) { |
| 73 | rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr)); |
| 74 | if (rc < 0) |
| 75 | LOGP(DRTP, LOGL_ERROR, |
| 76 | "endpoint:%x CI:%i local interface auto detection failed, using configured addresses...\n", |
| 77 | ENDPOINT_NUMBER(endp), conn->conn->id); |
| 78 | else { |
| 79 | LOGP(DRTP, LOGL_DEBUG, |
| 80 | "endpoint:%x CI:%i selected local rtp bind ip %s by probing using remote ip %s\n", |
| 81 | ENDPOINT_NUMBER(endp), conn->conn->id, addr, |
| 82 | inet_ntoa(conn->end.addr)); |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Select from preconfigured IP-Addresses */ |
| 88 | if (endp->cfg->net_ports.bind_addr) { |
| 89 | /* Check there is a bind IP for the RTP traffic configured, |
| 90 | * if so, use that IP-Address */ |
| 91 | strncpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN); |
| 92 | LOGP(DRTP, LOGL_DEBUG, |
| 93 | "endpoint:%x CI:%i using configured rtp bind ip as local bind ip %s\n", |
| 94 | ENDPOINT_NUMBER(endp), conn->conn->id, addr); |
| 95 | } else { |
| 96 | /* No specific bind IP is configured for the RTP traffic, so |
| 97 | * assume the IP where we listen for incoming MGCP messages |
| 98 | * as bind IP */ |
| 99 | strncpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN); |
| 100 | LOGP(DRTP, LOGL_DEBUG, |
| 101 | "endpoint:%x CI:%i using mgcp bind ip as local rtp bind ip: %s\n", |
| 102 | ENDPOINT_NUMBER(endp), conn->conn->id, addr); |
| 103 | } |
| 104 | } |
| 105 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | /* This does not need to be a precision timestamp and |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | * is allowed to wrap quite fast. The returned value is |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 108 | * 1/codec_rate seconds. */ |
| 109 | static uint32_t get_current_ts(unsigned codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 110 | { |
| 111 | struct timespec tp; |
| 112 | uint64_t ret; |
| 113 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 114 | if (!codec_rate) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | |
| 117 | memset(&tp, 0, sizeof(tp)); |
| 118 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 119 | LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 120 | |
| 121 | /* convert it to 1/unit seconds */ |
| 122 | ret = tp.tv_sec; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 123 | ret *= codec_rate; |
| 124 | ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 129 | /*! send udp packet. |
| 130 | * \param[in] fd associated file descriptor |
| 131 | * \param[in] addr destination ip-address |
| 132 | * \param[in] port destination UDP port |
| 133 | * \param[in] buf buffer that holds the data to be send |
| 134 | * \param[in] len length of the data to be sent |
| 135 | * \returns bytes sent, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 136 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
| 137 | { |
| 138 | struct sockaddr_in out; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 139 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 140 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 141 | "sending %i bytes length packet to %s:%u ...\n", |
| 142 | len, inet_ntoa(*addr), ntohs(port)); |
| 143 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 144 | out.sin_family = AF_INET; |
| 145 | out.sin_port = port; |
| 146 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 147 | |
| 148 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 149 | } |
| 150 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 151 | /*! send RTP dummy packet (to keep NAT connection open). |
| 152 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 153 | * \param[in] conn associated RTP connection |
| 154 | * \returns bytes sent, -1 on error */ |
| 155 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | { |
| 157 | static char buf[] = { MGCP_DUMMY_LOAD }; |
| 158 | int rc; |
| 159 | int was_rtcp = 0; |
| 160 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 161 | OSMO_ASSERT(endp); |
| 162 | OSMO_ASSERT(conn); |
| 163 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 164 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 165 | "endpoint:%x sending dummy packet...\n", ENDPOINT_NUMBER(endp)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 166 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 167 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 168 | |
| 169 | rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, |
| 170 | conn->end.rtp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 171 | |
| 172 | if (rc == -1) |
| 173 | goto failed; |
| 174 | |
| 175 | if (endp->tcfg->omit_rtcp) |
| 176 | return rc; |
| 177 | |
| 178 | was_rtcp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 179 | rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr, |
| 180 | conn->end.rtcp_port, buf, 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 181 | |
| 182 | if (rc >= 0) |
| 183 | return rc; |
| 184 | |
| 185 | failed: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 186 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 187 | "endpoint:%x Failed to send dummy %s packet.\n", |
| 188 | ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 189 | |
| 190 | return -1; |
| 191 | } |
| 192 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 193 | /* Compute timestamp alignment error */ |
| 194 | static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate, |
| 195 | int ptime, uint32_t timestamp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | { |
| 197 | int32_t timestamp_delta; |
| 198 | |
| 199 | if (ptime == 0) |
| 200 | return 0; |
| 201 | |
| 202 | /* Align according to: T - Tlast = k * Tptime */ |
| 203 | timestamp_delta = timestamp - sstate->last_timestamp; |
| 204 | |
| 205 | return timestamp_delta % ptime; |
| 206 | } |
| 207 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 208 | /* Check timestamp and sequence number for plausibility */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 209 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
| 210 | struct mgcp_rtp_state *state, |
| 211 | struct mgcp_rtp_stream_state *sstate, |
| 212 | struct mgcp_rtp_end *rtp_end, |
| 213 | struct sockaddr_in *addr, |
| 214 | uint16_t seq, uint32_t timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 215 | const char *text, int32_t * tsdelta_out) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 216 | { |
| 217 | int32_t tsdelta; |
| 218 | int32_t timestamp_error; |
| 219 | |
| 220 | /* Not fully intialized, skip */ |
| 221 | if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp) |
| 222 | return 0; |
| 223 | |
| 224 | if (seq == sstate->last_seq) { |
| 225 | if (timestamp != sstate->last_timestamp) { |
| 226 | sstate->err_ts_counter += 1; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 227 | LOGP(DRTP, LOGL_ERROR, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 228 | "The %s timestamp delta is != 0 but the sequence " |
| 229 | "number %d is the same, " |
| 230 | "TS offset: %d, SeqNo offset: %d " |
| 231 | "on 0x%x SSRC: %u timestamp: %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 232 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 233 | text, seq, |
| 234 | state->timestamp_offset, state->seq_offset, |
| 235 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 236 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | tsdelta = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 242 | (int32_t)(timestamp - sstate->last_timestamp) / |
| 243 | (int16_t)(seq - sstate->last_seq); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 244 | |
| 245 | if (tsdelta == 0) { |
| 246 | /* Don't update *tsdelta_out */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 247 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 248 | "The %s timestamp delta is %d " |
| 249 | "on 0x%x SSRC: %u timestamp: %u " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 250 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 251 | text, tsdelta, |
| 252 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 253 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | if (sstate->last_tsdelta != tsdelta) { |
| 259 | if (sstate->last_tsdelta) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 260 | LOGP(DRTP, LOGL_INFO, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 261 | "The %s timestamp delta changes from %d to %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 262 | "on 0x%x SSRC: %u timestamp: %u from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 263 | text, sstate->last_tsdelta, tsdelta, |
| 264 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 265 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | if (tsdelta_out) |
| 270 | *tsdelta_out = tsdelta; |
| 271 | |
| 272 | timestamp_error = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 273 | ts_alignment_error(sstate, state->packet_duration, timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 274 | |
| 275 | if (timestamp_error) { |
| 276 | sstate->err_ts_counter += 1; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 277 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 278 | "The %s timestamp has an alignment error of %d " |
| 279 | "on 0x%x SSRC: %u " |
| 280 | "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 281 | "from %s:%d. ptime: %d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 282 | text, timestamp_error, |
| 283 | ENDPOINT_NUMBER(endp), sstate->ssrc, |
| 284 | (int16_t)(seq - sstate->last_seq), |
| 285 | (int32_t)(timestamp - sstate->last_timestamp), |
| 286 | tsdelta, |
| 287 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 288 | state->packet_duration); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 289 | } |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | /* Set the timestamp offset according to the packet duration. */ |
| 294 | static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 295 | struct mgcp_rtp_state *state, |
| 296 | struct mgcp_rtp_end *rtp_end, |
| 297 | struct sockaddr_in *addr, |
| 298 | int16_t delta_seq, uint32_t in_timestamp) |
| 299 | { |
| 300 | int32_t tsdelta = state->packet_duration; |
| 301 | int timestamp_offset; |
| 302 | uint32_t out_timestamp; |
| 303 | |
| 304 | if (tsdelta == 0) { |
| 305 | tsdelta = state->out_stream.last_tsdelta; |
| 306 | if (tsdelta != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 307 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 308 | "A fixed packet duration is not available on 0x%x, " |
| 309 | "using last output timestamp delta instead: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 310 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | ENDPOINT_NUMBER(endp), tsdelta, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 312 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 313 | } else { |
| 314 | tsdelta = rtp_end->codec.rate * 20 / 1000; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 315 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 316 | "Fixed packet duration and last timestamp delta " |
| 317 | "are not available on 0x%x, " |
| 318 | "using fixed 20ms instead: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 319 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 320 | ENDPOINT_NUMBER(endp), tsdelta, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 321 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta; |
| 326 | timestamp_offset = out_timestamp - in_timestamp; |
| 327 | |
| 328 | if (state->timestamp_offset != timestamp_offset) { |
| 329 | state->timestamp_offset = timestamp_offset; |
| 330 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 331 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 332 | "Timestamp offset change on 0x%x SSRC: %u " |
| 333 | "SeqNo delta: %d, TS offset: %d, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 334 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 335 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 336 | delta_seq, state->timestamp_offset, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 337 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return timestamp_offset; |
| 341 | } |
| 342 | |
| 343 | /* Set the timestamp offset according to the packet duration. */ |
| 344 | static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 345 | struct mgcp_rtp_state *state, |
| 346 | struct mgcp_rtp_end *rtp_end, |
| 347 | struct sockaddr_in *addr, |
| 348 | uint32_t timestamp) |
| 349 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 350 | int ts_error = 0; |
| 351 | int ts_check = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 352 | int ptime = state->packet_duration; |
| 353 | |
| 354 | /* Align according to: T + Toffs - Tlast = k * Tptime */ |
| 355 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 356 | ts_error = ts_alignment_error(&state->out_stream, ptime, |
| 357 | timestamp + state->timestamp_offset); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 358 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 359 | /* If there is an alignment error, we have to compensate it */ |
| 360 | if (ts_error) { |
| 361 | state->timestamp_offset += ptime - ts_error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 362 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 363 | LOGP(DRTP, LOGL_NOTICE, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 364 | "Corrected timestamp alignment error of %d on 0x%x SSRC: %u " |
| 365 | "new TS offset: %d, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 366 | "from %s:%d\n", |
| 367 | ts_error, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 368 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 369 | state->timestamp_offset, inet_ntoa(addr->sin_addr), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 370 | ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 373 | /* Check we really managed to compensate the timestamp |
| 374 | * offset. There should not be any remaining error, failing |
| 375 | * here would point to a serous problem with the alingnment |
| 376 | * error computation fuction */ |
| 377 | ts_check = ts_alignment_error(&state->out_stream, ptime, |
| 378 | timestamp + state->timestamp_offset); |
| 379 | OSMO_ASSERT(ts_check == 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 380 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 381 | /* Return alignment error before compensation */ |
| 382 | return ts_error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 385 | /*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb). |
| 386 | * \param[in] associated endpoint |
| 387 | * \param[in] destination RTP end |
| 388 | * \param[in,out] pointer to buffer with voice data |
| 389 | * \param[in] voice data length |
| 390 | * \param[in] maxmimum size of caller provided voice data buffer |
| 391 | * \returns ignores input parameters, return always 0 */ |
| 392 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, |
| 393 | struct mgcp_rtp_end *dst_end, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 394 | char *data, int *len, int buf_size) |
| 395 | { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 396 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 397 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 401 | /*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb). |
| 402 | * \param[in] associated endpoint |
| 403 | * \param[in] destination RTP end |
| 404 | * \param[in] source RTP end |
| 405 | * \returns ignores input parameters, return always 0 */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 406 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 407 | struct mgcp_rtp_end *dst_end, |
| 408 | struct mgcp_rtp_end *src_end) |
| 409 | { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 410 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 411 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 416 | int *payload_type, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 417 | const char **audio_name, |
| 418 | const char **fmtp_extra, |
| 419 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 420 | { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 421 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 422 | "endpoint:%x conn:%s using format defaults\n", |
| 423 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 424 | |
| 425 | *payload_type = conn->end.codec.payload_type; |
| 426 | *audio_name = conn->end.codec.audio_name; |
| 427 | *fmtp_extra = conn->end.fmtp_extra; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 430 | void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, |
| 431 | struct mgcp_rtp_state *state, const uint16_t seq, |
| 432 | const int32_t transit, const uint32_t ssrc) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 433 | { |
| 434 | int32_t d; |
| 435 | |
| 436 | /* initialize or re-initialize */ |
| 437 | if (!state->stats_initialized || state->stats_ssrc != ssrc) { |
| 438 | state->stats_initialized = 1; |
| 439 | state->stats_base_seq = seq; |
| 440 | state->stats_max_seq = seq - 1; |
| 441 | state->stats_ssrc = ssrc; |
| 442 | state->stats_jitter = 0; |
| 443 | state->stats_transit = transit; |
| 444 | state->stats_cycles = 0; |
| 445 | } else { |
| 446 | uint16_t udelta; |
| 447 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 448 | /* The below takes the shape of the validation of |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 449 | * Appendix A. Check if there is something weird with |
| 450 | * the sequence number, otherwise check for a wrap |
| 451 | * around in the sequence number. |
| 452 | * It can't wrap during the initialization so let's |
| 453 | * skip it here. The Appendix A probably doesn't have |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 454 | * this issue because of the probation. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 455 | udelta = seq - state->stats_max_seq; |
| 456 | if (udelta < RTP_MAX_DROPOUT) { |
| 457 | if (seq < state->stats_max_seq) |
| 458 | state->stats_cycles += RTP_SEQ_MOD; |
| 459 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 460 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 461 | "RTP seqno made a very large jump on 0x%x delta: %u\n", |
| 462 | ENDPOINT_NUMBER(endp), udelta); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 466 | /* Calculate the jitter between the two packages. The TS should be |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 467 | * taken closer to the read function. This was taken from the |
| 468 | * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 469 | * resolution. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 470 | d = transit - state->stats_transit; |
| 471 | state->stats_transit = transit; |
| 472 | if (d < 0) |
| 473 | d = -d; |
| 474 | state->stats_jitter += d - ((state->stats_jitter + 8) >> 4); |
| 475 | state->stats_max_seq = seq; |
| 476 | } |
| 477 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 478 | /* The RFC 3550 Appendix A assumes there are multiple sources but |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 479 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
| 480 | * one source and this code will patch RTP header to appear as if there |
| 481 | * is only one source. |
| 482 | * There is also no probation period for new sources. Every RTP header |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 483 | * we receive will be seen as a switch in streams. */ |
| 484 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, |
| 485 | struct mgcp_rtp_state *state, |
| 486 | struct mgcp_rtp_end *rtp_end, |
| 487 | struct sockaddr_in *addr, char *data, int len) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 488 | { |
| 489 | uint32_t arrival_time; |
| 490 | int32_t transit; |
| 491 | uint16_t seq; |
| 492 | uint32_t timestamp, ssrc; |
| 493 | struct rtp_hdr *rtp_hdr; |
| 494 | int payload = rtp_end->codec.payload_type; |
| 495 | |
| 496 | if (len < sizeof(*rtp_hdr)) |
| 497 | return; |
| 498 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 499 | rtp_hdr = (struct rtp_hdr *)data; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 500 | seq = ntohs(rtp_hdr->sequence); |
| 501 | timestamp = ntohl(rtp_hdr->timestamp); |
| 502 | arrival_time = get_current_ts(rtp_end->codec.rate); |
| 503 | ssrc = ntohl(rtp_hdr->ssrc); |
| 504 | transit = arrival_time - timestamp; |
| 505 | |
| 506 | mgcp_rtp_annex_count(endp, state, seq, transit, ssrc); |
| 507 | |
| 508 | if (!state->initialized) { |
| 509 | state->initialized = 1; |
| 510 | state->in_stream.last_seq = seq - 1; |
| 511 | state->in_stream.ssrc = state->orig_ssrc = ssrc; |
| 512 | state->in_stream.last_tsdelta = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 513 | state->packet_duration = |
| 514 | mgcp_rtp_packet_duration(endp, rtp_end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 515 | state->out_stream = state->in_stream; |
| 516 | state->out_stream.last_timestamp = timestamp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 517 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 518 | LOGP(DRTP, LOGL_INFO, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 519 | "endpoint:%x initializing stream, SSRC: %u timestamp: %u " |
| 520 | "pkt-duration: %d, from %s:%d\n", |
| 521 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 522 | state->seq_offset, state->packet_duration, |
| 523 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 524 | if (state->packet_duration == 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 525 | state->packet_duration = |
| 526 | rtp_end->codec.rate * 20 / 1000; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 527 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 528 | "endpoint:%x fixed packet duration is not available, " |
| 529 | "using fixed 20ms instead: %d from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 530 | ENDPOINT_NUMBER(endp), state->packet_duration, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 531 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 532 | } |
| 533 | } else if (state->in_stream.ssrc != ssrc) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 534 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 535 | "endpoint:%x SSRC changed: %u -> %u " |
| 536 | "from %s:%d\n", |
| 537 | ENDPOINT_NUMBER(endp), |
| 538 | state->in_stream.ssrc, rtp_hdr->ssrc, |
| 539 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 540 | |
| 541 | state->in_stream.ssrc = ssrc; |
| 542 | if (rtp_end->force_constant_ssrc) { |
| 543 | int16_t delta_seq; |
| 544 | |
| 545 | /* Always increment seqno by 1 */ |
| 546 | state->seq_offset = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 547 | (state->out_stream.last_seq + 1) - seq; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 548 | |
| 549 | /* Estimate number of packets that would have been sent */ |
| 550 | delta_seq = |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 551 | (arrival_time - state->in_stream.last_arrival_time |
| 552 | + state->packet_duration / 2) / |
| 553 | state->packet_duration; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 554 | |
| 555 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 556 | delta_seq, timestamp); |
| 557 | |
| 558 | state->patch_ssrc = 1; |
| 559 | ssrc = state->orig_ssrc; |
| 560 | if (rtp_end->force_constant_ssrc != -1) |
| 561 | rtp_end->force_constant_ssrc -= 1; |
| 562 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 563 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 564 | "endpoint:%x SSRC patching enabled, SSRC: %u " |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 565 | "SeqNo offset: %d, TS offset: %d " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 566 | "from %s:%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 567 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 568 | state->seq_offset, state->timestamp_offset, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 569 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | state->in_stream.last_tsdelta = 0; |
| 573 | } else { |
| 574 | /* Compute current per-packet timestamp delta */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 575 | check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, |
| 576 | addr, seq, timestamp, "input", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 577 | &state->in_stream.last_tsdelta); |
| 578 | |
| 579 | if (state->patch_ssrc) |
| 580 | ssrc = state->orig_ssrc; |
| 581 | } |
| 582 | |
| 583 | /* Save before patching */ |
| 584 | state->in_stream.last_timestamp = timestamp; |
| 585 | state->in_stream.last_seq = seq; |
| 586 | state->in_stream.last_arrival_time = arrival_time; |
| 587 | |
| 588 | if (rtp_end->force_aligned_timing && |
| 589 | state->out_stream.ssrc == ssrc && state->packet_duration) |
| 590 | /* Align the timestamp offset */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 591 | align_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 592 | timestamp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 593 | |
| 594 | /* Store the updated SSRC back to the packet */ |
| 595 | if (state->patch_ssrc) |
| 596 | rtp_hdr->ssrc = htonl(ssrc); |
| 597 | |
| 598 | /* Apply the offset and store it back to the packet. |
| 599 | * This won't change anything if the offset is 0, so the conditional is |
| 600 | * omitted. */ |
| 601 | seq += state->seq_offset; |
| 602 | rtp_hdr->sequence = htons(seq); |
| 603 | timestamp += state->timestamp_offset; |
| 604 | rtp_hdr->timestamp = htonl(timestamp); |
| 605 | |
| 606 | /* Check again, whether the timestamps are still valid */ |
| 607 | if (state->out_stream.ssrc == ssrc) |
| 608 | check_rtp_timestamp(endp, state, &state->out_stream, rtp_end, |
| 609 | addr, seq, timestamp, "output", |
| 610 | &state->out_stream.last_tsdelta); |
| 611 | |
| 612 | /* Save output values */ |
| 613 | state->out_stream.last_seq = seq; |
| 614 | state->out_stream.last_timestamp = timestamp; |
| 615 | state->out_stream.ssrc = ssrc; |
| 616 | |
| 617 | if (payload < 0) |
| 618 | return; |
| 619 | |
| 620 | #if 0 |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 621 | DEBUGP(DRTP, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 622 | "endpoint:%x payload hdr payload %u -> endp payload %u\n", |
| 623 | ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 624 | rtp_hdr->payload_type = payload; |
| 625 | #endif |
| 626 | } |
| 627 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 628 | /* Forward data to a debug tap. This is debug function that is intended for |
| 629 | * debugging the voice traffic with tools like gstreamer */ |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 630 | static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, |
| 631 | int len) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 632 | { |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 633 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 634 | |
Philipp Maier | e6f172d | 2017-11-07 12:00:01 +0100 | [diff] [blame] | 635 | if (!tap->enabled) |
| 636 | return; |
| 637 | |
| 638 | rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward, |
| 639 | sizeof(tap->forward)); |
| 640 | |
| 641 | if (rc < 0) |
| 642 | LOGP(DRTP, LOGL_ERROR, |
| 643 | "Forwarding tapped (debug) voice data failed.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 646 | /*! Send RTP/RTCP data to a specified destination connection. |
| 647 | * \param[in] endp associated endpoint (for configuration, logging) |
| 648 | * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP |
| 649 | * \param[in] spoofed source address (set to NULL to disable) |
| 650 | * \param[in] buf buffer that contains the RTP/RTCP data |
| 651 | * \param[in] len length of the buffer that contains the RTP/RTCP data |
| 652 | * \param[in] conn_src associated source connection |
| 653 | * \param[in] conn_dst associated destination connection |
| 654 | * \returns 0 on success, -1 on ERROR */ |
| 655 | int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr, |
| 656 | char *buf, int len, struct mgcp_conn_rtp *conn_src, |
| 657 | struct mgcp_conn_rtp *conn_dst) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 658 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 659 | /*! When no destination connection is available (e.g. when only one |
| 660 | * connection in loopback mode exists), then the source connection |
| 661 | * shall be specified as destination connection */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 662 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 663 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 664 | struct mgcp_rtp_end *rtp_end; |
| 665 | struct mgcp_rtp_state *rtp_state; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 666 | char *dest_name; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 667 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 668 | OSMO_ASSERT(conn_src); |
| 669 | OSMO_ASSERT(conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 670 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 671 | if (is_rtp) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 672 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 673 | "endpoint:%x delivering RTP packet...\n", |
| 674 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 675 | } else { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 676 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 677 | "endpoint:%x delivering RTCP packet...\n", |
| 678 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 679 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 680 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 681 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 682 | "endpoint:%x loop:%d, mode:%d ", |
| 683 | ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode); |
| 684 | if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 685 | LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 686 | else |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 687 | LOGPC(DRTP, LOGL_DEBUG, "\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 688 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 689 | /* Note: In case of loopback configuration, both, the source and the |
| 690 | * destination will point to the same connection. */ |
| 691 | rtp_end = &conn_dst->end; |
| 692 | rtp_state = &conn_src->state; |
| 693 | dest_name = conn_dst->conn->name; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 694 | |
| 695 | if (!rtp_end->output_enabled) { |
| 696 | rtp_end->dropped_packets += 1; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 697 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 698 | "endpoint:%x output disabled, drop to %s %s " |
| 699 | "rtp_port:%u rtcp_port:%u\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 700 | ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 701 | dest_name, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 702 | inet_ntoa(rtp_end->addr), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 703 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 704 | ); |
| 705 | } else if (is_rtp) { |
| 706 | int cont; |
| 707 | int nbytes = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 708 | int buflen = len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 709 | do { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 710 | /* Run transcoder */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 711 | cont = endp->cfg->rtp_processing_cb(endp, rtp_end, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 712 | buf, &buflen, |
| 713 | RTP_BUF_SIZE); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 714 | if (cont < 0) |
| 715 | break; |
| 716 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 717 | if (addr) |
| 718 | mgcp_patch_and_count(endp, rtp_state, rtp_end, |
| 719 | addr, buf, buflen); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 720 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 721 | "endpoint:%x process/send to %s %s " |
| 722 | "rtp_port:%u rtcp_port:%u\n", |
| 723 | ENDPOINT_NUMBER(endp), dest_name, |
| 724 | inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port), |
| 725 | ntohs(rtp_end->rtcp_port) |
| 726 | ); |
| 727 | |
| 728 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 729 | forward_data(rtp_end->rtp.fd, &conn_src->tap_out, |
| 730 | buf, buflen); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 731 | |
| 732 | /* FIXME: HACK HACK HACK. See OS#2459. |
| 733 | * The ip.access nano3G needs the first RTP payload's first two bytes to read hex |
| 734 | * 'e400', or it will reject the RAB assignment. It seems to not harm other femto |
| 735 | * cells (as long as we patch only the first RTP payload in each stream). |
| 736 | */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 737 | if (!rtp_state->patched_first_rtp_payload) { |
| 738 | uint8_t *data = (uint8_t *) & buf[12]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 739 | data[0] = 0xe4; |
| 740 | data[1] = 0x00; |
| 741 | rtp_state->patched_first_rtp_payload = true; |
| 742 | } |
| 743 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 744 | len = mgcp_udp_send(rtp_end->rtp.fd, |
| 745 | &rtp_end->addr, |
| 746 | rtp_end->rtp_port, buf, buflen); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 747 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 748 | if (len <= 0) |
| 749 | return len; |
| 750 | |
| 751 | conn_dst->end.packets_tx += 1; |
| 752 | conn_dst->end.octets_tx += len; |
| 753 | |
| 754 | nbytes += len; |
| 755 | buflen = cont; |
| 756 | } while (buflen > 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 757 | return nbytes; |
| 758 | } else if (!tcfg->omit_rtcp) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 759 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 760 | "endpoint:%x send to %s %s rtp_port:%u rtcp_port:%u\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 761 | ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 762 | dest_name, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 763 | inet_ntoa(rtp_end->addr), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 764 | ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 765 | ); |
| 766 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 767 | len = mgcp_udp_send(rtp_end->rtcp.fd, |
| 768 | &rtp_end->addr, |
| 769 | rtp_end->rtcp_port, buf, len); |
| 770 | |
| 771 | conn_dst->end.packets_tx += 1; |
| 772 | conn_dst->end.octets_tx += len; |
| 773 | |
| 774 | return len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 780 | /* Helper function for mgcp_recv(), |
| 781 | Receive one RTP Packet + Originating address from file descriptor */ |
| 782 | static int receive_from(struct mgcp_endpoint *endp, int fd, |
| 783 | struct sockaddr_in *addr, char *buf, int bufsize) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 784 | { |
| 785 | int rc; |
| 786 | socklen_t slen = sizeof(*addr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 787 | struct sockaddr_in addr_sink; |
| 788 | char buf_sink[RTP_BUF_SIZE]; |
| 789 | bool tossed = false; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 790 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 791 | if (!addr) |
| 792 | addr = &addr_sink; |
| 793 | if (!buf) { |
| 794 | tossed = true; |
| 795 | buf = buf_sink; |
| 796 | bufsize = sizeof(buf_sink); |
| 797 | } |
| 798 | |
| 799 | rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen); |
| 800 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 801 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 802 | "receiving %u bytes length packet from %s:%u ...\n", |
| 803 | rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); |
| 804 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 805 | if (rc < 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 806 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 807 | "endpoint:%x failed to receive packet, errno: %d/%s\n", |
| 808 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 809 | return -1; |
| 810 | } |
| 811 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 812 | if (tossed) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 813 | LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 814 | ENDPOINT_NUMBER(endp)); |
| 815 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 816 | |
| 817 | return rc; |
| 818 | } |
| 819 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 820 | /* Check if the origin (addr) matches the address/port data of the RTP |
| 821 | * connections. */ |
| 822 | static int check_rtp_origin(struct mgcp_conn_rtp *conn, |
| 823 | struct sockaddr_in *addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 824 | { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 825 | struct mgcp_endpoint *endp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 826 | endp = conn->conn->endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 827 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 828 | /* Note: Check if the inbound RTP data comes from the same host to |
| 829 | * which we send our outgoing RTP traffic. */ |
| 830 | if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr)) |
| 831 | != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 832 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 833 | "endpoint:%x data from wrong address: %s, ", |
| 834 | ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 835 | LOGPC(DRTP, LOGL_ERROR, "expected: %s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 836 | inet_ntoa(conn->end.addr)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 837 | LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 838 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 839 | return -1; |
| 840 | } |
| 841 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 842 | /* Note: Usually the remote remote port of the data we receive will be |
| 843 | * the same as the remote port where we transmit outgoing RTP traffic |
| 844 | * to (set by MDCX). We use this to check the origin of the data for |
| 845 | * plausibility. */ |
| 846 | if (conn->end.rtp_port != addr->sin_port && |
| 847 | conn->end.rtcp_port != addr->sin_port) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 848 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 849 | "endpoint:%x data from wrong source port: %d, ", |
| 850 | ENDPOINT_NUMBER(endp), ntohs(addr->sin_port)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 851 | LOGPC(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 852 | "expected: %d for RTP or %d for RTCP\n", |
| 853 | ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 854 | LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 855 | ENDPOINT_NUMBER(endp)); |
| 856 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 862 | /* Check the if the destination address configuration of an RTP connection |
| 863 | * makes sense */ |
| 864 | static int check_rtp_destin(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 865 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 866 | struct mgcp_endpoint *endp; |
| 867 | endp = conn->conn->endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 868 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 869 | if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 870 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 871 | "endpoint:%x destination IP-address is invalid\n", |
| 872 | ENDPOINT_NUMBER(endp)); |
| 873 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 874 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 875 | |
| 876 | if (conn->end.rtp_port == 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 877 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 878 | "endpoint:%x destination rtp port is invalid\n", |
| 879 | ENDPOINT_NUMBER(endp)); |
| 880 | return -1; |
| 881 | } |
| 882 | |
| 883 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 886 | /* Receive RTP data from a specified source connection and dispatch it to a |
| 887 | * destination connection. */ |
| 888 | static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf, |
| 889 | unsigned int buf_size, struct osmo_fd *fd) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 890 | { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 891 | struct mgcp_endpoint *endp; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 892 | struct mgcp_conn_rtp *conn; |
| 893 | struct mgcp_trunk_config *tcfg; |
| 894 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 895 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 896 | conn = (struct mgcp_conn_rtp*) fd->data; |
| 897 | endp = conn->conn->endp; |
| 898 | tcfg = endp->tcfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 899 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 900 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x receiving RTP/RTCP packet...\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 901 | ENDPOINT_NUMBER(endp)); |
| 902 | |
| 903 | rc = receive_from(endp, fd->fd, addr, buf, buf_size); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 904 | if (rc <= 0) |
| 905 | return -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 906 | *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 907 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 908 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x ", ENDPOINT_NUMBER(endp)); |
| 909 | LOGPC(DRTP, LOGL_DEBUG, "receiveing from %s %s %d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 910 | conn->conn->name, inet_ntoa(addr->sin_addr), |
| 911 | ntohs(addr->sin_port)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 912 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x conn:%s\n", ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 913 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 914 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 915 | /* Check if the origin of the RTP packet seems plausible */ |
| 916 | if (tcfg->rtp_accept_all == 0) { |
| 917 | if (check_rtp_origin(conn, addr) != 0) |
| 918 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 919 | } |
| 920 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 921 | /* Filter out dummy message */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 922 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 923 | LOGP(DRTP, LOGL_NOTICE, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 924 | "endpoint:%x dummy message received\n", |
| 925 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 926 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 927 | "endpoint:%x packet tossed\n", ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 931 | /* Increment RX statistics */ |
| 932 | conn->end.packets_rx += 1; |
| 933 | conn->end.octets_rx += rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 934 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 935 | /* Forward a copy of the RTP data to a debug ip/port */ |
| 936 | forward_data(fd->fd, &conn->tap_in, buf, rc); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 937 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 938 | return rc; |
| 939 | } |
| 940 | |
| 941 | /* Send RTP data. Possible options are standard RTP packet |
| 942 | * transmission or trsmission via an osmux connection */ |
| 943 | static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf, |
| 944 | unsigned int buf_size, |
| 945 | struct mgcp_conn_rtp *conn_src, |
| 946 | struct mgcp_conn_rtp *conn_dst) |
| 947 | { |
| 948 | struct mgcp_endpoint *endp; |
| 949 | endp = conn_src->conn->endp; |
| 950 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 951 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x destin conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 952 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn)); |
| 953 | |
| 954 | /* Before we try to deliver the packet, we check if the destination |
| 955 | * port and IP-Address make sense at all. If not, we will be unable |
| 956 | * to deliver the packet. */ |
| 957 | if (check_rtp_destin(conn_dst) != 0) |
| 958 | return -1; |
| 959 | |
| 960 | /* Depending on the RTP connection type, deliver the RTP packet to the |
| 961 | * destination connection. */ |
| 962 | switch (conn_dst->type) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 963 | case MGCP_RTP_DEFAULT: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 964 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 965 | "endpoint:%x endpoint type is MGCP_RTP_DEFAULT, " |
| 966 | "using mgcp_send() to forward data directly\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 967 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 968 | return mgcp_send(endp, proto == MGCP_PROTO_RTP, |
| 969 | addr, buf, buf_size, conn_src, conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 970 | case MGCP_OSMUX_BSC_NAT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 971 | case MGCP_OSMUX_BSC: |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 972 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 973 | "endpoint:%x endpoint type is MGCP_OSMUX_BSC_NAT, " |
| 974 | "using osmux_xfrm_to_osmux() to forward data through OSMUX\n", |
| 975 | ENDPOINT_NUMBER(endp)); |
| 976 | return osmux_xfrm_to_osmux(buf, buf_size, conn_dst); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 977 | } |
| 978 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 979 | /* If the data has not been handled/forwarded until here, it will |
| 980 | * be discarded, this should not happen, normally the MGCP type |
| 981 | * should be properly set */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 982 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 983 | "endpoint:%x bad MGCP type -- data discarded!\n", |
| 984 | ENDPOINT_NUMBER(endp)); |
| 985 | |
| 986 | return -1; |
| 987 | } |
| 988 | |
| 989 | /*! dispatch incoming RTP packet to opposite RTP connection. |
| 990 | * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP) |
| 991 | * \param[in] addr socket address where the RTP packet has been received from |
| 992 | * \param[in] buf buffer that hold the RTP payload |
| 993 | * \param[in] buf_size size data length of buf |
| 994 | * \param[in] conn originating connection |
| 995 | * \returns 0 on success, -1 on ERROR */ |
| 996 | int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf, |
| 997 | unsigned int buf_size, struct mgcp_conn *conn) |
| 998 | { |
| 999 | struct mgcp_conn *conn_dst; |
| 1000 | struct mgcp_endpoint *endp; |
| 1001 | endp = conn->endp; |
| 1002 | |
| 1003 | /*! NOTE: This callback function implements the endpoint specific |
| 1004 | * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed |
| 1005 | * that the endpoint will hold only two connections. This premise |
| 1006 | * is used to determine the opposite connection (it is always the |
| 1007 | * connection that is not the originating connection). Once the |
| 1008 | * destination connection is known the RTP packet is sent via |
| 1009 | * the destination connection. */ |
| 1010 | |
| 1011 | /* Find a destination connection. */ |
| 1012 | /* NOTE: This code path runs every time an RTP packet is received. The |
| 1013 | * function mgcp_find_dst_conn() we use to determine the detination |
| 1014 | * connection will iterate the connection list inside the endpoint. |
| 1015 | * Since list iterations are quite costly, we will figure out the |
| 1016 | * destination only once and use the optional private data pointer of |
| 1017 | * the connection to cache the destination connection pointer. */ |
| 1018 | if (!conn->priv) { |
| 1019 | conn_dst = mgcp_find_dst_conn(conn); |
| 1020 | conn->priv = conn_dst; |
| 1021 | } else { |
| 1022 | conn_dst = (struct mgcp_conn *)conn->priv; |
| 1023 | } |
| 1024 | |
| 1025 | /* There is no destination conn, stop here */ |
| 1026 | if (!conn_dst) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1027 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1028 | "endpoint:%x unable to find destination conn\n", |
| 1029 | ENDPOINT_NUMBER(endp)); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | /* The destination conn is not an RTP connection */ |
| 1034 | if (conn_dst->type != MGCP_CONN_TYPE_RTP) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1035 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1036 | "endpoint:%x unable to find suitable destination conn\n", |
| 1037 | ENDPOINT_NUMBER(endp)); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | |
| 1041 | /* Dispatch RTP packet to destination RTP connection */ |
| 1042 | return mgcp_send_rtp(proto, addr, buf, |
| 1043 | buf_size, &conn->u.rtp, &conn_dst->u.rtp); |
| 1044 | |
| 1045 | } |
| 1046 | |
| 1047 | /* Handle incoming RTP data from NET */ |
| 1048 | static int rtp_data_net(struct osmo_fd *fd, unsigned int what) |
| 1049 | { |
| 1050 | /* NOTE: This is a generic implementation. RTP data is received. In |
| 1051 | * case of loopback the data is just sent back to its origin. All |
| 1052 | * other cases implement endpoint specific behaviour (e.g. how is the |
| 1053 | * destination connection determined?). That specific behaviour is |
| 1054 | * implemented by the callback function that is called at the end of |
| 1055 | * the function */ |
| 1056 | |
| 1057 | struct mgcp_conn_rtp *conn_src; |
| 1058 | struct mgcp_endpoint *endp; |
| 1059 | struct sockaddr_in addr; |
| 1060 | |
| 1061 | char buf[RTP_BUF_SIZE]; |
| 1062 | int proto; |
Philipp Maier | b969455 | 2017-11-08 16:53:57 +0100 | [diff] [blame] | 1063 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1064 | |
| 1065 | conn_src = (struct mgcp_conn_rtp *)fd->data; |
| 1066 | OSMO_ASSERT(conn_src); |
| 1067 | endp = conn_src->conn->endp; |
| 1068 | OSMO_ASSERT(endp); |
| 1069 | |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1070 | LOGP(DRTP, LOGL_DEBUG, "endpoint:%x source conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1071 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn)); |
| 1072 | |
| 1073 | /* Receive packet */ |
Philipp Maier | b969455 | 2017-11-08 16:53:57 +0100 | [diff] [blame] | 1074 | len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd); |
| 1075 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1076 | return -1; |
| 1077 | |
| 1078 | /* Check if the connection is in loopback mode, if yes, just send the |
| 1079 | * incoming data back to the origin */ |
| 1080 | if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) { |
| 1081 | return mgcp_send_rtp(proto, &addr, buf, |
Philipp Maier | b969455 | 2017-11-08 16:53:57 +0100 | [diff] [blame] | 1082 | len, conn_src, conn_src); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* Execute endpoint specific implementation that handles the |
| 1086 | * dispatching of the RTP data */ |
Philipp Maier | b969455 | 2017-11-08 16:53:57 +0100 | [diff] [blame] | 1087 | return endp->type->dispatch_rtp_cb(proto, &addr, buf, len, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1088 | conn_src->conn); |
| 1089 | } |
| 1090 | |
| 1091 | /*! set IP Type of Service parameter. |
| 1092 | * \param[in] fd associated file descriptor |
| 1093 | * \param[in] tos dscp value |
| 1094 | * \returns 0 on success, -1 on ERROR */ |
| 1095 | int mgcp_set_ip_tos(int fd, int tos) |
| 1096 | { |
| 1097 | int ret; |
| 1098 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1099 | |
| 1100 | if (ret < 0) |
| 1101 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1105 | /*! bind RTP port to osmo_fd. |
| 1106 | * \param[in] source_addr source (local) address to bind on |
| 1107 | * \param[in] fd associated file descriptor |
| 1108 | * \param[in] port to bind on |
| 1109 | * \returns 0 on success, -1 on ERROR */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1110 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port) |
| 1111 | { |
| 1112 | struct sockaddr_in addr; |
| 1113 | int on = 1; |
| 1114 | |
| 1115 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 1116 | if (fd->fd < 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1117 | LOGP(DRTP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1118 | source_addr, port); |
| 1119 | return -1; |
| 1120 | } else { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1121 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1122 | "created UDP port (%s:%i).\n", source_addr, port); |
| 1123 | } |
| 1124 | |
| 1125 | if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1126 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1127 | "failed to set socket options (%s:%i).\n", source_addr, |
| 1128 | port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
| 1131 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1132 | memset(&addr, 0, sizeof(addr)); |
| 1133 | addr.sin_family = AF_INET; |
| 1134 | addr.sin_port = htons(port); |
| 1135 | inet_aton(source_addr, &addr.sin_addr); |
| 1136 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1137 | if (bind(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1138 | close(fd->fd); |
| 1139 | fd->fd = -1; |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1140 | LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1141 | source_addr, port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1142 | return -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1143 | } else { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1144 | LOGP(DRTP, LOGL_DEBUG, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1145 | "bound UDP port (%s:%i).\n", source_addr, port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1151 | /* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1152 | static int bind_rtp(struct mgcp_config *cfg, const char *source_addr, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1153 | struct mgcp_rtp_end *rtp_end, int endpno) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1154 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1155 | /* NOTE: The port that is used for RTCP is the RTP port incremented by one |
| 1156 | * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */ |
| 1157 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1158 | if (mgcp_create_bind(source_addr, &rtp_end->rtp, |
| 1159 | rtp_end->local_port) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1160 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1161 | "endpoint:%x failed to create RTP port: %s:%d\n", endpno, |
| 1162 | source_addr, rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1163 | goto cleanup0; |
| 1164 | } |
| 1165 | |
| 1166 | if (mgcp_create_bind(source_addr, &rtp_end->rtcp, |
| 1167 | rtp_end->local_port + 1) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1168 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1169 | "endpoint:%x failed to create RTCP port: %s:%d\n", endpno, |
| 1170 | source_addr, rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1171 | goto cleanup1; |
| 1172 | } |
| 1173 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1174 | /* Set Type of Service (DSCP-Value) as configured via VTY */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1175 | mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 1176 | mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
| 1177 | |
| 1178 | rtp_end->rtp.when = BSC_FD_READ; |
| 1179 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1180 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1181 | "endpoint:%x failed to register RTP port %d\n", endpno, |
| 1182 | rtp_end->local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1183 | goto cleanup2; |
| 1184 | } |
| 1185 | |
| 1186 | rtp_end->rtcp.when = BSC_FD_READ; |
| 1187 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1188 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1189 | "endpoint:%x failed to register RTCP port %d\n", endpno, |
| 1190 | rtp_end->local_port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1191 | goto cleanup3; |
| 1192 | } |
| 1193 | |
| 1194 | return 0; |
| 1195 | |
| 1196 | cleanup3: |
| 1197 | osmo_fd_unregister(&rtp_end->rtp); |
| 1198 | cleanup2: |
| 1199 | close(rtp_end->rtcp.fd); |
| 1200 | rtp_end->rtcp.fd = -1; |
| 1201 | cleanup1: |
| 1202 | close(rtp_end->rtp.fd); |
| 1203 | rtp_end->rtp.fd = -1; |
| 1204 | cleanup0: |
| 1205 | return -1; |
| 1206 | } |
| 1207 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1208 | /*! bind RTP port to endpoint/connection. |
| 1209 | * \param[in] endp endpoint that holds the RTP connection |
| 1210 | * \param[in] rtp_port port number to bind on |
| 1211 | * \param[in] conn associated RTP connection |
| 1212 | * \returns 0 on success, -1 on ERROR */ |
| 1213 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 1214 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1215 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1216 | char name[512]; |
| 1217 | struct mgcp_rtp_end *end; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1218 | char local_ip_addr[INET_ADDRSTRLEN]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1219 | |
| 1220 | snprintf(name, sizeof(name), "%s-%u", conn->conn->name, conn->conn->id); |
| 1221 | end = &conn->end; |
| 1222 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1223 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 1224 | LOGP(DRTP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1225 | "endpoint:%x %u was already bound on conn:%s\n", |
| 1226 | ENDPOINT_NUMBER(endp), rtp_port, |
| 1227 | mgcp_conn_dump(conn->conn)); |
| 1228 | |
| 1229 | /* Double bindings should never occour! Since we always allocate |
| 1230 | * connections dynamically and free them when they are not |
| 1231 | * needed anymore, there must be no previous binding leftover. |
| 1232 | * Should there be a connection bound twice, we have a serious |
| 1233 | * problem and must exit immediately! */ |
| 1234 | OSMO_ASSERT(false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | end->local_port = rtp_port; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1238 | end->rtp.cb = rtp_data_net; |
| 1239 | end->rtp.data = conn; |
| 1240 | end->rtcp.data = conn; |
| 1241 | end->rtcp.cb = rtp_data_net; |
| 1242 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1243 | mgcp_get_local_addr(local_ip_addr, conn); |
| 1244 | |
| 1245 | return bind_rtp(endp->cfg, local_ip_addr, end, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1246 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1249 | /*! free allocated RTP and RTCP ports. |
| 1250 | * \param[in] end RTP end */ |
| 1251 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1252 | { |
| 1253 | if (end->rtp.fd != -1) { |
| 1254 | close(end->rtp.fd); |
| 1255 | end->rtp.fd = -1; |
| 1256 | osmo_fd_unregister(&end->rtp); |
| 1257 | } |
| 1258 | |
| 1259 | if (end->rtcp.fd != -1) { |
| 1260 | close(end->rtcp.fd); |
| 1261 | end->rtcp.fd = -1; |
| 1262 | osmo_fd_unregister(&end->rtcp); |
| 1263 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1264 | } |