blob: c14b9133b5add3aeb88a0181e2e08217c24527ae [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 Erlbeck50079a12013-11-25 12:53:28 +0100137static 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
203
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200204/**
205 * The RFC 3550 Appendix A assumes there are multiple sources but
206 * some of the supported endpoints (e.g. the nanoBTS) can only handle
207 * one source and this code will patch packages to appear as if there
208 * is only one source.
209 * There is also no probation period for new sources. Every package
210 * we receive will be seen as a switch in streams.
211 */
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100212void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
213 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
214 char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100215{
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200216 uint32_t arrival_time;
217 int32_t transit, d;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200218 uint16_t seq, udelta;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000219 uint32_t timestamp;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100220 struct rtp_hdr *rtp_hdr;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100221 int payload = rtp_end->payload_type;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100222
223 if (len < sizeof(*rtp_hdr))
224 return;
225
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800226 rtp_hdr = (struct rtp_hdr *) data;
227 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000228 timestamp = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200229 arrival_time = get_current_ts();
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800230
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000231 if (!state->initialized) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100232 state->in_stream.last_seq = seq - 1;
233 state->in_stream.ssrc = state->orig_ssrc = rtp_hdr->ssrc;
234 state->in_stream.last_tsdelta = 0;
Holger Hans Peter Freythered3a6612012-10-22 17:08:48 +0200235 state->base_seq = seq;
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000236 state->initialized = 1;
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200237 state->jitter = 0;
238 state->transit = arrival_time - timestamp;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100239 state->out_stream = state->in_stream;
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100240 state->out_stream.last_timestamp = timestamp;
241 /* force output SSRC change */
242 state->out_stream.ssrc = rtp_hdr->ssrc - 1;
243 LOGP(DMGCP, LOGL_INFO,
244 "Initializing stream on 0x%x SSRC: %u timestamp: %u "
245 "from %s:%d in %d\n",
246 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
247 state->seq_offset,
248 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
249 endp->conn_mode);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100250 } else if (state->in_stream.ssrc != rtp_hdr->ssrc) {
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100251 LOGP(DMGCP, LOGL_NOTICE,
252 "The SSRC changed on 0x%x: %u -> %u "
253 "from %s:%d in %d\n",
254 ENDPOINT_NUMBER(endp),
255 state->in_stream.ssrc, rtp_hdr->ssrc,
256 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
257 endp->conn_mode);
258
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100259 state->in_stream.ssrc = rtp_hdr->ssrc;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100260 if (rtp_end->force_constant_ssrc) {
261 int32_t tsdelta = state->out_stream.last_tsdelta;
262 if (tsdelta == 0) {
263 tsdelta = rtp_end->rate * rtp_end->frames_per_packet *
264 rtp_end->frame_duration_num /
265 rtp_end->frame_duration_den;
266 LOGP(DMGCP, LOGL_NOTICE,
267 "Computed timestamp delta %d based on "
268 "rate %d, num frames %d, frame duration %d/%d\n",
269 tsdelta, rtp_end->rate, rtp_end->frames_per_packet,
270 rtp_end->frame_duration_num,
271 rtp_end->frame_duration_den);
272 }
273 state->seq_offset =
274 (state->out_stream.last_seq + 1) - seq;
275 state->timestamp_offset =
276 (state->out_stream.last_timestamp + tsdelta) -
277 timestamp;
278 state->patch = 1;
279
280 LOGP(DMGCP, LOGL_NOTICE,
281 "SSRC patching enabled on 0x%x SSRC: %u "
282 "offset: %d tsdelta: %d "
283 "from %s:%d in %d\n",
284 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
285 state->seq_offset, tsdelta,
286 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
287 endp->conn_mode);
288 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100289
290 state->in_stream.last_tsdelta = 0;
291 } else {
292 /* Compute current per-packet timestamp delta */
293 check_rtp_timestamp(endp, &state->in_stream, rtp_end, addr,
294 seq, timestamp, "input",
295 &state->in_stream.last_tsdelta);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000296 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800297
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100298 /* Save before patching */
299 state->in_stream.last_timestamp = timestamp;
300 state->in_stream.last_seq = seq;
301
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000302 /* apply the offset and store it back to the packet */
Holger Hans Peter Freyther5c1e6cf2010-08-03 15:36:57 +0000303 if (state->patch) {
304 seq += state->seq_offset;
305 rtp_hdr->sequence = htons(seq);
306 rtp_hdr->ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000307
Holger Hans Peter Freyther5c1e6cf2010-08-03 15:36:57 +0000308 timestamp += state->timestamp_offset;
309 rtp_hdr->timestamp = htonl(timestamp);
310 }
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000311
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100312 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100313 if (state->out_stream.ssrc == rtp_hdr->ssrc)
314 check_rtp_timestamp(endp, &state->out_stream, rtp_end, addr,
315 seq, timestamp, "output",
316 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100317
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200318 /*
319 * The below takes the shape of the validation from Appendix A. Check
320 * if there is something weird with the sequence number, otherwise check
321 * for a wrap around in the sequence number.
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100322 *
323 * Note that last_seq is used where the appendix mentions max_seq.
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200324 */
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100325 udelta = seq - state->out_stream.last_seq;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200326 if (udelta < RTP_MAX_DROPOUT) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100327 if (seq < state->out_stream.last_seq)
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200328 state->cycles += RTP_SEQ_MOD;
Holger Hans Peter Freyther7e7ee5f2012-12-16 13:07:45 +0100329 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200330 LOGP(DMGCP, LOGL_NOTICE,
331 "RTP seqno made a very large jump on 0x%x delta: %u\n",
332 ENDPOINT_NUMBER(endp), udelta);
333 }
334
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200335 /*
336 * calculate the jitter between the two packages. The TS should be
337 * taken closer to the read function. This was taken from the
338 * Appendix A of RFC 3550. The local timestamp has a usec resolution.
339 */
340 transit = arrival_time - timestamp;
341 d = transit - state->transit;
342 state->transit = transit;
343 if (d < 0)
344 d = -d;
345 state->jitter += d - ((state->jitter + 8) >> 4);
346
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100347 /* Save output values */
348 state->out_stream.last_seq = seq;
349 state->out_stream.last_timestamp = timestamp;
350 state->out_stream.ssrc = rtp_hdr->ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000351
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200352 if (payload < 0)
353 return;
354
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100355 rtp_hdr->payload_type = payload;
356}
357
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100358/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800359 * The below code is for dispatching. We have a dedicated port for
360 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100361 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800362static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
363{
364 if (!tap->enabled)
365 return 0;
366
367 return sendto(fd, buf, len, 0,
368 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
369}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800370
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200371static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
372 struct mgcp_config *cfg, int is_rtp,
373 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800374{
375 int rc;
376 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800377 struct sockaddr_in addr;
378
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100379 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800380
381 addr.sin_family = AF_INET;
382 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100383 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800384
385 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100386 end->rtp.fd :
387 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800388 (struct sockaddr *) &addr, sizeof(addr));
389
390 if (rc != len)
391 LOGP(DMGCP, LOGL_ERROR,
392 "Failed to send data to the transcoder: %s\n",
393 strerror(errno));
394
395 return rc;
396}
397
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200398static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
399 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800400{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100401 struct mgcp_trunk_config *tcfg = endp->tcfg;
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800402 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100403 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800404 dest = !dest;
405
406 /* Loop based on the conn_mode, maybe undoing the above */
407 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
408 dest = !dest;
409
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200410 if (dest == MGCP_DEST_NET) {
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800411 if (is_rtp) {
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100412 mgcp_patch_and_count(endp, &endp->bts_state,
413 &endp->net_end,
414 addr, buf, rc);
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800415 forward_data(endp->net_end.rtp.fd,
416 &endp->taps[MGCP_TAP_NET_OUT], buf, rc);
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200417 return mgcp_udp_send(endp->net_end.rtp.fd,
418 &endp->net_end.addr,
419 endp->net_end.rtp_port, buf, rc);
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200420 } else if (!tcfg->omit_rtcp) {
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200421 return mgcp_udp_send(endp->net_end.rtcp.fd,
422 &endp->net_end.addr,
423 endp->net_end.rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800424 }
425 } else {
426 if (is_rtp) {
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100427 mgcp_patch_and_count(endp, &endp->net_state,
428 &endp->bts_end,
429 addr, buf, rc);
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800430 forward_data(endp->bts_end.rtp.fd,
431 &endp->taps[MGCP_TAP_BTS_OUT], buf, rc);
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200432 return mgcp_udp_send(endp->bts_end.rtp.fd,
433 &endp->bts_end.addr,
434 endp->bts_end.rtp_port, buf, rc);
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200435 } else if (!tcfg->omit_rtcp) {
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200436 return mgcp_udp_send(endp->bts_end.rtcp.fd,
437 &endp->bts_end.addr,
438 endp->bts_end.rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800439 }
440 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200441
442 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800443}
444
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200445static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
446 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100447{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800448 int rc;
449 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100450
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800451 rc = recvfrom(fd, buf, bufsize, 0,
452 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100453 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200454 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
455 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100456 return -1;
457 }
458
459 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800460 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100461 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100462
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800463 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
464
465 return rc;
466}
467
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200468static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800469{
470 char buf[4096];
471 struct sockaddr_in addr;
472 struct mgcp_endpoint *endp;
473 int rc, proto;
474
475 endp = (struct mgcp_endpoint *) fd->data;
476
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200477 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800478 if (rc <= 0)
479 return -1;
480
481 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
482 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100483 "Endpoint 0x%x data from wrong address %s vs. ",
484 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
485 LOGPC(DMGCP, LOGL_ERROR,
486 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800487 return -1;
488 }
489
490 if (endp->net_end.rtp_port != addr.sin_port &&
491 endp->net_end.rtcp_port != addr.sin_port) {
492 LOGP(DMGCP, LOGL_ERROR,
493 "Data from wrong source port %d on 0x%x\n",
494 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
495 return -1;
496 }
497
498 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200499 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800500 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
501 ENDPOINT_NUMBER(endp));
502 return 0;
503 }
504
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200505 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800506 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200507 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800508
509 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200510
511 switch (endp->type) {
512 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200513 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
514 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200515 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200516 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
517 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200518 }
519
520 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
521 endp->type, ENDPOINT_NUMBER(endp));
522 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800523}
524
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800525static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
526{
527 struct mgcp_config *cfg = endp->cfg;
528
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200529 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800530 if (!cfg->bts_ip ||
531 memcmp(&addr->sin_addr,
532 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
533 memcmp(&addr->sin_addr,
534 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
535
536 endp->bts_end.rtp_port = addr->sin_port;
537 endp->bts_end.addr = addr->sin_addr;
538
539 LOGP(DMGCP, LOGL_NOTICE,
540 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
541 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
542 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
543 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200544 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800545 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
546 sizeof(endp->bts_end.addr)) == 0) {
547 endp->bts_end.rtcp_port = addr->sin_port;
548 }
549 }
550}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800551
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200552static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800553{
554 char buf[4096];
555 struct sockaddr_in addr;
556 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800557 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800558
559 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800560
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200561 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800562 if (rc <= 0)
563 return -1;
564
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200565 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100566
567 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800568 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800569 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100570
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000571 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
572 LOGP(DMGCP, LOGL_ERROR,
573 "Data from wrong bts %s on 0x%x\n",
574 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
575 return -1;
576 }
577
578 if (endp->bts_end.rtp_port != addr.sin_port &&
579 endp->bts_end.rtcp_port != addr.sin_port) {
580 LOGP(DMGCP, LOGL_ERROR,
581 "Data from wrong bts source port %d on 0x%x\n",
582 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
583 return -1;
584 }
585
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800586 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200587 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800588 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800589 ENDPOINT_NUMBER(endp));
590 return 0;
591 }
592
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200593 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800594 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200595 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200596
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800597 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200598
599 switch (endp->type) {
600 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200601 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
602 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200603 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200604 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
605 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200606 }
607
608 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
609 endp->type, ENDPOINT_NUMBER(endp));
610 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800611}
612
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100613static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200614 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800615{
616 char buf[4096];
617 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800618 struct mgcp_config *cfg;
619 int rc, proto;
620
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100621 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200622 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800623 if (rc <= 0)
624 return -1;
625
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200626 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800627
628 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
629 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100630 "Data not coming from transcoder dest: %d %s on 0x%x\n",
631 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800632 return -1;
633 }
634
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100635 if (end->rtp_port != addr.sin_port &&
636 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800637 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100638 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
639 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800640 return -1;
641 }
642
643 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200644 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100645 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
646 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800647 return 0;
648 }
649
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100650 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200651 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100652}
653
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200654static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100655{
656 struct mgcp_endpoint *endp;
657 endp = (struct mgcp_endpoint *) fd->data;
658
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200659 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100660}
661
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200662static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100663{
664 struct mgcp_endpoint *endp;
665 endp = (struct mgcp_endpoint *) fd->data;
666
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200667 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100668}
669
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200670static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd,
671 int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100672{
673 struct sockaddr_in addr;
674 int on = 1;
675
676 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
677 if (fd->fd < 0) {
678 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
679 return -1;
680 }
681
682 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
683 memset(&addr, 0, sizeof(addr));
684 addr.sin_family = AF_INET;
685 addr.sin_port = htons(port);
686 inet_aton(source_addr, &addr.sin_addr);
687
688 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800689 close(fd->fd);
690 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100691 return -1;
692 }
693
694 return 0;
695}
696
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800697static int set_ip_tos(int fd, int tos)
698{
699 int ret;
700 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
701 &tos, sizeof(tos));
702 return ret != 0;
703}
704
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800705static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100706{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200707 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
708 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100709 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800710 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100711 goto cleanup0;
712 }
713
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200714 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
715 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100716 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800717 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100718 goto cleanup1;
719 }
720
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800721 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
722 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800723
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800724 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200725 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100726 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800727 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100728 goto cleanup2;
729 }
730
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800731 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200732 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100733 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800734 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100735 goto cleanup3;
736 }
737
738 return 0;
739
740cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200741 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100742cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800743 close(rtp_end->rtcp.fd);
744 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100745cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800746 close(rtp_end->rtp.fd);
747 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100748cleanup0:
749 return -1;
750}
751
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100752static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200753 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100754 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100755{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100756 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
757 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
758 port, ENDPOINT_NUMBER(_endp));
759 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800760 }
761
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100762 end->local_port = rtp_port;
763 end->rtp.cb = cb;
764 end->rtp.data = _endp;
765 end->rtcp.data = _endp;
766 end->rtcp.cb = cb;
767 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
768}
769
770
771int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
772{
773 return int_bind("bts-port", &endp->bts_end,
774 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800775}
776
777int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
778{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100779 return int_bind("net-port", &endp->net_end,
780 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100781}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800782
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100783int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800784{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100785 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100786 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800787}
788
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100789int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
790{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100791 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100792 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100793}
794
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800795int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
796{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800797 if (end->rtp.fd != -1) {
798 close(end->rtp.fd);
799 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200800 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800801 }
802
803 if (end->rtcp.fd != -1) {
804 close(end->rtcp.fd);
805 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200806 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800807 }
808
809 return 0;
810}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200811
812
813void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
814 struct mgcp_rtp_end *end, uint32_t *expected,
815 int *loss)
816{
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100817 *expected = state->cycles + state->out_stream.last_seq;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200818 *expected = *expected - state->base_seq + 1;
819
820 if (!state->initialized) {
821 *expected = 0;
822 *loss = 0;
823 return;
824 }
825
826 /*
827 * Make sure the sign is correct and use the biggest
828 * positive/negative number that fits.
829 */
830 *loss = *expected - end->packets;
831 if (*expected < end->packets) {
832 if (*loss > 0)
833 *loss = INT_MIN;
834 } else {
835 if (*loss < 0)
836 *loss = INT_MAX;
837 }
838}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +0200839
840uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
841{
842 if (!state->initialized)
843 return 0;
844 return state->jitter >> 4;
845}