]>
git.saurik.com Git - apple/network_cmds.git/blob - alias/alias_irc.c
910e9343404ac302c68055caabc9dea55b6ec08a
1 /* Alias_irc.c intercepts packages contain IRC CTCP commands, and
2 changes DCC commands to export a port on the aliasing host instead
5 For this routine to work, the DCC command must fit entirely into a
6 single TCP packet. This will usually happen, but is not
9 The interception is likely to change the length of the packet.
10 The handling of this is copied more-or-less verbatim from
13 This software is placed into the public domain with no restrictions
16 Initial version: Eivind Eklund <perhaps@yes.no> (ee) 97-01-29
18 Version 2.1: May, 1997 (cjm)
19 Very minor changes to conform with
20 local/global/function naming conventions
21 withing the packet alising module.
28 #include <sys/types.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/in.h>
31 #include <netinet/ip.h>
32 #include <netinet/tcp.h>
35 #include "alias_local.h"
42 AliasHandleIrcOut(struct ip
*pip
, /* IP packet to examine */
43 struct alias_link
*link
, /* Which link are we on? */
44 int maxsize
/* Maximum size of IP packet including headers */
48 struct in_addr true_addr
;
52 int i
; /* Iterator through the source */
54 /* Calculate data length of TCP packet */
55 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
56 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
57 tlen
= ntohs(pip
->ip_len
);
60 /* Return if data length is too short - assume an entire PRIVMSG in each packet. */
61 if (dlen
<sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a")-1)
64 /* Place string pointer at beginning of data */
67 maxsize
-= hlen
; /* We're interested in maximum size of data, not packet */
69 /* Search for a CTCP command [Note 1] */
70 for( i
=0; i
<dlen
; i
++ ) {
74 return; /* No CTCP commands in */
75 /* Handle CTCP commands - the buffer may have to be copied */
78 char newpacket
[65536]; /* Estimate of maximum packet size :) */
79 int copyat
= i
; /* Same */
80 int iCopy
= 0; /* How much data have we written to copy-back string? */
81 unsigned long org_addr
; /* Original IP address */
82 unsigned short org_port
; /* Original source port address */
84 if( i
>= dlen
|| iCopy
>= sizeof(newpacket
) )
86 newpacket
[iCopy
++] = sptr
[i
++]; /* Copy the CTCP start character */
88 if( i
+4 >= dlen
) /* Too short for DCC */
90 if( sptr
[i
+0] != 'D' )
92 if( sptr
[i
+1] != 'C' )
94 if( sptr
[i
+2] != 'C' )
96 if( sptr
[i
+3] != ' ' )
98 /* We have a DCC command - handle it! */
99 i
+= 4; /* Skip "DCC " */
100 if( iCopy
+4 > sizeof(newpacket
) )
102 newpacket
[iCopy
++] = 'D';
103 newpacket
[iCopy
++] = 'C';
104 newpacket
[iCopy
++] = 'C';
105 newpacket
[iCopy
++] = ' ';
107 DBprintf(("Found DCC\n"));
108 /* Skip any extra spaces (should not occur according to
109 protocol, but DCC breaks CTCP protocol anyway */
110 while(sptr
[i
] == ' ') {
112 DBprintf(("DCC packet terminated in just spaces\n"));
117 DBprintf(("Transferring command...\n"));
118 while(sptr
[i
] != ' ') {
119 newpacket
[iCopy
++] = sptr
[i
];
120 if( ++i
>= dlen
|| iCopy
>= sizeof(newpacket
) ) {
121 DBprintf(("DCC packet terminated during command\n"));
125 /* Copy _one_ space */
126 if( i
+1 < dlen
&& iCopy
< sizeof(newpacket
) )
127 newpacket
[iCopy
++] = sptr
[i
++];
129 DBprintf(("Done command - removing spaces\n"));
130 /* Skip any extra spaces (should not occur according to
131 protocol, but DCC breaks CTCP protocol anyway */
132 while(sptr
[i
] == ' ') {
134 DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
139 DBprintf(("Transferring filename...\n"));
140 while(sptr
[i
] != ' ') {
141 newpacket
[iCopy
++] = sptr
[i
];
142 if( ++i
>= dlen
|| iCopy
>= sizeof(newpacket
) ) {
143 DBprintf(("DCC packet terminated during filename\n"));
147 /* Copy _one_ space */
148 if( i
+1 < dlen
&& iCopy
< sizeof(newpacket
) )
149 newpacket
[iCopy
++] = sptr
[i
++];
151 DBprintf(("Done filename - removing spaces\n"));
152 /* Skip any extra spaces (should not occur according to
153 protocol, but DCC breaks CTCP protocol anyway */
154 while(sptr
[i
] == ' ') {
156 DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
161 DBprintf(("Fetching IP address\n"));
162 /* Fetch IP address */
164 while(i
<dlen
&& isdigit(sptr
[i
])) {
165 if( org_addr
> ULONG_MAX
/10UL ) { /* Terminate on overflow */
166 DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr
, sptr
[i
]));
170 org_addr
+= sptr
[i
++]-'0';
172 DBprintf(("Skipping space\n"));
173 if( i
+1 >= dlen
|| sptr
[i
] != ' ' ) {
174 DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i
+1, dlen
, sptr
[i
]));
177 /* Skip any extra spaces (should not occur according to
178 protocol, but DCC breaks CTCP protocol anyway, so we might
179 as well play it safe */
180 while(sptr
[i
] == ' ') {
182 DBprintf(("Packet failure - space overflow.\n"));
186 DBprintf(("Fetching port number\n"));
187 /* Fetch source port */
189 while(i
<dlen
&& isdigit(sptr
[i
])) {
190 if( org_port
> 6554 ) { /* Terminate on overflow (65536/10 rounded up*/
191 DBprintf(("DCC: port number overflow\n"));
195 org_port
+= sptr
[i
++]-'0';
197 /* Skip illegal addresses (or early termination) */
198 if( i
>= dlen
|| (sptr
[i
] != '\001' && sptr
[i
] != ' ') ) {
199 DBprintf(("Bad port termination\n"));
202 DBprintf(("Got IP %lu and port %u\n", org_addr
, (unsigned)org_port
));
204 /* We've got the address and port - now alias it */
206 struct alias_link
*dcc_link
;
207 struct in_addr destaddr
;
210 true_port
= htons(org_port
);
211 true_addr
.s_addr
= htonl(org_addr
);
214 /* Steal the FTP_DATA_PORT - it doesn't really matter, and this
215 would probably allow it through at least _some_
217 dcc_link
= FindUdpTcpOut (true_addr
,
221 DBprintf(("Got a DCC link\n"));
223 struct in_addr alias_address
; /* Address from aliasing */
224 u_short alias_port
; /* Port given by aliasing */
227 /* Generate firewall hole as appropriate */
228 PunchFWHole(dcc_link
);
231 alias_address
= GetAliasAddress(link
);
232 iCopy
+= snprintf(&newpacket
[iCopy
],
233 sizeof(newpacket
)-iCopy
,
234 "%lu ", (u_long
)htonl(alias_address
.s_addr
));
235 if( iCopy
>= sizeof(newpacket
) ) { /* Truncated/fit exactly - bad news */
236 DBprintf(("DCC constructed packet overflow.\n"));
239 alias_port
= GetAliasPort(dcc_link
);
240 iCopy
+= snprintf(&newpacket
[iCopy
],
241 sizeof(newpacket
)-iCopy
,
242 "%u", htons(alias_port
) );
243 /* Done - truncated cases will be taken care of by lBAD_CTCP */
244 DBprintf(("Aliased IP %lu and port %u\n", alias_address
.s_addr
, (unsigned)alias_port
));
247 /* An uninteresting CTCP - state entered right after '\001' has
248 been pushed. Also used to copy the rest of a DCC, after IP
249 address and port has been handled */
251 for(; i
<dlen
&& iCopy
<sizeof(newpacket
); i
++,iCopy
++) {
252 newpacket
[iCopy
] = sptr
[i
]; /* Copy CTCP unchanged */
253 if(sptr
[i
] == '\001') {
260 for(; i
<dlen
&& iCopy
<sizeof(newpacket
); i
++,iCopy
++) {
261 newpacket
[iCopy
] = sptr
[i
]; /* Copy CTCP unchanged */
262 if(sptr
[i
] == '\001') {
266 /* Handle the end of a packet */
268 iCopy
= iCopy
> maxsize
-copyat
? maxsize
-copyat
: iCopy
;
269 memcpy(sptr
+copyat
, newpacket
, iCopy
);
271 /* Save information regarding modified seq and ack numbers */
275 SetAckModified(link
);
276 delta
= GetDeltaSeqOut(pip
, link
);
277 AddSeq(pip
, link
, delta
+copyat
+iCopy
-dlen
);
280 /* Revise IP header */
284 new_len
= htons(hlen
+ iCopy
+ copyat
);
285 DifferentialChecksum(&pip
->ip_sum
,
289 pip
->ip_len
= new_len
;
292 /* Compute TCP checksum for revised packet */
294 tc
->th_sum
= TcpChecksum(pip
);
301 The initial search will most often fail; it could be replaced with a 32-bit specific search.
302 Such a search would be done for 32-bit unsigned value V:
303 V ^= 0x01010101; (Search is for null bytes)
304 if( ((V-0x01010101)^V) & 0x80808080 ) {
305 (found a null bytes which was a 01 byte)
307 To assert that the processor is 32-bits, do
308 extern int ircdccar[32]; (32 bits)
309 extern int ircdccar[CHAR_BIT*sizeof(unsigned int)];
310 which will generate a type-error on all but 32-bit machines.
312 [Note 2] This routine really ought to be replaced with one that
313 creates a transparent proxy on the aliasing host, to allow arbitary
314 changes in the TCP stream. This should not be too difficult given
315 this base; I (ee) will try to do this some time later.