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