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) { |
Jacob Erlbeck | d9e4039 | 2013-11-21 19:05:42 +0100 | [diff] [blame] | 154 | if (payload_len < 1) { |
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; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 170 | if (payload_len != RTP_LEN_GSM_FULL) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 171 | DEBUGPC(DLMUX, "received RTP full rate frame with " |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 172 | "payload length != %d (len = %d)\n", |
| 173 | RTP_LEN_GSM_FULL, payload_len); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 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; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 179 | if (payload_len != RTP_LEN_GSM_EFR) { |
| 180 | DEBUGPC(DLMUX, "received RTP extended full rate frame " |
| 181 | "with payload length != %d (len = %d)\n", |
| 182 | RTP_LEN_GSM_EFR, payload_len); |
| 183 | return -EINVAL; |
| 184 | } |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 185 | break; |
Andreas Eversberg | 63bfdd8 | 2014-01-17 19:06:38 +0100 | [diff] [blame] | 186 | case RTP_PT_GSM_HALF: |
| 187 | msg_type = GSM_TCHH_FRAME; |
| 188 | if (payload_len != RTP_LEN_GSM_HALF) { |
| 189 | DEBUGPC(DLMUX, "received RTP half rate frame with " |
| 190 | "payload length != %d (len = %d)\n", |
| 191 | RTP_LEN_GSM_HALF, payload_len); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 195 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 196 | DEBUGPC(DLMUX, "received RTP frame with unknown payload " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 197 | "type %d\n", rtph->payload_type); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | new_msg = msgb_alloc(sizeof(struct gsm_data_frame) + payload_len, |
| 202 | "GSM-DATA"); |
| 203 | if (!new_msg) |
| 204 | return -ENOMEM; |
| 205 | frame = (struct gsm_data_frame *)(new_msg->data); |
| 206 | frame->msg_type = msg_type; |
| 207 | frame->callref = callref; |
| 208 | memcpy(frame->data, payload, payload_len); |
| 209 | msgb_put(new_msg, sizeof(struct gsm_data_frame) + payload_len); |
| 210 | |
| 211 | *data = new_msg; |
| 212 | return 0; |
| 213 | } |
| 214 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 215 | /* "to - from" */ |
| 216 | static void tv_difference(struct timeval *diff, const struct timeval *from, |
| 217 | const struct timeval *__to) |
| 218 | { |
| 219 | struct timeval _to = *__to, *to = &_to; |
| 220 | |
| 221 | if (to->tv_usec < from->tv_usec) { |
| 222 | to->tv_sec -= 1; |
| 223 | to->tv_usec += 1000000; |
| 224 | } |
| 225 | |
| 226 | diff->tv_usec = to->tv_usec - from->tv_usec; |
| 227 | diff->tv_sec = to->tv_sec - from->tv_sec; |
| 228 | } |
| 229 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 230 | /*! \brief encode and send a rtp frame |
| 231 | * \param[in] rs RTP socket through which we shall send |
| 232 | * \param[in] frame GSM RTP frame to be sent |
| 233 | */ |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 234 | int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame) |
| 235 | { |
| 236 | struct rtp_sub_socket *rss = &rs->rtp; |
| 237 | struct msgb *msg; |
| 238 | struct rtp_hdr *rtph; |
| 239 | int payload_type; |
| 240 | int payload_len; |
| 241 | int duration; /* in samples */ |
| 242 | |
| 243 | if (rs->tx_action != RTP_SEND_DOWNSTREAM) { |
| 244 | /* initialize sequences */ |
| 245 | rs->tx_action = RTP_SEND_DOWNSTREAM; |
| 246 | rs->transmit.ssrc = rand(); |
| 247 | rs->transmit.sequence = random(); |
| 248 | rs->transmit.timestamp = random(); |
| 249 | } |
| 250 | |
| 251 | switch (frame->msg_type) { |
| 252 | case GSM_TCHF_FRAME: |
| 253 | payload_type = RTP_PT_GSM_FULL; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 254 | payload_len = RTP_LEN_GSM_FULL; |
| 255 | duration = RTP_GSM_DURATION; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 256 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 257 | case GSM_TCHF_FRAME_EFR: |
| 258 | payload_type = RTP_PT_GSM_EFR; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 259 | payload_len = RTP_LEN_GSM_EFR; |
| 260 | duration = RTP_GSM_DURATION; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 261 | break; |
Andreas Eversberg | 63bfdd8 | 2014-01-17 19:06:38 +0100 | [diff] [blame] | 262 | case GSM_TCHH_FRAME: |
| 263 | payload_type = RTP_PT_GSM_HALF; |
| 264 | payload_len = RTP_LEN_GSM_HALF; |
| 265 | duration = RTP_GSM_DURATION; |
| 266 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 267 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 268 | DEBUGPC(DLMUX, "unsupported message type %d\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 269 | frame->msg_type); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 273 | { |
| 274 | struct timeval tv, tv_diff; |
| 275 | long int usec_diff, frame_diff; |
| 276 | |
| 277 | gettimeofday(&tv, NULL); |
| 278 | tv_difference(&tv_diff, &rs->transmit.last_tv, &tv); |
| 279 | rs->transmit.last_tv = tv; |
| 280 | |
| 281 | usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec; |
| 282 | frame_diff = (usec_diff / 20000); |
| 283 | |
| 284 | if (abs(frame_diff) > 1) { |
| 285 | long int frame_diff_excess = frame_diff - 1; |
| 286 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 287 | LOGP(DLMUX, LOGL_NOTICE, |
Harald Welte (local) | 9fcf6d7 | 2009-12-27 17:01:40 +0100 | [diff] [blame] | 288 | "Correcting frame difference of %ld frames\n", frame_diff_excess); |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 289 | rs->transmit.sequence += frame_diff_excess; |
| 290 | rs->transmit.timestamp += frame_diff_excess * duration; |
| 291 | } |
| 292 | } |
| 293 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 294 | msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL"); |
| 295 | if (!msg) |
| 296 | return -ENOMEM; |
| 297 | rtph = (struct rtp_hdr *)msg->data; |
| 298 | rtph->version = RTP_VERSION; |
| 299 | rtph->padding = 0; |
| 300 | rtph->extension = 0; |
| 301 | rtph->csrc_count = 0; |
| 302 | rtph->marker = 0; |
| 303 | rtph->payload_type = payload_type; |
| 304 | rtph->sequence = htons(rs->transmit.sequence++); |
| 305 | rtph->timestamp = htonl(rs->transmit.timestamp); |
| 306 | rs->transmit.timestamp += duration; |
| 307 | rtph->ssrc = htonl(rs->transmit.ssrc); |
| 308 | memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len); |
| 309 | msgb_put(msg, sizeof(struct rtp_hdr) + payload_len); |
| 310 | msgb_enqueue(&rss->tx_queue, msg); |
| 311 | rss->bfd.when |= BSC_FD_WRITE; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Holger Hans Peter Freyther | 89acf06 | 2009-07-29 06:46:26 +0200 | [diff] [blame] | 316 | /* 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] | 317 | * replace all of those with 'new_cname' */ |
| 318 | 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] | 319 | uint16_t *rtcp_len, const char *new_cname) |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 320 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 321 | uint8_t *rtcp_end; |
| 322 | uint8_t *cur = (uint8_t *) rh; |
| 323 | uint8_t tag, len = 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 324 | |
| 325 | rtcp_end = cur + *rtcp_len; |
| 326 | /* move cur to end of RTP header */ |
Harald Welte | 198f3f5 | 2009-07-29 10:46:41 +0200 | [diff] [blame] | 327 | cur += sizeof(*rh); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 328 | |
| 329 | /* iterate over Chunks */ |
| 330 | while (cur+4 < rtcp_end) { |
| 331 | /* skip four bytes SSRC/CSRC */ |
| 332 | cur += 4; |
| 333 | |
| 334 | /* iterate over IE's inside the chunk */ |
| 335 | while (cur+1 < rtcp_end) { |
| 336 | tag = *cur++; |
| 337 | if (tag == 0) { |
| 338 | /* end of chunk, skip additional zero */ |
| 339 | while (*cur++ == 0) { } |
| 340 | break; |
| 341 | } |
| 342 | len = *cur++; |
| 343 | |
| 344 | if (tag == RTCP_IE_CNAME) { |
| 345 | /* we've found the CNAME, lets mangle it */ |
| 346 | if (len < strlen(new_cname)) { |
| 347 | /* we need to make more space */ |
| 348 | int increase = strlen(new_cname) - len; |
| 349 | |
| 350 | msgb_push(msg, increase); |
| 351 | memmove(cur+len+increase, cur+len, |
| 352 | rtcp_end - (cur+len)); |
| 353 | /* FIXME: we have to respect RTCP |
| 354 | * padding/alignment rules! */ |
| 355 | len += increase; |
| 356 | *(cur-1) += increase; |
| 357 | rtcp_end += increase; |
| 358 | *rtcp_len += increase; |
| 359 | } |
| 360 | /* copy new CNAME into message */ |
| 361 | memcpy(cur, new_cname, strlen(new_cname)); |
| 362 | /* FIXME: zero the padding in case new CNAME |
| 363 | * is smaller than old one !!! */ |
| 364 | } |
| 365 | cur += len; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs) |
| 373 | { |
| 374 | struct rtp_sub_socket *rss = &rs->rtcp; |
| 375 | struct rtcp_hdr *rtph; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 376 | uint16_t old_len; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 377 | int rc; |
| 378 | |
| 379 | if (!mangle_rtcp_cname) |
| 380 | return 0; |
| 381 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 382 | printf("RTCP\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 383 | /* iterate over list of RTCP messages */ |
| 384 | rtph = (struct rtcp_hdr *)msg->data; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 385 | while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 386 | old_len = (ntohs(rtph->length) + 1) * 4; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 387 | if ((void *)rtph + old_len > (void *)msg->data + msg->len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 388 | DEBUGPC(DLMUX, "received RTCP packet too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 389 | "length element\n"); |
| 390 | return -EINVAL; |
| 391 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 392 | if (rtph->type == RTCP_TYPE_SDES) { |
| 393 | char new_cname[255]; |
| 394 | strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr), |
| 395 | sizeof(new_cname)); |
| 396 | new_cname[sizeof(new_cname)-1] = '\0'; |
| 397 | rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len, |
| 398 | new_cname); |
| 399 | if (rc < 0) |
| 400 | return rc; |
| 401 | } |
| 402 | rtph = (void *)rtph + old_len; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 408 | /* read from incoming RTP/RTCP socket */ |
| 409 | static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 410 | { |
| 411 | int rc; |
| 412 | struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP"); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 413 | struct msgb *new_msg; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 414 | struct rtp_sub_socket *other_rss; |
| 415 | |
| 416 | if (!msg) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE); |
| 420 | if (rc <= 0) { |
| 421 | rss->bfd.when &= ~BSC_FD_READ; |
| 422 | return rc; |
| 423 | } |
| 424 | |
| 425 | msgb_put(msg, rc); |
| 426 | |
| 427 | switch (rs->rx_action) { |
| 428 | case RTP_PROXY: |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 429 | if (!rs->proxy.other_sock) { |
| 430 | rc = -EIO; |
| 431 | goto out_free; |
| 432 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 433 | if (rss->bfd.priv_nr == RTP_PRIV_RTP) |
| 434 | other_rss = &rs->proxy.other_sock->rtp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 435 | else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 436 | other_rss = &rs->proxy.other_sock->rtcp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 437 | /* modify RTCP SDES CNAME */ |
| 438 | rc = rtcp_mangle(msg, rs); |
| 439 | if (rc < 0) |
| 440 | goto out_free; |
| 441 | } else { |
| 442 | rc = -EINVAL; |
| 443 | goto out_free; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 444 | } |
| 445 | msgb_enqueue(&other_rss->tx_queue, msg); |
| 446 | other_rss->bfd.when |= BSC_FD_WRITE; |
| 447 | break; |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 448 | |
| 449 | case RTP_RECV_UPSTREAM: |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 450 | if (!rs->receive.callref || !rs->receive.net) { |
| 451 | rc = -EIO; |
| 452 | goto out_free; |
| 453 | } |
| 454 | if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
| 455 | if (!mangle_rtcp_cname) { |
| 456 | msgb_free(msg); |
| 457 | break; |
| 458 | } |
| 459 | /* modify RTCP SDES CNAME */ |
| 460 | rc = rtcp_mangle(msg, rs); |
| 461 | if (rc < 0) |
| 462 | goto out_free; |
| 463 | msgb_enqueue(&rss->tx_queue, msg); |
| 464 | rss->bfd.when |= BSC_FD_WRITE; |
| 465 | break; |
| 466 | } |
| 467 | if (rss->bfd.priv_nr != RTP_PRIV_RTP) { |
| 468 | rc = -EINVAL; |
| 469 | goto out_free; |
| 470 | } |
| 471 | rc = rtp_decode(msg, rs->receive.callref, &new_msg); |
| 472 | if (rc < 0) |
| 473 | goto out_free; |
| 474 | msgb_free(msg); |
Harald Welte | 31c00f7 | 2011-03-03 23:29:05 +0100 | [diff] [blame] | 475 | trau_tx_to_mncc(rs->receive.net, new_msg); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | case RTP_NONE: /* if socket exists, but disabled by app */ |
| 479 | msgb_free(msg); |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 480 | break; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 483 | return 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 484 | |
| 485 | out_free: |
| 486 | msgb_free(msg); |
| 487 | return rc; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 490 | /* \brief write from tx_queue to RTP/RTCP socket */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 491 | static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 492 | { |
| 493 | struct msgb *msg; |
| 494 | int written; |
| 495 | |
| 496 | msg = msgb_dequeue(&rss->tx_queue); |
| 497 | if (!msg) { |
| 498 | rss->bfd.when &= ~BSC_FD_WRITE; |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | written = write(rss->bfd.fd, msg->data, msg->len); |
| 503 | if (written < msg->len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 504 | LOGP(DLMIB, LOGL_ERROR, "short write"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 505 | msgb_free(msg); |
| 506 | return -EIO; |
| 507 | } |
| 508 | |
| 509 | msgb_free(msg); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 515 | /*! \brief callback for the select.c:bfd_* layer */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 516 | static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 517 | { |
| 518 | struct rtp_socket *rs = bfd->data; |
| 519 | struct rtp_sub_socket *rss; |
| 520 | |
| 521 | switch (bfd->priv_nr) { |
| 522 | case RTP_PRIV_RTP: |
| 523 | rss = &rs->rtp; |
| 524 | break; |
| 525 | case RTP_PRIV_RTCP: |
| 526 | rss = &rs->rtcp; |
| 527 | break; |
| 528 | default: |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | if (flags & BSC_FD_READ) |
| 533 | rtp_socket_read(rs, rss); |
| 534 | |
| 535 | if (flags & BSC_FD_WRITE) |
| 536 | rtp_socket_write(rs, rss); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 541 | /*! \brief initialize one rtp sub-socket */ |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 542 | static void init_rss(struct rtp_sub_socket *rss, |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 543 | struct rtp_socket *rs, int fd, int priv_nr) |
| 544 | { |
| 545 | /* initialize bfd */ |
| 546 | rss->bfd.fd = fd; |
| 547 | rss->bfd.data = rs; |
| 548 | rss->bfd.priv_nr = priv_nr; |
| 549 | rss->bfd.cb = rtp_bfd_cb; |
| 550 | } |
| 551 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 552 | /*! \brief create a new RTP/RTCP socket and bind it */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 553 | struct rtp_socket *rtp_socket_create(void) |
| 554 | { |
| 555 | int rc; |
| 556 | struct rtp_socket *rs; |
| 557 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 558 | DEBUGP(DLMUX, "rtp_socket_create(): "); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 559 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 560 | rs = talloc_zero(tall_bsc_ctx, struct rtp_socket); |
| 561 | if (!rs) |
| 562 | return NULL; |
| 563 | |
| 564 | INIT_LLIST_HEAD(&rs->rtp.tx_queue); |
| 565 | INIT_LLIST_HEAD(&rs->rtcp.tx_queue); |
| 566 | |
| 567 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 568 | if (rc < 0) |
| 569 | goto out_free; |
| 570 | |
| 571 | init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 572 | rc = osmo_fd_register(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 573 | if (rc < 0) |
| 574 | goto out_rtp_socket; |
| 575 | |
| 576 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 577 | if (rc < 0) |
| 578 | goto out_rtp_bfd; |
| 579 | |
| 580 | init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 581 | rc = osmo_fd_register(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 582 | if (rc < 0) |
| 583 | goto out_rtcp_socket; |
| 584 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 585 | DEBUGPC(DLMUX, "success\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 586 | |
| 587 | rc = rtp_socket_bind(rs, INADDR_ANY); |
| 588 | if (rc < 0) |
| 589 | goto out_rtcp_bfd; |
| 590 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 591 | return rs; |
| 592 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 593 | out_rtcp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 594 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 595 | out_rtcp_socket: |
| 596 | close(rs->rtcp.bfd.fd); |
| 597 | out_rtp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 598 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 599 | out_rtp_socket: |
| 600 | close(rs->rtp.bfd.fd); |
| 601 | out_free: |
| 602 | talloc_free(rs); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 603 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 604 | return NULL; |
| 605 | } |
| 606 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 607 | static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip, |
| 608 | uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 609 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 610 | int rc; |
| 611 | socklen_t alen = sizeof(rss->sin_local); |
| 612 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 613 | rss->sin_local.sin_family = AF_INET; |
| 614 | rss->sin_local.sin_addr.s_addr = htonl(ip); |
| 615 | rss->sin_local.sin_port = htons(port); |
| 616 | rss->bfd.when |= BSC_FD_READ; |
| 617 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 618 | rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 619 | sizeof(rss->sin_local)); |
| 620 | if (rc < 0) |
| 621 | return rc; |
| 622 | |
| 623 | /* retrieve the address we actually bound to, in case we |
| 624 | * passed INADDR_ANY as IP address */ |
| 625 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 626 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | #define RTP_PORT_BASE 30000 |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 630 | static unsigned int next_udp_port = RTP_PORT_BASE; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 631 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 632 | /*! \brief bind a RTP socket to a specific local address |
| 633 | * \param[in] rs RTP socket to be bound |
| 634 | * \param[in] ip local IP address to which socket is to be bound |
| 635 | */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 636 | int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 637 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 638 | int rc = -EIO; |
| 639 | struct in_addr ia; |
| 640 | |
| 641 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 642 | DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs, |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 643 | inet_ntoa(ia)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 644 | |
| 645 | /* try to bind to a consecutive pair of ports */ |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 646 | for (next_udp_port = next_udp_port % 0xffff; |
| 647 | next_udp_port < 0xffff; next_udp_port += 2) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 648 | rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port); |
| 649 | if (rc != 0) |
| 650 | continue; |
| 651 | |
| 652 | rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1); |
| 653 | if (rc == 0) |
| 654 | break; |
| 655 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 656 | if (rc < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 657 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 658 | return rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 659 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 660 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 661 | ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 662 | DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 663 | inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 664 | return ntohs(rs->rtp.sin_local.sin_port); |
| 665 | } |
| 666 | |
| 667 | 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] | 668 | uint32_t ip, uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 669 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 670 | int rc; |
| 671 | socklen_t alen = sizeof(rss->sin_local); |
| 672 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 673 | rss->sin_remote.sin_family = AF_INET; |
| 674 | rss->sin_remote.sin_addr.s_addr = htonl(ip); |
| 675 | rss->sin_remote.sin_port = htons(port); |
| 676 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 677 | rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote, |
| 678 | sizeof(rss->sin_remote)); |
| 679 | if (rc < 0) |
| 680 | return rc; |
| 681 | |
| 682 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 683 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 686 | /*! \brief 'connect' a RTP socket to a remote peer |
| 687 | * \param[in] rs RTP socket to be connected |
| 688 | * \param[in] ip remote IP address to which to connect |
| 689 | * \param[in] port remote UDP port number to which to connect |
| 690 | */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 691 | 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] | 692 | { |
| 693 | int rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 694 | struct in_addr ia; |
| 695 | |
| 696 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 697 | DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 698 | rs, inet_ntoa(ia), port); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 699 | |
| 700 | rc = rtp_sub_socket_connect(&rs->rtp, ip, port); |
| 701 | if (rc < 0) |
| 702 | return rc; |
| 703 | |
| 704 | return rtp_sub_socket_connect(&rs->rtcp, ip, port+1); |
| 705 | } |
| 706 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 707 | /*! \brief bind two RTP/RTCP sockets together in the proxy |
| 708 | * \param[in] this First RTP socket |
| 709 | * \param[in] other Second RTP socket |
| 710 | */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 711 | int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other) |
| 712 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 713 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 714 | this, other); |
| 715 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 716 | this->rx_action = RTP_PROXY; |
| 717 | this->proxy.other_sock = other; |
| 718 | |
| 719 | other->rx_action = RTP_PROXY; |
| 720 | other->proxy.other_sock = this; |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 725 | /*! \brief bind RTP/RTCP socket to application, disabling proxy |
| 726 | * \param[in] this RTP socket |
| 727 | * \param[in] net gsm_network argument to trau_tx_to_mncc() |
| 728 | * \param[in] callref callref argument to trau_tx_to_mncc() |
| 729 | */ |
Harald Welte | 9fb1f10 | 2009-12-20 17:07:23 +0100 | [diff] [blame] | 730 | 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] | 731 | uint32_t callref) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 732 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 733 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 734 | this, callref); |
| 735 | |
| 736 | if (callref) { |
| 737 | this->rx_action = RTP_RECV_UPSTREAM; |
| 738 | this->receive.net = net; |
| 739 | this->receive.callref = callref; |
| 740 | } else |
| 741 | this->rx_action = RTP_NONE; |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 746 | static void free_tx_queue(struct rtp_sub_socket *rss) |
| 747 | { |
| 748 | struct msgb *msg; |
| 749 | |
| 750 | while ((msg = msgb_dequeue(&rss->tx_queue))) |
| 751 | msgb_free(msg); |
| 752 | } |
| 753 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 754 | /*! \brief Free/release a previously allocated RTP socket |
| 755 | * \param[in[] rs RTP/RTCP socket to be released |
| 756 | */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 757 | int rtp_socket_free(struct rtp_socket *rs) |
| 758 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 759 | DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 760 | |
| 761 | /* make sure we don't leave references dangling to us */ |
| 762 | if (rs->rx_action == RTP_PROXY && |
| 763 | rs->proxy.other_sock) |
| 764 | rs->proxy.other_sock->proxy.other_sock = NULL; |
| 765 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 766 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 767 | close(rs->rtp.bfd.fd); |
| 768 | free_tx_queue(&rs->rtp); |
| 769 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 770 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 771 | close(rs->rtcp.bfd.fd); |
| 772 | free_tx_queue(&rs->rtcp); |
| 773 | |
| 774 | talloc_free(rs); |
| 775 | |
| 776 | return 0; |
| 777 | } |