2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Copyright (c) 2000 Whistle Communications, Inc.
33 * All rights reserved.
35 * Subject to the following obligations and disclaimer of warranty, use and
36 * redistribution of this software, in source or object code forms, with or
37 * without modifications are expressly permitted by Whistle Communications;
38 * provided, however, that:
39 * 1. Any and all reproductions of the source or object code must include the
40 * copyright notice above and the following disclaimer of warranties; and
41 * 2. No rights are granted, in any manner or form, to use Whistle
42 * Communications, Inc. trademarks, including the mark "WHISTLE
43 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
44 * such appears in the above copyright notice or in the software.
46 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
47 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
48 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
49 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
50 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
51 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
52 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
53 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
54 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
55 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
56 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
57 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
58 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
64 * Copyright (c) 2000 Junichi SATOH <junichi@astec.co.jp>
65 * <junichi@junichi.org>
66 * All rights reserved.
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
77 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * Authors: Erik Salander <erik@whistle.com>
90 * Junichi SATOH <junichi@astec.co.jp>
91 * <junichi@junichi.org>
94 * $FreeBSD: src/lib/libalias/alias_smedia.c,v 1.1.2.4 2001/03/05 03:48:00 kris Exp $
98 Alias_smedia.c is meant to contain the aliasing code for streaming media
99 protocols. It performs special processing for RSTP sessions under TCP.
100 Specifically, when a SETUP request is sent by a client, or a 200 reply
101 is sent by a server, it is intercepted and modified. The address is
102 changed to the gateway machine and an aliasing port is used.
104 More specifically, the "client_port" configuration parameter is
105 parsed for SETUP requests. The "server_port" configuration parameter is
106 parsed for 200 replies eminating from a server. This is intended to handle
109 RTSP also allows a redirection of a stream to another client by using the
110 "destination" configuration parameter. The destination config parm would
111 indicate a different IP address. This function is NOT supported by the
112 RTSP translation code below.
114 The RTSP multicast functions without any address translation intervention.
116 For this routine to work, the SETUP/200 must fit entirely
117 into a single TCP packet. This is typically the case, but exceptions
118 can easily be envisioned under the actual specifications.
120 Probably the most troubling aspect of the approach taken here is
121 that the new SETUP/200 will typically be a different length, and
122 this causes a certain amount of bookkeeping to keep track of the
123 changes of sequence and acknowledgment numbers, since the client
124 machine is totally unaware of the modification to the TCP stream.
126 Initial version: May, 2000 (eds)
132 #include <sys/types.h>
133 #include <netinet/in_systm.h>
134 #include <netinet/in.h>
135 #include <netinet/ip.h>
136 #include <netinet/tcp.h>
137 #include <netinet/udp.h>
139 #include "alias_local.h"
141 #define RTSP_CONTROL_PORT_NUMBER_1 554
142 #define RTSP_CONTROL_PORT_NUMBER_2 7070
143 #define RTSP_PORT_GROUP 2
145 #define ISDIGIT(a) (((a) >= '0') && ((a) <= '9'))
148 search_string(char *data
, int dlen
, const char *search_str
)
153 search_str_len
= strlen(search_str
);
154 for (i
= 0; i
< dlen
- search_str_len
; i
++) {
155 for (j
= i
, k
= 0; j
< dlen
- search_str_len
; j
++, k
++) {
156 if (data
[j
] != search_str
[k
] &&
157 data
[j
] != search_str
[k
] - ('a' - 'A')) {
160 if (k
== search_str_len
- 1) {
169 alias_rtsp_out(struct ip
*pip
,
170 struct alias_link
*link
,
172 const char *port_str
)
174 int hlen
, tlen
, dlen
;
176 int i
, j
, pos
, state
, port_dlen
, new_dlen
, delta
;
177 u_short p
[2], new_len
;
178 u_short sport
, eport
, base_port
;
179 u_short salias
= 0, ealias
= 0, base_alias
= 0;
180 const char *transport_str
= "transport:";
181 char newdata
[2048], *port_data
, *port_newdata
, stemp
[80];
182 int links_created
= 0, pkt_updated
= 0;
183 struct alias_link
*rtsp_link
= NULL
;
184 struct in_addr null_addr
;
186 /* Calculate data length of TCP packet */
187 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
188 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
189 tlen
= ntohs(pip
->ip_len
);
192 if (dlen
> sizeof(newdata
)) {
194 fprintf(stderr
, "alias_rtsp_out: data len too big");
198 /* Find keyword, "Transport: " */
199 pos
= search_string(data
, dlen
, transport_str
);
202 fprintf(stderr
, "alias_rtsp_out: Transport not found");
206 if (pos
>= sizeof(newdata
)) {
208 fprintf(stderr
, "alias_rtsp_out: Transport too far");
212 port_data
= data
+ pos
;
213 port_dlen
= dlen
- pos
;
215 memcpy(newdata
, data
, pos
);
216 port_newdata
= newdata
+ pos
;
218 while (port_dlen
> strlen(port_str
)) {
219 /* Find keyword, appropriate port string */
220 pos
= search_string(port_data
, port_dlen
, port_str
);
221 if (pos
< 0 || port_data
+ pos
+ 1 > newdata
+ sizeof(newdata
)) {
223 fprintf(stderr
, "alias_rtsp_out: port_str too far\n");
228 memcpy (port_newdata
, port_data
, pos
+ 1);
229 port_newdata
+= (pos
+ 1);
234 for (i
= pos
; i
< port_dlen
; i
++) {
237 if (port_data
[i
] == '=') {
242 if (ISDIGIT(port_data
[i
])) {
243 p
[0] = p
[0] * 10 + port_data
[i
] - '0';
245 if (port_data
[i
] == ';') {
248 if (port_data
[i
] == '-') {
254 if (ISDIGIT(port_data
[i
])) {
255 p
[1] = p
[1] * 10 + port_data
[i
] - '0';
265 if (!links_created
) {
268 /* Find an even numbered port number base that
269 satisfies the contiguous number of ports we need */
270 null_addr
.s_addr
= 0;
271 if (0 == (salias
= FindNewPortGroup(null_addr
,
272 FindAliasAddress(pip
->ip_src
),
278 "PacketAlias/RTSP: Cannot find contiguous RTSP data ports\n");
282 base_alias
= ntohs(salias
);
283 for (j
= 0; j
< RTSP_PORT_GROUP
; j
++) {
284 /* Establish link to port found in RTSP packet */
285 rtsp_link
= FindRtspOut(GetOriginalAddress(link
), null_addr
,
286 htons(base_port
+ j
), htons(base_alias
+ j
),
288 if (rtsp_link
!= NULL
) {
290 /* Punch hole in firewall */
291 PunchFWHole(rtsp_link
);
296 "PacketAlias/RTSP: Cannot allocate RTSP data ports\n");
302 ealias
= htons(base_alias
+ (RTSP_PORT_GROUP
- 1));
305 if (salias
&& rtsp_link
) {
308 /* Copy into IP packet */
309 snprintf(stemp
, sizeof(stemp
), "%d", ntohs(salias
));
310 lentmp
= strlen(stemp
);
311 /* account for ending ';' */
312 if (port_newdata
+ lentmp
+ 1 > newdata
+ sizeof(newdata
)) {
314 fprintf(stderr
, "PacketAlias/RTSP: salias too far\n");
318 memcpy(port_newdata
, stemp
, lentmp
);
319 port_newdata
+= lentmp
;
322 snprintf(stemp
, sizeof(stemp
), "%d", ntohs(ealias
));
323 lentmp
= strlen(stemp
);
325 /* account for middle '-' and for ending ';' */
326 if (port_newdata
+ lentmp
+ 2 > newdata
+ sizeof(newdata
)) {
328 fprintf(stderr
, "PacketAlias/RTSP: ealias too far\n");
335 /* Copy into IP packet */
336 memcpy(port_newdata
, stemp
, lentmp
);
337 port_newdata
+= lentmp
;
357 fprintf(stderr
, "PacketAlias/RTSP: Packet not updated\n");
362 memcpy (port_newdata
, port_data
, port_dlen
);
363 port_newdata
+= port_dlen
;
364 *port_newdata
= '\0';
366 /* Create new packet */
367 new_dlen
= port_newdata
- newdata
;
368 memcpy (data
, newdata
, new_dlen
);
370 SetAckModified(link
);
371 delta
= GetDeltaSeqOut(pip
, link
);
372 AddSeq(pip
, link
, delta
+ new_dlen
- dlen
);
374 new_len
= htons(hlen
+ new_dlen
);
375 DifferentialChecksum(&pip
->ip_sum
,
379 pip
->ip_len
= new_len
;
382 tc
->th_sum
= TcpChecksum(pip
);
387 /* Support the protocol used by early versions of RealPlayer */
390 alias_pna_out(struct ip
*pip
,
391 struct alias_link
*link
,
395 struct alias_link
*pna_links
;
396 u_short msg_id
, msg_len
;
398 u_short alias_port
, port
;
403 while (work
+ 4 < data
+ dlen
) {
404 memcpy(&msg_id
, work
, 2);
406 memcpy(&msg_len
, work
, 2);
408 if (ntohs(msg_id
) == 0) {
412 if ((ntohs(msg_id
) == 1) || (ntohs(msg_id
) == 7)) {
413 memcpy(&port
, work
, 2);
414 pna_links
= FindUdpTcpOut(pip
->ip_src
, GetDestAddress(link
),
415 port
, 0, IPPROTO_UDP
, 1);
416 if (pna_links
!= NULL
) {
418 /* Punch hole in firewall */
419 PunchFWHole(pna_links
);
421 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
422 alias_port
= GetAliasPort(pna_links
);
423 memcpy(work
, &alias_port
, 2);
425 /* Compute TCP checksum for revised packet */
427 tc
->th_sum
= TcpChecksum(pip
);
430 work
+= ntohs(msg_len
);
437 AliasHandleRtspOut(struct ip
*pip
, struct alias_link
*link
, int maxpacketsize
)
439 int hlen
, tlen
, dlen
;
442 const char *setup
= "SETUP", *pna
= "PNA", *str200
= "200";
443 const char *okstr
= "OK", *client_port_str
= "client_port";
444 const char *server_port_str
= "server_port";
447 tc
= (struct tcphdr
*)((char *)pip
+ (pip
->ip_hl
<< 2));
448 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
449 tlen
= ntohs(pip
->ip_len
);
455 /* When aliasing a client, check for the SETUP request */
456 if ((ntohs(tc
->th_dport
) == RTSP_CONTROL_PORT_NUMBER_1
) ||
457 (ntohs(tc
->th_dport
) == RTSP_CONTROL_PORT_NUMBER_2
)) {
459 if (dlen
>= strlen(setup
)) {
460 if (memcmp(data
, setup
, strlen(setup
)) == 0) {
461 alias_rtsp_out(pip
, link
, data
, client_port_str
);
465 if (dlen
>= strlen(pna
)) {
466 if (memcmp(data
, pna
, strlen(pna
)) == 0) {
467 alias_pna_out(pip
, link
, data
, dlen
);
473 /* When aliasing a server, check for the 200 reply
474 Accomodate varying number of blanks between 200 & OK */
476 if (dlen
>= strlen(str200
)) {
478 for (parseOk
= 0, i
= 0;
479 i
<= dlen
- strlen(str200
);
481 if (memcmp(&data
[i
], str200
, strlen(str200
)) == 0) {
488 i
+= strlen(str200
); /* skip string found */
489 while(data
[i
] == ' ') /* skip blank(s) */
492 if ((dlen
- i
) >= strlen(okstr
)) {
494 if (memcmp(&data
[i
], okstr
, strlen(okstr
)) == 0)
495 alias_rtsp_out(pip
, link
, data
, server_port_str
);