blob: 7876b33b48597159b30c48379d0c95870a194077 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <sys/socket.h>
31#include <arpa/inet.h>
32
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010035#include <osmocom/core/socket.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/osmux.h>
42#include <osmocom/mgcp/mgcp_conn.h>
43#include <osmocom/mgcp/mgcp_ep.h>
Philipp Maierc3413882017-10-27 12:26:54 +020044#include <osmocom/mgcp/debug.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020045
46#define RTP_SEQ_MOD (1 << 16)
47#define RTP_MAX_DROPOUT 3000
48#define RTP_MAX_MISORDER 100
49#define RTP_BUF_SIZE 4096
50
51enum {
52 MGCP_PROTO_RTP,
53 MGCP_PROTO_RTCP,
54};
55
Philipp Maier1cb1e382017-11-02 17:16:04 +010056/*! Determine the local rtp bind IP-address.
57 * \param[out] addr caller provided memory to store the resulting IP-Address
58 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
59 *
60 * The local bind IP-address is automatically selected by probing the
61 * IP-Address of the interface that is pointing towards the remote IP-Address,
62 * if no remote IP-Address is known yet, the statically configured
63 * IP-Addresses are used as fallback. */
64void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
65{
66
67 struct mgcp_endpoint *endp;
68 int rc;
69 endp = conn->conn->endp;
70
71 /* Try probing the local IP-Address */
72 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
73 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
74 if (rc < 0)
75 LOGP(DRTP, LOGL_ERROR,
76 "endpoint:%x CI:%i local interface auto detection failed, using configured addresses...\n",
77 ENDPOINT_NUMBER(endp), conn->conn->id);
78 else {
79 LOGP(DRTP, LOGL_DEBUG,
80 "endpoint:%x CI:%i selected local rtp bind ip %s by probing using remote ip %s\n",
81 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
82 inet_ntoa(conn->end.addr));
83 return;
84 }
85 }
86
87 /* Select from preconfigured IP-Addresses */
88 if (endp->cfg->net_ports.bind_addr) {
89 /* Check there is a bind IP for the RTP traffic configured,
90 * if so, use that IP-Address */
91 strncpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
92 LOGP(DRTP, LOGL_DEBUG,
93 "endpoint:%x CI:%i using configured rtp bind ip as local bind ip %s\n",
94 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
95 } else {
96 /* No specific bind IP is configured for the RTP traffic, so
97 * assume the IP where we listen for incoming MGCP messages
98 * as bind IP */
99 strncpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
100 LOGP(DRTP, LOGL_DEBUG,
101 "endpoint:%x CI:%i using mgcp bind ip as local rtp bind ip: %s\n",
102 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
103 }
104}
105
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 * 1/codec_rate seconds. */
109static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110{
111 struct timespec tp;
112 uint64_t ret;
113
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200115 return 0;
116
117 memset(&tp, 0, sizeof(tp));
118 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200119 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200120
121 /* convert it to 1/unit seconds */
122 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 ret *= codec_rate;
124 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125
126 return ret;
127}
128
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129/*! send udp packet.
130 * \param[in] fd associated file descriptor
131 * \param[in] addr destination ip-address
132 * \param[in] port destination UDP port
133 * \param[in] buf buffer that holds the data to be send
134 * \param[in] len length of the data to be sent
135 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
137{
138 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200139
Philipp Maierc3413882017-10-27 12:26:54 +0200140 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200141 "sending %i bytes length packet to %s:%u ...\n",
142 len, inet_ntoa(*addr), ntohs(port));
143
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 out.sin_family = AF_INET;
145 out.sin_port = port;
146 memcpy(&out.sin_addr, addr, sizeof(*addr));
147
148 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
149}
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151/*! send RTP dummy packet (to keep NAT connection open).
152 * \param[in] endp mcgp endpoint that holds the RTP connection
153 * \param[in] conn associated RTP connection
154 * \returns bytes sent, -1 on error */
155int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156{
157 static char buf[] = { MGCP_DUMMY_LOAD };
158 int rc;
159 int was_rtcp = 0;
160
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 OSMO_ASSERT(endp);
162 OSMO_ASSERT(conn);
163
Philipp Maierc3413882017-10-27 12:26:54 +0200164 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 "endpoint:%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200166 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
168
169 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
170 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171
172 if (rc == -1)
173 goto failed;
174
175 if (endp->tcfg->omit_rtcp)
176 return rc;
177
178 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
180 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181
182 if (rc >= 0)
183 return rc;
184
185failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200186 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 "endpoint:%x Failed to send dummy %s packet.\n",
188 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189
190 return -1;
191}
192
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193/* Compute timestamp alignment error */
194static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
195 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196{
197 int32_t timestamp_delta;
198
199 if (ptime == 0)
200 return 0;
201
202 /* Align according to: T - Tlast = k * Tptime */
203 timestamp_delta = timestamp - sstate->last_timestamp;
204
205 return timestamp_delta % ptime;
206}
207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209static int check_rtp_timestamp(struct mgcp_endpoint *endp,
210 struct mgcp_rtp_state *state,
211 struct mgcp_rtp_stream_state *sstate,
212 struct mgcp_rtp_end *rtp_end,
213 struct sockaddr_in *addr,
214 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200215 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216{
217 int32_t tsdelta;
218 int32_t timestamp_error;
219
220 /* Not fully intialized, skip */
221 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
222 return 0;
223
224 if (seq == sstate->last_seq) {
225 if (timestamp != sstate->last_timestamp) {
226 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200227 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228 "The %s timestamp delta is != 0 but the sequence "
229 "number %d is the same, "
230 "TS offset: %d, SeqNo offset: %d "
231 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233 text, seq,
234 state->timestamp_offset, state->seq_offset,
235 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 }
238 return 0;
239 }
240
241 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 (int32_t)(timestamp - sstate->last_timestamp) /
243 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244
245 if (tsdelta == 0) {
246 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200247 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248 "The %s timestamp delta is %d "
249 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251 text, tsdelta,
252 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
255 return 0;
256 }
257
258 if (sstate->last_tsdelta != tsdelta) {
259 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200260 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263 text, sstate->last_tsdelta, tsdelta,
264 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200265 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266 }
267 }
268
269 if (tsdelta_out)
270 *tsdelta_out = tsdelta;
271
272 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274
275 if (timestamp_error) {
276 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200277 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278 "The %s timestamp has an alignment error of %d "
279 "on 0x%x SSRC: %u "
280 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 text, timestamp_error,
283 ENDPOINT_NUMBER(endp), sstate->ssrc,
284 (int16_t)(seq - sstate->last_seq),
285 (int32_t)(timestamp - sstate->last_timestamp),
286 tsdelta,
287 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289 }
290 return 1;
291}
292
293/* Set the timestamp offset according to the packet duration. */
294static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
295 struct mgcp_rtp_state *state,
296 struct mgcp_rtp_end *rtp_end,
297 struct sockaddr_in *addr,
298 int16_t delta_seq, uint32_t in_timestamp)
299{
300 int32_t tsdelta = state->packet_duration;
301 int timestamp_offset;
302 uint32_t out_timestamp;
303
304 if (tsdelta == 0) {
305 tsdelta = state->out_stream.last_tsdelta;
306 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200307 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 "A fixed packet duration is not available on 0x%x, "
309 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200313 } else {
314 tsdelta = rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200315 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316 "Fixed packet duration and last timestamp delta "
317 "are not available on 0x%x, "
318 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200319 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322 }
323 }
324
325 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
326 timestamp_offset = out_timestamp - in_timestamp;
327
328 if (state->timestamp_offset != timestamp_offset) {
329 state->timestamp_offset = timestamp_offset;
330
Philipp Maierc3413882017-10-27 12:26:54 +0200331 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332 "Timestamp offset change on 0x%x SSRC: %u "
333 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200334 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
336 delta_seq, state->timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200337 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 }
339
340 return timestamp_offset;
341}
342
343/* Set the timestamp offset according to the packet duration. */
344static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
345 struct mgcp_rtp_state *state,
346 struct mgcp_rtp_end *rtp_end,
347 struct sockaddr_in *addr,
348 uint32_t timestamp)
349{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200350 int ts_error = 0;
351 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352 int ptime = state->packet_duration;
353
354 /* Align according to: T + Toffs - Tlast = k * Tptime */
355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 ts_error = ts_alignment_error(&state->out_stream, ptime,
357 timestamp + state->timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 /* If there is an alignment error, we have to compensate it */
360 if (ts_error) {
361 state->timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362
Philipp Maierc3413882017-10-27 12:26:54 +0200363 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
365 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366 "from %s:%d\n",
367 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
369 state->timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371 }
372
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373 /* Check we really managed to compensate the timestamp
374 * offset. There should not be any remaining error, failing
375 * here would point to a serous problem with the alingnment
376 * error computation fuction */
377 ts_check = ts_alignment_error(&state->out_stream, ptime,
378 timestamp + state->timestamp_offset);
379 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 /* Return alignment error before compensation */
382 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383}
384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
386 * \param[in] associated endpoint
387 * \param[in] destination RTP end
388 * \param[in,out] pointer to buffer with voice data
389 * \param[in] voice data length
390 * \param[in] maxmimum size of caller provided voice data buffer
391 * \returns ignores input parameters, return always 0 */
392int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
393 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394 char *data, int *len, int buf_size)
395{
Philipp Maierc3413882017-10-27 12:26:54 +0200396 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398 return 0;
399}
400
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
402 * \param[in] associated endpoint
403 * \param[in] destination RTP end
404 * \param[in] source RTP end
405 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
407 struct mgcp_rtp_end *dst_end,
408 struct mgcp_rtp_end *src_end)
409{
Philipp Maierc3413882017-10-27 12:26:54 +0200410 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412 return 0;
413}
414
415void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
416 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 const char **audio_name,
418 const char **fmtp_extra,
419 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200420{
Philipp Maierc3413882017-10-27 12:26:54 +0200421 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200422 "endpoint:%x conn:%s using format defaults\n",
423 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
424
425 *payload_type = conn->end.codec.payload_type;
426 *audio_name = conn->end.codec.audio_name;
427 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428}
429
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
431 struct mgcp_rtp_state *state, const uint16_t seq,
432 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433{
434 int32_t d;
435
436 /* initialize or re-initialize */
437 if (!state->stats_initialized || state->stats_ssrc != ssrc) {
438 state->stats_initialized = 1;
439 state->stats_base_seq = seq;
440 state->stats_max_seq = seq - 1;
441 state->stats_ssrc = ssrc;
442 state->stats_jitter = 0;
443 state->stats_transit = transit;
444 state->stats_cycles = 0;
445 } else {
446 uint16_t udelta;
447
Philipp Maier87bd9be2017-08-22 16:35:41 +0200448 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 * Appendix A. Check if there is something weird with
450 * the sequence number, otherwise check for a wrap
451 * around in the sequence number.
452 * It can't wrap during the initialization so let's
453 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 * this issue because of the probation. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 udelta = seq - state->stats_max_seq;
456 if (udelta < RTP_MAX_DROPOUT) {
457 if (seq < state->stats_max_seq)
458 state->stats_cycles += RTP_SEQ_MOD;
459 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200460 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 "RTP seqno made a very large jump on 0x%x delta: %u\n",
462 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 }
464 }
465
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 * taken closer to the read function. This was taken from the
468 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200469 * resolution. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200470 d = transit - state->stats_transit;
471 state->stats_transit = transit;
472 if (d < 0)
473 d = -d;
474 state->stats_jitter += d - ((state->stats_jitter + 8) >> 4);
475 state->stats_max_seq = seq;
476}
477
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479 * some of the supported endpoints (e.g. the nanoBTS) can only handle
480 * one source and this code will patch RTP header to appear as if there
481 * is only one source.
482 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483 * we receive will be seen as a switch in streams. */
484void mgcp_patch_and_count(struct mgcp_endpoint *endp,
485 struct mgcp_rtp_state *state,
486 struct mgcp_rtp_end *rtp_end,
487 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200488{
489 uint32_t arrival_time;
490 int32_t transit;
491 uint16_t seq;
492 uint32_t timestamp, ssrc;
493 struct rtp_hdr *rtp_hdr;
494 int payload = rtp_end->codec.payload_type;
495
496 if (len < sizeof(*rtp_hdr))
497 return;
498
Philipp Maier87bd9be2017-08-22 16:35:41 +0200499 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500 seq = ntohs(rtp_hdr->sequence);
501 timestamp = ntohl(rtp_hdr->timestamp);
502 arrival_time = get_current_ts(rtp_end->codec.rate);
503 ssrc = ntohl(rtp_hdr->ssrc);
504 transit = arrival_time - timestamp;
505
506 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
507
508 if (!state->initialized) {
509 state->initialized = 1;
510 state->in_stream.last_seq = seq - 1;
511 state->in_stream.ssrc = state->orig_ssrc = ssrc;
512 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 state->packet_duration =
514 mgcp_rtp_packet_duration(endp, rtp_end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 state->out_stream = state->in_stream;
516 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200518 LOGP(DRTP, LOGL_INFO,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200519 "endpoint:%x initializing stream, SSRC: %u timestamp: %u "
520 "pkt-duration: %d, from %s:%d\n",
521 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
522 state->seq_offset, state->packet_duration,
523 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200525 state->packet_duration =
526 rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200527 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200528 "endpoint:%x fixed packet duration is not available, "
529 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200531 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 }
533 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200534 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535 "endpoint:%x SSRC changed: %u -> %u "
536 "from %s:%d\n",
537 ENDPOINT_NUMBER(endp),
538 state->in_stream.ssrc, rtp_hdr->ssrc,
539 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540
541 state->in_stream.ssrc = ssrc;
542 if (rtp_end->force_constant_ssrc) {
543 int16_t delta_seq;
544
545 /* Always increment seqno by 1 */
546 state->seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548
549 /* Estimate number of packets that would have been sent */
550 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 (arrival_time - state->in_stream.last_arrival_time
552 + state->packet_duration / 2) /
553 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554
555 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
556 delta_seq, timestamp);
557
558 state->patch_ssrc = 1;
559 ssrc = state->orig_ssrc;
560 if (rtp_end->force_constant_ssrc != -1)
561 rtp_end->force_constant_ssrc -= 1;
562
Philipp Maierc3413882017-10-27 12:26:54 +0200563 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 "endpoint:%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
568 state->seq_offset, state->timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200569 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570 }
571
572 state->in_stream.last_tsdelta = 0;
573 } else {
574 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200575 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
576 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200577 &state->in_stream.last_tsdelta);
578
579 if (state->patch_ssrc)
580 ssrc = state->orig_ssrc;
581 }
582
583 /* Save before patching */
584 state->in_stream.last_timestamp = timestamp;
585 state->in_stream.last_seq = seq;
586 state->in_stream.last_arrival_time = arrival_time;
587
588 if (rtp_end->force_aligned_timing &&
589 state->out_stream.ssrc == ssrc && state->packet_duration)
590 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200591 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
592 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593
594 /* Store the updated SSRC back to the packet */
595 if (state->patch_ssrc)
596 rtp_hdr->ssrc = htonl(ssrc);
597
598 /* Apply the offset and store it back to the packet.
599 * This won't change anything if the offset is 0, so the conditional is
600 * omitted. */
601 seq += state->seq_offset;
602 rtp_hdr->sequence = htons(seq);
603 timestamp += state->timestamp_offset;
604 rtp_hdr->timestamp = htonl(timestamp);
605
606 /* Check again, whether the timestamps are still valid */
607 if (state->out_stream.ssrc == ssrc)
608 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
609 addr, seq, timestamp, "output",
610 &state->out_stream.last_tsdelta);
611
612 /* Save output values */
613 state->out_stream.last_seq = seq;
614 state->out_stream.last_timestamp = timestamp;
615 state->out_stream.ssrc = ssrc;
616
617 if (payload < 0)
618 return;
619
620#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200621 DEBUGP(DRTP,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622 "endpoint:%x payload hdr payload %u -> endp payload %u\n",
623 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624 rtp_hdr->payload_type = payload;
625#endif
626}
627
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628/* Forward data to a debug tap. This is debug function that is intended for
629 * debugging the voice traffic with tools like gstreamer */
630static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
631 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632{
633 if (!tap->enabled)
634 return 0;
635
636 return sendto(fd, buf, len, 0,
637 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
638}
639
Philipp Maier87bd9be2017-08-22 16:35:41 +0200640/*! Send RTP/RTCP data to a specified destination connection.
641 * \param[in] endp associated endpoint (for configuration, logging)
642 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
643 * \param[in] spoofed source address (set to NULL to disable)
644 * \param[in] buf buffer that contains the RTP/RTCP data
645 * \param[in] len length of the buffer that contains the RTP/RTCP data
646 * \param[in] conn_src associated source connection
647 * \param[in] conn_dst associated destination connection
648 * \returns 0 on success, -1 on ERROR */
649int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
650 char *buf, int len, struct mgcp_conn_rtp *conn_src,
651 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200653 /*! When no destination connection is available (e.g. when only one
654 * connection in loopback mode exists), then the source connection
655 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 struct mgcp_trunk_config *tcfg = endp->tcfg;
658 struct mgcp_rtp_end *rtp_end;
659 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200660 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661
Philipp Maier87bd9be2017-08-22 16:35:41 +0200662 OSMO_ASSERT(conn_src);
663 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664
Philipp Maier87bd9be2017-08-22 16:35:41 +0200665 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200666 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200667 "endpoint:%x delivering RTP packet...\n",
668 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200670 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 "endpoint:%x delivering RTCP packet...\n",
672 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200673 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674
Philipp Maierc3413882017-10-27 12:26:54 +0200675 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676 "endpoint:%x loop:%d, mode:%d ",
677 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
678 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
Philipp Maierc3413882017-10-27 12:26:54 +0200679 LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680 else
Philipp Maierc3413882017-10-27 12:26:54 +0200681 LOGPC(DRTP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682
Philipp Maier87bd9be2017-08-22 16:35:41 +0200683 /* Note: In case of loopback configuration, both, the source and the
684 * destination will point to the same connection. */
685 rtp_end = &conn_dst->end;
686 rtp_state = &conn_src->state;
687 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688
689 if (!rtp_end->output_enabled) {
690 rtp_end->dropped_packets += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200691 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200692 "endpoint:%x output disabled, drop to %s %s "
693 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200695 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200698 );
699 } else if (is_rtp) {
700 int cont;
701 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200702 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200703 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200704 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 buf, &buflen,
707 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 if (cont < 0)
709 break;
710
Philipp Maier87bd9be2017-08-22 16:35:41 +0200711 if (addr)
712 mgcp_patch_and_count(endp, rtp_state, rtp_end,
713 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200714 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200715 "endpoint:%x process/send to %s %s "
716 "rtp_port:%u rtcp_port:%u\n",
717 ENDPOINT_NUMBER(endp), dest_name,
718 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
719 ntohs(rtp_end->rtcp_port)
720 );
721
722 /* Forward a copy of the RTP data to a debug ip/port */
723 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
724 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725
726 /* FIXME: HACK HACK HACK. See OS#2459.
727 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
728 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
729 * cells (as long as we patch only the first RTP payload in each stream).
730 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200731 if (!rtp_state->patched_first_rtp_payload) {
732 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200733 data[0] = 0xe4;
734 data[1] = 0x00;
735 rtp_state->patched_first_rtp_payload = true;
736 }
737
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 len = mgcp_udp_send(rtp_end->rtp.fd,
739 &rtp_end->addr,
740 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741
Philipp Maier87bd9be2017-08-22 16:35:41 +0200742 if (len <= 0)
743 return len;
744
745 conn_dst->end.packets_tx += 1;
746 conn_dst->end.octets_tx += len;
747
748 nbytes += len;
749 buflen = cont;
750 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200751 return nbytes;
752 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200753 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200754 "endpoint:%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200758 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759 );
760
Philipp Maier87bd9be2017-08-22 16:35:41 +0200761 len = mgcp_udp_send(rtp_end->rtcp.fd,
762 &rtp_end->addr,
763 rtp_end->rtcp_port, buf, len);
764
765 conn_dst->end.packets_tx += 1;
766 conn_dst->end.octets_tx += len;
767
768 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200769 }
770
771 return 0;
772}
773
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774/* Helper function for mgcp_recv(),
775 Receive one RTP Packet + Originating address from file descriptor */
776static int receive_from(struct mgcp_endpoint *endp, int fd,
777 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778{
779 int rc;
780 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781 struct sockaddr_in addr_sink;
782 char buf_sink[RTP_BUF_SIZE];
783 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 if (!addr)
786 addr = &addr_sink;
787 if (!buf) {
788 tossed = true;
789 buf = buf_sink;
790 bufsize = sizeof(buf_sink);
791 }
792
793 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
794
Philipp Maierc3413882017-10-27 12:26:54 +0200795 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796 "receiving %u bytes length packet from %s:%u ...\n",
797 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
798
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200800 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 "endpoint:%x failed to receive packet, errno: %d/%s\n",
802 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200803 return -1;
804 }
805
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806 if (tossed) {
Philipp Maierc3413882017-10-27 12:26:54 +0200807 LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 ENDPOINT_NUMBER(endp));
809 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810
811 return rc;
812}
813
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814/* Check if the origin (addr) matches the address/port data of the RTP
815 * connections. */
816static int check_rtp_origin(struct mgcp_conn_rtp *conn,
817 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821
Philipp Maier87bd9be2017-08-22 16:35:41 +0200822 /* Note: Check if the inbound RTP data comes from the same host to
823 * which we send our outgoing RTP traffic. */
824 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
825 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200826 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 "endpoint:%x data from wrong address: %s, ",
828 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200829 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 inet_ntoa(conn->end.addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200831 LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200832 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833 return -1;
834 }
835
Philipp Maier87bd9be2017-08-22 16:35:41 +0200836 /* Note: Usually the remote remote port of the data we receive will be
837 * the same as the remote port where we transmit outgoing RTP traffic
838 * to (set by MDCX). We use this to check the origin of the data for
839 * plausibility. */
840 if (conn->end.rtp_port != addr->sin_port &&
841 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200842 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200843 "endpoint:%x data from wrong source port: %d, ",
844 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200845 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846 "expected: %d for RTP or %d for RTCP\n",
847 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200848 LOGP(DRTP, LOGL_ERROR, "endpoint:%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 ENDPOINT_NUMBER(endp));
850 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200851 }
852
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 return 0;
854}
855
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856/* Check the if the destination address configuration of an RTP connection
857 * makes sense */
858static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 struct mgcp_endpoint *endp;
861 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200864 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200865 "endpoint:%x destination IP-address is invalid\n",
866 ENDPOINT_NUMBER(endp));
867 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200868 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200869
870 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200871 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 "endpoint:%x destination rtp port is invalid\n",
873 ENDPOINT_NUMBER(endp));
874 return -1;
875 }
876
877 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878}
879
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880/* Receive RTP data from a specified source connection and dispatch it to a
881 * destination connection. */
882static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
883 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 struct mgcp_conn_rtp *conn;
887 struct mgcp_trunk_config *tcfg;
888 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 conn = (struct mgcp_conn_rtp*) fd->data;
891 endp = conn->conn->endp;
892 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893
Philipp Maierc3413882017-10-27 12:26:54 +0200894 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 ENDPOINT_NUMBER(endp));
896
897 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898 if (rc <= 0)
899 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901
Philipp Maierc3413882017-10-27 12:26:54 +0200902 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x ", ENDPOINT_NUMBER(endp));
903 LOGPC(DRTP, LOGL_DEBUG, "receiveing from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200904 conn->conn->name, inet_ntoa(addr->sin_addr),
905 ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200906 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 /* Check if the origin of the RTP packet seems plausible */
910 if (tcfg->rtp_accept_all == 0) {
911 if (check_rtp_origin(conn, addr) != 0)
912 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913 }
914
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +0200917 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918 "endpoint:%x dummy message received\n",
919 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200920 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921 "endpoint:%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 return 0;
923 }
924
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 /* Increment RX statistics */
926 conn->end.packets_rx += 1;
927 conn->end.octets_rx += rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928
Philipp Maier87bd9be2017-08-22 16:35:41 +0200929 /* Forward a copy of the RTP data to a debug ip/port */
930 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 return rc;
933}
934
935/* Send RTP data. Possible options are standard RTP packet
936 * transmission or trsmission via an osmux connection */
937static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
938 unsigned int buf_size,
939 struct mgcp_conn_rtp *conn_src,
940 struct mgcp_conn_rtp *conn_dst)
941{
942 struct mgcp_endpoint *endp;
943 endp = conn_src->conn->endp;
944
Philipp Maierc3413882017-10-27 12:26:54 +0200945 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200946 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
947
948 /* Before we try to deliver the packet, we check if the destination
949 * port and IP-Address make sense at all. If not, we will be unable
950 * to deliver the packet. */
951 if (check_rtp_destin(conn_dst) != 0)
952 return -1;
953
954 /* Depending on the RTP connection type, deliver the RTP packet to the
955 * destination connection. */
956 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +0200958 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 "endpoint:%x endpoint type is MGCP_RTP_DEFAULT, "
960 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
963 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200964 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +0200966 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 "endpoint:%x endpoint type is MGCP_OSMUX_BSC_NAT, "
968 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
969 ENDPOINT_NUMBER(endp));
970 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200971 }
972
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 /* If the data has not been handled/forwarded until here, it will
974 * be discarded, this should not happen, normally the MGCP type
975 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +0200976 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200977 "endpoint:%x bad MGCP type -- data discarded!\n",
978 ENDPOINT_NUMBER(endp));
979
980 return -1;
981}
982
983/*! dispatch incoming RTP packet to opposite RTP connection.
984 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
985 * \param[in] addr socket address where the RTP packet has been received from
986 * \param[in] buf buffer that hold the RTP payload
987 * \param[in] buf_size size data length of buf
988 * \param[in] conn originating connection
989 * \returns 0 on success, -1 on ERROR */
990int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
991 unsigned int buf_size, struct mgcp_conn *conn)
992{
993 struct mgcp_conn *conn_dst;
994 struct mgcp_endpoint *endp;
995 endp = conn->endp;
996
997 /*! NOTE: This callback function implements the endpoint specific
998 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
999 * that the endpoint will hold only two connections. This premise
1000 * is used to determine the opposite connection (it is always the
1001 * connection that is not the originating connection). Once the
1002 * destination connection is known the RTP packet is sent via
1003 * the destination connection. */
1004
1005 /* Find a destination connection. */
1006 /* NOTE: This code path runs every time an RTP packet is received. The
1007 * function mgcp_find_dst_conn() we use to determine the detination
1008 * connection will iterate the connection list inside the endpoint.
1009 * Since list iterations are quite costly, we will figure out the
1010 * destination only once and use the optional private data pointer of
1011 * the connection to cache the destination connection pointer. */
1012 if (!conn->priv) {
1013 conn_dst = mgcp_find_dst_conn(conn);
1014 conn->priv = conn_dst;
1015 } else {
1016 conn_dst = (struct mgcp_conn *)conn->priv;
1017 }
1018
1019 /* There is no destination conn, stop here */
1020 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001021 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 "endpoint:%x unable to find destination conn\n",
1023 ENDPOINT_NUMBER(endp));
1024 return -1;
1025 }
1026
1027 /* The destination conn is not an RTP connection */
1028 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001029 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030 "endpoint:%x unable to find suitable destination conn\n",
1031 ENDPOINT_NUMBER(endp));
1032 return -1;
1033 }
1034
1035 /* Dispatch RTP packet to destination RTP connection */
1036 return mgcp_send_rtp(proto, addr, buf,
1037 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1038
1039}
1040
1041/* Handle incoming RTP data from NET */
1042static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1043{
1044 /* NOTE: This is a generic implementation. RTP data is received. In
1045 * case of loopback the data is just sent back to its origin. All
1046 * other cases implement endpoint specific behaviour (e.g. how is the
1047 * destination connection determined?). That specific behaviour is
1048 * implemented by the callback function that is called at the end of
1049 * the function */
1050
1051 struct mgcp_conn_rtp *conn_src;
1052 struct mgcp_endpoint *endp;
1053 struct sockaddr_in addr;
1054
1055 char buf[RTP_BUF_SIZE];
1056 int proto;
1057 int rc;
1058
1059 conn_src = (struct mgcp_conn_rtp *)fd->data;
1060 OSMO_ASSERT(conn_src);
1061 endp = conn_src->conn->endp;
1062 OSMO_ASSERT(endp);
1063
Philipp Maierc3413882017-10-27 12:26:54 +02001064 LOGP(DRTP, LOGL_DEBUG, "endpoint:%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1066
1067 /* Receive packet */
1068 rc = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1069 if (rc < 0)
1070 return -1;
1071
1072 /* Check if the connection is in loopback mode, if yes, just send the
1073 * incoming data back to the origin */
1074 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
1075 return mgcp_send_rtp(proto, &addr, buf,
1076 sizeof(buf), conn_src, conn_src);
1077 }
1078
1079 /* Execute endpoint specific implementation that handles the
1080 * dispatching of the RTP data */
1081 return endp->type->dispatch_rtp_cb(proto, &addr, buf, sizeof(buf),
1082 conn_src->conn);
1083}
1084
1085/*! set IP Type of Service parameter.
1086 * \param[in] fd associated file descriptor
1087 * \param[in] tos dscp value
1088 * \returns 0 on success, -1 on ERROR */
1089int mgcp_set_ip_tos(int fd, int tos)
1090{
1091 int ret;
1092 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1093
1094 if (ret < 0)
1095 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 return 0;
1097}
1098
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099/*! bind RTP port to osmo_fd.
1100 * \param[in] source_addr source (local) address to bind on
1101 * \param[in] fd associated file descriptor
1102 * \param[in] port to bind on
1103 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1105{
1106 struct sockaddr_in addr;
1107 int on = 1;
1108
1109 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
1110 if (fd->fd < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001111 LOGP(DRTP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 source_addr, port);
1113 return -1;
1114 } else {
Philipp Maierc3413882017-10-27 12:26:54 +02001115 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116 "created UDP port (%s:%i).\n", source_addr, port);
1117 }
1118
1119 if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001120 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 "failed to set socket options (%s:%i).\n", source_addr,
1122 port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001123 return -1;
1124 }
1125
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 memset(&addr, 0, sizeof(addr));
1127 addr.sin_family = AF_INET;
1128 addr.sin_port = htons(port);
1129 inet_aton(source_addr, &addr.sin_addr);
1130
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131 if (bind(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001132 close(fd->fd);
1133 fd->fd = -1;
Philipp Maierc3413882017-10-27 12:26:54 +02001134 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001135 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001137 } else {
Philipp Maierc3413882017-10-27 12:26:54 +02001138 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001139 "bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140 }
1141
1142 return 0;
1143}
1144
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001148{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1150 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1151
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1153 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001154 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001155 "endpoint:%x failed to create RTP port: %s:%d\n", endpno,
1156 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157 goto cleanup0;
1158 }
1159
1160 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1161 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001162 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001163 "endpoint:%x failed to create RTCP port: %s:%d\n", endpno,
1164 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165 goto cleanup1;
1166 }
1167
Philipp Maier87bd9be2017-08-22 16:35:41 +02001168 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1170 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1171
1172 rtp_end->rtp.when = BSC_FD_READ;
1173 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001174 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175 "endpoint:%x failed to register RTP port %d\n", endpno,
1176 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177 goto cleanup2;
1178 }
1179
1180 rtp_end->rtcp.when = BSC_FD_READ;
1181 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001182 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001183 "endpoint:%x failed to register RTCP port %d\n", endpno,
1184 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 goto cleanup3;
1186 }
1187
1188 return 0;
1189
1190cleanup3:
1191 osmo_fd_unregister(&rtp_end->rtp);
1192cleanup2:
1193 close(rtp_end->rtcp.fd);
1194 rtp_end->rtcp.fd = -1;
1195cleanup1:
1196 close(rtp_end->rtp.fd);
1197 rtp_end->rtp.fd = -1;
1198cleanup0:
1199 return -1;
1200}
1201
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202/*! bind RTP port to endpoint/connection.
1203 * \param[in] endp endpoint that holds the RTP connection
1204 * \param[in] rtp_port port number to bind on
1205 * \param[in] conn associated RTP connection
1206 * \returns 0 on success, -1 on ERROR */
1207int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1208 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001209{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001210 char name[512];
1211 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001212 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001213
1214 snprintf(name, sizeof(name), "%s-%u", conn->conn->name, conn->conn->id);
1215 end = &conn->end;
1216
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001218 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001219 "endpoint:%x %u was already bound on conn:%s\n",
1220 ENDPOINT_NUMBER(endp), rtp_port,
1221 mgcp_conn_dump(conn->conn));
1222
1223 /* Double bindings should never occour! Since we always allocate
1224 * connections dynamically and free them when they are not
1225 * needed anymore, there must be no previous binding leftover.
1226 * Should there be a connection bound twice, we have a serious
1227 * problem and must exit immediately! */
1228 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 }
1230
1231 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 end->rtp.cb = rtp_data_net;
1233 end->rtp.data = conn;
1234 end->rtcp.data = conn;
1235 end->rtcp.cb = rtp_data_net;
1236
Philipp Maier1cb1e382017-11-02 17:16:04 +01001237 mgcp_get_local_addr(local_ip_addr, conn);
1238
1239 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241}
1242
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243/*! free allocated RTP and RTCP ports.
1244 * \param[in] end RTP end */
1245void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246{
1247 if (end->rtp.fd != -1) {
1248 close(end->rtp.fd);
1249 end->rtp.fd = -1;
1250 osmo_fd_unregister(&end->rtp);
1251 }
1252
1253 if (end->rtcp.fd != -1) {
1254 close(end->rtcp.fd);
1255 end->rtcp.fd = -1;
1256 osmo_fd_unregister(&end->rtcp);
1257 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258}