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