Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 1 | /* RTP proxy handling for ip.access nanoBTS */ |
| 2 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 3 | /* (C) 2009-2013 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 26 | #include <sys/time.h> /* gettimeofday() */ |
| 27 | #include <unistd.h> /* get..() */ |
| 28 | #include <time.h> /* clock() */ |
| 29 | #include <sys/utsname.h> /* uname() */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 32 | #include <openbsc/gsm_data.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/select.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 36 | #include <openbsc/rtp_proxy.h> |
Harald Welte | f142c97 | 2011-05-24 13:25:38 +0200 | [diff] [blame] | 37 | #include <openbsc/mncc.h> |
Alexander Huemer | 0a65461 | 2011-09-06 00:09:49 +0200 | [diff] [blame] | 38 | #include <openbsc/trau_upqueue.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 40 | /* attempt to determine byte order */ |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 41 | #include <sys/param.h> |
| 42 | #include <limits.h> |
| 43 | |
| 44 | #ifndef __BYTE_ORDER |
Tobias Engel | aff2071 | 2012-10-24 17:53:50 +0200 | [diff] [blame] | 45 | # ifdef __APPLE__ |
| 46 | # define __BYTE_ORDER __DARWIN_BYTE_ORDER |
| 47 | # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN |
| 48 | # define __BIG_ENDIAN __DARWIN_BIG_ENDIAN |
| 49 | # else |
| 50 | # error "__BYTE_ORDER should be defined by someone" |
| 51 | # endif |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 54 | static LLIST_HEAD(rtp_sockets); |
| 55 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 56 | /* should we mangle the CNAME inside SDES of RTCP packets? We disable |
| 57 | * this by default, as it seems to be not needed */ |
| 58 | static int mangle_rtcp_cname = 0; |
| 59 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 60 | enum rtp_bfd_priv { |
| 61 | RTP_PRIV_NONE, |
| 62 | RTP_PRIV_RTP, |
| 63 | RTP_PRIV_RTCP |
| 64 | }; |
| 65 | |
| 66 | #define RTP_ALLOC_SIZE 1500 |
| 67 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 68 | /* according to RFC 1889 */ |
| 69 | struct rtcp_hdr { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 70 | uint8_t byte0; |
| 71 | uint8_t type; |
| 72 | uint16_t length; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 73 | } __attribute__((packed)); |
| 74 | |
| 75 | #define RTCP_TYPE_SDES 202 |
| 76 | |
| 77 | #define RTCP_IE_CNAME 1 |
| 78 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 79 | /* according to RFC 3550 */ |
| 80 | struct rtp_hdr { |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 81 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 82 | uint8_t csrc_count:4, |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 83 | extension:1, |
| 84 | padding:1, |
| 85 | version:2; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 86 | uint8_t payload_type:7, |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 87 | marker:1; |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 88 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 89 | uint8_t version:2, |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 90 | padding:1, |
| 91 | extension:1, |
| 92 | csrc_count:4; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 93 | uint8_t marker:1, |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 94 | payload_type:7; |
| 95 | #endif |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 96 | uint16_t sequence; |
| 97 | uint32_t timestamp; |
| 98 | uint32_t ssrc; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 99 | } __attribute__((packed)); |
| 100 | |
| 101 | struct rtp_x_hdr { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 102 | uint16_t by_profile; |
| 103 | uint16_t length; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 104 | } __attribute__((packed)); |
| 105 | |
| 106 | #define RTP_VERSION 2 |
| 107 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 108 | /* decode an rtp frame and create a new buffer with payload */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 109 | static int rtp_decode(struct msgb *msg, uint32_t callref, struct msgb **data) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 110 | { |
| 111 | struct msgb *new_msg; |
| 112 | struct gsm_data_frame *frame; |
| 113 | struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data; |
| 114 | struct rtp_x_hdr *rtpxh; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 115 | uint8_t *payload; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 116 | int payload_len; |
| 117 | int msg_type; |
| 118 | int x_len; |
| 119 | |
| 120 | if (msg->len < 12) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 121 | DEBUGPC(DLMUX, "received RTP frame too short (len = %d)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 122 | msg->len); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | if (rtph->version != RTP_VERSION) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 126 | DEBUGPC(DLMUX, "received RTP version %d not supported.\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 127 | rtph->version); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | payload = msg->data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2); |
| 131 | payload_len = msg->len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2); |
| 132 | if (payload_len < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 133 | DEBUGPC(DLMUX, "received RTP frame too short (len = %d, " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 134 | "csrc count = %d)\n", msg->len, rtph->csrc_count); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | if (rtph->extension) { |
| 138 | if (payload_len < sizeof(struct rtp_x_hdr)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 139 | DEBUGPC(DLMUX, "received RTP frame too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 140 | "extension header\n"); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | rtpxh = (struct rtp_x_hdr *)payload; |
| 144 | x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr); |
| 145 | payload += x_len; |
| 146 | payload_len -= x_len; |
| 147 | if (payload_len < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 148 | DEBUGPC(DLMUX, "received RTP frame too short, " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 149 | "extension header exceeds frame length\n"); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | } |
| 153 | if (rtph->padding) { |
| 154 | if (payload_len < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 155 | DEBUGPC(DLMUX, "received RTP frame too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 156 | "padding length\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | payload_len -= payload[payload_len - 1]; |
| 160 | if (payload_len < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 161 | DEBUGPC(DLMUX, "received RTP frame with padding " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 162 | "greater than payload\n"); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | switch (rtph->payload_type) { |
| 168 | case RTP_PT_GSM_FULL: |
| 169 | msg_type = GSM_TCHF_FRAME; |
| 170 | if (payload_len != 33) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 171 | DEBUGPC(DLMUX, "received RTP full rate frame with " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 172 | "payload length != 32 (len = %d)\n", |
| 173 | payload_len); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 177 | case RTP_PT_GSM_EFR: |
| 178 | msg_type = GSM_TCHF_FRAME_EFR; |
| 179 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 180 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 181 | DEBUGPC(DLMUX, "received RTP frame with unknown payload " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 182 | "type %d\n", rtph->payload_type); |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | new_msg = msgb_alloc(sizeof(struct gsm_data_frame) + payload_len, |
| 187 | "GSM-DATA"); |
| 188 | if (!new_msg) |
| 189 | return -ENOMEM; |
| 190 | frame = (struct gsm_data_frame *)(new_msg->data); |
| 191 | frame->msg_type = msg_type; |
| 192 | frame->callref = callref; |
| 193 | memcpy(frame->data, payload, payload_len); |
| 194 | msgb_put(new_msg, sizeof(struct gsm_data_frame) + payload_len); |
| 195 | |
| 196 | *data = new_msg; |
| 197 | return 0; |
| 198 | } |
| 199 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 200 | /* "to - from" */ |
| 201 | static void tv_difference(struct timeval *diff, const struct timeval *from, |
| 202 | const struct timeval *__to) |
| 203 | { |
| 204 | struct timeval _to = *__to, *to = &_to; |
| 205 | |
| 206 | if (to->tv_usec < from->tv_usec) { |
| 207 | to->tv_sec -= 1; |
| 208 | to->tv_usec += 1000000; |
| 209 | } |
| 210 | |
| 211 | diff->tv_usec = to->tv_usec - from->tv_usec; |
| 212 | diff->tv_sec = to->tv_sec - from->tv_sec; |
| 213 | } |
| 214 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 215 | /*! \brief encode and send a rtp frame |
| 216 | * \param[in] rs RTP socket through which we shall send |
| 217 | * \param[in] frame GSM RTP frame to be sent |
| 218 | */ |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 219 | int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame) |
| 220 | { |
| 221 | struct rtp_sub_socket *rss = &rs->rtp; |
| 222 | struct msgb *msg; |
| 223 | struct rtp_hdr *rtph; |
| 224 | int payload_type; |
| 225 | int payload_len; |
| 226 | int duration; /* in samples */ |
| 227 | |
| 228 | if (rs->tx_action != RTP_SEND_DOWNSTREAM) { |
| 229 | /* initialize sequences */ |
| 230 | rs->tx_action = RTP_SEND_DOWNSTREAM; |
| 231 | rs->transmit.ssrc = rand(); |
| 232 | rs->transmit.sequence = random(); |
| 233 | rs->transmit.timestamp = random(); |
| 234 | } |
| 235 | |
| 236 | switch (frame->msg_type) { |
| 237 | case GSM_TCHF_FRAME: |
| 238 | payload_type = RTP_PT_GSM_FULL; |
| 239 | payload_len = 33; |
| 240 | duration = 160; |
| 241 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 242 | case GSM_TCHF_FRAME_EFR: |
| 243 | payload_type = RTP_PT_GSM_EFR; |
| 244 | payload_len = 31; |
| 245 | duration = 160; |
| 246 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 247 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 248 | DEBUGPC(DLMUX, "unsupported message type %d\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 249 | frame->msg_type); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 253 | { |
| 254 | struct timeval tv, tv_diff; |
| 255 | long int usec_diff, frame_diff; |
| 256 | |
| 257 | gettimeofday(&tv, NULL); |
| 258 | tv_difference(&tv_diff, &rs->transmit.last_tv, &tv); |
| 259 | rs->transmit.last_tv = tv; |
| 260 | |
| 261 | usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec; |
| 262 | frame_diff = (usec_diff / 20000); |
| 263 | |
| 264 | if (abs(frame_diff) > 1) { |
| 265 | long int frame_diff_excess = frame_diff - 1; |
| 266 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 267 | LOGP(DLMUX, LOGL_NOTICE, |
Harald Welte (local) | 9fcf6d7 | 2009-12-27 17:01:40 +0100 | [diff] [blame] | 268 | "Correcting frame difference of %ld frames\n", frame_diff_excess); |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 269 | rs->transmit.sequence += frame_diff_excess; |
| 270 | rs->transmit.timestamp += frame_diff_excess * duration; |
| 271 | } |
| 272 | } |
| 273 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 274 | msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL"); |
| 275 | if (!msg) |
| 276 | return -ENOMEM; |
| 277 | rtph = (struct rtp_hdr *)msg->data; |
| 278 | rtph->version = RTP_VERSION; |
| 279 | rtph->padding = 0; |
| 280 | rtph->extension = 0; |
| 281 | rtph->csrc_count = 0; |
| 282 | rtph->marker = 0; |
| 283 | rtph->payload_type = payload_type; |
| 284 | rtph->sequence = htons(rs->transmit.sequence++); |
| 285 | rtph->timestamp = htonl(rs->transmit.timestamp); |
| 286 | rs->transmit.timestamp += duration; |
| 287 | rtph->ssrc = htonl(rs->transmit.ssrc); |
| 288 | memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len); |
| 289 | msgb_put(msg, sizeof(struct rtp_hdr) + payload_len); |
| 290 | msgb_enqueue(&rss->tx_queue, msg); |
| 291 | rss->bfd.when |= BSC_FD_WRITE; |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Holger Hans Peter Freyther | 89acf06 | 2009-07-29 06:46:26 +0200 | [diff] [blame] | 296 | /* iterate over all chunks in one RTCP message, look for CNAME IEs and |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 297 | * replace all of those with 'new_cname' */ |
| 298 | static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 299 | uint16_t *rtcp_len, const char *new_cname) |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 300 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 301 | uint8_t *rtcp_end; |
| 302 | uint8_t *cur = (uint8_t *) rh; |
| 303 | uint8_t tag, len = 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 304 | |
| 305 | rtcp_end = cur + *rtcp_len; |
| 306 | /* move cur to end of RTP header */ |
Harald Welte | 198f3f5 | 2009-07-29 10:46:41 +0200 | [diff] [blame] | 307 | cur += sizeof(*rh); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 308 | |
| 309 | /* iterate over Chunks */ |
| 310 | while (cur+4 < rtcp_end) { |
| 311 | /* skip four bytes SSRC/CSRC */ |
| 312 | cur += 4; |
| 313 | |
| 314 | /* iterate over IE's inside the chunk */ |
| 315 | while (cur+1 < rtcp_end) { |
| 316 | tag = *cur++; |
| 317 | if (tag == 0) { |
| 318 | /* end of chunk, skip additional zero */ |
| 319 | while (*cur++ == 0) { } |
| 320 | break; |
| 321 | } |
| 322 | len = *cur++; |
| 323 | |
| 324 | if (tag == RTCP_IE_CNAME) { |
| 325 | /* we've found the CNAME, lets mangle it */ |
| 326 | if (len < strlen(new_cname)) { |
| 327 | /* we need to make more space */ |
| 328 | int increase = strlen(new_cname) - len; |
| 329 | |
| 330 | msgb_push(msg, increase); |
| 331 | memmove(cur+len+increase, cur+len, |
| 332 | rtcp_end - (cur+len)); |
| 333 | /* FIXME: we have to respect RTCP |
| 334 | * padding/alignment rules! */ |
| 335 | len += increase; |
| 336 | *(cur-1) += increase; |
| 337 | rtcp_end += increase; |
| 338 | *rtcp_len += increase; |
| 339 | } |
| 340 | /* copy new CNAME into message */ |
| 341 | memcpy(cur, new_cname, strlen(new_cname)); |
| 342 | /* FIXME: zero the padding in case new CNAME |
| 343 | * is smaller than old one !!! */ |
| 344 | } |
| 345 | cur += len; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs) |
| 353 | { |
| 354 | struct rtp_sub_socket *rss = &rs->rtcp; |
| 355 | struct rtcp_hdr *rtph; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 356 | uint16_t old_len; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 357 | int rc; |
| 358 | |
| 359 | if (!mangle_rtcp_cname) |
| 360 | return 0; |
| 361 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 362 | printf("RTCP\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 363 | /* iterate over list of RTCP messages */ |
| 364 | rtph = (struct rtcp_hdr *)msg->data; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 365 | while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 366 | old_len = (ntohs(rtph->length) + 1) * 4; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 367 | if ((void *)rtph + old_len > (void *)msg->data + msg->len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 368 | DEBUGPC(DLMUX, "received RTCP packet too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 369 | "length element\n"); |
| 370 | return -EINVAL; |
| 371 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 372 | if (rtph->type == RTCP_TYPE_SDES) { |
| 373 | char new_cname[255]; |
| 374 | strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr), |
| 375 | sizeof(new_cname)); |
| 376 | new_cname[sizeof(new_cname)-1] = '\0'; |
| 377 | rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len, |
| 378 | new_cname); |
| 379 | if (rc < 0) |
| 380 | return rc; |
| 381 | } |
| 382 | rtph = (void *)rtph + old_len; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 388 | /* read from incoming RTP/RTCP socket */ |
| 389 | static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 390 | { |
| 391 | int rc; |
| 392 | struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP"); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 393 | struct msgb *new_msg; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 394 | struct rtp_sub_socket *other_rss; |
| 395 | |
| 396 | if (!msg) |
| 397 | return -ENOMEM; |
| 398 | |
| 399 | rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE); |
| 400 | if (rc <= 0) { |
| 401 | rss->bfd.when &= ~BSC_FD_READ; |
| 402 | return rc; |
| 403 | } |
| 404 | |
| 405 | msgb_put(msg, rc); |
| 406 | |
| 407 | switch (rs->rx_action) { |
| 408 | case RTP_PROXY: |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 409 | if (!rs->proxy.other_sock) { |
| 410 | rc = -EIO; |
| 411 | goto out_free; |
| 412 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 413 | if (rss->bfd.priv_nr == RTP_PRIV_RTP) |
| 414 | other_rss = &rs->proxy.other_sock->rtp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 415 | else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 416 | other_rss = &rs->proxy.other_sock->rtcp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 417 | /* modify RTCP SDES CNAME */ |
| 418 | rc = rtcp_mangle(msg, rs); |
| 419 | if (rc < 0) |
| 420 | goto out_free; |
| 421 | } else { |
| 422 | rc = -EINVAL; |
| 423 | goto out_free; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 424 | } |
| 425 | msgb_enqueue(&other_rss->tx_queue, msg); |
| 426 | other_rss->bfd.when |= BSC_FD_WRITE; |
| 427 | break; |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 428 | |
| 429 | case RTP_RECV_UPSTREAM: |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 430 | if (!rs->receive.callref || !rs->receive.net) { |
| 431 | rc = -EIO; |
| 432 | goto out_free; |
| 433 | } |
| 434 | if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
| 435 | if (!mangle_rtcp_cname) { |
| 436 | msgb_free(msg); |
| 437 | break; |
| 438 | } |
| 439 | /* modify RTCP SDES CNAME */ |
| 440 | rc = rtcp_mangle(msg, rs); |
| 441 | if (rc < 0) |
| 442 | goto out_free; |
| 443 | msgb_enqueue(&rss->tx_queue, msg); |
| 444 | rss->bfd.when |= BSC_FD_WRITE; |
| 445 | break; |
| 446 | } |
| 447 | if (rss->bfd.priv_nr != RTP_PRIV_RTP) { |
| 448 | rc = -EINVAL; |
| 449 | goto out_free; |
| 450 | } |
| 451 | rc = rtp_decode(msg, rs->receive.callref, &new_msg); |
| 452 | if (rc < 0) |
| 453 | goto out_free; |
| 454 | msgb_free(msg); |
Harald Welte | 31c00f7 | 2011-03-03 23:29:05 +0100 | [diff] [blame] | 455 | trau_tx_to_mncc(rs->receive.net, new_msg); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 456 | break; |
| 457 | |
| 458 | case RTP_NONE: /* if socket exists, but disabled by app */ |
| 459 | msgb_free(msg); |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 460 | break; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 463 | return 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 464 | |
| 465 | out_free: |
| 466 | msgb_free(msg); |
| 467 | return rc; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 470 | /* \brief write from tx_queue to RTP/RTCP socket */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 471 | static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 472 | { |
| 473 | struct msgb *msg; |
| 474 | int written; |
| 475 | |
| 476 | msg = msgb_dequeue(&rss->tx_queue); |
| 477 | if (!msg) { |
| 478 | rss->bfd.when &= ~BSC_FD_WRITE; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | written = write(rss->bfd.fd, msg->data, msg->len); |
| 483 | if (written < msg->len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 484 | LOGP(DLMIB, LOGL_ERROR, "short write"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 485 | msgb_free(msg); |
| 486 | return -EIO; |
| 487 | } |
| 488 | |
| 489 | msgb_free(msg); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 495 | /*! \brief callback for the select.c:bfd_* layer */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 496 | static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 497 | { |
| 498 | struct rtp_socket *rs = bfd->data; |
| 499 | struct rtp_sub_socket *rss; |
| 500 | |
| 501 | switch (bfd->priv_nr) { |
| 502 | case RTP_PRIV_RTP: |
| 503 | rss = &rs->rtp; |
| 504 | break; |
| 505 | case RTP_PRIV_RTCP: |
| 506 | rss = &rs->rtcp; |
| 507 | break; |
| 508 | default: |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | if (flags & BSC_FD_READ) |
| 513 | rtp_socket_read(rs, rss); |
| 514 | |
| 515 | if (flags & BSC_FD_WRITE) |
| 516 | rtp_socket_write(rs, rss); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 521 | /*! \brief initialize one rtp sub-socket */ |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 522 | static void init_rss(struct rtp_sub_socket *rss, |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 523 | struct rtp_socket *rs, int fd, int priv_nr) |
| 524 | { |
| 525 | /* initialize bfd */ |
| 526 | rss->bfd.fd = fd; |
| 527 | rss->bfd.data = rs; |
| 528 | rss->bfd.priv_nr = priv_nr; |
| 529 | rss->bfd.cb = rtp_bfd_cb; |
| 530 | } |
| 531 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 532 | /*! \brief create a new RTP/RTCP socket and bind it */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 533 | struct rtp_socket *rtp_socket_create(void) |
| 534 | { |
| 535 | int rc; |
| 536 | struct rtp_socket *rs; |
| 537 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 538 | DEBUGP(DLMUX, "rtp_socket_create(): "); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 539 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 540 | rs = talloc_zero(tall_bsc_ctx, struct rtp_socket); |
| 541 | if (!rs) |
| 542 | return NULL; |
| 543 | |
| 544 | INIT_LLIST_HEAD(&rs->rtp.tx_queue); |
| 545 | INIT_LLIST_HEAD(&rs->rtcp.tx_queue); |
| 546 | |
| 547 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 548 | if (rc < 0) |
| 549 | goto out_free; |
| 550 | |
| 551 | init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 552 | rc = osmo_fd_register(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 553 | if (rc < 0) |
| 554 | goto out_rtp_socket; |
| 555 | |
| 556 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 557 | if (rc < 0) |
| 558 | goto out_rtp_bfd; |
| 559 | |
| 560 | init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 561 | rc = osmo_fd_register(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 562 | if (rc < 0) |
| 563 | goto out_rtcp_socket; |
| 564 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 565 | DEBUGPC(DLMUX, "success\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 566 | |
| 567 | rc = rtp_socket_bind(rs, INADDR_ANY); |
| 568 | if (rc < 0) |
| 569 | goto out_rtcp_bfd; |
| 570 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 571 | return rs; |
| 572 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 573 | out_rtcp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 574 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 575 | out_rtcp_socket: |
| 576 | close(rs->rtcp.bfd.fd); |
| 577 | out_rtp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 578 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 579 | out_rtp_socket: |
| 580 | close(rs->rtp.bfd.fd); |
| 581 | out_free: |
| 582 | talloc_free(rs); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 583 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
| 586 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 587 | static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip, |
| 588 | uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 589 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 590 | int rc; |
| 591 | socklen_t alen = sizeof(rss->sin_local); |
| 592 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 593 | rss->sin_local.sin_family = AF_INET; |
| 594 | rss->sin_local.sin_addr.s_addr = htonl(ip); |
| 595 | rss->sin_local.sin_port = htons(port); |
| 596 | rss->bfd.when |= BSC_FD_READ; |
| 597 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 598 | rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 599 | sizeof(rss->sin_local)); |
| 600 | if (rc < 0) |
| 601 | return rc; |
| 602 | |
| 603 | /* retrieve the address we actually bound to, in case we |
| 604 | * passed INADDR_ANY as IP address */ |
| 605 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 606 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | #define RTP_PORT_BASE 30000 |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 610 | static unsigned int next_udp_port = RTP_PORT_BASE; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 611 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 612 | /*! \brief bind a RTP socket to a specific local address |
| 613 | * \param[in] rs RTP socket to be bound |
| 614 | * \param[in] ip local IP address to which socket is to be bound |
| 615 | */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 616 | int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 617 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 618 | int rc = -EIO; |
| 619 | struct in_addr ia; |
| 620 | |
| 621 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 622 | DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs, |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 623 | inet_ntoa(ia)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 624 | |
| 625 | /* try to bind to a consecutive pair of ports */ |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 626 | for (next_udp_port = next_udp_port % 0xffff; |
| 627 | next_udp_port < 0xffff; next_udp_port += 2) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 628 | rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port); |
| 629 | if (rc != 0) |
| 630 | continue; |
| 631 | |
| 632 | rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1); |
| 633 | if (rc == 0) |
| 634 | break; |
| 635 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 636 | if (rc < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 637 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 638 | return rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 639 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 640 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 641 | ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 642 | DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 643 | inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 644 | return ntohs(rs->rtp.sin_local.sin_port); |
| 645 | } |
| 646 | |
| 647 | static int rtp_sub_socket_connect(struct rtp_sub_socket *rss, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 648 | uint32_t ip, uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 649 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 650 | int rc; |
| 651 | socklen_t alen = sizeof(rss->sin_local); |
| 652 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 653 | rss->sin_remote.sin_family = AF_INET; |
| 654 | rss->sin_remote.sin_addr.s_addr = htonl(ip); |
| 655 | rss->sin_remote.sin_port = htons(port); |
| 656 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 657 | rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote, |
| 658 | sizeof(rss->sin_remote)); |
| 659 | if (rc < 0) |
| 660 | return rc; |
| 661 | |
| 662 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 663 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 666 | /*! \brief 'connect' a RTP socket to a remote peer |
| 667 | * \param[in] rs RTP socket to be connected |
| 668 | * \param[in] ip remote IP address to which to connect |
| 669 | * \param[in] port remote UDP port number to which to connect |
| 670 | */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 671 | int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 672 | { |
| 673 | int rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 674 | struct in_addr ia; |
| 675 | |
| 676 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 677 | DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 678 | rs, inet_ntoa(ia), port); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 679 | |
| 680 | rc = rtp_sub_socket_connect(&rs->rtp, ip, port); |
| 681 | if (rc < 0) |
| 682 | return rc; |
| 683 | |
| 684 | return rtp_sub_socket_connect(&rs->rtcp, ip, port+1); |
| 685 | } |
| 686 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 687 | /*! \brief bind two RTP/RTCP sockets together in the proxy |
| 688 | * \param[in] this First RTP socket |
| 689 | * \param[in] other Second RTP socket |
| 690 | */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 691 | int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other) |
| 692 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 693 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 694 | this, other); |
| 695 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 696 | this->rx_action = RTP_PROXY; |
| 697 | this->proxy.other_sock = other; |
| 698 | |
| 699 | other->rx_action = RTP_PROXY; |
| 700 | other->proxy.other_sock = this; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 705 | /*! \brief bind RTP/RTCP socket to application, disabling proxy |
| 706 | * \param[in] this RTP socket |
| 707 | * \param[in] net gsm_network argument to trau_tx_to_mncc() |
| 708 | * \param[in] callref callref argument to trau_tx_to_mncc() |
| 709 | */ |
Harald Welte | 9fb1f10 | 2009-12-20 17:07:23 +0100 | [diff] [blame] | 710 | int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 711 | uint32_t callref) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 712 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 713 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 714 | this, callref); |
| 715 | |
| 716 | if (callref) { |
| 717 | this->rx_action = RTP_RECV_UPSTREAM; |
| 718 | this->receive.net = net; |
| 719 | this->receive.callref = callref; |
| 720 | } else |
| 721 | this->rx_action = RTP_NONE; |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 726 | static void free_tx_queue(struct rtp_sub_socket *rss) |
| 727 | { |
| 728 | struct msgb *msg; |
| 729 | |
| 730 | while ((msg = msgb_dequeue(&rss->tx_queue))) |
| 731 | msgb_free(msg); |
| 732 | } |
| 733 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 734 | /*! \brief Free/release a previously allocated RTP socket |
| 735 | * \param[in[] rs RTP/RTCP socket to be released |
| 736 | */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 737 | int rtp_socket_free(struct rtp_socket *rs) |
| 738 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 739 | DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 740 | |
| 741 | /* make sure we don't leave references dangling to us */ |
| 742 | if (rs->rx_action == RTP_PROXY && |
| 743 | rs->proxy.other_sock) |
| 744 | rs->proxy.other_sock->proxy.other_sock = NULL; |
| 745 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 746 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 747 | close(rs->rtp.bfd.fd); |
| 748 | free_tx_queue(&rs->rtp); |
| 749 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 750 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 751 | close(rs->rtcp.bfd.fd); |
| 752 | free_tx_queue(&rs->rtcp); |
| 753 | |
| 754 | talloc_free(rs); |
| 755 | |
| 756 | return 0; |
| 757 | } |