Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2010 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
Holger Hans Peter Freyther | adb1992 | 2010-05-14 02:14:09 +0800 | [diff] [blame] | 26 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 28 | #include <endian.h> |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <arpa/inet.h> |
| 33 | |
Holger Hans Peter Freyther | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 34 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 35 | #include <osmocore/select.h> |
| 36 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 37 | #include <openbsc/debug.h> |
| 38 | #include <openbsc/mgcp.h> |
| 39 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 40 | |
| 41 | #warning "Make use of the rtp proxy code" |
| 42 | |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 43 | /* according to rtp_proxy.c RFC 3550 */ |
| 44 | struct rtp_hdr { |
| 45 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 46 | uint8_t csrc_count:4, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 47 | extension:1, |
| 48 | padding:1, |
| 49 | version:2; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 50 | uint8_t payload_type:7, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 51 | marker:1; |
| 52 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 53 | uint8_t version:2, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 54 | padding:1, |
| 55 | extension:1, |
| 56 | csrc_count:4; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 57 | uint8_t marker:1, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 58 | payload_type:7; |
| 59 | #endif |
Holger Hans Peter Freyther | 1884623 | 2010-07-23 18:56:01 +0800 | [diff] [blame] | 60 | uint16_t sequence; |
Holger Hans Peter Freyther | 142475b | 2010-07-23 18:55:38 +0800 | [diff] [blame] | 61 | uint32_t timestamp; |
| 62 | uint32_t ssrc; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 63 | } __attribute__((packed)); |
| 64 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 65 | |
| 66 | enum { |
| 67 | DEST_NETWORK = 0, |
| 68 | DEST_BTS = 1, |
| 69 | }; |
| 70 | |
| 71 | enum { |
| 72 | PROTO_RTP, |
| 73 | PROTO_RTCP, |
| 74 | }; |
| 75 | |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 76 | #define DUMMY_LOAD 0x23 |
| 77 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 78 | |
| 79 | static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
| 80 | { |
| 81 | struct sockaddr_in out; |
| 82 | out.sin_family = AF_INET; |
| 83 | out.sin_port = port; |
| 84 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 85 | |
| 86 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 87 | } |
| 88 | |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 89 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 90 | { |
| 91 | static char buf[] = { DUMMY_LOAD }; |
| 92 | |
Holger Hans Peter Freyther | bfb68b7 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 93 | return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 94 | endp->net_end.rtp_port, buf, 1); |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 95 | } |
| 96 | |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 97 | static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 98 | int payload, struct sockaddr_in *addr, char *data, int len) |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 99 | { |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 100 | uint16_t seq; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 101 | uint32_t timestamp; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 102 | struct rtp_hdr *rtp_hdr; |
| 103 | |
| 104 | if (len < sizeof(*rtp_hdr)) |
| 105 | return; |
| 106 | |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 107 | rtp_hdr = (struct rtp_hdr *) data; |
| 108 | seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 109 | timestamp = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 110 | |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 111 | if (!state->initialized) { |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 112 | state->seq_no = seq - 1; |
| 113 | state->ssrc = state->orig_ssrc = rtp_hdr->ssrc; |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 114 | state->initialized = 1; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 115 | state->last_timestamp = timestamp; |
| 116 | } else if (state->ssrc != rtp_hdr->ssrc) { |
| 117 | state->ssrc = rtp_hdr->ssrc; |
| 118 | state->seq_offset = (state->seq_no + 1) - seq; |
| 119 | state->timestamp_offset = state->last_timestamp - timestamp; |
Holger Hans Peter Freyther | 73aa514 | 2010-08-05 12:07:00 +0000 | [diff] [blame] | 120 | state->patch = endp->allow_patch; |
Holger Hans Peter Freyther | eb0446b | 2010-08-04 21:45:39 +0800 | [diff] [blame] | 121 | LOGP(DMGCP, LOGL_NOTICE, |
Holger Hans Peter Freyther | 8e2acb7 | 2010-08-04 22:14:46 +0800 | [diff] [blame] | 122 | "The SSRC changed on 0x%x SSRC: %u offset: %d from %s:%d in %d\n", |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 123 | ENDPOINT_NUMBER(endp), state->ssrc, state->seq_offset, |
| 124 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), endp->conn_mode); |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 125 | } |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 126 | |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 127 | /* apply the offset and store it back to the packet */ |
Holger Hans Peter Freyther | 6230eb2 | 2010-08-03 15:36:57 +0000 | [diff] [blame] | 128 | if (state->patch) { |
| 129 | seq += state->seq_offset; |
| 130 | rtp_hdr->sequence = htons(seq); |
| 131 | rtp_hdr->ssrc = state->orig_ssrc; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 132 | |
Holger Hans Peter Freyther | 6230eb2 | 2010-08-03 15:36:57 +0000 | [diff] [blame] | 133 | timestamp += state->timestamp_offset; |
| 134 | rtp_hdr->timestamp = htonl(timestamp); |
| 135 | } |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 136 | |
| 137 | /* seq changed, now compare if we have lost something */ |
| 138 | if (state->seq_no + 1u != seq) |
| 139 | state->lost_no = abs(seq - (state->seq_no + 1)); |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 140 | state->seq_no = seq; |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 141 | |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 142 | state->last_timestamp = timestamp; |
| 143 | |
Holger Hans Peter Freyther | 0052955 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 144 | if (payload < 0) |
| 145 | return; |
| 146 | |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 147 | rtp_hdr->payload_type = payload; |
| 148 | } |
| 149 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 150 | /* |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 151 | * The below code is for dispatching. We have a dedicated port for |
| 152 | * the data coming from the net and one to discover the BTS. |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 153 | */ |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 154 | static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) |
| 155 | { |
| 156 | if (!tap->enabled) |
| 157 | return 0; |
| 158 | |
| 159 | return sendto(fd, buf, len, 0, |
| 160 | (struct sockaddr *)&tap->forward, sizeof(tap->forward)); |
| 161 | } |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 162 | |
| 163 | static int send_transcoder(struct mgcp_endpoint *endp, int is_rtp, |
| 164 | const char *buf, int len) |
| 165 | { |
| 166 | int rc; |
| 167 | int port; |
| 168 | struct mgcp_config *cfg = endp->cfg; |
| 169 | struct sockaddr_in addr; |
| 170 | |
| 171 | if (endp->transcoder_end.rtp_port == 0) { |
| 172 | LOGP(DMGCP, LOGL_ERROR, "Transcoder port not known on 0x%x\n", |
| 173 | ENDPOINT_NUMBER(endp)); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | port = rtp_calculate_port(ENDPOINT_NUMBER(endp), 4000); |
| 178 | if (!is_rtp) |
| 179 | port += 1; |
| 180 | |
| 181 | addr.sin_family = AF_INET; |
| 182 | addr.sin_addr = cfg->transcoder_in; |
| 183 | addr.sin_port = htons(port); |
| 184 | |
| 185 | rc = sendto(is_rtp ? |
| 186 | endp->bts_end.rtp.fd : |
| 187 | endp->bts_end.rtcp.fd, buf, len, 0, |
| 188 | (struct sockaddr *) &addr, sizeof(addr)); |
| 189 | |
| 190 | if (rc != len) |
| 191 | LOGP(DMGCP, LOGL_ERROR, |
| 192 | "Failed to send data to the transcoder: %s\n", |
| 193 | strerror(errno)); |
| 194 | |
| 195 | return rc; |
| 196 | } |
| 197 | |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 198 | static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 199 | struct sockaddr_in *addr, char *buf, int rc) |
| 200 | { |
Holger Hans Peter Freyther | 84026ba | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 201 | struct mgcp_config *cfg = endp->cfg; |
| 202 | /* For loop toggle the destination and then dispatch. */ |
| 203 | if (cfg->audio_loop) |
| 204 | dest = !dest; |
| 205 | |
| 206 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 207 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 208 | dest = !dest; |
| 209 | |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 210 | if (dest == DEST_NETWORK) { |
| 211 | if (is_rtp) { |
| 212 | patch_and_count(endp, &endp->bts_state, |
| 213 | endp->net_end.payload_type, |
| 214 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 215 | forward_data(endp->net_end.rtp.fd, |
| 216 | &endp->taps[MGCP_TAP_NET_OUT], buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 217 | return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
| 218 | endp->net_end.rtp_port, buf, rc); |
| 219 | } else { |
| 220 | return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, |
| 221 | endp->net_end.rtcp_port, buf, rc); |
| 222 | } |
| 223 | } else { |
| 224 | if (is_rtp) { |
| 225 | patch_and_count(endp, &endp->net_state, |
| 226 | endp->bts_end.payload_type, |
| 227 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 228 | forward_data(endp->bts_end.rtp.fd, |
| 229 | &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 230 | return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr, |
| 231 | endp->bts_end.rtp_port, buf, rc); |
| 232 | } else { |
| 233 | return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr, |
| 234 | endp->bts_end.rtcp_port, buf, rc); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 239 | static int recevice_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 240 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 241 | { |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 242 | int rc; |
| 243 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 244 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 245 | rc = recvfrom(fd, buf, bufsize, 0, |
| 246 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 247 | if (rc < 0) { |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 248 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 249 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | e15223b | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 254 | if (!endp->allocated) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 255 | return -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 256 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 257 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 258 | |
| 259 | return rc; |
| 260 | } |
| 261 | |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 262 | static int rtp_data_net(struct bsc_fd *fd, unsigned int what) |
| 263 | { |
| 264 | char buf[4096]; |
| 265 | struct sockaddr_in addr; |
| 266 | struct mgcp_endpoint *endp; |
| 267 | int rc, proto; |
| 268 | |
| 269 | endp = (struct mgcp_endpoint *) fd->data; |
| 270 | |
| 271 | rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
| 272 | if (rc <= 0) |
| 273 | return -1; |
| 274 | |
| 275 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 276 | LOGP(DMGCP, LOGL_ERROR, |
| 277 | "Data from wrong address %s on 0x%x\n", |
| 278 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | if (endp->net_end.rtp_port != addr.sin_port && |
| 283 | endp->net_end.rtcp_port != addr.sin_port) { |
| 284 | LOGP(DMGCP, LOGL_ERROR, |
| 285 | "Data from wrong source port %d on 0x%x\n", |
| 286 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | /* throw away the dummy message */ |
| 291 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 292 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 293 | ENDPOINT_NUMBER(endp)); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP; |
| 298 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 299 | |
| 300 | forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 301 | return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr, &buf[0], rc); |
| 302 | } |
| 303 | |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 304 | static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) |
| 305 | { |
| 306 | struct mgcp_config *cfg = endp->cfg; |
| 307 | |
| 308 | if (proto == PROTO_RTP && endp->bts_end.rtp_port == 0) { |
| 309 | if (!cfg->bts_ip || |
| 310 | memcmp(&addr->sin_addr, |
| 311 | &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || |
| 312 | memcmp(&addr->sin_addr, |
| 313 | &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { |
| 314 | |
| 315 | endp->bts_end.rtp_port = addr->sin_port; |
| 316 | endp->bts_end.addr = addr->sin_addr; |
| 317 | |
| 318 | LOGP(DMGCP, LOGL_NOTICE, |
| 319 | "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 320 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), |
| 321 | ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); |
| 322 | } |
| 323 | } else if (proto == PROTO_RTCP && endp->bts_end.rtcp_port == 0) { |
| 324 | if (memcmp(&endp->bts_end.addr, &addr->sin_addr, |
| 325 | sizeof(endp->bts_end.addr)) == 0) { |
| 326 | endp->bts_end.rtcp_port = addr->sin_port; |
| 327 | } |
| 328 | } |
| 329 | } |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 330 | |
| 331 | static int rtp_data_bts(struct bsc_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 332 | { |
| 333 | char buf[4096]; |
| 334 | struct sockaddr_in addr; |
| 335 | struct mgcp_endpoint *endp; |
| 336 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 337 | int rc, proto; |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 338 | |
| 339 | endp = (struct mgcp_endpoint *) fd->data; |
| 340 | cfg = endp->cfg; |
| 341 | |
| 342 | rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
| 343 | if (rc <= 0) |
| 344 | return -1; |
| 345 | |
Holger Hans Peter Freyther | 29e0188 | 2010-08-06 07:39:26 +0800 | [diff] [blame] | 346 | proto = fd == &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 347 | |
| 348 | /* We have no idea who called us, maybe it is the BTS. */ |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 349 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 350 | discover_bts(endp, proto, &addr); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 351 | |
Holger Hans Peter Freyther | 935137c | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 352 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 353 | LOGP(DMGCP, LOGL_ERROR, |
| 354 | "Data from wrong bts %s on 0x%x\n", |
| 355 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 360 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 361 | LOGP(DMGCP, LOGL_ERROR, |
| 362 | "Data from wrong bts source port %d on 0x%x\n", |
| 363 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 364 | return -1; |
| 365 | } |
| 366 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 367 | /* throw away the dummy message */ |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 368 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 369 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n", |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 370 | ENDPOINT_NUMBER(endp)); |
| 371 | return 0; |
| 372 | } |
| 373 | |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 374 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 375 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 376 | |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 377 | forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 378 | if (cfg->transcoder_ip) |
| 379 | return send_transcoder(endp, proto == PROTO_RTP, &buf[0], rc); |
| 380 | else |
| 381 | return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr, &buf[0], rc); |
| 382 | } |
| 383 | |
| 384 | static int rtp_data_transcoder(struct bsc_fd *fd, unsigned int what) |
| 385 | { |
| 386 | char buf[4096]; |
| 387 | struct sockaddr_in addr; |
| 388 | struct mgcp_endpoint *endp; |
| 389 | struct mgcp_config *cfg; |
| 390 | int rc, proto; |
| 391 | |
| 392 | endp = (struct mgcp_endpoint *) fd->data; |
| 393 | cfg = endp->cfg; |
| 394 | |
| 395 | rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
| 396 | if (rc <= 0) |
| 397 | return -1; |
| 398 | |
| 399 | proto = fd == &endp->transcoder_end.rtp ? PROTO_RTP : PROTO_RTCP; |
| 400 | |
| 401 | if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { |
| 402 | LOGP(DMGCP, LOGL_ERROR, |
| 403 | "Data not coming from transcoder: %s on 0x%x\n", |
| 404 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | if (endp->transcoder_end.rtp_port != addr.sin_port && |
| 409 | endp->transcoder_end.rtcp_port != addr.sin_port) { |
| 410 | LOGP(DMGCP, LOGL_ERROR, |
| 411 | "Data from wrong transcoder source port %d on 0x%x\n", |
| 412 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | /* throw away the dummy message */ |
| 417 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 418 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder on 0x%x\n", |
| 419 | ENDPOINT_NUMBER(endp)); |
| 420 | return 0; |
| 421 | } |
| 422 | |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 423 | return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr, &buf[0], rc); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 427 | { |
| 428 | struct sockaddr_in addr; |
| 429 | int on = 1; |
| 430 | |
| 431 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 432 | if (fd->fd < 0) { |
| 433 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 438 | memset(&addr, 0, sizeof(addr)); |
| 439 | addr.sin_family = AF_INET; |
| 440 | addr.sin_port = htons(port); |
| 441 | inet_aton(source_addr, &addr.sin_addr); |
| 442 | |
| 443 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | 1de4d84 | 2010-08-06 03:00:17 +0800 | [diff] [blame] | 444 | close(fd->fd); |
| 445 | fd->fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 452 | static int set_ip_tos(int fd, int tos) |
| 453 | { |
| 454 | int ret; |
| 455 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 456 | &tos, sizeof(tos)); |
| 457 | return ret != 0; |
| 458 | } |
| 459 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 460 | static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 461 | { |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 462 | if (create_bind(cfg->source_addr, &rtp_end->rtp, rtp_end->local_port) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 463 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 464 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 465 | goto cleanup0; |
| 466 | } |
| 467 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 468 | if (create_bind(cfg->source_addr, &rtp_end->rtcp, rtp_end->local_port + 1) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 469 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 470 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 471 | goto cleanup1; |
| 472 | } |
| 473 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 474 | set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 475 | set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 476 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 477 | rtp_end->rtp.when = BSC_FD_READ; |
| 478 | if (bsc_register_fd(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 479 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 480 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 481 | goto cleanup2; |
| 482 | } |
| 483 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 484 | rtp_end->rtcp.when = BSC_FD_READ; |
| 485 | if (bsc_register_fd(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 486 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 487 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 488 | goto cleanup3; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | |
| 493 | cleanup3: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 494 | bsc_unregister_fd(&rtp_end->rtp); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 495 | cleanup2: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 496 | close(rtp_end->rtcp.fd); |
| 497 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 498 | cleanup1: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 499 | close(rtp_end->rtp.fd); |
| 500 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 501 | cleanup0: |
| 502 | return -1; |
| 503 | } |
| 504 | |
Holger Hans Peter Freyther | d1725f4 | 2010-08-05 03:27:41 +0800 | [diff] [blame] | 505 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 506 | { |
Holger Hans Peter Freyther | 6af36c0 | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 507 | if (endp->bts_end.rtp.fd != -1 || endp->bts_end.rtcp.fd != -1) { |
| 508 | LOGP(DMGCP, LOGL_ERROR, "Previous bts-port was still bound on %d\n", |
| 509 | ENDPOINT_NUMBER(endp)); |
| 510 | mgcp_free_rtp_port(&endp->bts_end); |
| 511 | } |
| 512 | |
Holger Hans Peter Freyther | 148c5d3 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 513 | endp->bts_end.local_port = rtp_port; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 514 | endp->bts_end.rtp.cb = rtp_data_bts; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 515 | endp->bts_end.rtp.data = endp; |
| 516 | endp->bts_end.rtcp.data = endp; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 517 | endp->bts_end.rtcp.cb = rtp_data_bts; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 518 | return bind_rtp(endp->cfg, &endp->bts_end, ENDPOINT_NUMBER(endp)); |
| 519 | } |
| 520 | |
| 521 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 522 | { |
Holger Hans Peter Freyther | 6af36c0 | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 523 | if (endp->net_end.rtp.fd != -1 || endp->net_end.rtcp.fd != -1) { |
| 524 | LOGP(DMGCP, LOGL_ERROR, "Previous net-port was still bound on %d\n", |
| 525 | ENDPOINT_NUMBER(endp)); |
| 526 | mgcp_free_rtp_port(&endp->net_end); |
| 527 | } |
| 528 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 529 | endp->net_end.local_port = rtp_port; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 530 | endp->net_end.rtp.cb = rtp_data_net; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 531 | endp->net_end.rtp.data = endp; |
| 532 | endp->net_end.rtcp.data = endp; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 533 | endp->net_end.rtcp.cb = rtp_data_net; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 534 | return bind_rtp(endp->cfg, &endp->net_end, ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 535 | } |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 536 | |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 537 | int mgcp_bind_transcoder_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 538 | { |
| 539 | if (endp->transcoder_end.rtp.fd != -1 || endp->transcoder_end.rtcp.fd != -1) { |
| 540 | LOGP(DMGCP, LOGL_ERROR, "Previous net-port was still bound on %d\n", |
| 541 | ENDPOINT_NUMBER(endp)); |
| 542 | mgcp_free_rtp_port(&endp->transcoder_end); |
| 543 | } |
| 544 | |
| 545 | endp->transcoder_end.local_port = rtp_port; |
| 546 | endp->transcoder_end.rtp.cb = rtp_data_transcoder; |
| 547 | endp->transcoder_end.rtp.data = endp; |
| 548 | endp->transcoder_end.rtcp.data = endp; |
| 549 | endp->transcoder_end.rtcp.cb = rtp_data_transcoder; |
| 550 | return bind_rtp(endp->cfg, &endp->transcoder_end, ENDPOINT_NUMBER(endp)); |
| 551 | } |
| 552 | |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 553 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 554 | { |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 555 | if (end->rtp.fd != -1) { |
| 556 | close(end->rtp.fd); |
| 557 | end->rtp.fd = -1; |
| 558 | bsc_unregister_fd(&end->rtp); |
| 559 | } |
| 560 | |
| 561 | if (end->rtcp.fd != -1) { |
| 562 | close(end->rtcp.fd); |
| 563 | end->rtcp.fd = -1; |
| 564 | bsc_unregister_fd(&end->rtcp); |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |