2 * Copyright (c) 2001 Charles Mott <cmott@scientech.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 /* Alias_irc.c intercepts packages contain IRC CTCP commands, and
30 changes DCC commands to export a port on the aliasing host instead
33 For this routine to work, the DCC command must fit entirely into a
34 single TCP packet. This will usually happen, but is not
37 The interception is likely to change the length of the packet.
38 The handling of this is copied more-or-less verbatim from
41 This software is placed into the public domain with no restrictions
44 Initial version: Eivind Eklund <perhaps@yes.no> (ee) 97-01-29
46 Version 2.1: May, 1997 (cjm)
47 Very minor changes to conform with
48 local/global/function naming conventions
49 withing the packet alising module.
56 #include <sys/types.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
63 #include "alias_local.h"
70 AliasHandleIrcOut(struct ip *pip, /* IP packet to examine */
71 struct alias_link *link, /* Which link are we on? */
72 int maxsize /* Maximum size of IP packet including headers */
76 struct in_addr true_addr;
80 int i; /* Iterator through the source */
82 /* Calculate data length of TCP packet */
83 tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
84 hlen = (pip->ip_hl + tc->th_off) << 2;
85 tlen = ntohs(pip->ip_len);
88 /* Return if data length is too short - assume an entire PRIVMSG in each packet. */
89 if (dlen<sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a")-1)
92 /* Place string pointer at beginning of data */
95 maxsize -= hlen; /* We're interested in maximum size of data, not packet */
97 /* Search for a CTCP command [Note 1] */
98 for( i=0; i<dlen; i++ ) {
102 return; /* No CTCP commands in */
103 /* Handle CTCP commands - the buffer may have to be copied */
106 char newpacket[65536]; /* Estimate of maximum packet size :) */
107 int copyat = i; /* Same */
108 int iCopy = 0; /* How much data have we written to copy-back string? */
109 unsigned long org_addr; /* Original IP address */
110 unsigned short org_port; /* Original source port address */
112 if( i >= dlen || iCopy >= sizeof(newpacket) )
114 newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start character */
115 /* Start of a CTCP */
116 if( i+4 >= dlen ) /* Too short for DCC */
118 if( sptr[i+0] != 'D' )
120 if( sptr[i+1] != 'C' )
122 if( sptr[i+2] != 'C' )
124 if( sptr[i+3] != ' ' )
126 /* We have a DCC command - handle it! */
127 i+= 4; /* Skip "DCC " */
128 if( iCopy+4 > sizeof(newpacket) )
130 newpacket[iCopy++] = 'D';
131 newpacket[iCopy++] = 'C';
132 newpacket[iCopy++] = 'C';
133 newpacket[iCopy++] = ' ';
135 DBprintf(("Found DCC\n"));
136 /* Skip any extra spaces (should not occur according to
137 protocol, but DCC breaks CTCP protocol anyway */
138 while(sptr[i] == ' ') {
140 DBprintf(("DCC packet terminated in just spaces\n"));
145 DBprintf(("Transferring command...\n"));
146 while(sptr[i] != ' ') {
147 newpacket[iCopy++] = sptr[i];
148 if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
149 DBprintf(("DCC packet terminated during command\n"));
153 /* Copy _one_ space */
154 if( i+1 < dlen && iCopy < sizeof(newpacket) )
155 newpacket[iCopy++] = sptr[i++];
157 DBprintf(("Done command - removing spaces\n"));
158 /* Skip any extra spaces (should not occur according to
159 protocol, but DCC breaks CTCP protocol anyway */
160 while(sptr[i] == ' ') {
162 DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
167 DBprintf(("Transferring filename...\n"));
168 while(sptr[i] != ' ') {
169 newpacket[iCopy++] = sptr[i];
170 if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
171 DBprintf(("DCC packet terminated during filename\n"));
175 /* Copy _one_ space */
176 if( i+1 < dlen && iCopy < sizeof(newpacket) )
177 newpacket[iCopy++] = sptr[i++];
179 DBprintf(("Done filename - removing spaces\n"));
180 /* Skip any extra spaces (should not occur according to
181 protocol, but DCC breaks CTCP protocol anyway */
182 while(sptr[i] == ' ') {
184 DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
189 DBprintf(("Fetching IP address\n"));
190 /* Fetch IP address */
192 while(i<dlen && isdigit(sptr[i])) {
193 if( org_addr > ULONG_MAX/10UL ) { /* Terminate on overflow */
194 DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
198 org_addr += sptr[i++]-'0';
200 DBprintf(("Skipping space\n"));
201 if( i+1 >= dlen || sptr[i] != ' ' ) {
202 DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i+1, dlen, sptr[i]));
205 /* Skip any extra spaces (should not occur according to
206 protocol, but DCC breaks CTCP protocol anyway, so we might
207 as well play it safe */
208 while(sptr[i] == ' ') {
210 DBprintf(("Packet failure - space overflow.\n"));
214 DBprintf(("Fetching port number\n"));
215 /* Fetch source port */
217 while(i<dlen && isdigit(sptr[i])) {
218 if( org_port > 6554 ) { /* Terminate on overflow (65536/10 rounded up*/
219 DBprintf(("DCC: port number overflow\n"));
223 org_port += sptr[i++]-'0';
225 /* Skip illegal addresses (or early termination) */
226 if( i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ') ) {
227 DBprintf(("Bad port termination\n"));
230 DBprintf(("Got IP %lu and port %u\n", org_addr, (unsigned)org_port));
232 /* We've got the address and port - now alias it */
234 struct alias_link *dcc_link;
235 struct in_addr destaddr;
238 true_port = htons(org_port);
239 true_addr.s_addr = htonl(org_addr);
242 /* Steal the FTP_DATA_PORT - it doesn't really matter, and this
243 would probably allow it through at least _some_
245 dcc_link = FindUdpTcpOut(true_addr, destaddr,
248 DBprintf(("Got a DCC link\n"));
250 struct in_addr alias_address; /* Address from aliasing */
251 u_short alias_port; /* Port given by aliasing */
254 /* Generate firewall hole as appropriate */
255 PunchFWHole(dcc_link);
258 alias_address = GetAliasAddress(link);
259 iCopy += snprintf(&newpacket[iCopy],
260 sizeof(newpacket)-iCopy,
261 "%lu ", (u_long)htonl(alias_address.s_addr));
262 if( iCopy >= sizeof(newpacket) ) { /* Truncated/fit exactly - bad news */
263 DBprintf(("DCC constructed packet overflow.\n"));
266 alias_port = GetAliasPort(dcc_link);
267 iCopy += snprintf(&newpacket[iCopy],
268 sizeof(newpacket)-iCopy,
269 "%u", htons(alias_port) );
270 /* Done - truncated cases will be taken care of by lBAD_CTCP */
271 DBprintf(("Aliased IP %lu and port %u\n", alias_address.s_addr, (unsigned)alias_port));
274 /* An uninteresting CTCP - state entered right after '\001' has
275 been pushed. Also used to copy the rest of a DCC, after IP
276 address and port has been handled */
278 for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
279 newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
280 if(sptr[i] == '\001') {
287 for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
288 newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
289 if(sptr[i] == '\001') {
293 /* Handle the end of a packet */
295 iCopy = iCopy > maxsize-copyat ? maxsize-copyat : iCopy;
296 memcpy(sptr+copyat, newpacket, iCopy);
298 /* Save information regarding modified seq and ack numbers */
302 SetAckModified(link);
303 delta = GetDeltaSeqOut(pip, link);
304 AddSeq(pip, link, delta+copyat+iCopy-dlen);
307 /* Revise IP header */
311 new_len = htons(hlen + iCopy + copyat);
312 DifferentialChecksum(&pip->ip_sum,
316 pip->ip_len = new_len;
319 /* Compute TCP checksum for revised packet */
321 tc->th_sum = TcpChecksum(pip);
328 The initial search will most often fail; it could be replaced with a 32-bit specific search.
329 Such a search would be done for 32-bit unsigned value V:
330 V ^= 0x01010101; (Search is for null bytes)
331 if( ((V-0x01010101)^V) & 0x80808080 ) {
332 (found a null bytes which was a 01 byte)
334 To assert that the processor is 32-bits, do
335 extern int ircdccar[32]; (32 bits)
336 extern int ircdccar[CHAR_BIT*sizeof(unsigned int)];
337 which will generate a type-error on all but 32-bit machines.
339 [Note 2] This routine really ought to be replaced with one that
340 creates a transparent proxy on the aliasing host, to allow arbitary
341 changes in the TCP stream. This should not be too difficult given
342 this base; I (ee) will try to do this some time later.