blob: d19b56e4c994cb7598d839fdb76b1a90aa627afc [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 Erlbeck72c30902013-11-25 15:23:35 +0100251 int32_t tsdelta = state->out_stream.last_tsdelta;
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100252
253 LOGP(DMGCP, LOGL_NOTICE,
254 "The SSRC changed on 0x%x: %u -> %u "
255 "from %s:%d in %d\n",
256 ENDPOINT_NUMBER(endp),
257 state->in_stream.ssrc, rtp_hdr->ssrc,
258 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
259 endp->conn_mode);
260
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100261 if (tsdelta == 0) {
262 tsdelta = rtp_end->rate * rtp_end->frames_per_packet *
263 rtp_end->frame_duration_num /
264 rtp_end->frame_duration_den;
265 LOGP(DMGCP, LOGL_NOTICE,
266 "Computed timestamp delta %d based on "
267 "rate %d, num frames %d, frame duration %d/%d\n",
268 tsdelta, rtp_end->rate, rtp_end->frames_per_packet,
269 rtp_end->frame_duration_num,
270 rtp_end->frame_duration_den);
271 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100272 state->in_stream.ssrc = rtp_hdr->ssrc;
273 state->seq_offset = (state->out_stream.last_seq + 1) - seq;
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100274 state->timestamp_offset =
275 (state->out_stream.last_timestamp + tsdelta) - timestamp;
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100276 state->patch = rtp_end->force_constant_ssrc;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100277
278 state->in_stream.last_tsdelta = 0;
279 } else {
280 /* Compute current per-packet timestamp delta */
281 check_rtp_timestamp(endp, &state->in_stream, rtp_end, addr,
282 seq, timestamp, "input",
283 &state->in_stream.last_tsdelta);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000284 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800285
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100286 /* Save before patching */
287 state->in_stream.last_timestamp = timestamp;
288 state->in_stream.last_seq = seq;
289
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000290 /* apply the offset and store it back to the packet */
Holger Hans Peter Freyther5c1e6cf2010-08-03 15:36:57 +0000291 if (state->patch) {
292 seq += state->seq_offset;
293 rtp_hdr->sequence = htons(seq);
294 rtp_hdr->ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000295
Holger Hans Peter Freyther5c1e6cf2010-08-03 15:36:57 +0000296 timestamp += state->timestamp_offset;
297 rtp_hdr->timestamp = htonl(timestamp);
298 }
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000299
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100300 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100301 if (state->out_stream.ssrc == rtp_hdr->ssrc)
302 check_rtp_timestamp(endp, &state->out_stream, rtp_end, addr,
303 seq, timestamp, "output",
304 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100305
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200306 /*
307 * The below takes the shape of the validation from Appendix A. Check
308 * if there is something weird with the sequence number, otherwise check
309 * for a wrap around in the sequence number.
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100310 *
311 * Note that last_seq is used where the appendix mentions max_seq.
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200312 */
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100313 udelta = seq - state->out_stream.last_seq;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200314 if (udelta < RTP_MAX_DROPOUT) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100315 if (seq < state->out_stream.last_seq)
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200316 state->cycles += RTP_SEQ_MOD;
Holger Hans Peter Freyther7e7ee5f2012-12-16 13:07:45 +0100317 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200318 LOGP(DMGCP, LOGL_NOTICE,
319 "RTP seqno made a very large jump on 0x%x delta: %u\n",
320 ENDPOINT_NUMBER(endp), udelta);
321 }
322
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200323 /*
324 * calculate the jitter between the two packages. The TS should be
325 * taken closer to the read function. This was taken from the
326 * Appendix A of RFC 3550. The local timestamp has a usec resolution.
327 */
328 transit = arrival_time - timestamp;
329 d = transit - state->transit;
330 state->transit = transit;
331 if (d < 0)
332 d = -d;
333 state->jitter += d - ((state->jitter + 8) >> 4);
334
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100335 /* Save output values */
336 state->out_stream.last_seq = seq;
337 state->out_stream.last_timestamp = timestamp;
338 state->out_stream.ssrc = rtp_hdr->ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000339
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200340 if (payload < 0)
341 return;
342
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100343 rtp_hdr->payload_type = payload;
344}
345
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100346/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800347 * The below code is for dispatching. We have a dedicated port for
348 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100349 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800350static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
351{
352 if (!tap->enabled)
353 return 0;
354
355 return sendto(fd, buf, len, 0,
356 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
357}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800358
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200359static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
360 struct mgcp_config *cfg, int is_rtp,
361 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800362{
363 int rc;
364 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800365 struct sockaddr_in addr;
366
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100367 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800368
369 addr.sin_family = AF_INET;
370 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100371 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800372
373 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100374 end->rtp.fd :
375 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800376 (struct sockaddr *) &addr, sizeof(addr));
377
378 if (rc != len)
379 LOGP(DMGCP, LOGL_ERROR,
380 "Failed to send data to the transcoder: %s\n",
381 strerror(errno));
382
383 return rc;
384}
385
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200386static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
387 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800388{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100389 struct mgcp_trunk_config *tcfg = endp->tcfg;
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800390 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100391 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800392 dest = !dest;
393
394 /* Loop based on the conn_mode, maybe undoing the above */
395 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
396 dest = !dest;
397
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200398 if (dest == MGCP_DEST_NET) {
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800399 if (is_rtp) {
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100400 mgcp_patch_and_count(endp, &endp->bts_state,
401 &endp->net_end,
402 addr, buf, rc);
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800403 forward_data(endp->net_end.rtp.fd,
404 &endp->taps[MGCP_TAP_NET_OUT], buf, rc);
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200405 return mgcp_udp_send(endp->net_end.rtp.fd,
406 &endp->net_end.addr,
407 endp->net_end.rtp_port, buf, rc);
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200408 } else if (!tcfg->omit_rtcp) {
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200409 return mgcp_udp_send(endp->net_end.rtcp.fd,
410 &endp->net_end.addr,
411 endp->net_end.rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800412 }
413 } else {
414 if (is_rtp) {
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100415 mgcp_patch_and_count(endp, &endp->net_state,
416 &endp->bts_end,
417 addr, buf, rc);
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800418 forward_data(endp->bts_end.rtp.fd,
419 &endp->taps[MGCP_TAP_BTS_OUT], buf, rc);
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200420 return mgcp_udp_send(endp->bts_end.rtp.fd,
421 &endp->bts_end.addr,
422 endp->bts_end.rtp_port, buf, rc);
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200423 } else if (!tcfg->omit_rtcp) {
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200424 return mgcp_udp_send(endp->bts_end.rtcp.fd,
425 &endp->bts_end.addr,
426 endp->bts_end.rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800427 }
428 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200429
430 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800431}
432
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200433static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
434 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100435{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800436 int rc;
437 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100438
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800439 rc = recvfrom(fd, buf, bufsize, 0,
440 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100441 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200442 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
443 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100444 return -1;
445 }
446
447 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800448 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100449 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100450
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800451 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
452
453 return rc;
454}
455
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200456static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800457{
458 char buf[4096];
459 struct sockaddr_in addr;
460 struct mgcp_endpoint *endp;
461 int rc, proto;
462
463 endp = (struct mgcp_endpoint *) fd->data;
464
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200465 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800466 if (rc <= 0)
467 return -1;
468
469 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
470 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100471 "Endpoint 0x%x data from wrong address %s vs. ",
472 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
473 LOGPC(DMGCP, LOGL_ERROR,
474 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800475 return -1;
476 }
477
478 if (endp->net_end.rtp_port != addr.sin_port &&
479 endp->net_end.rtcp_port != addr.sin_port) {
480 LOGP(DMGCP, LOGL_ERROR,
481 "Data from wrong source port %d on 0x%x\n",
482 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
483 return -1;
484 }
485
486 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200487 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800488 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
489 ENDPOINT_NUMBER(endp));
490 return 0;
491 }
492
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200493 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800494 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200495 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800496
497 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200498
499 switch (endp->type) {
500 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200501 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
502 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200503 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200504 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
505 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200506 }
507
508 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
509 endp->type, ENDPOINT_NUMBER(endp));
510 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800511}
512
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800513static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
514{
515 struct mgcp_config *cfg = endp->cfg;
516
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200517 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800518 if (!cfg->bts_ip ||
519 memcmp(&addr->sin_addr,
520 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
521 memcmp(&addr->sin_addr,
522 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
523
524 endp->bts_end.rtp_port = addr->sin_port;
525 endp->bts_end.addr = addr->sin_addr;
526
527 LOGP(DMGCP, LOGL_NOTICE,
528 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
529 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
530 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
531 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200532 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800533 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
534 sizeof(endp->bts_end.addr)) == 0) {
535 endp->bts_end.rtcp_port = addr->sin_port;
536 }
537 }
538}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800539
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200540static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800541{
542 char buf[4096];
543 struct sockaddr_in addr;
544 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800545 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800546
547 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800548
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200549 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800550 if (rc <= 0)
551 return -1;
552
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200553 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100554
555 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800556 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800557 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100558
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000559 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
560 LOGP(DMGCP, LOGL_ERROR,
561 "Data from wrong bts %s on 0x%x\n",
562 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
563 return -1;
564 }
565
566 if (endp->bts_end.rtp_port != addr.sin_port &&
567 endp->bts_end.rtcp_port != addr.sin_port) {
568 LOGP(DMGCP, LOGL_ERROR,
569 "Data from wrong bts source port %d on 0x%x\n",
570 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
571 return -1;
572 }
573
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800574 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200575 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800576 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800577 ENDPOINT_NUMBER(endp));
578 return 0;
579 }
580
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200581 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800582 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200583 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200584
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800585 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200586
587 switch (endp->type) {
588 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200589 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
590 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200591 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200592 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
593 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200594 }
595
596 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
597 endp->type, ENDPOINT_NUMBER(endp));
598 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800599}
600
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100601static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200602 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800603{
604 char buf[4096];
605 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800606 struct mgcp_config *cfg;
607 int rc, proto;
608
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100609 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200610 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800611 if (rc <= 0)
612 return -1;
613
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200614 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800615
616 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
617 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100618 "Data not coming from transcoder dest: %d %s on 0x%x\n",
619 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800620 return -1;
621 }
622
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100623 if (end->rtp_port != addr.sin_port &&
624 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800625 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100626 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
627 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800628 return -1;
629 }
630
631 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200632 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100633 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
634 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800635 return 0;
636 }
637
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100638 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200639 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100640}
641
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200642static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100643{
644 struct mgcp_endpoint *endp;
645 endp = (struct mgcp_endpoint *) fd->data;
646
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200647 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100648}
649
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200650static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100651{
652 struct mgcp_endpoint *endp;
653 endp = (struct mgcp_endpoint *) fd->data;
654
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200655 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100656}
657
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200658static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd,
659 int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100660{
661 struct sockaddr_in addr;
662 int on = 1;
663
664 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
665 if (fd->fd < 0) {
666 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
667 return -1;
668 }
669
670 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
671 memset(&addr, 0, sizeof(addr));
672 addr.sin_family = AF_INET;
673 addr.sin_port = htons(port);
674 inet_aton(source_addr, &addr.sin_addr);
675
676 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800677 close(fd->fd);
678 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100679 return -1;
680 }
681
682 return 0;
683}
684
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800685static int set_ip_tos(int fd, int tos)
686{
687 int ret;
688 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
689 &tos, sizeof(tos));
690 return ret != 0;
691}
692
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800693static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100694{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200695 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
696 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100697 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800698 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100699 goto cleanup0;
700 }
701
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200702 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
703 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100704 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800705 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100706 goto cleanup1;
707 }
708
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800709 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
710 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800711
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800712 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200713 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100714 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800715 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100716 goto cleanup2;
717 }
718
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800719 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200720 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100721 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800722 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100723 goto cleanup3;
724 }
725
726 return 0;
727
728cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200729 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100730cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800731 close(rtp_end->rtcp.fd);
732 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100733cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800734 close(rtp_end->rtp.fd);
735 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100736cleanup0:
737 return -1;
738}
739
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100740static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200741 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100742 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100743{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100744 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
745 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
746 port, ENDPOINT_NUMBER(_endp));
747 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800748 }
749
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100750 end->local_port = rtp_port;
751 end->rtp.cb = cb;
752 end->rtp.data = _endp;
753 end->rtcp.data = _endp;
754 end->rtcp.cb = cb;
755 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
756}
757
758
759int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
760{
761 return int_bind("bts-port", &endp->bts_end,
762 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800763}
764
765int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
766{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100767 return int_bind("net-port", &endp->net_end,
768 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100769}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800770
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100771int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800772{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100773 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100774 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800775}
776
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100777int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
778{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100779 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100780 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100781}
782
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800783int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
784{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800785 if (end->rtp.fd != -1) {
786 close(end->rtp.fd);
787 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200788 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800789 }
790
791 if (end->rtcp.fd != -1) {
792 close(end->rtcp.fd);
793 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200794 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800795 }
796
797 return 0;
798}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200799
800
801void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
802 struct mgcp_rtp_end *end, uint32_t *expected,
803 int *loss)
804{
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100805 *expected = state->cycles + state->out_stream.last_seq;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200806 *expected = *expected - state->base_seq + 1;
807
808 if (!state->initialized) {
809 *expected = 0;
810 *loss = 0;
811 return;
812 }
813
814 /*
815 * Make sure the sign is correct and use the biggest
816 * positive/negative number that fits.
817 */
818 *loss = *expected - end->packets;
819 if (*expected < end->packets) {
820 if (*loss > 0)
821 *loss = INT_MIN;
822 } else {
823 if (*loss < 0)
824 *loss = INT_MAX;
825 }
826}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +0200827
828uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
829{
830 if (!state->initialized)
831 return 0;
832 return state->jitter >> 4;
833}