blob: fbbfbf29b68ba411c0b6dc2fd18dcdebd8349d92 [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* 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>
26#include <stdlib.h>
27#include <unistd.h>
28#include <endian.h>
29#include <errno.h>
30
31#include <sys/socket.h>
32#include <arpa/inet.h>
33
34#include <mgcp/mgcp.h>
35#include <mgcp/mgcp_internal.h>
36
37#include <laf0rge1/debug.h>
38#include <laf0rge1/msgb.h>
39#include <laf0rge1/talloc.h>
40#include <laf0rge1/select.h>
41
42#warning "Make use of the rtp proxy code"
43
44/* according to rtp_proxy.c RFC 3550 */
45struct rtp_hdr {
46#if __BYTE_ORDER == __LITTLE_ENDIAN
47 u_int8_t csrc_count:4,
48 extension:1,
49 padding:1,
50 version:2;
51 u_int8_t payload_type:7,
52 marker:1;
53#elif __BYTE_ORDER == __BIG_ENDIAN
54 u_int8_t version:2,
55 padding:1,
56 extension:1,
57 csrc_count:4;
58 u_int8_t marker:1,
59 payload_type:7;
60#endif
61 u_int16_t sequence;
62 u_int32_t timestamp;
63 u_int32_t ssrc;
64} __attribute__((packed));
65
66
67enum {
68 DEST_NETWORK = 0,
69 DEST_BTS = 1,
70};
71
72enum {
73 PROTO_RTP,
74 PROTO_RTCP,
75};
76
77#define DUMMY_LOAD 0x23
78
79
80static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
81{
82 struct sockaddr_in out;
83 out.sin_family = AF_INET;
84 out.sin_port = port;
85 memcpy(&out.sin_addr, addr, sizeof(*addr));
86
87 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
88}
89
90int mgcp_send_dummy(struct mgcp_endpoint *endp)
91{
92 static char buf[] = { DUMMY_LOAD };
93
94 return udp_send(endp->local_rtp.fd, &endp->remote,
95 endp->net_rtp, buf, 1);
96}
97
98static void patch_payload(int payload, char *data, int len)
99{
100 struct rtp_hdr *rtp_hdr;
101
102 if (len < sizeof(*rtp_hdr))
103 return;
104
105 if (payload < 0)
106 return;
107
108 rtp_hdr = (struct rtp_hdr *) data;
109 rtp_hdr->payload_type = payload;
110}
111
112/*
113 * There is data coming. We will have to figure out if it
114 * came from the BTS or the MediaGateway of the MSC. On top
115 * of that we need to figure out if it was RTP or RTCP.
116 *
117 * Currently we do not communicate with the BSC so we have
118 * no idea where the BTS is listening for RTP and need to
119 * do the classic routing trick. Wait for the first packet
120 * from the BTS and then go ahead.
121 */
122static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
123{
124 char buf[4096];
125 struct sockaddr_in addr;
126 socklen_t slen = sizeof(addr);
127 struct mgcp_endpoint *endp;
128 struct mgcp_config *cfg;
129 int rc, dest, proto;
130
131 endp = (struct mgcp_endpoint *) fd->data;
132 cfg = endp->cfg;
133
134 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
135 (struct sockaddr *) &addr, &slen);
136 if (rc < 0) {
137 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
138 ENDPOINT_NUMBER(endp), errno, strerror(errno));
139 return -1;
140 }
141
142 /* do not forward aynthing... maybe there is a packet from the bts */
143 if (endp->ci == CI_UNUSED) {
144 LOGP(DMGCP, LOGL_DEBUG, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
145 return -1;
146 }
147
148 /*
149 * Figure out where to forward it to. This code assumes that we
150 * have received the Connection Modify and know who is a legitimate
151 * partner. According to the spec we could attempt to forward even
152 * after the Create Connection but we will not as we are not really
153 * able to tell if this is legitimate.
154 */
155 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
156 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
157 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
158 ? DEST_BTS : DEST_NETWORK;
159 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
160
161 /* We have no idea who called us, maybe it is the BTS. */
162 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
163 /* it was the BTS... */
164 if (!cfg->bts_ip
165 || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0
166 || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) {
167 if (fd == &endp->local_rtp) {
168 endp->bts_rtp = addr.sin_port;
169 } else {
170 endp->bts_rtcp = addr.sin_port;
171 }
172
173 endp->bts = addr.sin_addr;
174 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
175 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
176 inet_ntoa(addr.sin_addr));
177
178 }
179 }
180
181 /* throw away the dummy message */
182 if (rc == 1 && buf[0] == DUMMY_LOAD) {
183 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n",
184 ENDPOINT_NUMBER(endp));
185 return 0;
186 }
187
188 /* do this before the loop handling */
189 if (dest == DEST_NETWORK)
190 ++endp->in_bts;
191 else
192 ++endp->in_remote;
193
194 /* For loop toggle the destination and then dispatch. */
195 if (cfg->audio_loop)
196 dest = !dest;
197
198 if (dest == DEST_NETWORK) {
199 if (proto == PROTO_RTP)
200 patch_payload(endp->net_payload_type, buf, rc);
201 return udp_send(fd->fd, &endp->remote,
202 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
203 buf, rc);
204 } else {
205 if (proto == PROTO_RTP)
206 patch_payload(endp->bts_payload_type, buf, rc);
207 return udp_send(fd->fd, &endp->bts,
208 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
209 buf, rc);
210 }
211}
212
213static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
214{
215 struct sockaddr_in addr;
216 int on = 1;
217
218 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
219 if (fd->fd < 0) {
220 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
221 return -1;
222 }
223
224 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
225 memset(&addr, 0, sizeof(addr));
226 addr.sin_family = AF_INET;
227 addr.sin_port = htons(port);
228 inet_aton(source_addr, &addr.sin_addr);
229
230 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
231 return -1;
232 }
233
234 return 0;
235}
236
237static int set_ip_tos(int fd, int tos)
238{
239 int ret;
240 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
241 &tos, sizeof(tos));
242 return ret != 0;
243}
244
245static int bind_rtp(struct mgcp_endpoint *endp)
246{
247 struct mgcp_config *cfg = endp->cfg;
248
249 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
250 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
251 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
252 goto cleanup0;
253 }
254
255 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
256 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
257 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
258 goto cleanup1;
259 }
260
261 set_ip_tos(endp->local_rtp.fd, cfg->endp_dscp);
262 set_ip_tos(endp->local_rtcp.fd, cfg->endp_dscp);
263
264 endp->local_rtp.cb = rtp_data_cb;
265 endp->local_rtp.data = endp;
266 endp->local_rtp.when = BSC_FD_READ;
267 if (bsc_register_fd(&endp->local_rtp) != 0) {
268 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
269 endp->rtp_port, ENDPOINT_NUMBER(endp));
270 goto cleanup2;
271 }
272
273 endp->local_rtcp.cb = rtp_data_cb;
274 endp->local_rtcp.data = endp;
275 endp->local_rtcp.when = BSC_FD_READ;
276 if (bsc_register_fd(&endp->local_rtcp) != 0) {
277 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
278 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
279 goto cleanup3;
280 }
281
282 return 0;
283
284cleanup3:
285 bsc_unregister_fd(&endp->local_rtp);
286cleanup2:
287 close(endp->local_rtcp.fd);
288 endp->local_rtcp.fd = -1;
289cleanup1:
290 close(endp->local_rtp.fd);
291 endp->local_rtp.fd = -1;
292cleanup0:
293 return -1;
294}
295
296int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
297{
298 endp->rtp_port = rtp_port;
299 return bind_rtp(endp);
300}