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 | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 162 | static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 163 | struct sockaddr_in *addr, char *buf, int rc) |
| 164 | { |
Holger Hans Peter Freyther | 84026ba | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 165 | struct mgcp_config *cfg = endp->cfg; |
| 166 | /* For loop toggle the destination and then dispatch. */ |
| 167 | if (cfg->audio_loop) |
| 168 | dest = !dest; |
| 169 | |
| 170 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 171 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 172 | dest = !dest; |
| 173 | |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 174 | if (dest == DEST_NETWORK) { |
| 175 | if (is_rtp) { |
| 176 | patch_and_count(endp, &endp->bts_state, |
| 177 | endp->net_end.payload_type, |
| 178 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 179 | forward_data(endp->net_end.rtp.fd, |
| 180 | &endp->taps[MGCP_TAP_NET_OUT], buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 181 | return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
| 182 | endp->net_end.rtp_port, buf, rc); |
| 183 | } else { |
| 184 | return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, |
| 185 | endp->net_end.rtcp_port, buf, rc); |
| 186 | } |
| 187 | } else { |
| 188 | if (is_rtp) { |
| 189 | patch_and_count(endp, &endp->net_state, |
| 190 | endp->bts_end.payload_type, |
| 191 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 192 | forward_data(endp->bts_end.rtp.fd, |
| 193 | &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 194 | return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr, |
| 195 | endp->bts_end.rtp_port, buf, rc); |
| 196 | } else { |
| 197 | return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr, |
| 198 | endp->bts_end.rtcp_port, buf, rc); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 203 | static int recevice_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 204 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 205 | { |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 206 | int rc; |
| 207 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 208 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 209 | rc = recvfrom(fd, buf, bufsize, 0, |
| 210 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 211 | if (rc < 0) { |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 212 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 213 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | baefbf4 | 2010-03-01 18:53:05 +0100 | [diff] [blame] | 218 | if (endp->ci == CI_UNUSED) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 219 | return -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 220 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 221 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 222 | |
| 223 | return rc; |
| 224 | } |
| 225 | |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 226 | static int rtp_data_net(struct bsc_fd *fd, unsigned int what) |
| 227 | { |
| 228 | char buf[4096]; |
| 229 | struct sockaddr_in addr; |
| 230 | struct mgcp_endpoint *endp; |
| 231 | int rc, proto; |
| 232 | |
| 233 | endp = (struct mgcp_endpoint *) fd->data; |
| 234 | |
| 235 | rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
| 236 | if (rc <= 0) |
| 237 | return -1; |
| 238 | |
| 239 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 240 | LOGP(DMGCP, LOGL_ERROR, |
| 241 | "Data from wrong address %s on 0x%x\n", |
| 242 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (endp->net_end.rtp_port != addr.sin_port && |
| 247 | endp->net_end.rtcp_port != addr.sin_port) { |
| 248 | LOGP(DMGCP, LOGL_ERROR, |
| 249 | "Data from wrong source port %d on 0x%x\n", |
| 250 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | /* throw away the dummy message */ |
| 255 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 256 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 257 | ENDPOINT_NUMBER(endp)); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP; |
| 262 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 263 | |
| 264 | 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] | 265 | return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr, &buf[0], rc); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | 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] | 270 | { |
| 271 | char buf[4096]; |
| 272 | struct sockaddr_in addr; |
| 273 | struct mgcp_endpoint *endp; |
| 274 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 275 | int rc, proto; |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 276 | |
| 277 | endp = (struct mgcp_endpoint *) fd->data; |
| 278 | cfg = endp->cfg; |
| 279 | |
| 280 | rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
| 281 | if (rc <= 0) |
| 282 | return -1; |
| 283 | |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 284 | proto = &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 285 | |
| 286 | /* 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] | 287 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 7944f89 | 2010-08-05 10:47:40 +0000 | [diff] [blame] | 288 | if (endp->bts_end.rtp_port == 0 && (!cfg->bts_ip |
| 289 | || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0 |
| 290 | || memcmp(&addr.sin_addr, &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0)) { |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 291 | if (proto == PROTO_RTP) |
| 292 | endp->bts_end.rtp_port = addr.sin_port; |
| 293 | else |
| 294 | endp->bts_end.rtcp_port = addr.sin_port; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 295 | |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 296 | endp->bts_end.addr = addr.sin_addr; |
| 297 | LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 298 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port), |
| 299 | inet_ntoa(addr.sin_addr)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Holger Hans Peter Freyther | 935137c | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 302 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 303 | LOGP(DMGCP, LOGL_ERROR, |
| 304 | "Data from wrong bts %s on 0x%x\n", |
| 305 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 310 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 311 | LOGP(DMGCP, LOGL_ERROR, |
| 312 | "Data from wrong bts source port %d on 0x%x\n", |
| 313 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 314 | return -1; |
| 315 | } |
| 316 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 317 | /* throw away the dummy message */ |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 318 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 319 | 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] | 320 | ENDPOINT_NUMBER(endp)); |
| 321 | return 0; |
| 322 | } |
| 323 | |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 324 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 325 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 326 | |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 327 | forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 328 | 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] | 329 | } |
| 330 | |
| 331 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 332 | { |
| 333 | struct sockaddr_in addr; |
| 334 | int on = 1; |
| 335 | |
| 336 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 337 | if (fd->fd < 0) { |
| 338 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 343 | memset(&addr, 0, sizeof(addr)); |
| 344 | addr.sin_family = AF_INET; |
| 345 | addr.sin_port = htons(port); |
| 346 | inet_aton(source_addr, &addr.sin_addr); |
| 347 | |
| 348 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 355 | static int set_ip_tos(int fd, int tos) |
| 356 | { |
| 357 | int ret; |
| 358 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 359 | &tos, sizeof(tos)); |
| 360 | return ret != 0; |
| 361 | } |
| 362 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 363 | 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] | 364 | { |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 365 | 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] | 366 | 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] | 367 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 368 | goto cleanup0; |
| 369 | } |
| 370 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 371 | 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] | 372 | 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] | 373 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 374 | goto cleanup1; |
| 375 | } |
| 376 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 377 | set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 378 | set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 379 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 380 | rtp_end->rtp.when = BSC_FD_READ; |
| 381 | if (bsc_register_fd(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 382 | 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] | 383 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 384 | goto cleanup2; |
| 385 | } |
| 386 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 387 | rtp_end->rtcp.when = BSC_FD_READ; |
| 388 | if (bsc_register_fd(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 389 | 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] | 390 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 391 | goto cleanup3; |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | |
| 396 | cleanup3: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 397 | bsc_unregister_fd(&rtp_end->rtp); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 398 | cleanup2: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 399 | close(rtp_end->rtcp.fd); |
| 400 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 401 | cleanup1: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 402 | close(rtp_end->rtp.fd); |
| 403 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 404 | cleanup0: |
| 405 | return -1; |
| 406 | } |
| 407 | |
Holger Hans Peter Freyther | d1725f4 | 2010-08-05 03:27:41 +0800 | [diff] [blame] | 408 | 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] | 409 | { |
Holger Hans Peter Freyther | 148c5d3 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 410 | endp->bts_end.local_port = rtp_port; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 411 | endp->bts_end.rtp.cb = rtp_data_bts; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 412 | endp->bts_end.rtp.data = endp; |
| 413 | endp->bts_end.rtcp.data = endp; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 414 | endp->bts_end.rtcp.cb = rtp_data_bts; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 415 | return bind_rtp(endp->cfg, &endp->bts_end, ENDPOINT_NUMBER(endp)); |
| 416 | } |
| 417 | |
| 418 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 419 | { |
| 420 | endp->net_end.local_port = rtp_port; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 421 | endp->net_end.rtp.cb = rtp_data_net; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 422 | endp->net_end.rtp.data = endp; |
| 423 | endp->net_end.rtcp.data = endp; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 424 | endp->net_end.rtcp.cb = rtp_data_net; |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 425 | 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] | 426 | } |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 427 | |
| 428 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 429 | { |
| 430 | if (end->local_alloc != PORT_ALLOC_DYNAMIC) { |
| 431 | LOGP(DMGCP, LOGL_ERROR, "Should only be called for dynamic ports.\n"); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | if (end->rtp.fd != -1) { |
| 436 | close(end->rtp.fd); |
| 437 | end->rtp.fd = -1; |
| 438 | bsc_unregister_fd(&end->rtp); |
| 439 | } |
| 440 | |
| 441 | if (end->rtcp.fd != -1) { |
| 442 | close(end->rtcp.fd); |
| 443 | end->rtcp.fd = -1; |
| 444 | bsc_unregister_fd(&end->rtcp); |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |