Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <string.h> |
Holger Hans Peter Freyther | b715d7f | 2010-05-14 02:14:09 +0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 27 | #include <errno.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 28 | |
| 29 | #include <sys/socket.h> |
| 30 | #include <arpa/inet.h> |
| 31 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | 1ebad74 | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 35 | #include <openbsc/mgcp.h> |
| 36 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 983c991 | 2014-06-22 22:30:28 +0200 | [diff] [blame] | 37 | #include <openbsc/rtp.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 38 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 39 | #include <openbsc/osmux.h> |
| 40 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 41 | #warning "Make use of the rtp proxy code" |
| 42 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 44 | #define RTP_SEQ_MOD (1 << 16) |
| 45 | #define RTP_MAX_DROPOUT 3000 |
| 46 | #define RTP_MAX_MISORDER 100 |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 47 | #define RTP_BUF_SIZE 4096 |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 48 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 49 | enum { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 50 | MGCP_PROTO_RTP, |
| 51 | MGCP_PROTO_RTCP, |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 54 | /** |
| 55 | * This does not need to be a precision timestamp and |
| 56 | * is allowed to wrap quite fast. The returned value is |
Jacob Erlbeck | 303b54a | 2014-01-30 21:01:34 +0100 | [diff] [blame] | 57 | * 1/unit seconds. |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 58 | */ |
Jacob Erlbeck | 303b54a | 2014-01-30 21:01:34 +0100 | [diff] [blame] | 59 | static uint32_t get_current_ts(unsigned unit) |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 60 | { |
| 61 | struct timespec tp; |
| 62 | uint64_t ret; |
| 63 | |
Jacob Erlbeck | 303b54a | 2014-01-30 21:01:34 +0100 | [diff] [blame] | 64 | if (!unit) |
| 65 | return 0; |
| 66 | |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 67 | memset(&tp, 0, sizeof(tp)); |
| 68 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
| 69 | LOGP(DMGCP, LOGL_NOTICE, |
| 70 | "Getting the clock failed.\n"); |
| 71 | |
Jacob Erlbeck | 303b54a | 2014-01-30 21:01:34 +0100 | [diff] [blame] | 72 | /* convert it to 1/unit seconds */ |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 73 | ret = tp.tv_sec; |
Jacob Erlbeck | 303b54a | 2014-01-30 21:01:34 +0100 | [diff] [blame] | 74 | ret *= unit; |
| 75 | ret += (int64_t)tp.tv_nsec * unit / 1000 / 1000 / 1000; |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 80 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 81 | { |
| 82 | struct sockaddr_in out; |
| 83 | out.sin_family = AF_INET; |
| 84 | out.sin_port = port; |
| 85 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 86 | |
| 87 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 90 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 91 | { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 92 | static char buf[] = { MGCP_DUMMY_LOAD }; |
Jacob Erlbeck | 34bdc9f | 2013-12-19 18:25:54 +0100 | [diff] [blame] | 93 | int rc; |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 94 | |
Jacob Erlbeck | 34bdc9f | 2013-12-19 18:25:54 +0100 | [diff] [blame] | 95 | rc = mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
| 96 | endp->net_end.rtp_port, buf, 1); |
| 97 | |
Jacob Erlbeck | 1dc022c | 2014-01-17 09:22:57 +0100 | [diff] [blame] | 98 | if (rc == -1) |
| 99 | goto failed; |
| 100 | |
| 101 | if (endp->tcfg->omit_rtcp) |
Jacob Erlbeck | 34bdc9f | 2013-12-19 18:25:54 +0100 | [diff] [blame] | 102 | return rc; |
| 103 | |
Jacob Erlbeck | 1dc022c | 2014-01-17 09:22:57 +0100 | [diff] [blame] | 104 | rc = mgcp_udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, |
| 105 | endp->net_end.rtcp_port, buf, 1); |
| 106 | |
| 107 | if (rc >= 0) |
| 108 | return rc; |
| 109 | |
| 110 | failed: |
| 111 | LOGP(DMGCP, LOGL_ERROR, |
| 112 | "Failed to send dummy packet: %s on: 0x%x to %s\n", |
| 113 | strerror(errno), ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr)); |
| 114 | |
| 115 | return -1; |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 118 | static int32_t compute_timestamp_aligment_error(struct mgcp_rtp_stream_state *sstate, |
| 119 | int ptime, uint32_t timestamp) |
| 120 | { |
| 121 | int32_t timestamp_delta; |
| 122 | |
| 123 | if (ptime == 0) |
| 124 | return 0; |
| 125 | |
| 126 | /* Align according to: T - Tlast = k * Tptime */ |
| 127 | timestamp_delta = timestamp - sstate->last_timestamp; |
| 128 | |
| 129 | return timestamp_delta % ptime; |
| 130 | } |
| 131 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 132 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 133 | struct mgcp_rtp_state *state, |
| 134 | struct mgcp_rtp_stream_state *sstate, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 135 | struct mgcp_rtp_end *rtp_end, |
| 136 | struct sockaddr_in *addr, |
| 137 | uint16_t seq, uint32_t timestamp, |
| 138 | const char *text, int32_t *tsdelta_out) |
| 139 | { |
| 140 | int32_t tsdelta; |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 141 | int32_t timestamp_error; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 142 | |
| 143 | /* Not fully intialized, skip */ |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 144 | if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp) |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 147 | if (seq == sstate->last_seq) { |
| 148 | if (timestamp != sstate->last_timestamp) { |
| 149 | sstate->err_ts_counter += 1; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 150 | LOGP(DMGCP, LOGL_ERROR, |
| 151 | "The %s timestamp delta is != 0 but the sequence " |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 152 | "number %d is the same, " |
| 153 | "TS offset: %d, SeqNo offset: %d " |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 154 | "on 0x%x SSRC: %u timestamp: %u " |
| 155 | "from %s:%d in %d\n", |
| 156 | text, seq, |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 157 | state->timestamp_offset, state->seq_offset, |
| 158 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 159 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 160 | endp->conn_mode); |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | tsdelta = |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 166 | (int32_t)(timestamp - sstate->last_timestamp) / |
| 167 | (int16_t)(seq - sstate->last_seq); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 168 | |
| 169 | if (tsdelta == 0) { |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 170 | /* Don't update *tsdelta_out */ |
| 171 | LOGP(DMGCP, LOGL_NOTICE, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 172 | "The %s timestamp delta is %d " |
| 173 | "on 0x%x SSRC: %u timestamp: %u " |
| 174 | "from %s:%d in %d\n", |
| 175 | text, tsdelta, |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 176 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 177 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 178 | endp->conn_mode); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 183 | if (sstate->last_tsdelta != tsdelta) { |
| 184 | if (sstate->last_tsdelta) { |
| 185 | LOGP(DMGCP, LOGL_INFO, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 186 | "The %s timestamp delta changes from %d to %d " |
| 187 | "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n", |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 188 | text, sstate->last_tsdelta, tsdelta, |
| 189 | ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 190 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 191 | endp->conn_mode); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (tsdelta_out) |
| 196 | *tsdelta_out = tsdelta; |
| 197 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 198 | timestamp_error = |
| 199 | compute_timestamp_aligment_error(sstate, state->packet_duration, |
| 200 | timestamp); |
| 201 | |
| 202 | if (timestamp_error) { |
| 203 | sstate->err_ts_counter += 1; |
| 204 | LOGP(DMGCP, LOGL_NOTICE, |
| 205 | "The %s timestamp has an alignment error of %d " |
| 206 | "on 0x%x SSRC: %u " |
| 207 | "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d " |
Holger Hans Peter Freyther | a7992e0 | 2014-06-27 19:51:52 +0200 | [diff] [blame] | 208 | "from %s:%d in mode %d. ptime: %d\n", |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 209 | text, timestamp_error, |
| 210 | ENDPOINT_NUMBER(endp), sstate->ssrc, |
| 211 | (int16_t)(seq - sstate->last_seq), |
| 212 | (int32_t)(timestamp - sstate->last_timestamp), |
| 213 | tsdelta, |
| 214 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
Holger Hans Peter Freyther | a7992e0 | 2014-06-27 19:51:52 +0200 | [diff] [blame] | 215 | endp->conn_mode, state->packet_duration); |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 216 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 220 | /* Set the timestamp offset according to the packet duration. */ |
| 221 | static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 222 | struct mgcp_rtp_state *state, |
| 223 | struct mgcp_rtp_end *rtp_end, |
| 224 | struct sockaddr_in *addr, |
Jacob Erlbeck | 785e3c9 | 2013-12-19 17:50:27 +0100 | [diff] [blame] | 225 | int16_t delta_seq, uint32_t in_timestamp) |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 226 | { |
| 227 | int32_t tsdelta = state->packet_duration; |
| 228 | int timestamp_offset; |
Jacob Erlbeck | 785e3c9 | 2013-12-19 17:50:27 +0100 | [diff] [blame] | 229 | uint32_t out_timestamp; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 230 | |
| 231 | if (tsdelta == 0) { |
| 232 | tsdelta = state->out_stream.last_tsdelta; |
| 233 | if (tsdelta != 0) { |
| 234 | LOGP(DMGCP, LOGL_NOTICE, |
| 235 | "A fixed packet duration is not available on 0x%x, " |
| 236 | "using last output timestamp delta instead: %d " |
| 237 | "from %s:%d in %d\n", |
| 238 | ENDPOINT_NUMBER(endp), tsdelta, |
| 239 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 240 | endp->conn_mode); |
| 241 | } else { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 242 | tsdelta = rtp_end->codec.rate * 20 / 1000; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 243 | LOGP(DMGCP, LOGL_NOTICE, |
| 244 | "Fixed packet duration and last timestamp delta " |
| 245 | "are not available on 0x%x, " |
| 246 | "using fixed 20ms instead: %d " |
| 247 | "from %s:%d in %d\n", |
| 248 | ENDPOINT_NUMBER(endp), tsdelta, |
| 249 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 250 | endp->conn_mode); |
| 251 | } |
| 252 | } |
| 253 | |
Jacob Erlbeck | 785e3c9 | 2013-12-19 17:50:27 +0100 | [diff] [blame] | 254 | out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta; |
| 255 | timestamp_offset = out_timestamp - in_timestamp; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 256 | |
| 257 | if (state->timestamp_offset != timestamp_offset) { |
| 258 | state->timestamp_offset = timestamp_offset; |
| 259 | |
| 260 | LOGP(DMGCP, LOGL_NOTICE, |
| 261 | "Timestamp offset change on 0x%x SSRC: %u " |
| 262 | "SeqNo delta: %d, TS offset: %d, " |
| 263 | "from %s:%d in %d\n", |
| 264 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 265 | delta_seq, state->timestamp_offset, |
| 266 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 267 | endp->conn_mode); |
| 268 | } |
| 269 | |
| 270 | return timestamp_offset; |
| 271 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 272 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 273 | /* Set the timestamp offset according to the packet duration. */ |
| 274 | static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 275 | struct mgcp_rtp_state *state, |
| 276 | struct mgcp_rtp_end *rtp_end, |
| 277 | struct sockaddr_in *addr, |
| 278 | uint32_t timestamp) |
| 279 | { |
| 280 | int timestamp_error = 0; |
| 281 | int ptime = state->packet_duration; |
| 282 | |
| 283 | /* Align according to: T + Toffs - Tlast = k * Tptime */ |
| 284 | |
| 285 | timestamp_error = compute_timestamp_aligment_error( |
| 286 | &state->out_stream, ptime, |
| 287 | timestamp + state->timestamp_offset); |
| 288 | |
| 289 | if (timestamp_error) { |
| 290 | state->timestamp_offset += ptime - timestamp_error; |
| 291 | |
| 292 | LOGP(DMGCP, LOGL_NOTICE, |
| 293 | "Corrected timestamp alignment error of %d on 0x%x SSRC: %u " |
| 294 | "new TS offset: %d, " |
| 295 | "from %s:%d in %d\n", |
| 296 | timestamp_error, |
| 297 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 298 | state->timestamp_offset, inet_ntoa(addr->sin_addr), |
| 299 | ntohs(addr->sin_port), endp->conn_mode); |
| 300 | } |
| 301 | |
| 302 | OSMO_ASSERT(compute_timestamp_aligment_error(&state->out_stream, ptime, |
| 303 | timestamp + state->timestamp_offset) == 0); |
| 304 | |
| 305 | return timestamp_error; |
| 306 | } |
| 307 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 308 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end, |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 309 | char *data, int *len, int buf_size) |
| 310 | { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 315 | struct mgcp_rtp_end *dst_end, |
| 316 | struct mgcp_rtp_end *src_end) |
| 317 | { |
| 318 | return 0; |
| 319 | } |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 320 | |
Jacob Erlbeck | 168ca00 | 2014-03-17 12:40:07 +0100 | [diff] [blame] | 321 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 322 | int *payload_type, |
| 323 | const char**audio_name, |
| 324 | const char**fmtp_extra) |
| 325 | { |
| 326 | /* Use the BTS side parameters when passing the SDP data (for |
| 327 | * downlink) to the net peer. |
| 328 | */ |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 329 | *payload_type = endp->bts_end.codec.payload_type; |
| 330 | *audio_name = endp->bts_end.codec.audio_name; |
Jacob Erlbeck | 168ca00 | 2014-03-17 12:40:07 +0100 | [diff] [blame] | 331 | *fmtp_extra = endp->bts_end.fmtp_extra; |
| 332 | } |
| 333 | |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 334 | |
| 335 | void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 336 | const uint16_t seq, const int32_t transit, |
| 337 | const uint32_t ssrc) |
| 338 | { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 339 | int32_t d; |
| 340 | |
| 341 | /* initialize or re-initialize */ |
| 342 | if (!state->stats_initialized || state->stats_ssrc != ssrc) { |
| 343 | state->stats_initialized = 1; |
| 344 | state->stats_base_seq = seq; |
| 345 | state->stats_max_seq = seq - 1; |
| 346 | state->stats_ssrc = ssrc; |
| 347 | state->stats_jitter = 0; |
| 348 | state->stats_transit = transit; |
| 349 | state->stats_cycles = 0; |
Holger Hans Peter Freyther | 05d481a | 2014-10-06 21:01:26 +0200 | [diff] [blame] | 350 | } else { |
| 351 | uint16_t udelta; |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 352 | |
Holger Hans Peter Freyther | 05d481a | 2014-10-06 21:01:26 +0200 | [diff] [blame] | 353 | /* |
| 354 | * The below takes the shape of the validation of |
| 355 | * Appendix A. Check if there is something weird with |
| 356 | * the sequence number, otherwise check for a wrap |
| 357 | * around in the sequence number. |
| 358 | * It can't wrap during the initialization so let's |
| 359 | * skip it here. The Appendix A probably doesn't have |
| 360 | * this issue because of the probation. |
| 361 | */ |
| 362 | udelta = seq - state->stats_max_seq; |
| 363 | if (udelta < RTP_MAX_DROPOUT) { |
| 364 | if (seq < state->stats_max_seq) |
| 365 | state->stats_cycles += RTP_SEQ_MOD; |
| 366 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
| 367 | LOGP(DMGCP, LOGL_NOTICE, |
| 368 | "RTP seqno made a very large jump on 0x%x delta: %u\n", |
| 369 | ENDPOINT_NUMBER(endp), udelta); |
| 370 | } |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Calculate the jitter between the two packages. The TS should be |
| 375 | * taken closer to the read function. This was taken from the |
| 376 | * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate |
| 377 | * resolution. |
| 378 | */ |
| 379 | d = transit - state->stats_transit; |
| 380 | state->stats_transit = transit; |
| 381 | if (d < 0) |
| 382 | d = -d; |
| 383 | state->stats_jitter += d - ((state->stats_jitter + 8) >> 4); |
| 384 | state->stats_max_seq = seq; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
Holger Hans Peter Freyther | 1e85af6 | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 389 | /** |
| 390 | * The RFC 3550 Appendix A assumes there are multiple sources but |
| 391 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
Holger Hans Peter Freyther | 2a7ab86 | 2014-10-06 17:16:25 +0200 | [diff] [blame] | 392 | * one source and this code will patch RTP header to appear as if there |
Holger Hans Peter Freyther | 1e85af6 | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 393 | * is only one source. |
Holger Hans Peter Freyther | 2a7ab86 | 2014-10-06 17:16:25 +0200 | [diff] [blame] | 394 | * There is also no probation period for new sources. Every RTP header |
Holger Hans Peter Freyther | 1e85af6 | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 395 | * we receive will be seen as a switch in streams. |
| 396 | */ |
Jacob Erlbeck | d62419b | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 397 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 398 | struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr, |
| 399 | char *data, int len) |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 400 | { |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 401 | uint32_t arrival_time; |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 402 | int32_t transit; |
| 403 | uint16_t seq; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 404 | uint32_t timestamp, ssrc; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 405 | struct rtp_hdr *rtp_hdr; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 406 | int payload = rtp_end->codec.payload_type; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 407 | |
| 408 | if (len < sizeof(*rtp_hdr)) |
| 409 | return; |
| 410 | |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 411 | rtp_hdr = (struct rtp_hdr *) data; |
| 412 | seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 413 | timestamp = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 414 | arrival_time = get_current_ts(rtp_end->codec.rate); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 415 | ssrc = ntohl(rtp_hdr->ssrc); |
Jacob Erlbeck | b281e4e | 2014-01-30 21:01:36 +0100 | [diff] [blame] | 416 | transit = arrival_time - timestamp; |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 417 | |
Holger Hans Peter Freyther | 7b76f82 | 2014-10-06 21:04:40 +0200 | [diff] [blame] | 418 | mgcp_rtp_annex_count(endp, state, seq, transit, ssrc); |
| 419 | |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 420 | if (!state->initialized) { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 421 | state->initialized = 1; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 422 | state->in_stream.last_seq = seq - 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 423 | state->in_stream.ssrc = state->orig_ssrc = ssrc; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 424 | state->in_stream.last_tsdelta = 0; |
Jacob Erlbeck | f6ec0e9 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 425 | state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 426 | state->out_stream = state->in_stream; |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 427 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 428 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 429 | LOGP(DMGCP, LOGL_INFO, |
| 430 | "Initializing stream on 0x%x SSRC: %u timestamp: %u " |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 431 | "pkt-duration: %d, from %s:%d in %d\n", |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 432 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 433 | state->seq_offset, state->packet_duration, |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 434 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 435 | endp->conn_mode); |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 436 | if (state->packet_duration == 0) { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 437 | state->packet_duration = rtp_end->codec.rate * 20 / 1000; |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 438 | LOGP(DMGCP, LOGL_NOTICE, |
| 439 | "Fixed packet duration is not available on 0x%x, " |
| 440 | "using fixed 20ms instead: %d from %s:%d in %d\n", |
| 441 | ENDPOINT_NUMBER(endp), state->packet_duration, |
| 442 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 443 | endp->conn_mode); |
| 444 | } |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 445 | } else if (state->in_stream.ssrc != ssrc) { |
Jacob Erlbeck | 5e9549e | 2013-12-05 12:20:05 +0100 | [diff] [blame] | 446 | LOGP(DMGCP, LOGL_NOTICE, |
| 447 | "The SSRC changed on 0x%x: %u -> %u " |
| 448 | "from %s:%d in %d\n", |
| 449 | ENDPOINT_NUMBER(endp), |
| 450 | state->in_stream.ssrc, rtp_hdr->ssrc, |
| 451 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 452 | endp->conn_mode); |
| 453 | |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 454 | state->in_stream.ssrc = ssrc; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 455 | if (rtp_end->force_constant_ssrc) { |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 456 | int16_t delta_seq; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 457 | |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 458 | /* Always increment seqno by 1 */ |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 459 | state->seq_offset = |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 460 | (state->out_stream.last_seq + 1) - seq; |
| 461 | |
| 462 | /* Estimate number of packets that would have been sent */ |
| 463 | delta_seq = |
| 464 | (arrival_time - state->in_stream.last_arrival_time |
| 465 | + state->packet_duration/2) / |
| 466 | state->packet_duration; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 467 | |
| 468 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 469 | delta_seq, timestamp); |
| 470 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 471 | state->patch_ssrc = 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 472 | ssrc = state->orig_ssrc; |
Jacob Erlbeck | e2292f3 | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 473 | if (rtp_end->force_constant_ssrc != -1) |
| 474 | rtp_end->force_constant_ssrc -= 1; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 475 | |
| 476 | LOGP(DMGCP, LOGL_NOTICE, |
| 477 | "SSRC patching enabled on 0x%x SSRC: %u " |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 478 | "SeqNo offset: %d, TS offset: %d " |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 479 | "from %s:%d in %d\n", |
| 480 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 481 | state->seq_offset, state->timestamp_offset, |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 482 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 483 | endp->conn_mode); |
| 484 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 485 | |
| 486 | state->in_stream.last_tsdelta = 0; |
| 487 | } else { |
| 488 | /* Compute current per-packet timestamp delta */ |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 489 | check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 490 | seq, timestamp, "input", |
| 491 | &state->in_stream.last_tsdelta); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 492 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 493 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 494 | ssrc = state->orig_ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 495 | } |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 496 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 497 | /* Save before patching */ |
| 498 | state->in_stream.last_timestamp = timestamp; |
| 499 | state->in_stream.last_seq = seq; |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 500 | state->in_stream.last_arrival_time = arrival_time; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 501 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 502 | if (rtp_end->force_aligned_timing && |
| 503 | state->out_stream.ssrc == ssrc && state->packet_duration) |
| 504 | /* Align the timestamp offset */ |
| 505 | align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp); |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 506 | |
| 507 | /* Store the updated SSRC back to the packet */ |
| 508 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 509 | rtp_hdr->ssrc = htonl(ssrc); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 510 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 511 | /* Apply the offset and store it back to the packet. |
| 512 | * This won't change anything if the offset is 0, so the conditional is |
| 513 | * omitted. */ |
| 514 | seq += state->seq_offset; |
| 515 | rtp_hdr->sequence = htons(seq); |
| 516 | timestamp += state->timestamp_offset; |
| 517 | rtp_hdr->timestamp = htonl(timestamp); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 518 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 519 | /* Check again, whether the timestamps are still valid */ |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 520 | if (state->out_stream.ssrc == ssrc) |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 521 | check_rtp_timestamp(endp, state, &state->out_stream, rtp_end, |
| 522 | addr, seq, timestamp, "output", |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 523 | &state->out_stream.last_tsdelta); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 524 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 525 | /* Save output values */ |
| 526 | state->out_stream.last_seq = seq; |
| 527 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 528 | state->out_stream.ssrc = ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 529 | |
Holger Hans Peter Freyther | 7279d24 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 530 | if (payload < 0) |
| 531 | return; |
| 532 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 533 | rtp_hdr->payload_type = payload; |
| 534 | } |
| 535 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 536 | /* |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 537 | * The below code is for dispatching. We have a dedicated port for |
| 538 | * the data coming from the net and one to discover the BTS. |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 539 | */ |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 540 | static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) |
| 541 | { |
| 542 | if (!tap->enabled) |
| 543 | return 0; |
| 544 | |
| 545 | return sendto(fd, buf, len, 0, |
| 546 | (struct sockaddr *)&tap->forward, sizeof(tap->forward)); |
| 547 | } |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 548 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 549 | static int mgcp_send_transcoder(struct mgcp_rtp_end *end, |
| 550 | struct mgcp_config *cfg, int is_rtp, |
| 551 | const char *buf, int len) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 552 | { |
| 553 | int rc; |
| 554 | int port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 555 | struct sockaddr_in addr; |
| 556 | |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 557 | port = is_rtp ? end->rtp_port : end->rtcp_port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 558 | |
| 559 | addr.sin_family = AF_INET; |
| 560 | addr.sin_addr = cfg->transcoder_in; |
Holger Hans Peter Freyther | 5f2cd84 | 2010-11-01 21:53:39 +0100 | [diff] [blame] | 561 | addr.sin_port = port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 562 | |
| 563 | rc = sendto(is_rtp ? |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 564 | end->rtp.fd : |
| 565 | end->rtcp.fd, buf, len, 0, |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 566 | (struct sockaddr *) &addr, sizeof(addr)); |
| 567 | |
| 568 | if (rc != len) |
| 569 | LOGP(DMGCP, LOGL_ERROR, |
| 570 | "Failed to send data to the transcoder: %s\n", |
| 571 | strerror(errno)); |
| 572 | |
| 573 | return rc; |
| 574 | } |
| 575 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 576 | int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 577 | struct sockaddr_in *addr, char *buf, int rc) |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 578 | { |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 579 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 580 | struct mgcp_rtp_end *rtp_end; |
| 581 | struct mgcp_rtp_state *rtp_state; |
| 582 | int tap_idx; |
| 583 | |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 584 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 585 | if (tcfg->audio_loop) |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 586 | dest = !dest; |
| 587 | |
| 588 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 589 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 590 | dest = !dest; |
| 591 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 592 | if (dest == MGCP_DEST_NET) { |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 593 | rtp_end = &endp->net_end; |
| 594 | rtp_state = &endp->bts_state; |
| 595 | tap_idx = MGCP_TAP_NET_OUT; |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 596 | } else { |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 597 | rtp_end = &endp->bts_end; |
| 598 | rtp_state = &endp->net_state; |
| 599 | tap_idx = MGCP_TAP_BTS_OUT; |
| 600 | } |
| 601 | |
Jacob Erlbeck | 0970bab | 2013-12-19 12:13:32 +0100 | [diff] [blame] | 602 | if (!rtp_end->output_enabled) |
| 603 | rtp_end->dropped_packets += 1; |
| 604 | else if (is_rtp) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 605 | int cont; |
| 606 | int nbytes = 0; |
| 607 | int len = rc; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 608 | do { |
| 609 | cont = endp->cfg->rtp_processing_cb(endp, rtp_end, |
| 610 | buf, &len, RTP_BUF_SIZE); |
| 611 | if (cont < 0) |
| 612 | break; |
| 613 | |
Holger Hans Peter Freyther | 1fc1ed2 | 2014-07-02 21:54:14 +0200 | [diff] [blame] | 614 | mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 615 | forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx], |
| 616 | buf, len); |
| 617 | rc = mgcp_udp_send(rtp_end->rtp.fd, |
| 618 | &rtp_end->addr, |
| 619 | rtp_end->rtp_port, buf, len); |
| 620 | |
| 621 | if (rc <= 0) |
| 622 | return rc; |
| 623 | nbytes += rc; |
| 624 | len = cont; |
| 625 | } while (len > 0); |
| 626 | return nbytes; |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 627 | } else if (!tcfg->omit_rtcp) { |
| 628 | return mgcp_udp_send(rtp_end->rtcp.fd, |
| 629 | &rtp_end->addr, |
| 630 | rtp_end->rtcp_port, buf, rc); |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 631 | } |
Holger Hans Peter Freyther | 8c3d069 | 2012-09-11 12:31:25 +0200 | [diff] [blame] | 632 | |
| 633 | return 0; |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 634 | } |
| 635 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 636 | static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 637 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 638 | { |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 639 | int rc; |
| 640 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 641 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 642 | rc = recvfrom(fd, buf, bufsize, 0, |
| 643 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 644 | if (rc < 0) { |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 645 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 646 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | 39a97e2 | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 651 | if (!endp->allocated) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 652 | return -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 653 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 654 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 655 | |
| 656 | return rc; |
| 657 | } |
| 658 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 659 | static int rtp_data_net(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 660 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 661 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 662 | struct sockaddr_in addr; |
| 663 | struct mgcp_endpoint *endp; |
| 664 | int rc, proto; |
| 665 | |
| 666 | endp = (struct mgcp_endpoint *) fd->data; |
| 667 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 668 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 669 | if (rc <= 0) |
| 670 | return -1; |
| 671 | |
| 672 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 673 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | eedb453 | 2012-11-12 10:55:29 +0100 | [diff] [blame] | 674 | "Endpoint 0x%x data from wrong address %s vs. ", |
| 675 | ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); |
| 676 | LOGPC(DMGCP, LOGL_ERROR, |
| 677 | "%s\n", inet_ntoa(endp->net_end.addr)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 678 | return -1; |
| 679 | } |
| 680 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 681 | switch(endp->type) { |
| 682 | case MGCP_RTP_DEFAULT: |
| 683 | case MGCP_RTP_TRANSCODED: |
| 684 | if (endp->net_end.rtp_port != addr.sin_port && |
| 685 | endp->net_end.rtcp_port != addr.sin_port) { |
| 686 | LOGP(DMGCP, LOGL_ERROR, |
| 687 | "Data from wrong source port %d on 0x%x\n", |
| 688 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 689 | return -1; |
| 690 | } |
| 691 | break; |
| 692 | case MGCP_OSMUX_BSC: |
| 693 | case MGCP_OSMUX_BSC_NAT: |
| 694 | break; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 698 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 699 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 700 | ENDPOINT_NUMBER(endp)); |
| 701 | return 0; |
| 702 | } |
| 703 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 704 | proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 705 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 706 | endp->net_end.octets += rc; |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 707 | |
| 708 | forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 709 | |
| 710 | switch (endp->type) { |
| 711 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 712 | return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP, |
| 713 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 714 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 715 | return mgcp_send_transcoder(&endp->trans_net, endp->cfg, |
| 716 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 717 | case MGCP_OSMUX_BSC_NAT: |
| 718 | return osmux_xfrm_to_osmux(MGCP_DEST_BTS, buf, rc, endp); |
| 719 | case MGCP_OSMUX_BSC: /* Should not happen */ |
| 720 | break; |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 724 | endp->type, ENDPOINT_NUMBER(endp)); |
| 725 | return 0; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 726 | } |
| 727 | |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 728 | static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) |
| 729 | { |
| 730 | struct mgcp_config *cfg = endp->cfg; |
| 731 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 732 | if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) { |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 733 | if (!cfg->bts_ip || |
| 734 | memcmp(&addr->sin_addr, |
| 735 | &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || |
| 736 | memcmp(&addr->sin_addr, |
| 737 | &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { |
| 738 | |
| 739 | endp->bts_end.rtp_port = addr->sin_port; |
| 740 | endp->bts_end.addr = addr->sin_addr; |
| 741 | |
| 742 | LOGP(DMGCP, LOGL_NOTICE, |
| 743 | "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 744 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), |
| 745 | ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); |
| 746 | } |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 747 | } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) { |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 748 | if (memcmp(&endp->bts_end.addr, &addr->sin_addr, |
| 749 | sizeof(endp->bts_end.addr)) == 0) { |
| 750 | endp->bts_end.rtcp_port = addr->sin_port; |
| 751 | } |
| 752 | } |
| 753 | } |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 754 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 755 | static int rtp_data_bts(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 756 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 757 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 758 | struct sockaddr_in addr; |
| 759 | struct mgcp_endpoint *endp; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 760 | int rc, proto; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 761 | |
| 762 | endp = (struct mgcp_endpoint *) fd->data; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 763 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 764 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 765 | if (rc <= 0) |
| 766 | return -1; |
| 767 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 768 | proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 769 | |
| 770 | /* We have no idea who called us, maybe it is the BTS. */ |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 771 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 772 | discover_bts(endp, proto, &addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 773 | |
Holger Hans Peter Freyther | ea97fbf | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 774 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 775 | LOGP(DMGCP, LOGL_ERROR, |
| 776 | "Data from wrong bts %s on 0x%x\n", |
| 777 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 782 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 783 | LOGP(DMGCP, LOGL_ERROR, |
| 784 | "Data from wrong bts source port %d on 0x%x\n", |
| 785 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 786 | return -1; |
| 787 | } |
| 788 | |
Holger Hans Peter Freyther | b3c206a | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 789 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 790 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 791 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n", |
Holger Hans Peter Freyther | aa9d3ce | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 792 | ENDPOINT_NUMBER(endp)); |
| 793 | return 0; |
| 794 | } |
| 795 | |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 796 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 797 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 798 | endp->bts_end.octets += rc; |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 799 | |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 800 | forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 801 | |
| 802 | switch (endp->type) { |
| 803 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 804 | return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP, |
| 805 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 806 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 807 | return mgcp_send_transcoder(&endp->trans_bts, endp->cfg, |
| 808 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 809 | case MGCP_OSMUX_BSC: |
| 810 | /* OSMUX translation: BTS -> BSC */ |
| 811 | return osmux_xfrm_to_osmux(MGCP_DEST_NET, buf, rc, endp); |
| 812 | case MGCP_OSMUX_BSC_NAT: |
| 813 | break; /* Should not happen */ |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 817 | endp->type, ENDPOINT_NUMBER(endp)); |
| 818 | return 0; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 819 | } |
| 820 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 821 | static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 822 | int dest, struct osmo_fd *fd) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 823 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 824 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 825 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 826 | struct mgcp_config *cfg; |
| 827 | int rc, proto; |
| 828 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 829 | cfg = _endp->cfg; |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 830 | rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 831 | if (rc <= 0) |
| 832 | return -1; |
| 833 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 834 | proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 835 | |
| 836 | if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { |
| 837 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 838 | "Data not coming from transcoder dest: %d %s on 0x%x\n", |
| 839 | dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 840 | return -1; |
| 841 | } |
| 842 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 843 | if (end->rtp_port != addr.sin_port && |
| 844 | end->rtcp_port != addr.sin_port) { |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 845 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 846 | "Data from wrong transcoder dest %d source port %d on 0x%x\n", |
| 847 | dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 852 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 853 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n", |
| 854 | dest, ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 858 | end->packets += 1; |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 859 | return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc); |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 862 | static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 863 | { |
| 864 | struct mgcp_endpoint *endp; |
| 865 | endp = (struct mgcp_endpoint *) fd->data; |
| 866 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 867 | return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd); |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 870 | static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 871 | { |
| 872 | struct mgcp_endpoint *endp; |
| 873 | endp = (struct mgcp_endpoint *) fd->data; |
| 874 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 875 | return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 878 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 879 | { |
| 880 | struct sockaddr_in addr; |
| 881 | int on = 1; |
| 882 | |
| 883 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 884 | if (fd->fd < 0) { |
| 885 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 886 | return -1; |
| 887 | } |
| 888 | |
| 889 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 890 | memset(&addr, 0, sizeof(addr)); |
| 891 | addr.sin_family = AF_INET; |
| 892 | addr.sin_port = htons(port); |
| 893 | inet_aton(source_addr, &addr.sin_addr); |
| 894 | |
| 895 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | 6f36e92 | 2010-08-06 03:00:17 +0800 | [diff] [blame] | 896 | close(fd->fd); |
| 897 | fd->fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 898 | return -1; |
| 899 | } |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
Holger Hans Peter Freyther | 9be675e | 2015-01-21 11:39:47 +0100 | [diff] [blame] | 904 | int mgcp_set_ip_tos(int fd, int tos) |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 905 | { |
| 906 | int ret; |
| 907 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 908 | &tos, sizeof(tos)); |
| 909 | return ret != 0; |
| 910 | } |
| 911 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 912 | static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 913 | { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 914 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp, |
| 915 | rtp_end->local_port) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 916 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 917 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 918 | goto cleanup0; |
| 919 | } |
| 920 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 921 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp, |
| 922 | rtp_end->local_port + 1) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 923 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 924 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 925 | goto cleanup1; |
| 926 | } |
| 927 | |
Holger Hans Peter Freyther | 9be675e | 2015-01-21 11:39:47 +0100 | [diff] [blame] | 928 | mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 929 | mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 930 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 931 | rtp_end->rtp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 932 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 933 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 934 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 935 | goto cleanup2; |
| 936 | } |
| 937 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 938 | rtp_end->rtcp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 939 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 940 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 941 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 942 | goto cleanup3; |
| 943 | } |
| 944 | |
| 945 | return 0; |
| 946 | |
| 947 | cleanup3: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 948 | osmo_fd_unregister(&rtp_end->rtp); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 949 | cleanup2: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 950 | close(rtp_end->rtcp.fd); |
| 951 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 952 | cleanup1: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 953 | close(rtp_end->rtp.fd); |
| 954 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 955 | cleanup0: |
| 956 | return -1; |
| 957 | } |
| 958 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 959 | static int int_bind(const char *port, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 960 | struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned), |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 961 | struct mgcp_endpoint *_endp, int rtp_port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 962 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 963 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
| 964 | LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n", |
| 965 | port, ENDPOINT_NUMBER(_endp)); |
| 966 | mgcp_free_rtp_port(end); |
Holger Hans Peter Freyther | 7fe2a3d | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 969 | end->local_port = rtp_port; |
| 970 | end->rtp.cb = cb; |
| 971 | end->rtp.data = _endp; |
| 972 | end->rtcp.data = _endp; |
| 973 | end->rtcp.cb = cb; |
| 974 | return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp)); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 979 | { |
| 980 | return int_bind("bts-port", &endp->bts_end, |
| 981 | rtp_data_bts, endp, rtp_port); |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 985 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 986 | return int_bind("net-port", &endp->net_end, |
| 987 | rtp_data_net, endp, rtp_port); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 988 | } |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 989 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 990 | int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 991 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 992 | return int_bind("trans-net", &endp->trans_net, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 993 | rtp_data_trans_net, endp, rtp_port); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 994 | } |
| 995 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 996 | int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 997 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 998 | return int_bind("trans-bts", &endp->trans_bts, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 999 | rtp_data_trans_bts, endp, rtp_port); |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1002 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 1003 | { |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1004 | if (end->rtp.fd != -1) { |
| 1005 | close(end->rtp.fd); |
| 1006 | end->rtp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1007 | osmo_fd_unregister(&end->rtp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | if (end->rtcp.fd != -1) { |
| 1011 | close(end->rtcp.fd); |
| 1012 | end->rtcp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1013 | osmo_fd_unregister(&end->rtcp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1018 | |
| 1019 | |
| 1020 | void mgcp_state_calc_loss(struct mgcp_rtp_state *state, |
| 1021 | struct mgcp_rtp_end *end, uint32_t *expected, |
| 1022 | int *loss) |
| 1023 | { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 1024 | *expected = state->stats_cycles + state->stats_max_seq; |
| 1025 | *expected = *expected - state->stats_base_seq + 1; |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1026 | |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 1027 | if (!state->stats_initialized) { |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1028 | *expected = 0; |
| 1029 | *loss = 0; |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * Make sure the sign is correct and use the biggest |
| 1035 | * positive/negative number that fits. |
| 1036 | */ |
| 1037 | *loss = *expected - end->packets; |
| 1038 | if (*expected < end->packets) { |
| 1039 | if (*loss > 0) |
| 1040 | *loss = INT_MIN; |
| 1041 | } else { |
| 1042 | if (*loss < 0) |
| 1043 | *loss = INT_MAX; |
| 1044 | } |
| 1045 | } |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1046 | |
| 1047 | uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state) |
| 1048 | { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 1049 | if (!state->stats_initialized) |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1050 | return 0; |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame] | 1051 | return state->stats_jitter >> 4; |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1052 | } |