]>
git.saurik.com Git - apple/network_cmds.git/blob - alias/alias_irc.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 2001 Charles Mott <cmott@scientech.com>
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * $FreeBSD: src/lib/libalias/alias_irc.c,v 1.5.2.4 2001/08/21 16:42:42 ru Exp $
51 /* Alias_irc.c intercepts packages contain IRC CTCP commands, and
52 changes DCC commands to export a port on the aliasing host instead
55 For this routine to work, the DCC command must fit entirely into a
56 single TCP packet. This will usually happen, but is not
59 The interception is likely to change the length of the packet.
60 The handling of this is copied more-or-less verbatim from
63 Initial version: Eivind Eklund <perhaps@yes.no> (ee) 97-01-29
65 Version 2.1: May, 1997 (cjm)
66 Very minor changes to conform with
67 local/global/function naming conventions
68 withing the packet alising module.
75 #include <sys/types.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in.h>
78 #include <netinet/ip.h>
79 #include <netinet/tcp.h>
82 #include "alias_local.h"
89 AliasHandleIrcOut(struct ip
*pip
, /* IP packet to examine */
90 struct alias_link
*link
, /* Which link are we on? */
91 int maxsize
/* Maximum size of IP packet including headers */
95 struct in_addr true_addr
;
99 int i
; /* Iterator through the source */
101 /* Calculate data length of TCP packet */
102 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
103 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
104 tlen
= ntohs(pip
->ip_len
);
107 /* Return if data length is too short - assume an entire PRIVMSG in each packet. */
108 if (dlen
<sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a")-1)
111 /* Place string pointer at beginning of data */
114 maxsize
-= hlen
; /* We're interested in maximum size of data, not packet */
116 /* Search for a CTCP command [Note 1] */
117 for( i
=0; i
<dlen
; i
++ ) {
121 return; /* No CTCP commands in */
122 /* Handle CTCP commands - the buffer may have to be copied */
125 char newpacket
[65536]; /* Estimate of maximum packet size :) */
126 int copyat
= i
; /* Same */
127 int iCopy
= 0; /* How much data have we written to copy-back string? */
128 in_addr_t org_addr
; /* Original IP address */
129 unsigned short org_port
; /* Original source port address */
131 if( i
>= dlen
|| iCopy
>= sizeof(newpacket
) )
133 newpacket
[iCopy
++] = sptr
[i
++]; /* Copy the CTCP start character */
134 /* Start of a CTCP */
135 if( i
+4 >= dlen
) /* Too short for DCC */
137 if( sptr
[i
+0] != 'D' )
139 if( sptr
[i
+1] != 'C' )
141 if( sptr
[i
+2] != 'C' )
143 if( sptr
[i
+3] != ' ' )
145 /* We have a DCC command - handle it! */
146 i
+= 4; /* Skip "DCC " */
147 if( iCopy
+4 > sizeof(newpacket
) )
149 newpacket
[iCopy
++] = 'D';
150 newpacket
[iCopy
++] = 'C';
151 newpacket
[iCopy
++] = 'C';
152 newpacket
[iCopy
++] = ' ';
154 DBprintf(("Found DCC\n"));
155 /* Skip any extra spaces (should not occur according to
156 protocol, but DCC breaks CTCP protocol anyway */
157 while(sptr
[i
] == ' ') {
159 DBprintf(("DCC packet terminated in just spaces\n"));
164 DBprintf(("Transferring command...\n"));
165 while(sptr
[i
] != ' ') {
166 newpacket
[iCopy
++] = sptr
[i
];
167 if( ++i
>= dlen
|| iCopy
>= sizeof(newpacket
) ) {
168 DBprintf(("DCC packet terminated during command\n"));
172 /* Copy _one_ space */
173 if( i
+1 < dlen
&& iCopy
< sizeof(newpacket
) )
174 newpacket
[iCopy
++] = sptr
[i
++];
176 DBprintf(("Done command - removing spaces\n"));
177 /* Skip any extra spaces (should not occur according to
178 protocol, but DCC breaks CTCP protocol anyway */
179 while(sptr
[i
] == ' ') {
181 DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
186 DBprintf(("Transferring filename...\n"));
187 while(sptr
[i
] != ' ') {
188 newpacket
[iCopy
++] = sptr
[i
];
189 if( ++i
>= dlen
|| iCopy
>= sizeof(newpacket
) ) {
190 DBprintf(("DCC packet terminated during filename\n"));
194 /* Copy _one_ space */
195 if( i
+1 < dlen
&& iCopy
< sizeof(newpacket
) )
196 newpacket
[iCopy
++] = sptr
[i
++];
198 DBprintf(("Done filename - removing spaces\n"));
199 /* Skip any extra spaces (should not occur according to
200 protocol, but DCC breaks CTCP protocol anyway */
201 while(sptr
[i
] == ' ') {
203 DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
208 DBprintf(("Fetching IP address\n"));
209 /* Fetch IP address */
211 while(i
<dlen
&& isdigit(sptr
[i
])) {
212 if( org_addr
> 0xFFFFFFFF/10UL ) { /* Terminate on overflow */
213 DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr
, sptr
[i
]));
217 org_addr
+= sptr
[i
++]-'0';
219 DBprintf(("Skipping space\n"));
220 if( i
+1 >= dlen
|| sptr
[i
] != ' ' ) {
221 DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i
+1, dlen
, sptr
[i
]));
224 /* Skip any extra spaces (should not occur according to
225 protocol, but DCC breaks CTCP protocol anyway, so we might
226 as well play it safe */
227 while(sptr
[i
] == ' ') {
229 DBprintf(("Packet failure - space overflow.\n"));
233 DBprintf(("Fetching port number\n"));
234 /* Fetch source port */
236 while(i
<dlen
&& isdigit(sptr
[i
])) {
237 if( org_port
> 6554 ) { /* Terminate on overflow (65536/10 rounded up*/
238 DBprintf(("DCC: port number overflow\n"));
242 org_port
+= sptr
[i
++]-'0';
244 /* Skip illegal addresses (or early termination) */
245 if( i
>= dlen
|| (sptr
[i
] != '\001' && sptr
[i
] != ' ') ) {
246 DBprintf(("Bad port termination\n"));
249 DBprintf(("Got IP %lu and port %u\n", org_addr
, (unsigned)org_port
));
251 /* We've got the address and port - now alias it */
253 struct alias_link
*dcc_link
;
254 struct in_addr destaddr
;
257 true_port
= htons(org_port
);
258 true_addr
.s_addr
= htonl(org_addr
);
261 /* Sanity/Security checking */
262 if (!org_addr
|| !org_port
||
263 pip
->ip_src
.s_addr
!= true_addr
.s_addr
||
264 org_port
< IPPORT_RESERVED
)
267 /* Steal the FTP_DATA_PORT - it doesn't really matter, and this
268 would probably allow it through at least _some_
270 dcc_link
= FindUdpTcpOut(true_addr
, destaddr
,
273 DBprintf(("Got a DCC link\n"));
275 struct in_addr alias_address
; /* Address from aliasing */
276 u_short alias_port
; /* Port given by aliasing */
279 /* Generate firewall hole as appropriate */
280 PunchFWHole(dcc_link
);
283 alias_address
= GetAliasAddress(link
);
284 iCopy
+= snprintf(&newpacket
[iCopy
],
285 sizeof(newpacket
)-iCopy
,
286 "%u ", (in_addr_t
)htonl(alias_address
.s_addr
));
287 if( iCopy
>= sizeof(newpacket
) ) { /* Truncated/fit exactly - bad news */
288 DBprintf(("DCC constructed packet overflow.\n"));
291 alias_port
= GetAliasPort(dcc_link
);
292 iCopy
+= snprintf(&newpacket
[iCopy
],
293 sizeof(newpacket
)-iCopy
,
294 "%u", htons(alias_port
) );
295 /* Done - truncated cases will be taken care of by lBAD_CTCP */
296 DBprintf(("Aliased IP %lu and port %u\n", alias_address
.s_addr
, (unsigned)alias_port
));
299 /* An uninteresting CTCP - state entered right after '\001' has
300 been pushed. Also used to copy the rest of a DCC, after IP
301 address and port has been handled */
303 for(; i
<dlen
&& iCopy
<sizeof(newpacket
); i
++,iCopy
++) {
304 newpacket
[iCopy
] = sptr
[i
]; /* Copy CTCP unchanged */
305 if(sptr
[i
] == '\001') {
312 for(; i
<dlen
&& iCopy
<sizeof(newpacket
); i
++,iCopy
++) {
313 newpacket
[iCopy
] = sptr
[i
]; /* Copy CTCP unchanged */
314 if(sptr
[i
] == '\001') {
318 /* Handle the end of a packet */
320 iCopy
= iCopy
> maxsize
-copyat
? maxsize
-copyat
: iCopy
;
321 memcpy(sptr
+copyat
, newpacket
, iCopy
);
323 /* Save information regarding modified seq and ack numbers */
327 SetAckModified(link
);
328 delta
= GetDeltaSeqOut(pip
, link
);
329 AddSeq(pip
, link
, delta
+copyat
+iCopy
-dlen
);
332 /* Revise IP header */
336 new_len
= htons(hlen
+ iCopy
+ copyat
);
337 DifferentialChecksum(&pip
->ip_sum
,
341 pip
->ip_len
= new_len
;
344 /* Compute TCP checksum for revised packet */
346 tc
->th_sum
= TcpChecksum(pip
);
353 The initial search will most often fail; it could be replaced with a 32-bit specific search.
354 Such a search would be done for 32-bit unsigned value V:
355 V ^= 0x01010101; (Search is for null bytes)
356 if( ((V-0x01010101)^V) & 0x80808080 ) {
357 (found a null bytes which was a 01 byte)
359 To assert that the processor is 32-bits, do
360 extern int ircdccar[32]; (32 bits)
361 extern int ircdccar[CHAR_BIT*sizeof(unsigned int)];
362 which will generate a type-error on all but 32-bit machines.
364 [Note 2] This routine really ought to be replaced with one that
365 creates a transparent proxy on the aliasing host, to allow arbitary
366 changes in the TCP stream. This should not be too difficult given
367 this base; I (ee) will try to do this some time later.