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 | { |
| 339 | uint16_t udelta; |
| 340 | int32_t d; |
| 341 | |
| 342 | /* initialize or re-initialize */ |
| 343 | if (!state->stats_initialized || state->stats_ssrc != ssrc) { |
| 344 | state->stats_initialized = 1; |
| 345 | state->stats_base_seq = seq; |
| 346 | state->stats_max_seq = seq - 1; |
| 347 | state->stats_ssrc = ssrc; |
| 348 | state->stats_jitter = 0; |
| 349 | state->stats_transit = transit; |
| 350 | state->stats_cycles = 0; |
| 351 | } |
| 352 | |
| 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 | } |
| 371 | |
| 372 | /* |
| 373 | * Calculate the jitter between the two packages. The TS should be |
| 374 | * taken closer to the read function. This was taken from the |
| 375 | * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate |
| 376 | * resolution. |
| 377 | */ |
| 378 | d = transit - state->stats_transit; |
| 379 | state->stats_transit = transit; |
| 380 | if (d < 0) |
| 381 | d = -d; |
| 382 | state->stats_jitter += d - ((state->stats_jitter + 8) >> 4); |
| 383 | state->stats_max_seq = seq; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
Holger Hans Peter Freyther | 1e85af6 | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 388 | /** |
| 389 | * The RFC 3550 Appendix A assumes there are multiple sources but |
| 390 | * 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] | 391 | * 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] | 392 | * is only one source. |
Holger Hans Peter Freyther | 2a7ab86 | 2014-10-06 17:16:25 +0200 | [diff] [blame] | 393 | * 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] | 394 | * we receive will be seen as a switch in streams. |
| 395 | */ |
Jacob Erlbeck | d62419b | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 396 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 397 | struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr, |
| 398 | char *data, int len) |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 399 | { |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 400 | uint32_t arrival_time; |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 401 | int32_t transit; |
| 402 | uint16_t seq; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 403 | uint32_t timestamp, ssrc; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 404 | struct rtp_hdr *rtp_hdr; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 405 | int payload = rtp_end->codec.payload_type; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 406 | |
| 407 | if (len < sizeof(*rtp_hdr)) |
| 408 | return; |
| 409 | |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 410 | rtp_hdr = (struct rtp_hdr *) data; |
| 411 | seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 412 | timestamp = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 413 | arrival_time = get_current_ts(rtp_end->codec.rate); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 414 | ssrc = ntohl(rtp_hdr->ssrc); |
Jacob Erlbeck | b281e4e | 2014-01-30 21:01:36 +0100 | [diff] [blame] | 415 | transit = arrival_time - timestamp; |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 416 | |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 417 | if (!state->initialized) { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 418 | state->initialized = 1; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 419 | state->in_stream.last_seq = seq - 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 420 | state->in_stream.ssrc = state->orig_ssrc = ssrc; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 421 | state->in_stream.last_tsdelta = 0; |
Jacob Erlbeck | f6ec0e9 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 422 | state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 423 | state->out_stream = state->in_stream; |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 424 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 425 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 426 | LOGP(DMGCP, LOGL_INFO, |
| 427 | "Initializing stream on 0x%x SSRC: %u timestamp: %u " |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 428 | "pkt-duration: %d, from %s:%d in %d\n", |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 429 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 430 | state->seq_offset, state->packet_duration, |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 431 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 432 | endp->conn_mode); |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 433 | if (state->packet_duration == 0) { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 434 | state->packet_duration = rtp_end->codec.rate * 20 / 1000; |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 435 | LOGP(DMGCP, LOGL_NOTICE, |
| 436 | "Fixed packet duration is not available on 0x%x, " |
| 437 | "using fixed 20ms instead: %d from %s:%d in %d\n", |
| 438 | ENDPOINT_NUMBER(endp), state->packet_duration, |
| 439 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 440 | endp->conn_mode); |
| 441 | } |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 442 | } else if (state->in_stream.ssrc != ssrc) { |
Jacob Erlbeck | 5e9549e | 2013-12-05 12:20:05 +0100 | [diff] [blame] | 443 | LOGP(DMGCP, LOGL_NOTICE, |
| 444 | "The SSRC changed on 0x%x: %u -> %u " |
| 445 | "from %s:%d in %d\n", |
| 446 | ENDPOINT_NUMBER(endp), |
| 447 | state->in_stream.ssrc, rtp_hdr->ssrc, |
| 448 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 449 | endp->conn_mode); |
| 450 | |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 451 | state->in_stream.ssrc = ssrc; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 452 | if (rtp_end->force_constant_ssrc) { |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 453 | int16_t delta_seq; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 454 | |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 455 | /* Always increment seqno by 1 */ |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 456 | state->seq_offset = |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 457 | (state->out_stream.last_seq + 1) - seq; |
| 458 | |
| 459 | /* Estimate number of packets that would have been sent */ |
| 460 | delta_seq = |
| 461 | (arrival_time - state->in_stream.last_arrival_time |
| 462 | + state->packet_duration/2) / |
| 463 | state->packet_duration; |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 464 | |
| 465 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 466 | delta_seq, timestamp); |
| 467 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 468 | state->patch_ssrc = 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 469 | ssrc = state->orig_ssrc; |
Jacob Erlbeck | e2292f3 | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 470 | if (rtp_end->force_constant_ssrc != -1) |
| 471 | rtp_end->force_constant_ssrc -= 1; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 472 | |
| 473 | LOGP(DMGCP, LOGL_NOTICE, |
| 474 | "SSRC patching enabled on 0x%x SSRC: %u " |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 475 | "SeqNo offset: %d, TS offset: %d " |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 476 | "from %s:%d in %d\n", |
| 477 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 478 | state->seq_offset, state->timestamp_offset, |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 479 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 480 | endp->conn_mode); |
| 481 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 482 | |
| 483 | state->in_stream.last_tsdelta = 0; |
| 484 | } else { |
| 485 | /* Compute current per-packet timestamp delta */ |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 486 | check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr, |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 487 | seq, timestamp, "input", |
| 488 | &state->in_stream.last_tsdelta); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 489 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 490 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 491 | ssrc = state->orig_ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 492 | } |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 493 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 494 | /* Save before patching */ |
| 495 | state->in_stream.last_timestamp = timestamp; |
| 496 | state->in_stream.last_seq = seq; |
Jacob Erlbeck | eacc9b9 | 2014-01-30 21:01:35 +0100 | [diff] [blame] | 497 | state->in_stream.last_arrival_time = arrival_time; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 498 | |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 499 | if (rtp_end->force_aligned_timing && |
| 500 | state->out_stream.ssrc == ssrc && state->packet_duration) |
| 501 | /* Align the timestamp offset */ |
| 502 | align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp); |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 503 | |
| 504 | /* Store the updated SSRC back to the packet */ |
| 505 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 506 | rtp_hdr->ssrc = htonl(ssrc); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 507 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 508 | /* Apply the offset and store it back to the packet. |
| 509 | * This won't change anything if the offset is 0, so the conditional is |
| 510 | * omitted. */ |
| 511 | seq += state->seq_offset; |
| 512 | rtp_hdr->sequence = htons(seq); |
| 513 | timestamp += state->timestamp_offset; |
| 514 | rtp_hdr->timestamp = htonl(timestamp); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 515 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 516 | /* Check again, whether the timestamps are still valid */ |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 517 | if (state->out_stream.ssrc == ssrc) |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 518 | check_rtp_timestamp(endp, state, &state->out_stream, rtp_end, |
| 519 | addr, seq, timestamp, "output", |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 520 | &state->out_stream.last_tsdelta); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 521 | |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 522 | mgcp_rtp_annex_count(endp, state, seq, transit, ssrc); |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 523 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 524 | /* Save output values */ |
| 525 | state->out_stream.last_seq = seq; |
| 526 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 527 | state->out_stream.ssrc = ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 528 | |
Holger Hans Peter Freyther | 7279d24 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 529 | if (payload < 0) |
| 530 | return; |
| 531 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 532 | rtp_hdr->payload_type = payload; |
| 533 | } |
| 534 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 535 | /* |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 536 | * The below code is for dispatching. We have a dedicated port for |
| 537 | * 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] | 538 | */ |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 539 | static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) |
| 540 | { |
| 541 | if (!tap->enabled) |
| 542 | return 0; |
| 543 | |
| 544 | return sendto(fd, buf, len, 0, |
| 545 | (struct sockaddr *)&tap->forward, sizeof(tap->forward)); |
| 546 | } |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 547 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 548 | static int mgcp_send_transcoder(struct mgcp_rtp_end *end, |
| 549 | struct mgcp_config *cfg, int is_rtp, |
| 550 | const char *buf, int len) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 551 | { |
| 552 | int rc; |
| 553 | int port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 554 | struct sockaddr_in addr; |
| 555 | |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 556 | port = is_rtp ? end->rtp_port : end->rtcp_port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 557 | |
| 558 | addr.sin_family = AF_INET; |
| 559 | addr.sin_addr = cfg->transcoder_in; |
Holger Hans Peter Freyther | 5f2cd84 | 2010-11-01 21:53:39 +0100 | [diff] [blame] | 560 | addr.sin_port = port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 561 | |
| 562 | rc = sendto(is_rtp ? |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 563 | end->rtp.fd : |
| 564 | end->rtcp.fd, buf, len, 0, |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 565 | (struct sockaddr *) &addr, sizeof(addr)); |
| 566 | |
| 567 | if (rc != len) |
| 568 | LOGP(DMGCP, LOGL_ERROR, |
| 569 | "Failed to send data to the transcoder: %s\n", |
| 570 | strerror(errno)); |
| 571 | |
| 572 | return rc; |
| 573 | } |
| 574 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 575 | int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 576 | struct sockaddr_in *addr, char *buf, int rc) |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 577 | { |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 578 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 579 | struct mgcp_rtp_end *rtp_end; |
| 580 | struct mgcp_rtp_state *rtp_state; |
| 581 | int tap_idx; |
| 582 | |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 583 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 584 | if (tcfg->audio_loop) |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 585 | dest = !dest; |
| 586 | |
| 587 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 588 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 589 | dest = !dest; |
| 590 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 591 | if (dest == MGCP_DEST_NET) { |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 592 | rtp_end = &endp->net_end; |
| 593 | rtp_state = &endp->bts_state; |
| 594 | tap_idx = MGCP_TAP_NET_OUT; |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 595 | } else { |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 596 | rtp_end = &endp->bts_end; |
| 597 | rtp_state = &endp->net_state; |
| 598 | tap_idx = MGCP_TAP_BTS_OUT; |
| 599 | } |
| 600 | |
Jacob Erlbeck | 0970bab | 2013-12-19 12:13:32 +0100 | [diff] [blame] | 601 | if (!rtp_end->output_enabled) |
| 602 | rtp_end->dropped_packets += 1; |
| 603 | else if (is_rtp) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 604 | int cont; |
| 605 | int nbytes = 0; |
| 606 | int len = rc; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 607 | do { |
| 608 | cont = endp->cfg->rtp_processing_cb(endp, rtp_end, |
| 609 | buf, &len, RTP_BUF_SIZE); |
| 610 | if (cont < 0) |
| 611 | break; |
| 612 | |
Holger Hans Peter Freyther | 1fc1ed2 | 2014-07-02 21:54:14 +0200 | [diff] [blame] | 613 | mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 614 | forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx], |
| 615 | buf, len); |
| 616 | rc = mgcp_udp_send(rtp_end->rtp.fd, |
| 617 | &rtp_end->addr, |
| 618 | rtp_end->rtp_port, buf, len); |
| 619 | |
| 620 | if (rc <= 0) |
| 621 | return rc; |
| 622 | nbytes += rc; |
| 623 | len = cont; |
| 624 | } while (len > 0); |
| 625 | return nbytes; |
Jacob Erlbeck | b830008 | 2013-12-19 10:00:34 +0100 | [diff] [blame] | 626 | } else if (!tcfg->omit_rtcp) { |
| 627 | return mgcp_udp_send(rtp_end->rtcp.fd, |
| 628 | &rtp_end->addr, |
| 629 | rtp_end->rtcp_port, buf, rc); |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 630 | } |
Holger Hans Peter Freyther | 8c3d069 | 2012-09-11 12:31:25 +0200 | [diff] [blame] | 631 | |
| 632 | return 0; |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 633 | } |
| 634 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 635 | static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 636 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 637 | { |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 638 | int rc; |
| 639 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 640 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 641 | rc = recvfrom(fd, buf, bufsize, 0, |
| 642 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 643 | if (rc < 0) { |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 644 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 645 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | /* 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] | 650 | if (!endp->allocated) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 651 | return -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 652 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 653 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 654 | |
| 655 | return rc; |
| 656 | } |
| 657 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 658 | 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] | 659 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 660 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 661 | struct sockaddr_in addr; |
| 662 | struct mgcp_endpoint *endp; |
| 663 | int rc, proto; |
| 664 | |
| 665 | endp = (struct mgcp_endpoint *) fd->data; |
| 666 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 667 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 668 | if (rc <= 0) |
| 669 | return -1; |
| 670 | |
| 671 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 672 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | eedb453 | 2012-11-12 10:55:29 +0100 | [diff] [blame] | 673 | "Endpoint 0x%x data from wrong address %s vs. ", |
| 674 | ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); |
| 675 | LOGPC(DMGCP, LOGL_ERROR, |
| 676 | "%s\n", inet_ntoa(endp->net_end.addr)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 677 | return -1; |
| 678 | } |
| 679 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 680 | switch(endp->type) { |
| 681 | case MGCP_RTP_DEFAULT: |
| 682 | case MGCP_RTP_TRANSCODED: |
| 683 | if (endp->net_end.rtp_port != addr.sin_port && |
| 684 | endp->net_end.rtcp_port != addr.sin_port) { |
| 685 | LOGP(DMGCP, LOGL_ERROR, |
| 686 | "Data from wrong source port %d on 0x%x\n", |
| 687 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 688 | return -1; |
| 689 | } |
| 690 | break; |
| 691 | case MGCP_OSMUX_BSC: |
| 692 | case MGCP_OSMUX_BSC_NAT: |
| 693 | break; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 697 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 698 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 699 | ENDPOINT_NUMBER(endp)); |
| 700 | return 0; |
| 701 | } |
| 702 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 703 | 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] | 704 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 705 | endp->net_end.octets += rc; |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 706 | |
| 707 | 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] | 708 | |
| 709 | switch (endp->type) { |
| 710 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 711 | return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP, |
| 712 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 713 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 714 | return mgcp_send_transcoder(&endp->trans_net, endp->cfg, |
| 715 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 716 | case MGCP_OSMUX_BSC_NAT: |
| 717 | return osmux_xfrm_to_osmux(MGCP_DEST_BTS, buf, rc, endp); |
| 718 | case MGCP_OSMUX_BSC: /* Should not happen */ |
| 719 | break; |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 723 | endp->type, ENDPOINT_NUMBER(endp)); |
| 724 | return 0; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 725 | } |
| 726 | |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 727 | static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) |
| 728 | { |
| 729 | struct mgcp_config *cfg = endp->cfg; |
| 730 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 731 | 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] | 732 | if (!cfg->bts_ip || |
| 733 | memcmp(&addr->sin_addr, |
| 734 | &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || |
| 735 | memcmp(&addr->sin_addr, |
| 736 | &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { |
| 737 | |
| 738 | endp->bts_end.rtp_port = addr->sin_port; |
| 739 | endp->bts_end.addr = addr->sin_addr; |
| 740 | |
| 741 | LOGP(DMGCP, LOGL_NOTICE, |
| 742 | "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 743 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), |
| 744 | ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); |
| 745 | } |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 746 | } 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] | 747 | if (memcmp(&endp->bts_end.addr, &addr->sin_addr, |
| 748 | sizeof(endp->bts_end.addr)) == 0) { |
| 749 | endp->bts_end.rtcp_port = addr->sin_port; |
| 750 | } |
| 751 | } |
| 752 | } |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 753 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 754 | 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] | 755 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 756 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 757 | struct sockaddr_in addr; |
| 758 | struct mgcp_endpoint *endp; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 759 | int rc, proto; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 760 | |
| 761 | endp = (struct mgcp_endpoint *) fd->data; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 762 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 763 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 764 | if (rc <= 0) |
| 765 | return -1; |
| 766 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 767 | 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] | 768 | |
| 769 | /* 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] | 770 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 771 | discover_bts(endp, proto, &addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 772 | |
Holger Hans Peter Freyther | ea97fbf | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 773 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 774 | LOGP(DMGCP, LOGL_ERROR, |
| 775 | "Data from wrong bts %s on 0x%x\n", |
| 776 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 781 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 782 | LOGP(DMGCP, LOGL_ERROR, |
| 783 | "Data from wrong bts source port %d on 0x%x\n", |
| 784 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 785 | return -1; |
| 786 | } |
| 787 | |
Holger Hans Peter Freyther | b3c206a | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 788 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 789 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 790 | 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] | 791 | ENDPOINT_NUMBER(endp)); |
| 792 | return 0; |
| 793 | } |
| 794 | |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 795 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 796 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 797 | endp->bts_end.octets += rc; |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 798 | |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 799 | 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] | 800 | |
| 801 | switch (endp->type) { |
| 802 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 803 | return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP, |
| 804 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 805 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 806 | return mgcp_send_transcoder(&endp->trans_bts, endp->cfg, |
| 807 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 808 | case MGCP_OSMUX_BSC: |
| 809 | /* OSMUX translation: BTS -> BSC */ |
| 810 | return osmux_xfrm_to_osmux(MGCP_DEST_NET, buf, rc, endp); |
| 811 | case MGCP_OSMUX_BSC_NAT: |
| 812 | break; /* Should not happen */ |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 816 | endp->type, ENDPOINT_NUMBER(endp)); |
| 817 | return 0; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 818 | } |
| 819 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 820 | 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] | 821 | int dest, struct osmo_fd *fd) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 822 | { |
Jacob Erlbeck | a0d64ce | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 823 | char buf[RTP_BUF_SIZE]; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 824 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 825 | struct mgcp_config *cfg; |
| 826 | int rc, proto; |
| 827 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 828 | cfg = _endp->cfg; |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 829 | rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 830 | if (rc <= 0) |
| 831 | return -1; |
| 832 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 833 | proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 834 | |
| 835 | if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { |
| 836 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 837 | "Data not coming from transcoder dest: %d %s on 0x%x\n", |
| 838 | dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 839 | return -1; |
| 840 | } |
| 841 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 842 | if (end->rtp_port != addr.sin_port && |
| 843 | end->rtcp_port != addr.sin_port) { |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 844 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 845 | "Data from wrong transcoder dest %d source port %d on 0x%x\n", |
| 846 | dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 847 | return -1; |
| 848 | } |
| 849 | |
| 850 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 851 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 852 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n", |
| 853 | dest, ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 857 | end->packets += 1; |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 858 | 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] | 859 | } |
| 860 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 861 | 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] | 862 | { |
| 863 | struct mgcp_endpoint *endp; |
| 864 | endp = (struct mgcp_endpoint *) fd->data; |
| 865 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 866 | 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] | 867 | } |
| 868 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 869 | 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] | 870 | { |
| 871 | struct mgcp_endpoint *endp; |
| 872 | endp = (struct mgcp_endpoint *) fd->data; |
| 873 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 874 | 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] | 875 | } |
| 876 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 877 | 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] | 878 | { |
| 879 | struct sockaddr_in addr; |
| 880 | int on = 1; |
| 881 | |
| 882 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 883 | if (fd->fd < 0) { |
| 884 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 889 | memset(&addr, 0, sizeof(addr)); |
| 890 | addr.sin_family = AF_INET; |
| 891 | addr.sin_port = htons(port); |
| 892 | inet_aton(source_addr, &addr.sin_addr); |
| 893 | |
| 894 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | 6f36e92 | 2010-08-06 03:00:17 +0800 | [diff] [blame] | 895 | close(fd->fd); |
| 896 | fd->fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 897 | return -1; |
| 898 | } |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 903 | static int set_ip_tos(int fd, int tos) |
| 904 | { |
| 905 | int ret; |
| 906 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 907 | &tos, sizeof(tos)); |
| 908 | return ret != 0; |
| 909 | } |
| 910 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 911 | 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] | 912 | { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 913 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp, |
| 914 | rtp_end->local_port) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 915 | 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] | 916 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 917 | goto cleanup0; |
| 918 | } |
| 919 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 920 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp, |
| 921 | rtp_end->local_port + 1) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 922 | 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] | 923 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 924 | goto cleanup1; |
| 925 | } |
| 926 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 927 | set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 928 | set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 929 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 930 | rtp_end->rtp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 931 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 932 | 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] | 933 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 934 | goto cleanup2; |
| 935 | } |
| 936 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 937 | rtp_end->rtcp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 938 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 939 | 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] | 940 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 941 | goto cleanup3; |
| 942 | } |
| 943 | |
| 944 | return 0; |
| 945 | |
| 946 | cleanup3: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 947 | osmo_fd_unregister(&rtp_end->rtp); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 948 | cleanup2: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 949 | close(rtp_end->rtcp.fd); |
| 950 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 951 | cleanup1: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 952 | close(rtp_end->rtp.fd); |
| 953 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 954 | cleanup0: |
| 955 | return -1; |
| 956 | } |
| 957 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 958 | static int int_bind(const char *port, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 959 | 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] | 960 | struct mgcp_endpoint *_endp, int rtp_port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 961 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 962 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
| 963 | LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n", |
| 964 | port, ENDPOINT_NUMBER(_endp)); |
| 965 | mgcp_free_rtp_port(end); |
Holger Hans Peter Freyther | 7fe2a3d | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 966 | } |
| 967 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 968 | end->local_port = rtp_port; |
| 969 | end->rtp.cb = cb; |
| 970 | end->rtp.data = _endp; |
| 971 | end->rtcp.data = _endp; |
| 972 | end->rtcp.cb = cb; |
| 973 | return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp)); |
| 974 | } |
| 975 | |
| 976 | |
| 977 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 978 | { |
| 979 | return int_bind("bts-port", &endp->bts_end, |
| 980 | rtp_data_bts, endp, rtp_port); |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 984 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 985 | return int_bind("net-port", &endp->net_end, |
| 986 | rtp_data_net, endp, rtp_port); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 987 | } |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 988 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 989 | 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] | 990 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 991 | return int_bind("trans-net", &endp->trans_net, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 992 | rtp_data_trans_net, endp, rtp_port); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 993 | } |
| 994 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 995 | int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 996 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 997 | return int_bind("trans-bts", &endp->trans_bts, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 998 | rtp_data_trans_bts, endp, rtp_port); |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1001 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 1002 | { |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1003 | if (end->rtp.fd != -1) { |
| 1004 | close(end->rtp.fd); |
| 1005 | end->rtp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1006 | osmo_fd_unregister(&end->rtp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | if (end->rtcp.fd != -1) { |
| 1010 | close(end->rtcp.fd); |
| 1011 | end->rtcp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1012 | osmo_fd_unregister(&end->rtcp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1017 | |
| 1018 | |
| 1019 | void mgcp_state_calc_loss(struct mgcp_rtp_state *state, |
| 1020 | struct mgcp_rtp_end *end, uint32_t *expected, |
| 1021 | int *loss) |
| 1022 | { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 1023 | *expected = state->stats_cycles + state->stats_max_seq; |
| 1024 | *expected = *expected - state->stats_base_seq + 1; |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1025 | |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 1026 | if (!state->stats_initialized) { |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1027 | *expected = 0; |
| 1028 | *loss = 0; |
| 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Make sure the sign is correct and use the biggest |
| 1034 | * positive/negative number that fits. |
| 1035 | */ |
| 1036 | *loss = *expected - end->packets; |
| 1037 | if (*expected < end->packets) { |
| 1038 | if (*loss > 0) |
| 1039 | *loss = INT_MIN; |
| 1040 | } else { |
| 1041 | if (*loss < 0) |
| 1042 | *loss = INT_MAX; |
| 1043 | } |
| 1044 | } |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1045 | |
| 1046 | uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state) |
| 1047 | { |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 1048 | if (!state->stats_initialized) |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1049 | return 0; |
Holger Hans Peter Freyther | a5a59c9 | 2014-10-06 20:04:42 +0200 | [diff] [blame^] | 1050 | return state->stats_jitter >> 4; |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1051 | } |