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