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 | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 37 | |
| 38 | #warning "Make use of the rtp proxy code" |
| 39 | |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 40 | /* attempt to determine byte order */ |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 41 | #include <sys/param.h> |
| 42 | #include <limits.h> |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 43 | #include <time.h> |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 44 | |
| 45 | #ifndef __BYTE_ORDER |
Tobias Engel | aff2071 | 2012-10-24 17:53:50 +0200 | [diff] [blame] | 46 | # ifdef __APPLE__ |
| 47 | # define __BYTE_ORDER __DARWIN_BYTE_ORDER |
| 48 | # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN |
| 49 | # define __BIG_ENDIAN __DARWIN_BIG_ENDIAN |
| 50 | # else |
| 51 | # error "__BYTE_ORDER should be defined by someone" |
| 52 | # endif |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 55 | /* according to rtp_proxy.c RFC 3550 */ |
| 56 | struct rtp_hdr { |
| 57 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | 7dece86 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 58 | uint8_t csrc_count:4, |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 59 | extension:1, |
| 60 | padding:1, |
| 61 | version:2; |
Holger Hans Peter Freyther | 7dece86 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 62 | uint8_t payload_type:7, |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 63 | marker:1; |
| 64 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | 7dece86 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 65 | uint8_t version:2, |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 66 | padding:1, |
| 67 | extension:1, |
| 68 | csrc_count:4; |
Holger Hans Peter Freyther | 7dece86 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 69 | uint8_t marker:1, |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 70 | payload_type:7; |
| 71 | #endif |
Holger Hans Peter Freyther | d340cd3 | 2010-07-23 18:56:01 +0800 | [diff] [blame] | 72 | uint16_t sequence; |
Holger Hans Peter Freyther | d9b18f8 | 2010-07-23 18:55:38 +0800 | [diff] [blame] | 73 | uint32_t timestamp; |
| 74 | uint32_t ssrc; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 75 | } __attribute__((packed)); |
| 76 | |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 77 | #define RTP_SEQ_MOD (1 << 16) |
| 78 | #define RTP_MAX_DROPOUT 3000 |
| 79 | #define RTP_MAX_MISORDER 100 |
| 80 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 81 | |
| 82 | enum { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 83 | MGCP_DEST_NET = 0, |
| 84 | MGCP_DEST_BTS, |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | enum { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 88 | MGCP_PROTO_RTP, |
| 89 | MGCP_PROTO_RTCP, |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 92 | #define MGCP_DUMMY_LOAD 0x23 |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 94 | |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 95 | /** |
| 96 | * This does not need to be a precision timestamp and |
| 97 | * is allowed to wrap quite fast. The returned value is |
| 98 | * milli seconds now. |
| 99 | */ |
| 100 | uint32_t get_current_ts(void) |
| 101 | { |
| 102 | struct timespec tp; |
| 103 | uint64_t ret; |
| 104 | |
| 105 | memset(&tp, 0, sizeof(tp)); |
| 106 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
| 107 | LOGP(DMGCP, LOGL_NOTICE, |
| 108 | "Getting the clock failed.\n"); |
| 109 | |
| 110 | /* convert it to useconds */ |
| 111 | ret = tp.tv_sec; |
| 112 | ret *= 1000; |
| 113 | ret += tp.tv_nsec / 1000 / 1000; |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 118 | static int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, |
| 119 | int len) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 120 | { |
| 121 | struct sockaddr_in out; |
| 122 | out.sin_family = AF_INET; |
| 123 | out.sin_port = port; |
| 124 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 125 | |
| 126 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 127 | } |
| 128 | |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 129 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 130 | { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 131 | static char buf[] = { MGCP_DUMMY_LOAD }; |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 132 | |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 133 | return mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
| 134 | endp->net_end.rtp_port, buf, 1); |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 137 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
| 138 | struct mgcp_rtp_stream_state *state, |
| 139 | struct mgcp_rtp_end *rtp_end, |
| 140 | struct sockaddr_in *addr, |
| 141 | uint16_t seq, uint32_t timestamp, |
| 142 | const char *text, int32_t *tsdelta_out) |
| 143 | { |
| 144 | int32_t tsdelta; |
| 145 | |
| 146 | /* Not fully intialized, skip */ |
| 147 | if (state->last_tsdelta == 0 && timestamp == state->last_timestamp) |
| 148 | return 0; |
| 149 | |
| 150 | if (seq == state->last_seq) { |
| 151 | if (timestamp != state->last_timestamp) { |
| 152 | state->err_ts_counter += 1; |
| 153 | LOGP(DMGCP, LOGL_ERROR, |
| 154 | "The %s timestamp delta is != 0 but the sequence " |
| 155 | "number %d is the same" |
| 156 | "on 0x%x SSRC: %u timestamp: %u " |
| 157 | "from %s:%d in %d\n", |
| 158 | text, seq, |
| 159 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 160 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 161 | endp->conn_mode); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | tsdelta = |
| 167 | (int32_t)(timestamp - state->last_timestamp) / |
| 168 | (int16_t)(seq - state->last_seq); |
| 169 | |
| 170 | if (tsdelta == 0) { |
| 171 | state->err_ts_counter += 1; |
| 172 | LOGP(DMGCP, LOGL_ERROR, |
| 173 | "The %s timestamp delta is %d " |
| 174 | "on 0x%x SSRC: %u timestamp: %u " |
| 175 | "from %s:%d in %d\n", |
| 176 | text, tsdelta, |
| 177 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 178 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 179 | endp->conn_mode); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | if (state->last_tsdelta != tsdelta) { |
| 185 | if (state->last_tsdelta) { |
| 186 | state->err_ts_counter += 1; |
| 187 | LOGP(DMGCP, LOGL_ERROR, |
| 188 | "The %s timestamp delta changes from %d to %d " |
| 189 | "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n", |
| 190 | text, state->last_tsdelta, tsdelta, |
| 191 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 192 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 193 | endp->conn_mode); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (tsdelta_out) |
| 198 | *tsdelta_out = tsdelta; |
| 199 | |
| 200 | return 1; |
| 201 | } |
| 202 | |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 203 | /* Set the timestamp offset according to the packet duration. */ |
| 204 | static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp, |
| 205 | struct mgcp_rtp_state *state, |
| 206 | struct mgcp_rtp_end *rtp_end, |
| 207 | struct sockaddr_in *addr, |
| 208 | int16_t delta_seq, uint32_t timestamp) |
| 209 | { |
| 210 | int32_t tsdelta = state->packet_duration; |
| 211 | int timestamp_offset; |
| 212 | |
| 213 | if (tsdelta == 0) { |
| 214 | tsdelta = state->out_stream.last_tsdelta; |
| 215 | if (tsdelta != 0) { |
| 216 | LOGP(DMGCP, LOGL_NOTICE, |
| 217 | "A fixed packet duration is not available on 0x%x, " |
| 218 | "using last output timestamp delta instead: %d " |
| 219 | "from %s:%d in %d\n", |
| 220 | ENDPOINT_NUMBER(endp), tsdelta, |
| 221 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 222 | endp->conn_mode); |
| 223 | } else { |
| 224 | tsdelta = rtp_end->rate * 20 / 1000; |
| 225 | LOGP(DMGCP, LOGL_NOTICE, |
| 226 | "Fixed packet duration and last timestamp delta " |
| 227 | "are not available on 0x%x, " |
| 228 | "using fixed 20ms instead: %d " |
| 229 | "from %s:%d in %d\n", |
| 230 | ENDPOINT_NUMBER(endp), tsdelta, |
| 231 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 232 | endp->conn_mode); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | timestamp_offset = |
| 237 | state->out_stream.last_timestamp - timestamp + |
| 238 | delta_seq * tsdelta; |
| 239 | |
| 240 | if (state->timestamp_offset != timestamp_offset) { |
| 241 | state->timestamp_offset = timestamp_offset; |
| 242 | |
| 243 | LOGP(DMGCP, LOGL_NOTICE, |
| 244 | "Timestamp offset change on 0x%x SSRC: %u " |
| 245 | "SeqNo delta: %d, TS offset: %d, " |
| 246 | "from %s:%d in %d\n", |
| 247 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 248 | delta_seq, state->timestamp_offset, |
| 249 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 250 | endp->conn_mode); |
| 251 | } |
| 252 | |
| 253 | return timestamp_offset; |
| 254 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 255 | |
Holger Hans Peter Freyther | 1e85af6 | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 256 | /** |
| 257 | * The RFC 3550 Appendix A assumes there are multiple sources but |
| 258 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
| 259 | * one source and this code will patch packages to appear as if there |
| 260 | * is only one source. |
| 261 | * There is also no probation period for new sources. Every package |
| 262 | * we receive will be seen as a switch in streams. |
| 263 | */ |
Jacob Erlbeck | d62419b | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 264 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 265 | struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr, |
| 266 | char *data, int len) |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 267 | { |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 268 | uint32_t arrival_time; |
| 269 | int32_t transit, d; |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 270 | uint16_t seq, udelta; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 271 | uint32_t timestamp, ssrc; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 272 | struct rtp_hdr *rtp_hdr; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 273 | int payload = rtp_end->payload_type; |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 274 | |
| 275 | if (len < sizeof(*rtp_hdr)) |
| 276 | return; |
| 277 | |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 278 | rtp_hdr = (struct rtp_hdr *) data; |
| 279 | seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 280 | timestamp = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 281 | arrival_time = get_current_ts(); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 282 | ssrc = ntohl(rtp_hdr->ssrc); |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 283 | |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 284 | if (!state->initialized) { |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 285 | state->in_stream.last_seq = seq - 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 286 | state->in_stream.ssrc = state->orig_ssrc = ssrc; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 287 | state->in_stream.last_tsdelta = 0; |
Holger Hans Peter Freyther | ed3a661 | 2012-10-22 17:08:48 +0200 | [diff] [blame] | 288 | state->base_seq = seq; |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 289 | state->initialized = 1; |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 290 | state->jitter = 0; |
| 291 | state->transit = arrival_time - timestamp; |
Jacob Erlbeck | f6ec0e9 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 292 | state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 293 | state->out_stream = state->in_stream; |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 294 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 295 | state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */ |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 296 | LOGP(DMGCP, LOGL_INFO, |
| 297 | "Initializing stream on 0x%x SSRC: %u timestamp: %u " |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 298 | "pkt-duration: %d, from %s:%d in %d\n", |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 299 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 300 | state->seq_offset, state->packet_duration, |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 301 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 302 | endp->conn_mode); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 303 | } else if (state->in_stream.ssrc != ssrc) { |
Jacob Erlbeck | 5e9549e | 2013-12-05 12:20:05 +0100 | [diff] [blame] | 304 | LOGP(DMGCP, LOGL_NOTICE, |
| 305 | "The SSRC changed on 0x%x: %u -> %u " |
| 306 | "from %s:%d in %d\n", |
| 307 | ENDPOINT_NUMBER(endp), |
| 308 | state->in_stream.ssrc, rtp_hdr->ssrc, |
| 309 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 310 | endp->conn_mode); |
| 311 | |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 312 | state->in_stream.ssrc = ssrc; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 313 | if (rtp_end->force_constant_ssrc) { |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 314 | const int16_t delta_seq = 1; |
| 315 | |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 316 | state->seq_offset = |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 317 | (state->out_stream.last_seq + delta_seq) - seq; |
| 318 | |
| 319 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 320 | delta_seq, timestamp); |
| 321 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 322 | state->patch_ssrc = 1; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 323 | ssrc = state->orig_ssrc; |
Jacob Erlbeck | e2292f3 | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 324 | if (rtp_end->force_constant_ssrc != -1) |
| 325 | rtp_end->force_constant_ssrc -= 1; |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 326 | |
| 327 | LOGP(DMGCP, LOGL_NOTICE, |
| 328 | "SSRC patching enabled on 0x%x SSRC: %u " |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 329 | "SeqNo offset: %d, TS offset: %d " |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 330 | "from %s:%d in %d\n", |
| 331 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 332 | state->seq_offset, state->timestamp_offset, |
Jacob Erlbeck | b35a777 | 2013-12-05 11:33:20 +0100 | [diff] [blame] | 333 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 334 | endp->conn_mode); |
| 335 | } |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 336 | |
| 337 | state->in_stream.last_tsdelta = 0; |
| 338 | } else { |
| 339 | /* Compute current per-packet timestamp delta */ |
| 340 | check_rtp_timestamp(endp, &state->in_stream, rtp_end, addr, |
| 341 | seq, timestamp, "input", |
| 342 | &state->in_stream.last_tsdelta); |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 343 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 344 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 345 | ssrc = state->orig_ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 346 | } |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 347 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 348 | /* Save before patching */ |
| 349 | state->in_stream.last_timestamp = timestamp; |
| 350 | state->in_stream.last_seq = seq; |
| 351 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 352 | if (rtp_end->force_constant_timing && |
| 353 | state->out_stream.ssrc == ssrc && state->packet_duration) { |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 354 | /* Update the timestamp offset */ |
| 355 | const int delta_seq = |
| 356 | seq + state->seq_offset - state->out_stream.last_seq; |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 357 | |
Jacob Erlbeck | 33f3009 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 358 | adjust_rtp_timestamp_offset(endp, state, rtp_end, addr, |
| 359 | delta_seq, timestamp); |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* Store the updated SSRC back to the packet */ |
| 363 | if (state->patch_ssrc) |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 364 | rtp_hdr->ssrc = htonl(ssrc); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 365 | |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 366 | /* Apply the offset and store it back to the packet. |
| 367 | * This won't change anything if the offset is 0, so the conditional is |
| 368 | * omitted. */ |
| 369 | seq += state->seq_offset; |
| 370 | rtp_hdr->sequence = htons(seq); |
| 371 | timestamp += state->timestamp_offset; |
| 372 | rtp_hdr->timestamp = htonl(timestamp); |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 373 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 374 | /* Check again, whether the timestamps are still valid */ |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 375 | if (state->out_stream.ssrc == ssrc) |
Jacob Erlbeck | 55ba140 | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 376 | check_rtp_timestamp(endp, &state->out_stream, rtp_end, addr, |
| 377 | seq, timestamp, "output", |
| 378 | &state->out_stream.last_tsdelta); |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 379 | |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 380 | /* |
| 381 | * The below takes the shape of the validation from Appendix A. Check |
| 382 | * if there is something weird with the sequence number, otherwise check |
| 383 | * for a wrap around in the sequence number. |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 384 | * |
| 385 | * Note that last_seq is used where the appendix mentions max_seq. |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 386 | */ |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 387 | udelta = seq - state->out_stream.last_seq; |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 388 | if (udelta < RTP_MAX_DROPOUT) { |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 389 | if (seq < state->out_stream.last_seq) |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 390 | state->cycles += RTP_SEQ_MOD; |
Holger Hans Peter Freyther | 7e7ee5f | 2012-12-16 13:07:45 +0100 | [diff] [blame] | 391 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 392 | LOGP(DMGCP, LOGL_NOTICE, |
| 393 | "RTP seqno made a very large jump on 0x%x delta: %u\n", |
| 394 | ENDPOINT_NUMBER(endp), udelta); |
| 395 | } |
| 396 | |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 397 | /* |
| 398 | * calculate the jitter between the two packages. The TS should be |
| 399 | * taken closer to the read function. This was taken from the |
| 400 | * Appendix A of RFC 3550. The local timestamp has a usec resolution. |
| 401 | */ |
| 402 | transit = arrival_time - timestamp; |
| 403 | d = transit - state->transit; |
| 404 | state->transit = transit; |
| 405 | if (d < 0) |
| 406 | d = -d; |
| 407 | state->jitter += d - ((state->jitter + 8) >> 4); |
| 408 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 409 | /* Save output values */ |
| 410 | state->out_stream.last_seq = seq; |
| 411 | state->out_stream.last_timestamp = timestamp; |
Jacob Erlbeck | 3da9e4e | 2013-12-03 15:47:04 +0100 | [diff] [blame] | 412 | state->out_stream.ssrc = ssrc; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 413 | |
Holger Hans Peter Freyther | 7279d24 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 414 | if (payload < 0) |
| 415 | return; |
| 416 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 417 | rtp_hdr->payload_type = payload; |
| 418 | } |
| 419 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 420 | /* |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 421 | * The below code is for dispatching. We have a dedicated port for |
| 422 | * 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] | 423 | */ |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 424 | static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) |
| 425 | { |
| 426 | if (!tap->enabled) |
| 427 | return 0; |
| 428 | |
| 429 | return sendto(fd, buf, len, 0, |
| 430 | (struct sockaddr *)&tap->forward, sizeof(tap->forward)); |
| 431 | } |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 432 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 433 | static int mgcp_send_transcoder(struct mgcp_rtp_end *end, |
| 434 | struct mgcp_config *cfg, int is_rtp, |
| 435 | const char *buf, int len) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 436 | { |
| 437 | int rc; |
| 438 | int port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 439 | struct sockaddr_in addr; |
| 440 | |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 441 | port = is_rtp ? end->rtp_port : end->rtcp_port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 442 | |
| 443 | addr.sin_family = AF_INET; |
| 444 | addr.sin_addr = cfg->transcoder_in; |
Holger Hans Peter Freyther | 5f2cd84 | 2010-11-01 21:53:39 +0100 | [diff] [blame] | 445 | addr.sin_port = port; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 446 | |
| 447 | rc = sendto(is_rtp ? |
Holger Hans Peter Freyther | 8b19dee | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 448 | end->rtp.fd : |
| 449 | end->rtcp.fd, buf, len, 0, |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 450 | (struct sockaddr *) &addr, sizeof(addr)); |
| 451 | |
| 452 | if (rc != len) |
| 453 | LOGP(DMGCP, LOGL_ERROR, |
| 454 | "Failed to send data to the transcoder: %s\n", |
| 455 | strerror(errno)); |
| 456 | |
| 457 | return rc; |
| 458 | } |
| 459 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 460 | static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 461 | struct sockaddr_in *addr, char *buf, int rc) |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 462 | { |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 463 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 464 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 465 | if (tcfg->audio_loop) |
Holger Hans Peter Freyther | 1fc4329 | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 466 | dest = !dest; |
| 467 | |
| 468 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 469 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 470 | dest = !dest; |
| 471 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 472 | if (dest == MGCP_DEST_NET) { |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 473 | if (is_rtp) { |
Jacob Erlbeck | d62419b | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 474 | mgcp_patch_and_count(endp, &endp->bts_state, |
| 475 | &endp->net_end, |
| 476 | addr, buf, rc); |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 477 | forward_data(endp->net_end.rtp.fd, |
| 478 | &endp->taps[MGCP_TAP_NET_OUT], buf, rc); |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 479 | return mgcp_udp_send(endp->net_end.rtp.fd, |
| 480 | &endp->net_end.addr, |
| 481 | endp->net_end.rtp_port, buf, rc); |
Holger Hans Peter Freyther | a8090d5 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 482 | } else if (!tcfg->omit_rtcp) { |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 483 | return mgcp_udp_send(endp->net_end.rtcp.fd, |
| 484 | &endp->net_end.addr, |
| 485 | endp->net_end.rtcp_port, buf, rc); |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 486 | } |
| 487 | } else { |
| 488 | if (is_rtp) { |
Jacob Erlbeck | d62419b | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 489 | mgcp_patch_and_count(endp, &endp->net_state, |
| 490 | &endp->bts_end, |
| 491 | addr, buf, rc); |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 492 | forward_data(endp->bts_end.rtp.fd, |
| 493 | &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 494 | return mgcp_udp_send(endp->bts_end.rtp.fd, |
| 495 | &endp->bts_end.addr, |
| 496 | endp->bts_end.rtp_port, buf, rc); |
Holger Hans Peter Freyther | a8090d5 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 497 | } else if (!tcfg->omit_rtcp) { |
Pablo Neira Ayuso | d81fec0 | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 498 | return mgcp_udp_send(endp->bts_end.rtcp.fd, |
| 499 | &endp->bts_end.addr, |
| 500 | endp->bts_end.rtcp_port, buf, rc); |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 501 | } |
| 502 | } |
Holger Hans Peter Freyther | 8c3d069 | 2012-09-11 12:31:25 +0200 | [diff] [blame] | 503 | |
| 504 | return 0; |
Holger Hans Peter Freyther | 84e1c47 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 505 | } |
| 506 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 507 | static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 508 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 509 | { |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 510 | int rc; |
| 511 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 512 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 513 | rc = recvfrom(fd, buf, bufsize, 0, |
| 514 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 515 | if (rc < 0) { |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 516 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 517 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* 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] | 522 | if (!endp->allocated) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 523 | return -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 524 | |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 525 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 526 | |
| 527 | return rc; |
| 528 | } |
| 529 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 530 | 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] | 531 | { |
| 532 | char buf[4096]; |
| 533 | struct sockaddr_in addr; |
| 534 | struct mgcp_endpoint *endp; |
| 535 | int rc, proto; |
| 536 | |
| 537 | endp = (struct mgcp_endpoint *) fd->data; |
| 538 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 539 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 540 | if (rc <= 0) |
| 541 | return -1; |
| 542 | |
| 543 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 544 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | eedb453 | 2012-11-12 10:55:29 +0100 | [diff] [blame] | 545 | "Endpoint 0x%x data from wrong address %s vs. ", |
| 546 | ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); |
| 547 | LOGPC(DMGCP, LOGL_ERROR, |
| 548 | "%s\n", inet_ntoa(endp->net_end.addr)); |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | if (endp->net_end.rtp_port != addr.sin_port && |
| 553 | endp->net_end.rtcp_port != addr.sin_port) { |
| 554 | LOGP(DMGCP, LOGL_ERROR, |
| 555 | "Data from wrong source port %d on 0x%x\n", |
| 556 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 561 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 562 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 563 | ENDPOINT_NUMBER(endp)); |
| 564 | return 0; |
| 565 | } |
| 566 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 567 | 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] | 568 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 569 | endp->net_end.octets += rc; |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 570 | |
| 571 | 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] | 572 | |
| 573 | switch (endp->type) { |
| 574 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 575 | return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP, |
| 576 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 577 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 578 | return mgcp_send_transcoder(&endp->trans_net, endp->cfg, |
| 579 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 583 | endp->type, ENDPOINT_NUMBER(endp)); |
| 584 | return 0; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 585 | } |
| 586 | |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 587 | static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) |
| 588 | { |
| 589 | struct mgcp_config *cfg = endp->cfg; |
| 590 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 591 | 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] | 592 | if (!cfg->bts_ip || |
| 593 | memcmp(&addr->sin_addr, |
| 594 | &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || |
| 595 | memcmp(&addr->sin_addr, |
| 596 | &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { |
| 597 | |
| 598 | endp->bts_end.rtp_port = addr->sin_port; |
| 599 | endp->bts_end.addr = addr->sin_addr; |
| 600 | |
| 601 | LOGP(DMGCP, LOGL_NOTICE, |
| 602 | "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 603 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), |
| 604 | ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); |
| 605 | } |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 606 | } 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] | 607 | if (memcmp(&endp->bts_end.addr, &addr->sin_addr, |
| 608 | sizeof(endp->bts_end.addr)) == 0) { |
| 609 | endp->bts_end.rtcp_port = addr->sin_port; |
| 610 | } |
| 611 | } |
| 612 | } |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 613 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 614 | 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] | 615 | { |
| 616 | char buf[4096]; |
| 617 | struct sockaddr_in addr; |
| 618 | struct mgcp_endpoint *endp; |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 619 | int rc, proto; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 620 | |
| 621 | endp = (struct mgcp_endpoint *) fd->data; |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 622 | |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 623 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | b615164 | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 624 | if (rc <= 0) |
| 625 | return -1; |
| 626 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 627 | 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] | 628 | |
| 629 | /* 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] | 630 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 52ccf6a | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 631 | discover_bts(endp, proto, &addr); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 632 | |
Holger Hans Peter Freyther | ea97fbf | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 633 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 634 | LOGP(DMGCP, LOGL_ERROR, |
| 635 | "Data from wrong bts %s on 0x%x\n", |
| 636 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 637 | return -1; |
| 638 | } |
| 639 | |
| 640 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 641 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 642 | LOGP(DMGCP, LOGL_ERROR, |
| 643 | "Data from wrong bts source port %d on 0x%x\n", |
| 644 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 645 | return -1; |
| 646 | } |
| 647 | |
Holger Hans Peter Freyther | b3c206a | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 648 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 649 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 650 | 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] | 651 | ENDPOINT_NUMBER(endp)); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 655 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | e602cd6 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 656 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 657 | endp->bts_end.octets += rc; |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 658 | |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 659 | 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] | 660 | |
| 661 | switch (endp->type) { |
| 662 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 663 | return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP, |
| 664 | &addr, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 665 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 666 | return mgcp_send_transcoder(&endp->trans_bts, endp->cfg, |
| 667 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 671 | endp->type, ENDPOINT_NUMBER(endp)); |
| 672 | return 0; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 673 | } |
| 674 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 675 | 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] | 676 | int dest, struct osmo_fd *fd) |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 677 | { |
| 678 | char buf[4096]; |
| 679 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 680 | struct mgcp_config *cfg; |
| 681 | int rc, proto; |
| 682 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 683 | cfg = _endp->cfg; |
Holger Hans Peter Freyther | 9bdf5a9 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 684 | rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 685 | if (rc <= 0) |
| 686 | return -1; |
| 687 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 688 | proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 689 | |
| 690 | if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { |
| 691 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 692 | "Data not coming from transcoder dest: %d %s on 0x%x\n", |
| 693 | dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 694 | return -1; |
| 695 | } |
| 696 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 697 | if (end->rtp_port != addr.sin_port && |
| 698 | end->rtcp_port != addr.sin_port) { |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 699 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 700 | "Data from wrong transcoder dest %d source port %d on 0x%x\n", |
| 701 | dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 706 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 707 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n", |
| 708 | dest, ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 712 | end->packets += 1; |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 713 | 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] | 714 | } |
| 715 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 716 | 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] | 717 | { |
| 718 | struct mgcp_endpoint *endp; |
| 719 | endp = (struct mgcp_endpoint *) fd->data; |
| 720 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 721 | 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] | 722 | } |
| 723 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 724 | 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] | 725 | { |
| 726 | struct mgcp_endpoint *endp; |
| 727 | endp = (struct mgcp_endpoint *) fd->data; |
| 728 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 729 | 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] | 730 | } |
| 731 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 732 | static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, |
| 733 | int port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 734 | { |
| 735 | struct sockaddr_in addr; |
| 736 | int on = 1; |
| 737 | |
| 738 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 739 | if (fd->fd < 0) { |
| 740 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 745 | memset(&addr, 0, sizeof(addr)); |
| 746 | addr.sin_family = AF_INET; |
| 747 | addr.sin_port = htons(port); |
| 748 | inet_aton(source_addr, &addr.sin_addr); |
| 749 | |
| 750 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | 6f36e92 | 2010-08-06 03:00:17 +0800 | [diff] [blame] | 751 | close(fd->fd); |
| 752 | fd->fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 759 | static int set_ip_tos(int fd, int tos) |
| 760 | { |
| 761 | int ret; |
| 762 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 763 | &tos, sizeof(tos)); |
| 764 | return ret != 0; |
| 765 | } |
| 766 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 767 | 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] | 768 | { |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 769 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp, |
| 770 | rtp_end->local_port) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 771 | 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] | 772 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 773 | goto cleanup0; |
| 774 | } |
| 775 | |
Pablo Neira Ayuso | 66b52c1 | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 776 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp, |
| 777 | rtp_end->local_port + 1) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 778 | 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] | 779 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 780 | goto cleanup1; |
| 781 | } |
| 782 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 783 | set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 784 | set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 785 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 786 | rtp_end->rtp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 787 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 788 | 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] | 789 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 790 | goto cleanup2; |
| 791 | } |
| 792 | |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 793 | rtp_end->rtcp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 794 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 795 | 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] | 796 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 797 | goto cleanup3; |
| 798 | } |
| 799 | |
| 800 | return 0; |
| 801 | |
| 802 | cleanup3: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 803 | osmo_fd_unregister(&rtp_end->rtp); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 804 | cleanup2: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 805 | close(rtp_end->rtcp.fd); |
| 806 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 807 | cleanup1: |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 808 | close(rtp_end->rtp.fd); |
| 809 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 810 | cleanup0: |
| 811 | return -1; |
| 812 | } |
| 813 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 814 | static int int_bind(const char *port, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 815 | 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] | 816 | struct mgcp_endpoint *_endp, int rtp_port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 817 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 818 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
| 819 | LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n", |
| 820 | port, ENDPOINT_NUMBER(_endp)); |
| 821 | mgcp_free_rtp_port(end); |
Holger Hans Peter Freyther | 7fe2a3d | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 822 | } |
| 823 | |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 824 | end->local_port = rtp_port; |
| 825 | end->rtp.cb = cb; |
| 826 | end->rtp.data = _endp; |
| 827 | end->rtcp.data = _endp; |
| 828 | end->rtcp.cb = cb; |
| 829 | return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp)); |
| 830 | } |
| 831 | |
| 832 | |
| 833 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 834 | { |
| 835 | return int_bind("bts-port", &endp->bts_end, |
| 836 | rtp_data_bts, endp, rtp_port); |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 840 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 841 | return int_bind("net-port", &endp->net_end, |
| 842 | rtp_data_net, endp, rtp_port); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 843 | } |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 844 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 845 | 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] | 846 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 847 | return int_bind("trans-net", &endp->trans_net, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 848 | rtp_data_trans_net, endp, rtp_port); |
Holger Hans Peter Freyther | 218f856 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 849 | } |
| 850 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 851 | int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 852 | { |
Holger Hans Peter Freyther | 386a940 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 853 | return int_bind("trans-bts", &endp->trans_bts, |
Holger Hans Peter Freyther | 3f29cc8 | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 854 | rtp_data_trans_bts, endp, rtp_port); |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 857 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 858 | { |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 859 | if (end->rtp.fd != -1) { |
| 860 | close(end->rtp.fd); |
| 861 | end->rtp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 862 | osmo_fd_unregister(&end->rtp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (end->rtcp.fd != -1) { |
| 866 | close(end->rtcp.fd); |
| 867 | end->rtcp.fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 868 | osmo_fd_unregister(&end->rtcp); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 873 | |
| 874 | |
| 875 | void mgcp_state_calc_loss(struct mgcp_rtp_state *state, |
| 876 | struct mgcp_rtp_end *end, uint32_t *expected, |
| 877 | int *loss) |
| 878 | { |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 879 | *expected = state->cycles + state->out_stream.last_seq; |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 880 | *expected = *expected - state->base_seq + 1; |
| 881 | |
| 882 | if (!state->initialized) { |
| 883 | *expected = 0; |
| 884 | *loss = 0; |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Make sure the sign is correct and use the biggest |
| 890 | * positive/negative number that fits. |
| 891 | */ |
| 892 | *loss = *expected - end->packets; |
| 893 | if (*expected < end->packets) { |
| 894 | if (*loss > 0) |
| 895 | *loss = INT_MIN; |
| 896 | } else { |
| 897 | if (*loss < 0) |
| 898 | *loss = INT_MAX; |
| 899 | } |
| 900 | } |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 901 | |
| 902 | uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state) |
| 903 | { |
| 904 | if (!state->initialized) |
| 905 | return 0; |
| 906 | return state->jitter >> 4; |
| 907 | } |