/* Alias_ftp.c performs special processing for FTP sessions under TCP. Specifically, when a PORT command from the client side is sent, it is intercepted and modified. The address is changed to the gateway machine and an aliasing port is used. For this routine to work, the PORT command must fit entirely into a single TCP packet. This is typically the case, but exceptions can easily be envisioned under the actual specifications. Probably the most troubling aspect of the approach taken here is that the new PORT command will typically be a different length, and this causes a certain amount of bookkeeping to keep track of the changes of sequence and acknowledgment numbers, since the client machine is totally unaware of the modification to the TCP stream. This software is placed into the public domain with no restrictions on its distribution. Initial version: August, 1996 (cjm) */ /* Includes */ #include #include #include #include #include #include #include #include /* Constants */ #define FTP_DATA_PORT_NUMBER 20 /* Prototypes */ #include "alias.p" void HandleFtpOut(pip, link) struct ip *pip; char *link; { int hlen, tlen, dlen; struct in_addr true_addr; u_short true_port; char *sptr; struct tcphdr *tc; /* Calculate data length of TCP packet */ tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)); hlen = (pip->ip_hl + tc->th_off) << 2; tlen = ntohs(pip->ip_len); dlen = tlen - hlen; /* Return is data length is too long or too short */ if (dlen<10 || dlen>80) return; /* Place string pointer and beginning of data */ sptr = (char *) pip; sptr += hlen; /* Parse through string using state diagram method */ { char ch, zero; int i, state; u_long a1, a2, a3, a4; u_short p1, p2; a1=0; a2=0; a3=0; a4=0; p1=0; p2=0; zero = '0'; state=-4; for (i=0; iip_hl << 2)); hlen = (pip->ip_hl + tc->th_off) << 2; tlen = ntohs(pip->ip_len); dlen = tlen - hlen; /* Create new PORT command */ { char stemp[80]; char *sptr; u_short alias_port; u_char *ptr; int a1, a2, a3, a4, p1, p2; struct in_addr aliasAddress; /* Decompose alias address into quad format */ aliasAddress = GetAliasAddress(); ptr = (char *) &aliasAddress; a1 = *ptr++; a2=*ptr++; a3=*ptr++; a4=*ptr; /* Decompose alias port into pair format */ alias_port = GetAliasPort(ftp_link); ptr = (char *) &alias_port; p1 = *ptr++; p2=*ptr; /* Generate command string */ snprintf(stemp, sizeof(stemp), "PORT %d,%d,%d,%d,%d,%d\r\n", a1,a2,a3,a4,p1,p2); /* Save string length for IP header modification */ slen = strlen(stemp); /* Copy into IP packet */ sptr = (char *) pip; sptr += hlen; strcpy(sptr, stemp); } /* Save information regarding modified seq and ack numbers */ { int delta; SetAckModified(link); delta = GetDeltaSeqOut(pip, link); AddSeq(pip, link, delta+slen-dlen); pip->ip_len = ntohs(hlen + slen); } /* Compute TCP checksum for revised packet */ tc->th_sum = 0; tc->th_sum = TcpChecksum(pip); } else { fprintf(stderr, "PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n"); } }