blob: 04e6cbef837b748c89ad8d8d7d16f3b5df9880f5 [file] [log] [blame]
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02005 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +01007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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 Freyther1b0ea972010-02-21 11:11:04 +010012 * (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 Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * 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 Freyther1b0ea972010-02-21 11:11:04 +010021 *
22 */
23
24#include <string.h>
Holger Hans Peter Freytherb715d7f2010-05-14 02:14:09 +080025#include <stdlib.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010026#include <unistd.h>
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +020027#include <errno.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010028
29#include <sys/socket.h>
30#include <arpa/inet.h>
31
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010034
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010035#include <openbsc/mgcp.h>
36#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010037
38#warning "Make use of the rtp proxy code"
39
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020040/* attempt to determine byte order */
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020041#include <sys/param.h>
42#include <limits.h>
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020043#include <time.h>
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020044
45#ifndef __BYTE_ORDER
Tobias Engelaff20712012-10-24 17:53:50 +020046# 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 Freyther3cb28792010-10-12 15:39:46 +020053#endif
54
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010055/* according to rtp_proxy.c RFC 3550 */
56struct rtp_hdr {
57#if __BYTE_ORDER == __LITTLE_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080058 uint8_t csrc_count:4,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010059 extension:1,
60 padding:1,
61 version:2;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080062 uint8_t payload_type:7,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010063 marker:1;
64#elif __BYTE_ORDER == __BIG_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080065 uint8_t version:2,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010066 padding:1,
67 extension:1,
68 csrc_count:4;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080069 uint8_t marker:1,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010070 payload_type:7;
71#endif
Holger Hans Peter Freytherd340cd32010-07-23 18:56:01 +080072 uint16_t sequence;
Holger Hans Peter Freytherd9b18f82010-07-23 18:55:38 +080073 uint32_t timestamp;
74 uint32_t ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010075} __attribute__((packed));
76
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +020077#define RTP_SEQ_MOD (1 << 16)
78#define RTP_MAX_DROPOUT 3000
79#define RTP_MAX_MISORDER 100
80
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010081
82enum {
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020083 MGCP_DEST_NET = 0,
84 MGCP_DEST_BTS,
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010085};
86
87enum {
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020088 MGCP_PROTO_RTP,
89 MGCP_PROTO_RTCP,
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010090};
91
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020092#define MGCP_DUMMY_LOAD 0x23
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +080093
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010094
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020095/**
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 */
100uint32_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 Ayusod81fec02013-08-25 17:39:38 +0200118static int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf,
119 int len)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100120{
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 Freytherb844b872010-04-21 21:25:13 +0800129int mgcp_send_dummy(struct mgcp_endpoint *endp)
130{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200131 static char buf[] = { MGCP_DUMMY_LOAD };
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800132
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200133 return mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
134 endp->net_end.rtp_port, buf, 1);
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800135}
136
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100137static int32_t compute_timestamp_aligment_error(struct mgcp_rtp_stream_state *sstate,
138 int ptime, uint32_t timestamp)
139{
140 int32_t timestamp_delta;
141
142 if (ptime == 0)
143 return 0;
144
145 /* Align according to: T - Tlast = k * Tptime */
146 timestamp_delta = timestamp - sstate->last_timestamp;
147
148 return timestamp_delta % ptime;
149}
150
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100151static int check_rtp_timestamp(struct mgcp_endpoint *endp,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100152 struct mgcp_rtp_state *state,
153 struct mgcp_rtp_stream_state *sstate,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100154 struct mgcp_rtp_end *rtp_end,
155 struct sockaddr_in *addr,
156 uint16_t seq, uint32_t timestamp,
157 const char *text, int32_t *tsdelta_out)
158{
159 int32_t tsdelta;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100160 int32_t timestamp_error;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100161
162 /* Not fully intialized, skip */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100163 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100164 return 0;
165
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100166 if (seq == sstate->last_seq) {
167 if (timestamp != sstate->last_timestamp) {
168 sstate->err_ts_counter += 1;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100169 LOGP(DMGCP, LOGL_ERROR,
170 "The %s timestamp delta is != 0 but the sequence "
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100171 "number %d is the same, "
172 "TS offset: %d, SeqNo offset: %d "
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100173 "on 0x%x SSRC: %u timestamp: %u "
174 "from %s:%d in %d\n",
175 text, seq,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100176 state->timestamp_offset, state->seq_offset,
177 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100178 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
179 endp->conn_mode);
180 }
181 return 0;
182 }
183
184 tsdelta =
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100185 (int32_t)(timestamp - sstate->last_timestamp) /
186 (int16_t)(seq - sstate->last_seq);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100187
188 if (tsdelta == 0) {
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100189 /* Don't update *tsdelta_out */
190 LOGP(DMGCP, LOGL_NOTICE,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100191 "The %s timestamp delta is %d "
192 "on 0x%x SSRC: %u timestamp: %u "
193 "from %s:%d in %d\n",
194 text, tsdelta,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100195 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100196 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
197 endp->conn_mode);
198
199 return 0;
200 }
201
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100202 if (sstate->last_tsdelta != tsdelta) {
203 if (sstate->last_tsdelta) {
204 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100205 "The %s timestamp delta changes from %d to %d "
206 "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n",
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100207 text, sstate->last_tsdelta, tsdelta,
208 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100209 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
210 endp->conn_mode);
211 }
212 }
213
214 if (tsdelta_out)
215 *tsdelta_out = tsdelta;
216
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100217 timestamp_error =
218 compute_timestamp_aligment_error(sstate, state->packet_duration,
219 timestamp);
220
221 if (timestamp_error) {
222 sstate->err_ts_counter += 1;
223 LOGP(DMGCP, LOGL_NOTICE,
224 "The %s timestamp has an alignment error of %d "
225 "on 0x%x SSRC: %u "
226 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
227 "from %s:%d in %d\n",
228 text, timestamp_error,
229 ENDPOINT_NUMBER(endp), sstate->ssrc,
230 (int16_t)(seq - sstate->last_seq),
231 (int32_t)(timestamp - sstate->last_timestamp),
232 tsdelta,
233 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
234 endp->conn_mode);
235 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100236 return 1;
237}
238
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100239/* Set the timestamp offset according to the packet duration. */
240static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
241 struct mgcp_rtp_state *state,
242 struct mgcp_rtp_end *rtp_end,
243 struct sockaddr_in *addr,
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100244 int16_t delta_seq, uint32_t in_timestamp)
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100245{
246 int32_t tsdelta = state->packet_duration;
247 int timestamp_offset;
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100248 uint32_t out_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100249
250 if (tsdelta == 0) {
251 tsdelta = state->out_stream.last_tsdelta;
252 if (tsdelta != 0) {
253 LOGP(DMGCP, LOGL_NOTICE,
254 "A fixed packet duration is not available on 0x%x, "
255 "using last output timestamp delta instead: %d "
256 "from %s:%d in %d\n",
257 ENDPOINT_NUMBER(endp), tsdelta,
258 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
259 endp->conn_mode);
260 } else {
261 tsdelta = rtp_end->rate * 20 / 1000;
262 LOGP(DMGCP, LOGL_NOTICE,
263 "Fixed packet duration and last timestamp delta "
264 "are not available on 0x%x, "
265 "using fixed 20ms instead: %d "
266 "from %s:%d in %d\n",
267 ENDPOINT_NUMBER(endp), tsdelta,
268 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
269 endp->conn_mode);
270 }
271 }
272
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100273 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
274 timestamp_offset = out_timestamp - in_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100275
276 if (state->timestamp_offset != timestamp_offset) {
277 state->timestamp_offset = timestamp_offset;
278
279 LOGP(DMGCP, LOGL_NOTICE,
280 "Timestamp offset change on 0x%x SSRC: %u "
281 "SeqNo delta: %d, TS offset: %d, "
282 "from %s:%d in %d\n",
283 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
284 delta_seq, state->timestamp_offset,
285 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
286 endp->conn_mode);
287 }
288
289 return timestamp_offset;
290}
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100291
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100292/* Set the timestamp offset according to the packet duration. */
293static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
294 struct mgcp_rtp_state *state,
295 struct mgcp_rtp_end *rtp_end,
296 struct sockaddr_in *addr,
297 uint32_t timestamp)
298{
299 int timestamp_error = 0;
300 int ptime = state->packet_duration;
301
302 /* Align according to: T + Toffs - Tlast = k * Tptime */
303
304 timestamp_error = compute_timestamp_aligment_error(
305 &state->out_stream, ptime,
306 timestamp + state->timestamp_offset);
307
308 if (timestamp_error) {
309 state->timestamp_offset += ptime - timestamp_error;
310
311 LOGP(DMGCP, LOGL_NOTICE,
312 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
313 "new TS offset: %d, "
314 "from %s:%d in %d\n",
315 timestamp_error,
316 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
317 state->timestamp_offset, inet_ntoa(addr->sin_addr),
318 ntohs(addr->sin_port), endp->conn_mode);
319 }
320
321 OSMO_ASSERT(compute_timestamp_aligment_error(&state->out_stream, ptime,
322 timestamp + state->timestamp_offset) == 0);
323
324 return timestamp_error;
325}
326
327
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200328/**
329 * The RFC 3550 Appendix A assumes there are multiple sources but
330 * some of the supported endpoints (e.g. the nanoBTS) can only handle
331 * one source and this code will patch packages to appear as if there
332 * is only one source.
333 * There is also no probation period for new sources. Every package
334 * we receive will be seen as a switch in streams.
335 */
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100336void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
337 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
338 char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100339{
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200340 uint32_t arrival_time;
341 int32_t transit, d;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200342 uint16_t seq, udelta;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100343 uint32_t timestamp, ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100344 struct rtp_hdr *rtp_hdr;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100345 int payload = rtp_end->payload_type;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100346
347 if (len < sizeof(*rtp_hdr))
348 return;
349
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800350 rtp_hdr = (struct rtp_hdr *) data;
351 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000352 timestamp = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200353 arrival_time = get_current_ts();
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100354 ssrc = ntohl(rtp_hdr->ssrc);
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800355
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000356 if (!state->initialized) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100357 state->in_stream.last_seq = seq - 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100358 state->in_stream.ssrc = state->orig_ssrc = ssrc;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100359 state->in_stream.last_tsdelta = 0;
Holger Hans Peter Freythered3a6612012-10-22 17:08:48 +0200360 state->base_seq = seq;
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000361 state->initialized = 1;
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200362 state->jitter = 0;
363 state->transit = arrival_time - timestamp;
Jacob Erlbeckf6ec0e92013-12-04 10:30:11 +0100364 state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100365 state->out_stream = state->in_stream;
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100366 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100367 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100368 LOGP(DMGCP, LOGL_INFO,
369 "Initializing stream on 0x%x SSRC: %u timestamp: %u "
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100370 "pkt-duration: %d, from %s:%d in %d\n",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100371 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100372 state->seq_offset, state->packet_duration,
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100373 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
374 endp->conn_mode);
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100375 if (state->packet_duration == 0) {
376 state->packet_duration = rtp_end->rate * 20 / 1000;
377 LOGP(DMGCP, LOGL_NOTICE,
378 "Fixed packet duration is not available on 0x%x, "
379 "using fixed 20ms instead: %d from %s:%d in %d\n",
380 ENDPOINT_NUMBER(endp), state->packet_duration,
381 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
382 endp->conn_mode);
383 }
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100384 } else if (state->in_stream.ssrc != ssrc) {
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100385 LOGP(DMGCP, LOGL_NOTICE,
386 "The SSRC changed on 0x%x: %u -> %u "
387 "from %s:%d in %d\n",
388 ENDPOINT_NUMBER(endp),
389 state->in_stream.ssrc, rtp_hdr->ssrc,
390 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
391 endp->conn_mode);
392
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100393 state->in_stream.ssrc = ssrc;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100394 if (rtp_end->force_constant_ssrc) {
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100395 const int16_t delta_seq = 1;
396
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100397 state->seq_offset =
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100398 (state->out_stream.last_seq + delta_seq) - seq;
399
400 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
401 delta_seq, timestamp);
402
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100403 state->patch_ssrc = 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100404 ssrc = state->orig_ssrc;
Jacob Erlbecke2292f32013-12-03 15:13:12 +0100405 if (rtp_end->force_constant_ssrc != -1)
406 rtp_end->force_constant_ssrc -= 1;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100407
408 LOGP(DMGCP, LOGL_NOTICE,
409 "SSRC patching enabled on 0x%x SSRC: %u "
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100410 "SeqNo offset: %d, TS offset: %d "
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100411 "from %s:%d in %d\n",
412 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100413 state->seq_offset, state->timestamp_offset,
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100414 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
415 endp->conn_mode);
416 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100417
418 state->in_stream.last_tsdelta = 0;
419 } else {
420 /* Compute current per-packet timestamp delta */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100421 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100422 seq, timestamp, "input",
423 &state->in_stream.last_tsdelta);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100424
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100425 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100426 ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000427 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800428
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100429 /* Save before patching */
430 state->in_stream.last_timestamp = timestamp;
431 state->in_stream.last_seq = seq;
432
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100433 if (rtp_end->force_aligned_timing &&
434 state->out_stream.ssrc == ssrc && state->packet_duration)
435 /* Align the timestamp offset */
436 align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp);
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100437
438 /* Store the updated SSRC back to the packet */
439 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100440 rtp_hdr->ssrc = htonl(ssrc);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000441
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100442 /* Apply the offset and store it back to the packet.
443 * This won't change anything if the offset is 0, so the conditional is
444 * omitted. */
445 seq += state->seq_offset;
446 rtp_hdr->sequence = htons(seq);
447 timestamp += state->timestamp_offset;
448 rtp_hdr->timestamp = htonl(timestamp);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000449
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100450 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100451 if (state->out_stream.ssrc == ssrc)
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100452 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
453 addr, seq, timestamp, "output",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100454 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100455
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200456 /*
457 * The below takes the shape of the validation from Appendix A. Check
458 * if there is something weird with the sequence number, otherwise check
459 * for a wrap around in the sequence number.
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100460 *
461 * Note that last_seq is used where the appendix mentions max_seq.
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200462 */
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100463 udelta = seq - state->out_stream.last_seq;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200464 if (udelta < RTP_MAX_DROPOUT) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100465 if (seq < state->out_stream.last_seq)
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200466 state->cycles += RTP_SEQ_MOD;
Holger Hans Peter Freyther7e7ee5f2012-12-16 13:07:45 +0100467 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200468 LOGP(DMGCP, LOGL_NOTICE,
469 "RTP seqno made a very large jump on 0x%x delta: %u\n",
470 ENDPOINT_NUMBER(endp), udelta);
471 }
472
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200473 /*
474 * calculate the jitter between the two packages. The TS should be
475 * taken closer to the read function. This was taken from the
476 * Appendix A of RFC 3550. The local timestamp has a usec resolution.
477 */
478 transit = arrival_time - timestamp;
479 d = transit - state->transit;
480 state->transit = transit;
481 if (d < 0)
482 d = -d;
483 state->jitter += d - ((state->jitter + 8) >> 4);
484
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100485 /* Save output values */
486 state->out_stream.last_seq = seq;
487 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100488 state->out_stream.ssrc = ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000489
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200490 if (payload < 0)
491 return;
492
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100493 rtp_hdr->payload_type = payload;
494}
495
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100496/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800497 * The below code is for dispatching. We have a dedicated port for
498 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100499 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800500static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
501{
502 if (!tap->enabled)
503 return 0;
504
505 return sendto(fd, buf, len, 0,
506 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
507}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800508
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200509static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
510 struct mgcp_config *cfg, int is_rtp,
511 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800512{
513 int rc;
514 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800515 struct sockaddr_in addr;
516
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100517 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800518
519 addr.sin_family = AF_INET;
520 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100521 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800522
523 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100524 end->rtp.fd :
525 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800526 (struct sockaddr *) &addr, sizeof(addr));
527
528 if (rc != len)
529 LOGP(DMGCP, LOGL_ERROR,
530 "Failed to send data to the transcoder: %s\n",
531 strerror(errno));
532
533 return rc;
534}
535
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200536static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
537 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800538{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100539 struct mgcp_trunk_config *tcfg = endp->tcfg;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100540 struct mgcp_rtp_end *rtp_end;
541 struct mgcp_rtp_state *rtp_state;
542 int tap_idx;
543
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800544 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100545 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800546 dest = !dest;
547
548 /* Loop based on the conn_mode, maybe undoing the above */
549 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
550 dest = !dest;
551
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200552 if (dest == MGCP_DEST_NET) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100553 rtp_end = &endp->net_end;
554 rtp_state = &endp->bts_state;
555 tap_idx = MGCP_TAP_NET_OUT;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800556 } else {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100557 rtp_end = &endp->bts_end;
558 rtp_state = &endp->net_state;
559 tap_idx = MGCP_TAP_BTS_OUT;
560 }
561
Jacob Erlbeck0970bab2013-12-19 12:13:32 +0100562 if (!rtp_end->output_enabled)
563 rtp_end->dropped_packets += 1;
564 else if (is_rtp) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100565 mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, rc);
566 forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx], buf, rc);
567 return mgcp_udp_send(rtp_end->rtp.fd,
568 &rtp_end->addr,
569 rtp_end->rtp_port, buf, rc);
570 } else if (!tcfg->omit_rtcp) {
571 return mgcp_udp_send(rtp_end->rtcp.fd,
572 &rtp_end->addr,
573 rtp_end->rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800574 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200575
576 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800577}
578
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200579static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
580 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100581{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800582 int rc;
583 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100584
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800585 rc = recvfrom(fd, buf, bufsize, 0,
586 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100587 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200588 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
589 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100590 return -1;
591 }
592
593 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800594 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100595 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100596
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800597 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
598
599 return rc;
600}
601
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200602static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800603{
604 char buf[4096];
605 struct sockaddr_in addr;
606 struct mgcp_endpoint *endp;
607 int rc, proto;
608
609 endp = (struct mgcp_endpoint *) fd->data;
610
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200611 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800612 if (rc <= 0)
613 return -1;
614
615 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
616 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100617 "Endpoint 0x%x data from wrong address %s vs. ",
618 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
619 LOGPC(DMGCP, LOGL_ERROR,
620 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800621 return -1;
622 }
623
624 if (endp->net_end.rtp_port != addr.sin_port &&
625 endp->net_end.rtcp_port != addr.sin_port) {
626 LOGP(DMGCP, LOGL_ERROR,
627 "Data from wrong source port %d on 0x%x\n",
628 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
629 return -1;
630 }
631
632 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200633 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800634 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
635 ENDPOINT_NUMBER(endp));
636 return 0;
637 }
638
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200639 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800640 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200641 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800642
643 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200644
645 switch (endp->type) {
646 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200647 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
648 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200649 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200650 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
651 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200652 }
653
654 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
655 endp->type, ENDPOINT_NUMBER(endp));
656 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800657}
658
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800659static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
660{
661 struct mgcp_config *cfg = endp->cfg;
662
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200663 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800664 if (!cfg->bts_ip ||
665 memcmp(&addr->sin_addr,
666 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
667 memcmp(&addr->sin_addr,
668 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
669
670 endp->bts_end.rtp_port = addr->sin_port;
671 endp->bts_end.addr = addr->sin_addr;
672
673 LOGP(DMGCP, LOGL_NOTICE,
674 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
675 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
676 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
677 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200678 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800679 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
680 sizeof(endp->bts_end.addr)) == 0) {
681 endp->bts_end.rtcp_port = addr->sin_port;
682 }
683 }
684}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800685
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200686static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800687{
688 char buf[4096];
689 struct sockaddr_in addr;
690 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800691 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800692
693 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800694
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200695 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800696 if (rc <= 0)
697 return -1;
698
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200699 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100700
701 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800702 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800703 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100704
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000705 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
706 LOGP(DMGCP, LOGL_ERROR,
707 "Data from wrong bts %s on 0x%x\n",
708 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
709 return -1;
710 }
711
712 if (endp->bts_end.rtp_port != addr.sin_port &&
713 endp->bts_end.rtcp_port != addr.sin_port) {
714 LOGP(DMGCP, LOGL_ERROR,
715 "Data from wrong bts source port %d on 0x%x\n",
716 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
717 return -1;
718 }
719
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800720 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200721 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800722 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800723 ENDPOINT_NUMBER(endp));
724 return 0;
725 }
726
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200727 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800728 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200729 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200730
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800731 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200732
733 switch (endp->type) {
734 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200735 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
736 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200737 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200738 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
739 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200740 }
741
742 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
743 endp->type, ENDPOINT_NUMBER(endp));
744 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800745}
746
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100747static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200748 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800749{
750 char buf[4096];
751 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800752 struct mgcp_config *cfg;
753 int rc, proto;
754
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100755 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200756 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800757 if (rc <= 0)
758 return -1;
759
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200760 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800761
762 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
763 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100764 "Data not coming from transcoder dest: %d %s on 0x%x\n",
765 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800766 return -1;
767 }
768
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100769 if (end->rtp_port != addr.sin_port &&
770 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800771 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100772 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
773 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800774 return -1;
775 }
776
777 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200778 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100779 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
780 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800781 return 0;
782 }
783
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100784 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200785 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100786}
787
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200788static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100789{
790 struct mgcp_endpoint *endp;
791 endp = (struct mgcp_endpoint *) fd->data;
792
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200793 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100794}
795
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200796static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100797{
798 struct mgcp_endpoint *endp;
799 endp = (struct mgcp_endpoint *) fd->data;
800
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200801 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100802}
803
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200804static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd,
805 int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100806{
807 struct sockaddr_in addr;
808 int on = 1;
809
810 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
811 if (fd->fd < 0) {
812 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
813 return -1;
814 }
815
816 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
817 memset(&addr, 0, sizeof(addr));
818 addr.sin_family = AF_INET;
819 addr.sin_port = htons(port);
820 inet_aton(source_addr, &addr.sin_addr);
821
822 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800823 close(fd->fd);
824 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100825 return -1;
826 }
827
828 return 0;
829}
830
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800831static int set_ip_tos(int fd, int tos)
832{
833 int ret;
834 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
835 &tos, sizeof(tos));
836 return ret != 0;
837}
838
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800839static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100840{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200841 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
842 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100843 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800844 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100845 goto cleanup0;
846 }
847
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200848 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
849 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100850 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800851 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100852 goto cleanup1;
853 }
854
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800855 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
856 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800857
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800858 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200859 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100860 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800861 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100862 goto cleanup2;
863 }
864
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800865 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200866 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100867 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800868 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100869 goto cleanup3;
870 }
871
872 return 0;
873
874cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200875 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100876cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800877 close(rtp_end->rtcp.fd);
878 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100879cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800880 close(rtp_end->rtp.fd);
881 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100882cleanup0:
883 return -1;
884}
885
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100886static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200887 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100888 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100889{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100890 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
891 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
892 port, ENDPOINT_NUMBER(_endp));
893 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800894 }
895
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100896 end->local_port = rtp_port;
897 end->rtp.cb = cb;
898 end->rtp.data = _endp;
899 end->rtcp.data = _endp;
900 end->rtcp.cb = cb;
901 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
902}
903
904
905int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
906{
907 return int_bind("bts-port", &endp->bts_end,
908 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800909}
910
911int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
912{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100913 return int_bind("net-port", &endp->net_end,
914 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100915}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800916
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100917int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800918{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100919 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100920 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800921}
922
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100923int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
924{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100925 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100926 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100927}
928
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800929int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
930{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800931 if (end->rtp.fd != -1) {
932 close(end->rtp.fd);
933 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200934 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800935 }
936
937 if (end->rtcp.fd != -1) {
938 close(end->rtcp.fd);
939 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200940 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800941 }
942
943 return 0;
944}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200945
946
947void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
948 struct mgcp_rtp_end *end, uint32_t *expected,
949 int *loss)
950{
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100951 *expected = state->cycles + state->out_stream.last_seq;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200952 *expected = *expected - state->base_seq + 1;
953
954 if (!state->initialized) {
955 *expected = 0;
956 *loss = 0;
957 return;
958 }
959
960 /*
961 * Make sure the sign is correct and use the biggest
962 * positive/negative number that fits.
963 */
964 *loss = *expected - end->packets;
965 if (*expected < end->packets) {
966 if (*loss > 0)
967 *loss = INT_MIN;
968 } else {
969 if (*loss < 0)
970 *loss = INT_MAX;
971 }
972}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +0200973
974uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
975{
976 if (!state->initialized)
977 return 0;
978 return state->jitter >> 4;
979}