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 | |
| 93 | return udp_send(endp->local_rtp.fd, &endp->remote, |
| 94 | endp->net_rtp, buf, 1); |
| 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 | 566e59b | 2010-08-04 21:49:19 +0800 | [diff] [blame] | 120 | //state->patch = 1; |
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 | /* |
| 151 | * There is data coming. We will have to figure out if it |
| 152 | * came from the BTS or the MediaGateway of the MSC. On top |
| 153 | * of that we need to figure out if it was RTP or RTCP. |
| 154 | * |
| 155 | * Currently we do not communicate with the BSC so we have |
| 156 | * no idea where the BTS is listening for RTP and need to |
| 157 | * do the classic routing trick. Wait for the first packet |
| 158 | * from the BTS and then go ahead. |
| 159 | */ |
| 160 | static int rtp_data_cb(struct bsc_fd *fd, unsigned int what) |
| 161 | { |
| 162 | char buf[4096]; |
| 163 | struct sockaddr_in addr; |
| 164 | socklen_t slen = sizeof(addr); |
| 165 | struct mgcp_endpoint *endp; |
| 166 | struct mgcp_config *cfg; |
| 167 | int rc, dest, proto; |
| 168 | |
| 169 | endp = (struct mgcp_endpoint *) fd->data; |
| 170 | cfg = endp->cfg; |
| 171 | |
| 172 | rc = recvfrom(fd->fd, &buf, sizeof(buf), 0, |
| 173 | (struct sockaddr *) &addr, &slen); |
| 174 | if (rc < 0) { |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 175 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 176 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | /* 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] | 181 | if (endp->ci == CI_UNUSED) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 182 | return -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Figure out where to forward it to. This code assumes that we |
| 186 | * have received the Connection Modify and know who is a legitimate |
| 187 | * partner. According to the spec we could attempt to forward even |
| 188 | * after the Create Connection but we will not as we are not really |
| 189 | * able to tell if this is legitimate. |
| 190 | */ |
| 191 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 192 | dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 && |
Holger Hans Peter Freyther | 8c99434 | 2010-04-30 13:32:05 +0800 | [diff] [blame] | 193 | (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 194 | ? DEST_BTS : DEST_NETWORK; |
| 195 | proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP; |
| 196 | |
| 197 | /* We have no idea who called us, maybe it is the BTS. */ |
Holger Hans Peter Freyther | 6434452 | 2010-08-05 01:28:22 +0800 | [diff] [blame^] | 198 | if (dest == DEST_NETWORK && endp->bts_rtp == 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 199 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 27c90f8 | 2010-04-01 07:56:04 +0200 | [diff] [blame] | 200 | if (!cfg->bts_ip |
| 201 | || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0 |
| 202 | || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 203 | if (fd == &endp->local_rtp) { |
| 204 | endp->bts_rtp = addr.sin_port; |
| 205 | } else { |
| 206 | endp->bts_rtcp = addr.sin_port; |
| 207 | } |
| 208 | |
| 209 | endp->bts = addr.sin_addr; |
Holger Hans Peter Freyther | 9c3f576 | 2010-04-09 18:53:01 +0200 | [diff] [blame] | 210 | LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 211 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), |
| 212 | inet_ntoa(addr.sin_addr)); |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 213 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 217 | /* throw away the dummy message */ |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 218 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 219 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n", |
| 220 | ENDPOINT_NUMBER(endp)); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 224 | /* do this before the loop handling */ |
| 225 | if (dest == DEST_NETWORK) |
| 226 | ++endp->in_bts; |
| 227 | else |
| 228 | ++endp->in_remote; |
| 229 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 230 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 231 | if (cfg->audio_loop) |
| 232 | dest = !dest; |
| 233 | |
Holger Hans Peter Freyther | 0bdf20d | 2010-08-03 02:27:21 +0800 | [diff] [blame] | 234 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 235 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 236 | dest = !dest; |
| 237 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 238 | if (dest == DEST_NETWORK) { |
Holger Hans Peter Freyther | 6e9c55b | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 239 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 240 | patch_and_count(endp, &endp->bts_state, |
Holger Hans Peter Freyther | 8c0deca | 2010-08-05 05:29:50 +0800 | [diff] [blame] | 241 | endp->net_payload_type, |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 242 | &addr, buf, rc); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 243 | return udp_send(fd->fd, &endp->remote, |
| 244 | proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp, |
| 245 | buf, rc); |
| 246 | } else { |
Holger Hans Peter Freyther | 6e9c55b | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 247 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 248 | patch_and_count(endp, &endp->net_state, |
Holger Hans Peter Freyther | 8c0deca | 2010-08-05 05:29:50 +0800 | [diff] [blame] | 249 | endp->bts_payload_type, |
Holger Hans Peter Freyther | b1e9ba9 | 2010-08-04 22:20:34 +0800 | [diff] [blame] | 250 | &addr, buf, rc); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 251 | return udp_send(fd->fd, &endp->bts, |
| 252 | proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp, |
| 253 | buf, rc); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 258 | { |
| 259 | struct sockaddr_in addr; |
| 260 | int on = 1; |
| 261 | |
| 262 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 263 | if (fd->fd < 0) { |
| 264 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 269 | memset(&addr, 0, sizeof(addr)); |
| 270 | addr.sin_family = AF_INET; |
| 271 | addr.sin_port = htons(port); |
| 272 | inet_aton(source_addr, &addr.sin_addr); |
| 273 | |
| 274 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 281 | static int set_ip_tos(int fd, int tos) |
| 282 | { |
| 283 | int ret; |
| 284 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 285 | &tos, sizeof(tos)); |
| 286 | return ret != 0; |
| 287 | } |
| 288 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 289 | static int bind_rtp(struct mgcp_endpoint *endp) |
| 290 | { |
| 291 | struct mgcp_config *cfg = endp->cfg; |
| 292 | |
| 293 | if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) { |
| 294 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
| 295 | cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 296 | goto cleanup0; |
| 297 | } |
| 298 | |
| 299 | if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) { |
| 300 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
| 301 | cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 302 | goto cleanup1; |
| 303 | } |
| 304 | |
Holger Hans Peter Freyther | 3140c4f | 2010-07-27 20:34:45 +0800 | [diff] [blame] | 305 | set_ip_tos(endp->local_rtp.fd, cfg->endp_dscp); |
| 306 | set_ip_tos(endp->local_rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 307 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 308 | endp->local_rtp.cb = rtp_data_cb; |
| 309 | endp->local_rtp.data = endp; |
| 310 | endp->local_rtp.when = BSC_FD_READ; |
| 311 | if (bsc_register_fd(&endp->local_rtp) != 0) { |
| 312 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
| 313 | endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 314 | goto cleanup2; |
| 315 | } |
| 316 | |
| 317 | endp->local_rtcp.cb = rtp_data_cb; |
| 318 | endp->local_rtcp.data = endp; |
| 319 | endp->local_rtcp.when = BSC_FD_READ; |
| 320 | if (bsc_register_fd(&endp->local_rtcp) != 0) { |
| 321 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
| 322 | endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 323 | goto cleanup3; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | |
| 328 | cleanup3: |
| 329 | bsc_unregister_fd(&endp->local_rtp); |
| 330 | cleanup2: |
| 331 | close(endp->local_rtcp.fd); |
| 332 | endp->local_rtcp.fd = -1; |
| 333 | cleanup1: |
| 334 | close(endp->local_rtp.fd); |
| 335 | endp->local_rtp.fd = -1; |
| 336 | cleanup0: |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 341 | { |
| 342 | endp->rtp_port = rtp_port; |
| 343 | return bind_rtp(endp); |
| 344 | } |