Run through indent(1) so I can read the code without getting a headache.

The result isn't quite knf, but it's knfer than the original, and far
more consistent.
This commit is contained in:
Dag-Erling Smørgrav 2004-03-16 21:30:41 +00:00
parent f02660b050
commit f0f93429cf
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=127094
28 changed files with 9898 additions and 10066 deletions

File diff suppressed because it is too large Load Diff

View File

@ -88,20 +88,21 @@ int PacketUnaliasOut(char *_ptr, int _maxpacketsize);
/* Port and address redirection functions. */
int PacketAliasAddServer(struct alias_link *_link,
int
PacketAliasAddServer(struct alias_link *_link,
struct in_addr _addr, unsigned short _port);
struct alias_link *
PacketAliasRedirectAddr(struct in_addr _src_addr,
PacketAliasRedirectAddr(struct in_addr _src_addr,
struct in_addr _alias_addr);
int PacketAliasRedirectDynamic(struct alias_link *_link);
void PacketAliasRedirectDelete(struct alias_link *_link);
struct alias_link *
PacketAliasRedirectPort(struct in_addr _src_addr,
PacketAliasRedirectPort(struct in_addr _src_addr,
unsigned short _src_port, struct in_addr _dst_addr,
unsigned short _dst_port, struct in_addr _alias_addr,
unsigned short _alias_port, unsigned char _proto);
struct alias_link *
PacketAliasRedirectProto(struct in_addr _src_addr,
PacketAliasRedirectProto(struct in_addr _src_addr,
struct in_addr _dst_addr, struct in_addr _alias_addr,
unsigned char _proto);
@ -131,26 +132,27 @@ unsigned int
void LibAliasUninit(struct libalias *);
/* Packet Handling functions. */
int LibAliasIn(struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
/* Port and address redirection functions. */
int LibAliasAddServer(struct libalias *, struct alias_link *_link,
int
LibAliasAddServer(struct libalias *, struct alias_link *_link,
struct in_addr _addr, unsigned short _port);
struct alias_link *
LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
struct in_addr _alias_addr);
int LibAliasRedirectDynamic(struct libalias *, struct alias_link *_link);
void LibAliasRedirectDelete(struct libalias *, struct alias_link *_link);
struct alias_link *
LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
unsigned short _src_port, struct in_addr _dst_addr,
unsigned short _dst_port, struct in_addr _alias_addr,
unsigned short _alias_port, unsigned char _proto);
struct alias_link *
LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
struct in_addr _dst_addr, struct in_addr _alias_addr,
unsigned char _proto);

View File

@ -56,14 +56,15 @@ struct cu_header {
struct oc_header {
u_int16_t client_count; /* Number of client info structs */
u_int32_t seq_no;
char user_name[20];
char reserved[4]; /* flags, version stuff, etc */
char user_name [20];
char reserved [4]; /* flags, version stuff, etc */
};
/* client info structures */
struct client_info {
u_int32_t address; /* Client address */
char reserved[8]; /* Flags, pruning bitfield, packet counts etc */
u_int32_t address;/* Client address */
char reserved [8]; /* Flags, pruning bitfield, packet
* counts etc */
};
void
@ -78,7 +79,7 @@ AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *li
cu = (struct cu_header *)(ud + 1);
if (cu->addr)
cu->addr = (u_int32_t)GetAliasAddress(link).s_addr;
cu->addr = (u_int32_t) GetAliasAddress(link).s_addr;
cu_link = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(link),
ud->uh_dport, 0, IPPROTO_UDP, 1);
@ -109,13 +110,13 @@ AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr origina
end = (char *)ud + ntohs(ud->uh_ulen);
if ((char *)oc <= end) {
if(cu->dest_addr)
cu->dest_addr = (u_int32_t)original_addr.s_addr;
if(ntohs(cu->data_type) == 101)
if (cu->dest_addr)
cu->dest_addr = (u_int32_t) original_addr.s_addr;
if (ntohs(cu->data_type) == 101)
/* Find and change our address */
for(i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
if(ci->address == (u_int32_t)alias_addr.s_addr) {
ci->address = (u_int32_t)original_addr.s_addr;
for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
if (ci->address == (u_int32_t) alias_addr.s_addr) {
ci->address = (u_int32_t) original_addr.s_addr;
break;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -102,10 +102,11 @@ static void NewFtpMessage(struct libalias *la, struct ip *, struct alias_link *,
void
AliasHandleFtpOut(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize /* The maximum size this packet can grow to (including headers) */)
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize /* The maximum size this packet can grow to
(including headers) */ )
{
int hlen, tlen, dlen, pflags;
char *sptr;
@ -113,13 +114,13 @@ int maxpacketsize /* The maximum size this packet can grow to (including header
int ftp_message_type;
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
/* Place string pointer and beginning of data */
sptr = (char *) pip;
sptr = (char *)pip;
sptr += hlen;
/*
@ -153,13 +154,13 @@ int maxpacketsize /* The maximum size this packet can grow to (including header
if (ftp_message_type != FTP_UNKNOWN_MESSAGE)
NewFtpMessage(la, pip, link, maxpacketsize, ftp_message_type);
}
/* Track the msgs which are CRLF term'd for PORT/PASV FW breach */
if (dlen) { /* only if there's data */
sptr = (char *) pip; /* start over at beginning */
tlen = ntohs(pip->ip_len); /* recalc tlen, pkt may have grown */
if (sptr[tlen-2] == '\r' && sptr[tlen-1] == '\n')
sptr = (char *)pip; /* start over at beginning */
tlen = ntohs(pip->ip_len); /* recalc tlen, pkt may
* have grown */
if (sptr[tlen - 2] == '\r' && sptr[tlen - 1] == '\n')
pflags &= ~WAIT_CRLF;
else
pflags |= WAIT_CRLF;
@ -187,24 +188,52 @@ ParseFtpPortCommand(struct libalias *la, char *sptr, int dlen)
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state) {
case -4: if (ch == 'P') state++; else return 0; break;
case -3: if (ch == 'O') state++; else return 0; break;
case -2: if (ch == 'R') state++; else return 0; break;
case -1: if (ch == 'T') state++; else return 0; break;
case -4:
if (ch == 'P')
state++;
else
return 0;
break;
case -3:
if (ch == 'O')
state++;
else
return 0;
break;
case -2:
if (ch == 'R')
state++;
else
return 0;
break;
case -1:
if (ch == 'T')
state++;
else
return 0;
break;
case 0:
if (isspace(ch))
break;
else
state++;
case 1: case 3: case 5: case 7: case 9: case 11:
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 2: case 4: case 6: case 8:
case 2:
case 4:
case 6:
case 8:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',') {
@ -213,7 +242,8 @@ ParseFtpPortCommand(struct libalias *la, char *sptr, int dlen)
} else
return 0;
break;
case 10: case 12:
case 10:
case 12:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',' || state == 12) {
@ -253,12 +283,31 @@ ParseFtpEprtCommand(struct libalias *la, char *sptr, int dlen)
state = -4;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -4: if (ch == 'E') state++; else return 0; break;
case -3: if (ch == 'P') state++; else return 0; break;
case -2: if (ch == 'R') state++; else return 0; break;
case -1: if (ch == 'T') state++; else return 0; break;
switch (state) {
case -4:
if (ch == 'E')
state++;
else
return 0;
break;
case -3:
if (ch == 'P')
state++;
else
return 0;
break;
case -2:
if (ch == 'R')
state++;
else
return 0;
break;
case -1:
if (ch == 'T')
state++;
else
return 0;
break;
case 0:
if (!isspace(ch)) {
@ -278,14 +327,20 @@ ParseFtpEprtCommand(struct libalias *la, char *sptr, int dlen)
else
return 0;
break;
case 3: case 5: case 7: case 9:
case 3:
case 5:
case 7:
case 9:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 4: case 6: case 8: case 10:
case 4:
case 6:
case 8:
case 10:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == '.' || state == 10) {
@ -340,24 +395,46 @@ ParseFtp227Reply(struct libalias *la, char *sptr, int dlen)
state = -3;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -3: if (ch == '2') state++; else return 0; break;
case -2: if (ch == '2') state++; else return 0; break;
case -1: if (ch == '7') state++; else return 0; break;
switch (state) {
case -3:
if (ch == '2')
state++;
else
return 0;
break;
case -2:
if (ch == '2')
state++;
else
return 0;
break;
case -1:
if (ch == '7')
state++;
else
return 0;
break;
case 0:
if (ch == '(')
state++;
break;
case 1: case 3: case 5: case 7: case 9: case 11:
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 2: case 4: case 6: case 8:
case 2:
case 4:
case 6:
case 8:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',') {
@ -366,7 +443,8 @@ ParseFtp227Reply(struct libalias *la, char *sptr, int dlen)
} else
return 0;
break;
case 10: case 12:
case 10:
case 12:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',' || (state == 12 && ch == ')')) {
@ -405,11 +483,25 @@ ParseFtp229Reply(struct libalias *la, char *sptr, int dlen)
state = -3;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -3: if (ch == '2') state++; else return 0; break;
case -2: if (ch == '2') state++; else return 0; break;
case -1: if (ch == '9') state++; else return 0; break;
switch (state) {
case -3:
if (ch == '2')
state++;
else
return 0;
break;
case -2:
if (ch == '2')
state++;
else
return 0;
break;
case -1:
if (ch == '9')
state++;
else
return 0;
break;
case 0:
if (ch == '(')
@ -419,7 +511,8 @@ ParseFtp229Reply(struct libalias *la, char *sptr, int dlen)
delim = ch;
state++;
break;
case 2: case 3:
case 2:
case 3:
if (ch == delim)
state++;
else
@ -475,8 +568,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
ftp_link = FindUdpTcpOut(la, la->true_addr, GetDestAddress(link),
htons(la->true_port), 0, IPPROTO_TCP, 1);
if (ftp_link != NULL)
{
if (ftp_link != NULL) {
int slen, hlen, tlen, dlen;
struct tcphdr *tc;
@ -486,7 +578,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
#endif
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
@ -502,34 +594,37 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
/* Decompose alias address into quad format */
alias_address = GetAliasAddress(link);
ptr = (u_char *) &alias_address.s_addr;
a1 = *ptr++; a2=*ptr++; a3=*ptr++; a4=*ptr;
ptr = (u_char *) & alias_address.s_addr;
a1 = *ptr++;
a2 = *ptr++;
a3 = *ptr++;
a4 = *ptr;
alias_port = GetAliasPort(ftp_link);
switch (ftp_message_type)
{
switch (ftp_message_type) {
case FTP_PORT_COMMAND:
case FTP_227_REPLY:
/* Decompose alias port into pair format. */
ptr = (char *) &alias_port;
p1 = *ptr++; p2=*ptr;
ptr = (char *)&alias_port;
p1 = *ptr++;
p2 = *ptr;
if (ftp_message_type == FTP_PORT_COMMAND) {
/* Generate PORT command string. */
sprintf(stemp, "PORT %d,%d,%d,%d,%d,%d\r\n",
a1,a2,a3,a4,p1,p2);
a1, a2, a3, a4, p1, p2);
} else {
/* Generate 227 reply string. */
sprintf(stemp,
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
a1,a2,a3,a4,p1,p2);
a1, a2, a3, a4, p1, p2);
}
break;
case FTP_EPRT_COMMAND:
/* Generate EPRT command string. */
sprintf(stemp, "EPRT |1|%d.%d.%d.%d|%d|\r\n",
a1,a2,a3,a4,ntohs(alias_port));
a1, a2, a3, a4, ntohs(alias_port));
break;
case FTP_229_REPLY:
/* Generate 229 reply string. */
@ -542,8 +637,9 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
slen = strlen(stemp);
/* Copy modified buffer into IP packet. */
sptr = (char *) pip; sptr += hlen;
strncpy(sptr, stemp, maxpacketsize-hlen);
sptr = (char *)pip;
sptr += hlen;
strncpy(sptr, stemp, maxpacketsize - hlen);
}
/* Save information regarding modified seq and ack numbers */
@ -552,7 +648,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+slen-dlen);
AddSeq(pip, link, delta + slen - dlen);
}
/* Revise IP header */
@ -570,9 +666,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
/* Compute TCP checksum for revised packet */
tc->th_sum = 0;
tc->th_sum = TcpChecksum(pip);
}
else
{
} else {
#ifdef DEBUG
fprintf(stderr,
"PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n");

View File

@ -68,8 +68,9 @@ void
AliasHandleIrcOut(struct libalias *la,
struct ip *pip, /* IP packet to examine */
struct alias_link *link, /* Which link are we on? */
int maxsize /* Maximum size of IP packet including headers */
)
int maxsize /* Maximum size of IP packet including
* headers */
)
{
int hlen, tlen, dlen;
struct in_addr true_addr;
@ -79,52 +80,61 @@ AliasHandleIrcOut(struct libalias *la,
int i; /* Iterator through the source */
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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 if data length is too short - assume an entire PRIVMSG in each packet. */
if (dlen<sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a")-1)
/*
* Return if data length is too short - assume an entire PRIVMSG in
* each packet.
*/
if (dlen < sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a") - 1)
return;
/* Place string pointer at beginning of data */
sptr = (char *) pip;
sptr = (char *)pip;
sptr += hlen;
maxsize -= hlen; /* We're interested in maximum size of data, not packet */
maxsize -= hlen; /* We're interested in maximum size of
* data, not packet */
/* Search for a CTCP command [Note 1] */
for( i=0; i<dlen; i++ ) {
if(sptr[i]=='\001')
for (i = 0; i < dlen; i++) {
if (sptr[i] == '\001')
goto lFOUND_CTCP;
}
return; /* No CTCP commands in */
/* Handle CTCP commands - the buffer may have to be copied */
lFOUND_CTCP:
{
char newpacket[65536]; /* Estimate of maximum packet size :) */
char newpacket[65536]; /* Estimate of maximum packet size
* :) */
int copyat = i; /* Same */
int iCopy = 0; /* How much data have we written to copy-back string? */
int iCopy = 0; /* How much data have we written to
* copy-back string? */
unsigned long org_addr; /* Original IP address */
unsigned short org_port; /* Original source port address */
lCTCP_START:
if( i >= dlen || iCopy >= sizeof(newpacket) )
unsigned short org_port; /* Original source port
* address */
lCTCP_START:
if (i >= dlen || iCopy >= sizeof(newpacket))
goto lPACKET_DONE;
newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start character */
newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start
* character */
/* Start of a CTCP */
if( i+4 >= dlen ) /* Too short for DCC */
if (i + 4 >= dlen) /* Too short for DCC */
goto lBAD_CTCP;
if( sptr[i+0] != 'D' )
if (sptr[i + 0] != 'D')
goto lBAD_CTCP;
if( sptr[i+1] != 'C' )
if (sptr[i + 1] != 'C')
goto lBAD_CTCP;
if( sptr[i+2] != 'C' )
if (sptr[i + 2] != 'C')
goto lBAD_CTCP;
if( sptr[i+3] != ' ' )
if (sptr[i + 3] != ' ')
goto lBAD_CTCP;
/* We have a DCC command - handle it! */
i+= 4; /* Skip "DCC " */
if( iCopy+4 > sizeof(newpacket) )
i += 4; /* Skip "DCC " */
if (iCopy + 4 > sizeof(newpacket))
goto lPACKET_DONE;
newpacket[iCopy++] = 'D';
newpacket[iCopy++] = 'C';
@ -132,54 +142,60 @@ AliasHandleIrcOut(struct libalias *la,
newpacket[iCopy++] = ' ';
DBprintf(("Found DCC\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces\n"));
goto lPACKET_DONE;
}
}
DBprintf(("Transferring command...\n"));
while(sptr[i] != ' ') {
while (sptr[i] != ' ') {
newpacket[iCopy++] = sptr[i];
if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
if (++i >= dlen || iCopy >= sizeof(newpacket)) {
DBprintf(("DCC packet terminated during command\n"));
goto lPACKET_DONE;
}
}
/* Copy _one_ space */
if( i+1 < dlen && iCopy < sizeof(newpacket) )
if (i + 1 < dlen && iCopy < sizeof(newpacket))
newpacket[iCopy++] = sptr[i++];
DBprintf(("Done command - removing spaces\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
goto lPACKET_DONE;
}
}
DBprintf(("Transferring filename...\n"));
while(sptr[i] != ' ') {
while (sptr[i] != ' ') {
newpacket[iCopy++] = sptr[i];
if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
if (++i >= dlen || iCopy >= sizeof(newpacket)) {
DBprintf(("DCC packet terminated during filename\n"));
goto lPACKET_DONE;
}
}
/* Copy _one_ space */
if( i+1 < dlen && iCopy < sizeof(newpacket) )
if (i + 1 < dlen && iCopy < sizeof(newpacket))
newpacket[iCopy++] = sptr[i++];
DBprintf(("Done filename - removing spaces\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
goto lPACKET_DONE;
}
@ -188,24 +204,26 @@ AliasHandleIrcOut(struct libalias *la,
DBprintf(("Fetching IP address\n"));
/* Fetch IP address */
org_addr = 0;
while(i<dlen && isdigit(sptr[i])) {
if( org_addr > ULONG_MAX/10UL ) { /* Terminate on overflow */
while (i < dlen && isdigit(sptr[i])) {
if (org_addr > ULONG_MAX / 10UL) { /* Terminate on overflow */
DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
goto lBAD_CTCP;
}
org_addr *= 10;
org_addr += sptr[i++]-'0';
org_addr += sptr[i++] - '0';
}
DBprintf(("Skipping space\n"));
if( i+1 >= dlen || sptr[i] != ' ' ) {
DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i+1, dlen, sptr[i]));
if (i + 1 >= dlen || sptr[i] != ' ') {
DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i + 1, dlen, sptr[i]));
goto lBAD_CTCP;
}
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway, so we might
as well play it safe */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway, so we
* might as well play it safe
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("Packet failure - space overflow.\n"));
goto lPACKET_DONE;
}
@ -213,16 +231,17 @@ AliasHandleIrcOut(struct libalias *la,
DBprintf(("Fetching port number\n"));
/* Fetch source port */
org_port = 0;
while(i<dlen && isdigit(sptr[i])) {
if( org_port > 6554 ) { /* Terminate on overflow (65536/10 rounded up*/
while (i < dlen && isdigit(sptr[i])) {
if (org_port > 6554) { /* Terminate on overflow
* (65536/10 rounded up */
DBprintf(("DCC: port number overflow\n"));
goto lBAD_CTCP;
}
org_port *= 10;
org_port += sptr[i++]-'0';
org_port += sptr[i++] - '0';
}
/* Skip illegal addresses (or early termination) */
if( i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ') ) {
if (i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ')) {
DBprintf(("Bad port termination\n"));
goto lBAD_CTCP;
}
@ -244,16 +263,19 @@ AliasHandleIrcOut(struct libalias *la,
org_port < IPPORT_RESERVED)
goto lBAD_CTCP;
/* Steal the FTP_DATA_PORT - it doesn't really matter, and this
would probably allow it through at least _some_
firewalls. */
/*
* Steal the FTP_DATA_PORT - it doesn't really
* matter, and this would probably allow it through
* at least _some_ firewalls.
*/
dcc_link = FindUdpTcpOut(la, true_addr, destaddr,
true_port, 0,
IPPROTO_TCP, 1);
DBprintf(("Got a DCC link\n"));
if ( dcc_link ) {
if (dcc_link) {
struct in_addr alias_address; /* Address from aliasing */
u_short alias_port; /* Port given by aliasing */
u_short alias_port; /* Port given by
* aliasing */
int n;
#ifndef NO_FW_PUNCH
@ -263,52 +285,58 @@ AliasHandleIrcOut(struct libalias *la,
alias_address = GetAliasAddress(link);
n = snprintf(&newpacket[iCopy],
sizeof(newpacket)-iCopy,
"%lu ", (u_long)htonl(alias_address.s_addr));
if( n < 0 ) {
sizeof(newpacket) - iCopy,
"%lu ", (u_long) htonl(alias_address.s_addr));
if (n < 0) {
DBprintf(("DCC packet construct failure.\n"));
goto lBAD_CTCP;
}
if( (iCopy += n) >= sizeof(newpacket) ) { /* Truncated/fit exactly - bad news */
if ((iCopy += n) >= sizeof(newpacket)) { /* Truncated/fit exactly
* - bad news */
DBprintf(("DCC constructed packet overflow.\n"));
goto lBAD_CTCP;
}
alias_port = GetAliasPort(dcc_link);
n = snprintf(&newpacket[iCopy],
sizeof(newpacket)-iCopy,
"%u", htons(alias_port) );
if( n < 0 ) {
sizeof(newpacket) - iCopy,
"%u", htons(alias_port));
if (n < 0) {
DBprintf(("DCC packet construct failure.\n"));
goto lBAD_CTCP;
}
iCopy += n;
/* Done - truncated cases will be taken care of by lBAD_CTCP */
/*
* Done - truncated cases will be taken
* care of by lBAD_CTCP
*/
DBprintf(("Aliased IP %lu and port %u\n", alias_address.s_addr, (unsigned)alias_port));
}
}
/* An uninteresting CTCP - state entered right after '\001' has
been pushed. Also used to copy the rest of a DCC, after IP
address and port has been handled */
lBAD_CTCP:
for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
/*
* An uninteresting CTCP - state entered right after '\001'
* has been pushed. Also used to copy the rest of a DCC,
* after IP address and port has been handled
*/
lBAD_CTCP:
for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
if(sptr[i] == '\001') {
if (sptr[i] == '\001') {
goto lNORMAL_TEXT;
}
}
goto lPACKET_DONE;
/* Normal text */
lNORMAL_TEXT:
for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
lNORMAL_TEXT:
for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
if(sptr[i] == '\001') {
if (sptr[i] == '\001') {
goto lCTCP_START;
}
}
/* Handle the end of a packet */
lPACKET_DONE:
iCopy = iCopy > maxsize-copyat ? maxsize-copyat : iCopy;
memcpy(sptr+copyat, newpacket, iCopy);
lPACKET_DONE:
iCopy = iCopy > maxsize - copyat ? maxsize - copyat : iCopy;
memcpy(sptr + copyat, newpacket, iCopy);
/* Save information regarding modified seq and ack numbers */
{
@ -316,7 +344,7 @@ AliasHandleIrcOut(struct libalias *la,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+copyat+iCopy-dlen);
AddSeq(pip, link, delta + copyat + iCopy - dlen);
}
/* Revise IP header */

View File

@ -70,11 +70,11 @@ struct libalias {
struct in_addr nullAddress; /* Used as a dummy parameter for */
/* some function calls */
LIST_HEAD(, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
/* Lookup table of pointers to */
/* chains of link records. Each */
LIST_HEAD(, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
/* link record is doubly indexed */
/* into input and output lookup */
/* tables. */
@ -95,7 +95,8 @@ struct libalias {
int timeStamp; /* System time in seconds for */
/* current packet */
int lastCleanupTime; /* Last time IncrementalCleanup() */
int lastCleanupTime; /* Last time
* IncrementalCleanup() */
/* was called */
int houseKeepingResidual; /* used by HouseKeeping() */
@ -116,9 +117,12 @@ struct libalias {
/* PacketAliasSetMode on first */
/* setting the PKT_ALIAS_PUNCH_FW */
/* flag. */
int fireWallBaseNum; /* The first firewall entry free for our use */
int fireWallNumNums; /* How many entries can we use? */
int fireWallActiveNum; /* Which entry did we last use? */
int fireWallBaseNum; /* The first firewall entry
* free for our use */
int fireWallNumNums; /* How many entries can we
* use? */
int fireWallActiveNum; /* Which entry did we last
* use? */
char *fireWallField; /* bool array for entries */
#endif
@ -163,55 +167,56 @@ struct libalias {
/* General utilities */
u_short IpChecksum(struct ip *_pip);
u_short TcpChecksum(struct ip *_pip);
void DifferentialChecksum(u_short *_cksum, u_short *_new, u_short *_old,
void
DifferentialChecksum(u_short * _cksum, u_short * _new, u_short * _old,
int _n);
/* Internal data access */
struct alias_link *
FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _id_alias, int _create);
struct alias_link *
FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _id, int _create);
struct alias_link *
FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _ip_id);
struct alias_link *
FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _ip_id);
struct alias_link *
AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
struct alias_link *
FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
struct alias_link *
FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_char _proto);
struct alias_link *
FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_char _proto);
struct alias_link *
FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
struct alias_link *
FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _src_port, u_short _dst_port, u_char _proto, int _create);
struct alias_link *
AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _src_call_id);
struct alias_link *
FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
struct in_addr _dst_addr, u_int16_t _src_call_id);
struct alias_link *
FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _dst_call_id);
struct alias_link *
FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
struct in_addr _dst_addr, u_int16_t _dst_call_id);
struct alias_link *
FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _alias_call_id);
struct alias_link *
FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _src_port, u_short _alias_port, u_char _proto);
struct in_addr
FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
@ -219,7 +224,8 @@ struct in_addr
FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
/* External data access/modification */
int FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
int
FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _src_port, u_short _dst_port, u_short _port_count,
u_char _proto, u_char _align);
void GetFragmentAddr(struct alias_link *_link, struct in_addr *_src_addr);
@ -228,7 +234,7 @@ void GetFragmentPtr(struct alias_link *_link, char **_fptr);
void SetFragmentPtr(struct alias_link *_link, char *fptr);
void SetStateIn(struct alias_link *_link, int _state);
void SetStateOut(struct alias_link *_link, int _state);
int GetStateIn(struct alias_link *_link);
int GetStateIn (struct alias_link *_link);
int GetStateOut(struct alias_link *_link);
struct in_addr
GetOriginalAddress(struct alias_link *_link);
@ -250,14 +256,16 @@ void SetAckModified(struct alias_link *_link);
int GetAckModified(struct alias_link *_link);
int GetDeltaAckIn(struct ip *_pip, struct alias_link *_link);
int GetDeltaSeqOut(struct ip *_pip, struct alias_link *_link);
void AddSeq(struct ip *_pip, struct alias_link *_link, int _delta);
void SetExpire(struct alias_link *_link, int _expire);
void AddSeq (struct ip *_pip, struct alias_link *_link, int _delta);
void SetExpire (struct alias_link *_link, int _expire);
void ClearCheckNewLink(struct libalias *la);
void SetProtocolFlags(struct alias_link *_link, int _pflags);
int GetProtocolFlags(struct alias_link *_link);
void SetDestCallId(struct alias_link *_link, u_int16_t _cid);
#ifndef NO_FW_PUNCH
void PunchFWHole(struct alias_link *_link);
#endif
/* Housekeeping function */
@ -267,15 +275,18 @@ void HouseKeeping(struct libalias *);
/* lint -save -library Suppress flexelint warnings */
/* FTP routines */
void AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxpacketsize);
/* IRC routines */
void AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxsize);
/* RTSP routines */
void AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxpacketsize);
/* PPTP routines */
@ -285,11 +296,13 @@ int AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
int AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
/* NetBIOS routines */
int AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int
AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short _alias_port);
int AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short *_alias_port,
struct in_addr *_original_address, u_short *_original_port);
int
AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short * _alias_port,
struct in_addr *_original_address, u_short * _original_port);
/* CUSeeMe routines */
void AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_link);
@ -299,9 +312,11 @@ void AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr
void AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_link);
/* Transparent proxy routines */
int ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
u_short *_proxy_server_port);
void ProxyModify(struct libalias *la, struct alias_link *_link, struct ip *_pip,
int
ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
u_short * _proxy_server_port);
void
ProxyModify(struct libalias *la, struct alias_link *_link, struct ip *_pip,
int _maxpacketsize, int _proxy_type);
enum alias_tcp_state {

View File

@ -80,7 +80,7 @@ typedef struct {
#define OpRefresh 8
typedef struct {
u_short nametrid;
u_short dir:1, opcode:4, nmflags:7, rcode:4;
u_short dir: 1, opcode:4, nmflags:7, rcode:4;
u_short qdcount;
u_short ancount;
u_short nscount;
@ -96,7 +96,9 @@ typedef struct {
#ifdef DEBUG
static void PrintRcode( u_char rcode ) {
static void
PrintRcode(u_char rcode)
{
switch (rcode) {
case FMT_ERR:
@ -112,15 +114,18 @@ static void PrintRcode( u_char rcode ) {
case CFT_ERR:
printf("\nName in conflict error.\n");
default:
printf("\n?%c?=%0x\n", '?', rcode );
printf("\n?%c?=%0x\n", '?', rcode);
}
}
#endif
/* Handling Name field */
static u_char *AliasHandleName ( u_char *p, char *pmax ) {
static u_char *
AliasHandleName(u_char * p, char *pmax)
{
u_char *s;
u_char c;
@ -129,23 +134,23 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
/* Following length field */
if (p == NULL || (char *)p >= pmax)
return(NULL);
return (NULL);
if (*p & 0xc0 ) {
if (*p & 0xc0) {
p = p + 2;
if ((char *)p > pmax)
return(NULL);
return ((u_char *)p);
return (NULL);
return ((u_char *) p);
}
while ( ( *p & 0x3f) != 0x00 ) {
while ((*p & 0x3f) != 0x00) {
s = p + 1;
if ( *p == 0x20 )
if (*p == 0x20)
compress = 1;
else
compress = 0;
/* Get next length field */
p = (u_char *)(p + (*p & 0x3f) + 1);
p = (u_char *) (p + (*p & 0x3f) + 1);
if ((char *)p > pmax) {
p = NULL;
break;
@ -154,15 +159,15 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
printf(":");
#endif
while (s < p) {
if ( compress == 1 ) {
c = (u_char )(((((*s & 0x0f) << 4) | (*(s+1) & 0x0f)) - 0x11));
if (compress == 1) {
c = (u_char) (((((*s & 0x0f) << 4) | (*(s + 1) & 0x0f)) - 0x11));
#ifdef DEBUG
if (isprint( c ) )
printf("%c", c );
if (isprint(c))
printf("%c", c);
else
printf("<0x%02x>", c );
printf("<0x%02x>", c);
#endif
s +=2;
s += 2;
} else {
#ifdef DEBUG
printf("%c", *s);
@ -181,7 +186,7 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
p = NULL;
else
p++;
return ((u_char *)p);
return ((u_char *) p);
}
/*
@ -195,58 +200,61 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
#define DGM_POSITIVE_RES 0x15
#define DGM_NEGATIVE_RES 0x16
int AliasHandleUdpNbt(
int
AliasHandleUdpNbt(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link,
struct in_addr *alias_address,
u_short alias_port
) {
struct udphdr * uh;
)
{
struct udphdr *uh;
NbtDataHeader *ndh;
u_char *p = NULL;
char *pmax;
/* Calculate data length of UDP packet */
uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
pmax = (char *)uh + ntohs( uh->uh_ulen );
uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
pmax = (char *)uh + ntohs(uh->uh_ulen);
ndh = (NbtDataHeader *)((char *)uh + (sizeof (struct udphdr)));
ndh = (NbtDataHeader *) ((char *)uh + (sizeof(struct udphdr)));
if ((char *)(ndh + 1) > pmax)
return(-1);
return (-1);
#ifdef DEBUG
printf("\nType=%02x,", ndh->type );
printf("\nType=%02x,", ndh->type);
#endif
switch ( ndh->type ) {
switch (ndh->type) {
case DGM_DIRECT_UNIQ:
case DGM_DIRECT_GROUP:
case DGM_BROADCAST:
p = (u_char *)ndh + 14;
p = AliasHandleName ( p, pmax ); /* Source Name */
p = AliasHandleName ( p, pmax ); /* Destination Name */
p = (u_char *) ndh + 14;
p = AliasHandleName(p, pmax); /* Source Name */
p = AliasHandleName(p, pmax); /* Destination Name */
break;
case DGM_ERROR:
p = (u_char *)ndh + 11;
p = (u_char *) ndh + 11;
break;
case DGM_QUERY:
case DGM_POSITIVE_RES:
case DGM_NEGATIVE_RES:
p = (u_char *)ndh + 10;
p = AliasHandleName ( p, pmax ); /* Destination Name */
p = (u_char *) ndh + 10;
p = AliasHandleName(p, pmax); /* Destination Name */
break;
}
if (p == NULL || (char *)p > pmax)
p = NULL;
#ifdef DEBUG
printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port) );
printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
#endif
/* Doing an IP address and Port number Translation */
if ( uh->uh_sum != 0 ) {
if (uh->uh_sum != 0) {
int acc;
u_short *sptr;
acc = ndh->source_port;
acc -= alias_port;
sptr = (u_short *) &(ndh->source_ip);
sptr = (u_short *) & (ndh->source_ip);
acc += *sptr++;
acc += *sptr;
sptr = (u_short *) alias_address;
@ -257,11 +265,12 @@ int AliasHandleUdpNbt(
ndh->source_ip = *alias_address;
ndh->source_port = alias_port;
#ifdef DEBUG
printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port) );
printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
fflush(stdout);
#endif
return((p == NULL) ? -1 : 0);
return ((p == NULL) ? -1 : 0);
}
/* Question Section */
#define QS_TYPE_NB 0x0020
#define QS_TYPE_NBSTAT 0x0021
@ -274,29 +283,28 @@ typedef struct {
static u_char *
AliasHandleQuestion(
u_short count,
NBTNsQuestion *q,
NBTNsQuestion * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
while ( count != 0 ) {
while (count != 0) {
/* Name Filed */
q = (NBTNsQuestion *)AliasHandleName((u_char *)q, pmax);
q = (NBTNsQuestion *) AliasHandleName((u_char *) q, pmax);
if (q == NULL || (char *)(q + 1) > pmax) {
q = NULL;
break;
}
/* Type and Class filed */
switch ( ntohs(q->type) ) {
switch (ntohs(q->type)) {
case QS_TYPE_NB:
case QS_TYPE_NBSTAT:
q= q+1;
q = q + 1;
break;
default:
#ifdef DEBUG
printf("\nUnknown Type on Question %0x\n", ntohs(q->type) );
printf("\nUnknown Type on Question %0x\n", ntohs(q->type));
#endif
break;
}
@ -304,7 +312,7 @@ AliasHandleQuestion(
}
/* Set up to out of Question Section */
return ((u_char *)q);
return ((u_char *) q);
}
/* Resource Record */
@ -324,54 +332,53 @@ typedef struct {
#define SizeOfNsRNB 6
typedef struct {
u_short g:1, ont:2, resv:13;
u_short g: 1 , ont:2, resv:13;
struct in_addr addr;
} NBTNsRNB;
static u_char *
AliasHandleResourceNB(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsRNB *nb;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Check out a length */
bcount = ntohs(q->rdlen);
/* Forward to Resource NB position */
nb = (NBTNsRNB *)((u_char *)q + SizeOfNsResource);
nb = (NBTNsRNB *) ((u_char *) q + SizeOfNsResource);
/* Processing all in_addr array */
#ifdef DEBUG
printf("NB rec[%s", inet_ntoa(nbtarg->oldaddr));
printf("->%s, %dbytes] ",inet_ntoa(nbtarg->newaddr ), bcount);
printf("->%s, %dbytes] ", inet_ntoa(nbtarg->newaddr), bcount);
#endif
while ( nb != NULL && bcount != 0 ) {
while (nb != NULL && bcount != 0) {
if ((char *)(nb + 1) > pmax) {
nb = NULL;
break;
}
#ifdef DEBUG
printf("<%s>", inet_ntoa(nb->addr) );
printf("<%s>", inet_ntoa(nb->addr));
#endif
if (!bcmp(&nbtarg->oldaddr,&nb->addr, sizeof(struct in_addr) ) ) {
if ( *nbtarg->uh_sum != 0 ) {
if (!bcmp(&nbtarg->oldaddr, &nb->addr, sizeof(struct in_addr))) {
if (*nbtarg->uh_sum != 0) {
int acc;
u_short *sptr;
sptr = (u_short *) &(nb->addr);
sptr = (u_short *) & (nb->addr);
acc = *sptr++;
acc += *sptr;
sptr = (u_short *) &(nbtarg->newaddr);
sptr = (u_short *) & (nbtarg->newaddr);
acc -= *sptr++;
acc -= *sptr;
ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
}
nb->addr = nbtarg->newaddr;
#ifdef DEBUG
printf("O");
@ -382,14 +389,13 @@ AliasHandleResourceNB(
printf(".");
}
#endif
nb=(NBTNsRNB *)((u_char *)nb + SizeOfNsRNB);
nb = (NBTNsRNB *) ((u_char *) nb + SizeOfNsRNB);
bcount -= SizeOfNsRNB;
}
if (nb == NULL || (char *)(nb + 1) > pmax) {
nb = NULL;
}
return ((u_char *)nb);
return ((u_char *) nb);
}
#define SizeOfResourceA 6
@ -399,18 +405,18 @@ typedef struct {
static u_char *
AliasHandleResourceA(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceA *a;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource A position */
a = (NBTNsResourceA *)( (u_char *)q + sizeof(NBTNsResource) );
a = (NBTNsResourceA *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
@ -418,36 +424,35 @@ AliasHandleResourceA(
/* Processing all in_addr array */
#ifdef DEBUG
printf("Arec [%s", inet_ntoa(nbtarg->oldaddr));
printf("->%s]",inet_ntoa(nbtarg->newaddr ));
printf("->%s]", inet_ntoa(nbtarg->newaddr));
#endif
while ( bcount != 0 ) {
while (bcount != 0) {
if (a == NULL || (char *)(a + 1) > pmax)
return(NULL);
return (NULL);
#ifdef DEBUG
printf("..%s", inet_ntoa(a->addr) );
printf("..%s", inet_ntoa(a->addr));
#endif
if ( !bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr) ) ) {
if ( *nbtarg->uh_sum != 0 ) {
if (!bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr))) {
if (*nbtarg->uh_sum != 0) {
int acc;
u_short *sptr;
sptr = (u_short *) &(a->addr); /* Old */
sptr = (u_short *) & (a->addr); /* Old */
acc = *sptr++;
acc += *sptr;
sptr = (u_short *) &nbtarg->newaddr; /* New */
sptr = (u_short *) & nbtarg->newaddr; /* New */
acc -= *sptr++;
acc -= *sptr;
ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
}
a->addr = nbtarg->newaddr;
}
a++; /*XXXX*/
a++; /* XXXX */
bcount -= SizeOfResourceA;
}
if (a == NULL || (char *)(a + 1) > pmax)
a = NULL;
return ((u_char *)a);
return ((u_char *) a);
}
typedef struct {
@ -456,24 +461,24 @@ typedef struct {
static u_char *
AliasHandleResourceNULL(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNULL *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NULL position */
n = (NBTNsResourceNULL *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
/* Processing all in_addr array */
while ( bcount != 0 ) {
while (bcount != 0) {
if ((char *)(n + 1) > pmax) {
n = NULL;
break;
@ -484,34 +489,34 @@ AliasHandleResourceNULL(
if ((char *)(n + 1) > pmax)
n = NULL;
return ((u_char *)n);
return ((u_char *) n);
}
static u_char *
AliasHandleResourceNS(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNULL *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NULL position */
n = (NBTNsResourceNULL *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
/* Resource Record Name Filed */
q = (NBTNsResource *)AliasHandleName( (u_char *)n, pmax ); /* XXX */
q = (NBTNsResource *) AliasHandleName((u_char *) n, pmax); /* XXX */
if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
return(NULL);
if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
return (NULL);
else
return ((u_char *)n + bcount);
return ((u_char *) n + bcount);
}
typedef struct {
@ -520,78 +525,78 @@ typedef struct {
static u_char *
AliasHandleResourceNBSTAT(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNBSTAT *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NBSTAT position */
n = (NBTNsResourceNBSTAT *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNBSTAT *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
return(NULL);
if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
return (NULL);
else
return ((u_char *)n + bcount);
return ((u_char *) n + bcount);
}
static u_char *
AliasHandleResource(
u_short count,
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments
*nbtarg)
* nbtarg)
{
while ( count != 0 ) {
while (count != 0) {
/* Resource Record Name Filed */
q = (NBTNsResource *)AliasHandleName( (u_char *)q, pmax );
q = (NBTNsResource *) AliasHandleName((u_char *) q, pmax);
if (q == NULL || (char *)(q + 1) > pmax)
break;
#ifdef DEBUG
printf("type=%02x, count=%d\n", ntohs(q->type), count );
printf("type=%02x, count=%d\n", ntohs(q->type), count);
#endif
/* Type and Class filed */
switch ( ntohs(q->type) ) {
switch (ntohs(q->type)) {
case RR_TYPE_NB:
q = (NBTNsResource *)AliasHandleResourceNB(
q = (NBTNsResource *) AliasHandleResourceNB(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_A:
q = (NBTNsResource *)AliasHandleResourceA(
q = (NBTNsResource *) AliasHandleResourceA(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NS:
q = (NBTNsResource *)AliasHandleResourceNS(
q = (NBTNsResource *) AliasHandleResourceNS(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NULL:
q = (NBTNsResource *)AliasHandleResourceNULL(
q = (NBTNsResource *) AliasHandleResourceNULL(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NBSTAT:
q = (NBTNsResource *)AliasHandleResourceNBSTAT(
q = (NBTNsResource *) AliasHandleResourceNBSTAT(
q,
pmax,
nbtarg
@ -609,21 +614,22 @@ AliasHandleResource(
count--;
}
fflush(stdout);
return ((u_char *)q);
return ((u_char *) q);
}
int AliasHandleUdpNbtNS(
int
AliasHandleUdpNbtNS(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link,
struct in_addr *alias_address,
u_short *alias_port,
u_short * alias_port,
struct in_addr *original_address,
u_short *original_port )
u_short * original_port)
{
struct udphdr * uh;
NbtNSHeader * nsh;
u_char * p;
struct udphdr *uh;
NbtNSHeader *nsh;
u_char *p;
char *pmax;
NBTArguments nbtarg;
@ -634,19 +640,19 @@ int AliasHandleUdpNbtNS(
nbtarg.newport = *original_port;
/* Calculate data length of UDP packet */
uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
nbtarg.uh_sum = &(uh->uh_sum);
nsh = (NbtNSHeader *)((char *)uh + (sizeof(struct udphdr)));
p = (u_char *)(nsh + 1);
pmax = (char *)uh + ntohs( uh->uh_ulen );
nsh = (NbtNSHeader *) ((char *)uh + (sizeof(struct udphdr)));
p = (u_char *) (nsh + 1);
pmax = (char *)uh + ntohs(uh->uh_ulen);
if ((char *)(nsh + 1) > pmax)
return(-1);
return (-1);
#ifdef DEBUG
printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
", an=%04x, ns=%04x, ar=%04x, [%d]-->",
nsh->dir ? "Response": "Request",
nsh->dir ? "Response" : "Request",
nsh->nametrid,
nsh->opcode,
nsh->nmflags,
@ -655,50 +661,46 @@ int AliasHandleUdpNbtNS(
ntohs(nsh->ancount),
ntohs(nsh->nscount),
ntohs(nsh->arcount),
(u_char *)p -(u_char *)nsh
(u_char *) p - (u_char *) nsh
);
#endif
/* Question Entries */
if (ntohs(nsh->qdcount) !=0 ) {
if (ntohs(nsh->qdcount) != 0) {
p = AliasHandleQuestion(
ntohs(nsh->qdcount),
(NBTNsQuestion *)p,
(NBTNsQuestion *) p,
pmax,
&nbtarg
);
}
/* Answer Resource Records */
if (ntohs(nsh->ancount) !=0 ) {
if (ntohs(nsh->ancount) != 0) {
p = AliasHandleResource(
ntohs(nsh->ancount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
/* Authority Resource Recodrs */
if (ntohs(nsh->nscount) !=0 ) {
if (ntohs(nsh->nscount) != 0) {
p = AliasHandleResource(
ntohs(nsh->nscount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
/* Additional Resource Recodrs */
if (ntohs(nsh->arcount) !=0 ) {
if (ntohs(nsh->arcount) != 0) {
p = AliasHandleResource(
ntohs(nsh->arcount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
#ifdef DEBUG
PrintRcode(nsh->rcode);
#endif

View File

@ -80,14 +80,14 @@ __FBSDID("$FreeBSD$");
* PPTP definitions
*/
struct grehdr /* Enhanced GRE header. */
{
struct grehdr { /* Enhanced GRE header. */
u_int16_t gh_flags; /* Flags. */
u_int16_t gh_protocol; /* Protocol type. */
u_int16_t gh_length; /* Payload length. */
u_int16_t gh_call_id; /* Call ID. */
u_int32_t gh_seq_no; /* Sequence number (optional). */
u_int32_t gh_ack_no; /* Acknowledgment number (optional). */
u_int32_t gh_ack_no; /* Acknowledgment number
* (optional). */
};
typedef struct grehdr GreHdr;
@ -120,26 +120,26 @@ enum {
};
/* Message structures */
struct pptpMsgHead {
struct pptpMsgHead {
u_int16_t length; /* total length */
u_int16_t msgType; /* PPTP message type */
u_int16_t msgType;/* PPTP message type */
u_int32_t magic; /* magic cookie */
u_int16_t type; /* control message type */
u_int16_t resv0; /* reserved */
};
typedef struct pptpMsgHead *PptpMsgHead;
};
typedef struct pptpMsgHead *PptpMsgHead;
struct pptpCodes {
u_int8_t resCode; /* Result Code */
u_int8_t errCode; /* Error Code */
};
typedef struct pptpCodes *PptpCode;
struct pptpCodes {
u_int8_t resCode;/* Result Code */
u_int8_t errCode;/* Error Code */
};
typedef struct pptpCodes *PptpCode;
struct pptpCallIds {
struct pptpCallIds {
u_int16_t cid1; /* Call ID field #1 */
u_int16_t cid2; /* Call ID field #2 */
};
typedef struct pptpCallIds *PptpCallId;
};
typedef struct pptpCallIds *PptpCallId;
static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
@ -147,8 +147,8 @@ static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
void
AliasHandlePptpOut(struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link) /* The PPTP control link */
{
struct alias_link *link)
{ /* The PPTP control link */
struct alias_link *pptp_link;
PptpCallId cptr;
PptpCode codes;
@ -165,13 +165,19 @@ AliasHandlePptpOut(struct libalias *la,
case PPTP_OutCallReply:
case PPTP_InCallRequest:
case PPTP_InCallReply:
/* Establish PPTP link for address and Call ID found in control message. */
/*
* Establish PPTP link for address and Call ID found in
* control message.
*/
pptp_link = AddPptp(la, GetOriginalAddress(link), GetDestAddress(link),
GetAliasAddress(link), cptr->cid1);
break;
case PPTP_CallClearRequest:
case PPTP_CallDiscNotify:
/* Find PPTP link for address and Call ID found in control message. */
/*
* Find PPTP link for address and Call ID found in control
* message.
*/
pptp_link = FindPptpOutByCallId(la, GetOriginalAddress(link),
GetDestAddress(link),
cptr->cid1);
@ -187,16 +193,18 @@ AliasHandlePptpOut(struct libalias *la,
cptr->cid1 = GetAliasPort(pptp_link);
/* Compute TCP checksum for revised packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
accumulate -= cptr->cid1;
ADJUST_CHECKSUM(accumulate, tc->th_sum);
switch (ctl_type) {
case PPTP_OutCallReply:
case PPTP_InCallReply:
codes = (PptpCode)(cptr + 1);
if (codes->resCode == 1) /* Connection established, */
SetDestCallId(pptp_link, /* note the Peer's Call ID. */
codes = (PptpCode) (cptr + 1);
if (codes->resCode == 1) /* Connection
* established, */
SetDestCallId(pptp_link, /* note the Peer's Call
* ID. */
cptr->cid2);
else
SetExpire(pptp_link, 0); /* Connection refused. */
@ -211,8 +219,8 @@ AliasHandlePptpOut(struct libalias *la,
void
AliasHandlePptpIn(struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link) /* The PPTP control link */
{
struct alias_link *link)
{ /* The PPTP control link */
struct alias_link *pptp_link;
PptpCallId cptr;
u_int16_t *pcall_id;
@ -224,8 +232,7 @@ AliasHandlePptpIn(struct libalias *la,
return;
/* Modify certain PPTP messages */
switch (ctl_type)
{
switch (ctl_type) {
case PPTP_InCallConn:
case PPTP_WanErrorNotify:
case PPTP_SetLinkInfo:
@ -258,14 +265,15 @@ AliasHandlePptpIn(struct libalias *la,
*pcall_id = GetOriginalPort(pptp_link);
/* Compute TCP checksum for modified packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
accumulate -= *pcall_id;
ADJUST_CHECKSUM(accumulate, tc->th_sum);
if (ctl_type == PPTP_OutCallReply || ctl_type == PPTP_InCallReply) {
PptpCode codes = (PptpCode)(cptr + 1);
PptpCode codes = (PptpCode) (cptr + 1);
if (codes->resCode == 1) /* Connection established, */
if (codes->resCode == 1) /* Connection
* established, */
SetDestCallId(pptp_link, /* note the Call ID. */
cptr->cid1);
else
@ -275,24 +283,24 @@ AliasHandlePptpIn(struct libalias *la,
}
static PptpCallId
AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch */
{
AliasVerifyPptp(struct ip *pip, u_int16_t * ptype)
{ /* IP packet to examine/patch */
int hlen, tlen, dlen;
PptpMsgHead hptr;
struct tcphdr *tc;
/* Calculate some lengths */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
/* Verify data length */
if (dlen < (sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
return(NULL);
return (NULL);
/* Move up to PPTP message header */
hptr = (PptpMsgHead)(((char *) pip) + hlen);
hptr = (PptpMsgHead) (((char *)pip) + hlen);
/* Return the control message type */
*ptype = ntohs(hptr->type);
@ -300,7 +308,7 @@ AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch
/* Verify PPTP Control Message */
if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
(ntohl(hptr->magic) != PPTP_MAGIC))
return(NULL);
return (NULL);
/* Verify data length. */
if ((*ptype == PPTP_OutCallReply || *ptype == PPTP_InCallReply) &&
@ -308,7 +316,7 @@ AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch
sizeof(struct pptpCodes)))
return (NULL);
else
return (PptpCallId)(hptr + 1);
return (PptpCallId) (hptr + 1);
}
@ -318,10 +326,10 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
GreHdr *gr;
struct alias_link *link;
gr = (GreHdr *)((char *)pip + (pip->ip_hl << 2));
gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
/* Check GRE header bits. */
if ((ntohl(*((u_int32_t *)gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
return (-1);
link = FindPptpOutByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
@ -330,12 +338,11 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
/* Change source IP address. */
DifferentialChecksum(&pip->ip_sum,
(u_short *)&alias_addr,
(u_short *)&pip->ip_src,
(u_short *) & alias_addr,
(u_short *) & pip->ip_src,
2);
pip->ip_src = alias_addr;
}
return (0);
}
@ -346,10 +353,10 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
GreHdr *gr;
struct alias_link *link;
gr = (GreHdr *)((char *)pip + (pip->ip_hl << 2));
gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
/* Check GRE header bits. */
if ((ntohl(*((u_int32_t *)gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
return (-1);
link = FindPptpInByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
@ -361,11 +368,10 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
/* Restore original IP address. */
DifferentialChecksum(&pip->ip_sum,
(u_short *)&src_addr,
(u_short *)&pip->ip_dst,
(u_short *) & src_addr,
(u_short *) & pip->ip_dst,
2);
pip->ip_dst = src_addr;
}
return (0);
}

View File

@ -84,8 +84,7 @@ __FBSDID("$FreeBSD$");
* A linked list of arbitrary length, based on struct proxy_entry is
* used to store proxy rules.
*/
struct proxy_entry
{
struct proxy_entry {
struct libalias *la;
#define PROXY_TYPE_ENCODE_NONE 1
#define PROXY_TYPE_ENCODE_TCPSTREAM 2
@ -153,7 +152,7 @@ IpMask(int nbits, struct in_addr *mask)
return -1;
imask = 0;
for (i=0; i<nbits; i++)
for (i = 0; i < nbits; i++)
imask = (imask >> 1) + 0x80000000;
mask->s_addr = htonl(imask);
@ -175,8 +174,7 @@ IpPort(char *s, int proto, int *port)
int n;
n = sscanf(s, "%d", port);
if (n != 1)
{
if (n != 1) {
struct servent *se;
if (proto == IPPROTO_TCP)
@ -191,7 +189,6 @@ IpPort(char *s, int proto, int *port)
*port = (u_int) ntohs(se->s_port);
}
return 0;
}
@ -202,8 +199,7 @@ RuleAdd(struct libalias *la, struct proxy_entry *entry)
struct proxy_entry *ptr;
struct proxy_entry *ptr_last;
if (la->proxyList == NULL)
{
if (la->proxyList == NULL) {
la->proxyList = entry;
entry->last = NULL;
entry->next = NULL;
@ -214,19 +210,15 @@ RuleAdd(struct libalias *la, struct proxy_entry *entry)
rule_index = entry->rule_index;
ptr = la->proxyList;
ptr_last = NULL;
while (ptr != NULL)
{
if (ptr->rule_index >= rule_index)
{
if (ptr_last == NULL)
{
while (ptr != NULL) {
if (ptr->rule_index >= rule_index) {
if (ptr_last == NULL) {
entry->next = la->proxyList;
entry->last = NULL;
la->proxyList->last = entry;
la->proxyList = entry;
return;
}
ptr_last->next = entry;
ptr->last = entry;
entry->last = ptr->last;
@ -267,17 +259,14 @@ RuleNumberDelete(struct libalias *la, int rule_index)
err = -1;
ptr = la->proxyList;
while (ptr != NULL)
{
while (ptr != NULL) {
struct proxy_entry *ptr_next;
ptr_next = ptr->next;
if (ptr->rule_index == rule_index)
{
if (ptr->rule_index == rule_index) {
err = 0;
RuleDelete(ptr);
}
ptr = ptr_next;
}
@ -294,21 +283,20 @@ ProxyEncodeTcpStream(struct alias_link *link,
struct tcphdr *tc;
/* Compute pointer to tcp header */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
/* Don't modify if once already modified */
if (GetAckModified (link))
if (GetAckModified(link))
return;
/* Translate destination address and port to string form */
snprintf(buffer, sizeof(buffer) - 2, "[DEST %s %d]",
inet_ntoa(GetProxyAddress (link)), (u_int) ntohs(GetProxyPort (link)));
inet_ntoa(GetProxyAddress(link)), (u_int) ntohs(GetProxyPort(link)));
/* Pad string out to a multiple of two in length */
slen = strlen(buffer);
switch (slen % 2)
{
switch (slen % 2) {
case 0:
strcat(buffer, " \n");
slen += 2;
@ -329,14 +317,14 @@ ProxyEncodeTcpStream(struct alias_link *link,
u_char *p;
hlen = (pip->ip_hl + tc->th_off) << 2;
dlen = ntohs (pip->ip_len) - hlen;
dlen = ntohs(pip->ip_len) - hlen;
/* Modify first packet that has data in it */
if (dlen == 0)
return;
p = (char *) pip;
p = (char *)pip;
p += hlen;
memmove(p + slen, p, dlen);
@ -349,7 +337,7 @@ ProxyEncodeTcpStream(struct alias_link *link,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+slen);
AddSeq(pip, link, delta + slen);
}
/* Update IP header packet length and checksum */
@ -367,7 +355,7 @@ ProxyEncodeTcpStream(struct alias_link *link,
already changed. */
tc->th_sum = 0;
tc->th_sum = TcpChecksum (pip);
tc->th_sum = TcpChecksum(pip);
}
static void
@ -400,10 +388,10 @@ ProxyEncodeIpHeader(struct ip *pip,
option[0] = 0x64; /* class: 3 (reserved), option 4 */
option[1] = OPTION_LEN_BYTES;
memcpy(&option[2], (u_char *) &pip->ip_dst, 4);
memcpy(&option[2], (u_char *) & pip->ip_dst, 4);
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
memcpy(&option[6], (u_char *) &tc->th_sport, 2);
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
memcpy(&option[6], (u_char *) & tc->th_sport, 2);
memcpy(ptr, option, 8);
}
@ -416,7 +404,7 @@ ProxyEncodeIpHeader(struct ip *pip,
sptr = (u_short *) option;
accumulate = 0;
for (i=0; i<OPTION_LEN_INT16; i++)
for (i = 0; i < OPTION_LEN_INT16; i++)
accumulate -= *(sptr++);
sptr = (u_short *) pip;
@ -452,7 +440,7 @@ ProxyEncodeIpHeader(struct ip *pip,
int
ProxyCheck(struct libalias *la, struct ip *pip,
struct in_addr *proxy_server_addr,
u_short *proxy_server_port)
u_short * proxy_server_port)
{
u_short dst_port;
struct in_addr src_addr;
@ -461,19 +449,17 @@ ProxyCheck(struct libalias *la, struct ip *pip,
src_addr = pip->ip_src;
dst_addr = pip->ip_dst;
dst_port = ((struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)))
dst_port = ((struct tcphdr *)((char *)pip + (pip->ip_hl << 2)))
->th_dport;
ptr = la->proxyList;
while (ptr != NULL)
{
while (ptr != NULL) {
u_short proxy_port;
proxy_port = ptr->proxy_port;
if ((dst_port == proxy_port || proxy_port == 0)
&& pip->ip_p == ptr->proto
&& src_addr.s_addr != ptr->server_addr.s_addr)
{
&& src_addr.s_addr != ptr->server_addr.s_addr) {
struct in_addr src_addr_masked;
struct in_addr dst_addr_masked;
@ -481,8 +467,7 @@ ProxyCheck(struct libalias *la, struct ip *pip,
dst_addr_masked.s_addr = dst_addr.s_addr & ptr->dst_mask.s_addr;
if ((src_addr_masked.s_addr == ptr->src_addr.s_addr)
&& (dst_addr_masked.s_addr == ptr->dst_addr.s_addr))
{
&& (dst_addr_masked.s_addr == ptr->dst_addr.s_addr)) {
if ((*proxy_server_port = ptr->server_port) == 0)
*proxy_server_port = dst_port;
*proxy_server_addr = ptr->server_addr;
@ -501,8 +486,7 @@ ProxyModify(struct libalias *la, struct alias_link *link,
int maxpacketsize,
int proxy_type)
{
switch (proxy_type)
{
switch (proxy_type) {
case PROXY_TYPE_ENCODE_IPHDR:
ProxyEncodeIpHeader(pip, maxpacketsize);
break;
@ -571,7 +555,7 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
/* Convert to lower case */
len = strlen(buffer);
for (i=0; i<len; i++)
for (i = 0; i < len; i++)
buffer[i] = tolower((unsigned char)buffer[i]);
/* Set default proxy type */
@ -604,11 +588,9 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
state = STATE_READ_KEYWORD;
token = strsep(&res, " \t");
token_count = 0;
while (token != NULL)
{
while (token != NULL) {
token_count++;
switch (state)
{
switch (state) {
case STATE_READ_KEYWORD:
if (strcmp(token, "type") == 0)
state = STATE_READ_TYPE;
@ -657,14 +639,11 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
while (*p != ':' && *p != 0)
p++;
if (*p != ':')
{
if (*p != ':') {
err = IpAddr(token, &server_addr);
if (err)
return -1;
}
else
{
} else {
*p = ' ';
n = sscanf(token, "%s %s", s, str_server_port);
@ -725,15 +704,12 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
while (*p != '/' && *p != 0)
p++;
if (*p != '/')
{
if (*p != '/') {
IpMask(32, &mask);
err = IpAddr(token, &addr);
if (err)
return -1;
}
else
{
} else {
int nbits;
char s[sizeof(buffer)];
@ -751,13 +727,10 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
return -1;
}
if (state == STATE_READ_SRC)
{
if (state == STATE_READ_SRC) {
src_addr = addr;
src_mask = mask;
}
else
{
} else {
dst_addr = addr;
dst_mask = mask;
}
@ -788,29 +761,23 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
the string is parsed, because the prototype might not be designated
before the ports (which might be symbolic entries in /etc/services) */
if (strlen(str_port) != 0)
{
if (strlen(str_port) != 0) {
int err;
err = IpPort(str_port, proto, &proxy_port);
if (err)
return -1;
}
else
{
} else {
proxy_port = 0;
}
if (strlen(str_server_port) != 0)
{
if (strlen(str_server_port) != 0) {
int err;
err = IpPort(str_server_port, proto, &server_port);
if (err)
return -1;
}
else
{
} else {
server_port = 0;
}

View File

@ -88,7 +88,7 @@ struct skinny_header {
struct RegisterMessage {
u_int32_t msgId;
char devName[16];
char devName [16];
u_int32_t uid;
u_int32_t instance;
u_int32_t ipAddr;
@ -154,7 +154,10 @@ alias_skinny_startmedia(struct StartMediaTransmission *start_media,
dst.s_addr = start_media->remoteIpAddr;
src.s_addr = localIpAddr;
/* XXX I should probably handle in bound global translations as well. */
/*
* XXX I should probably handle in bound global translations as
* well.
*/
return 0;
}
@ -174,8 +177,8 @@ alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
static int
alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack,
struct ip * pip, struct tcphdr *tc,
struct alias_link *link, u_int32_t *localIpAddr,
struct ip *pip, struct tcphdr *tc,
struct alias_link *link, u_int32_t * localIpAddr,
ConvDirection direction)
{
struct in_addr null_addr;
@ -208,12 +211,12 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
int orig_len, skinny_hdr_len = sizeof(struct skinny_header);
ConvDirection direction;
tc = (struct tcphdr *) ((char *)pip + (pip->ip_hl << 2));
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;
sd = (struct skinny_header *) ((char *)pip + hlen);
sd = (struct skinny_header *)((char *)pip + hlen);
/*
* XXX This direction is reserved for future use. I still need to
@ -234,11 +237,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
orig_len = dlen;
/*
* Skinny packets can contain many messages. We need to loop through
* the packet using len to determine message boundaries. This comes
* into play big time with port messages being in the same packet as
* register messages. Also, open receive channel acks are
* usually buried in a pakcet some 400 bytes long.
* Skinny packets can contain many messages. We need to loop
* through the packet using len to determine message boundaries.
* This comes into play big time with port messages being in the
* same packet as register messages. Also, open receive channel
* acks are usually buried in a pakcet some 400 bytes long.
*/
while (dlen >= skinny_hdr_len) {
len = (sd->len);
@ -253,8 +256,7 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
return;
}
switch (msgId) {
case REG_MSG:
{
case REG_MSG: {
struct RegisterMessage *reg_mesg;
if (len < sizeof(struct RegisterMessage)) {
@ -264,17 +266,17 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
#endif
return;
}
reg_mesg = (struct RegisterMessage *) & sd->msgId;
reg_mesg = (struct RegisterMessage *)&sd->msgId;
#ifdef DEBUG
fprintf(stderr,
"PacketAlias/Skinny: Received a register message");
#endif
alias_skinny_reg_msg(reg_mesg, pip, tc, link, direction);
}
break;
case IP_PORT_MSG:
{
}
case IP_PORT_MSG: {
struct IpPortMessage *port_mesg;
if (len < sizeof(struct IpPortMessage)) {
#ifdef DEBUG
fprintf(stderr,
@ -286,12 +288,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr
"PacketAlias/Skinny: Received ipport message\n");
#endif
port_mesg = (struct IpPortMessage *) & sd->msgId;
port_mesg = (struct IpPortMessage *)&sd->msgId;
alias_skinny_port_msg(port_mesg, pip, tc, link, direction);
}
break;
case OPNRCVCH_ACK:
{
}
case OPNRCVCH_ACK: {
struct OpenReceiveChannelAck *opnrcvchn_ack;
if (len < sizeof(struct OpenReceiveChannelAck)) {
@ -305,12 +306,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr,
"PacketAlias/Skinny: Received open rcv channel msg\n");
#endif
opnrcvchn_ack = (struct OpenReceiveChannelAck *) & sd->msgId;
opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, link, &lip, direction);
}
break;
case START_MEDIATX:
{
}
case START_MEDIATX: {
struct StartMediaTransmission *startmedia_tx;
if (len < sizeof(struct StartMediaTransmission)) {
@ -324,15 +324,15 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr,
"PacketAlias/Skinny: Received start media trans msg\n");
#endif
startmedia_tx = (struct StartMediaTransmission *) & sd->msgId;
startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
alias_skinny_startmedia(startmedia_tx, pip, tc, link, lip, direction);
}
break;
}
default:
break;
}
/* Place the pointer at the next message in the packet. */
dlen -= len + (skinny_hdr_len - sizeof(msgId));
sd = (struct skinny_header *) (((char *)&sd->msgId) + len);
sd = (struct skinny_header *)(((char *)&sd->msgId) + len);
}
}

View File

@ -155,7 +155,7 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
struct in_addr null_addr;
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
@ -177,15 +177,14 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (pos < 0) {
break;
}
memcpy (port_newdata, port_data, pos + 1);
memcpy(port_newdata, port_data, pos + 1);
port_newdata += (pos + 1);
p[0] = p[1] = 0;
sport = eport = 0;
state = 0;
for (i = pos; i < port_dlen; i++) {
switch(state) {
switch (state) {
case 0:
if (port_data[i] == '=') {
state++;
@ -218,8 +217,12 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (!links_created) {
links_created = 1;
/* Find an even numbered port number base that
satisfies the contiguous number of ports we need */
/*
* Find an even numbered port
* number base that satisfies the
* contiguous number of ports we
* need
*/
null_addr.s_addr = 0;
if (0 == (salias = FindNewPortGroup(la, null_addr,
FindAliasAddress(la, pip->ip_src),
@ -234,13 +237,21 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
base_alias = ntohs(salias);
for (j = 0; j < RTSP_PORT_GROUP; j++) {
/* Establish link to port found in RTSP packet */
/*
* Establish link
* to port found in
* RTSP packet
*/
rtsp_link = FindRtspOut(la, GetOriginalAddress(link), null_addr,
htons(base_port + j), htons(base_alias + j),
IPPROTO_UDP);
if (rtsp_link != NULL) {
#ifndef NO_FW_PUNCH
/* Punch hole in firewall */
/*
* Punch
* hole in
* firewall
*/
PunchFWHole(rtsp_link);
#endif
} else {
@ -254,7 +265,6 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
}
ealias = htons(base_alias + (RTSP_PORT_GROUP - 1));
}
if (salias && rtsp_link) {
pkt_updated = 1;
@ -273,7 +283,6 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
memcpy(port_newdata, stemp, strlen(stemp));
port_newdata += strlen(stemp);
}
*port_newdata = ';';
port_newdata++;
}
@ -291,13 +300,13 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (!pkt_updated)
return -1;
memcpy (port_newdata, port_data, port_dlen);
memcpy(port_newdata, port_data, port_dlen);
port_newdata += port_dlen;
*port_newdata = '\0';
/* Create new packet */
new_dlen = port_newdata - newdata;
memcpy (data, newdata, new_dlen);
memcpy(data, newdata, new_dlen);
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
@ -350,7 +359,7 @@ alias_pna_out(struct libalias *la, struct ip *pip,
/* Punch hole in firewall */
PunchFWHole(pna_links);
#endif
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
alias_port = GetAliasPort(pna_links);
memcpy(work, &alias_port, 2);
@ -381,7 +390,7 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
tlen = ntohs(pip->ip_len);
dlen = tlen - hlen;
data = (char*)pip;
data = (char *)pip;
data += hlen;
/* When aliasing a client, check for the SETUP request */
@ -399,11 +408,12 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
alias_pna_out(la, pip, link, data, dlen);
}
}
} else {
/* When aliasing a server, check for the 200 reply
Accomodate varying number of blanks between 200 & OK */
/*
* When aliasing a server, check for the 200 reply
* Accomodate varying number of blanks between 200 & OK
*/
if (dlen >= strlen(str200)) {
@ -418,7 +428,7 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
if (parseOk) {
i += strlen(str200); /* skip string found */
while(data[i] == ' ') /* skip blank(s) */
while (data[i] == ' ') /* skip blank(s) */
i++;
if ((dlen - i) >= strlen(okstr)) {

View File

@ -61,33 +61,31 @@ purposes);
#include "alias_local.h"
u_short
LibAliasInternetChecksum(struct libalias *la, u_short *ptr, int nbytes)
LibAliasInternetChecksum(struct libalias *la, u_short * ptr, int nbytes)
{
int sum, oddbyte;
sum = 0;
while (nbytes > 1)
{
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1)
{
if (nbytes == 1) {
oddbyte = 0;
((u_char *) &oddbyte)[0] = *(u_char *) ptr;
((u_char *) &oddbyte)[1] = 0;
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
((u_char *) & oddbyte)[1] = 0;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return(~sum);
return (~sum);
}
u_short
IpChecksum(struct ip *pip)
{
return( PacketAliasInternetChecksum((u_short *) pip,
(pip->ip_hl << 2)) );
return (PacketAliasInternetChecksum((u_short *) pip,
(pip->ip_hl << 2)));
}
@ -102,30 +100,27 @@ TcpChecksum(struct ip *pip)
nhdr = pip->ip_hl << 2;
ntcp = ntohs(pip->ip_len) - nhdr;
tc = (struct tcphdr *) ((char *) pip + nhdr);
tc = (struct tcphdr *)((char *)pip + nhdr);
ptr = (u_short *) tc;
/* Add up TCP header and data */
nbytes = ntcp;
sum = 0;
while (nbytes > 1)
{
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1)
{
if (nbytes == 1) {
oddbyte = 0;
((u_char *) &oddbyte)[0] = *(u_char *) ptr;
((u_char *) &oddbyte)[1] = 0;
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
((u_char *) & oddbyte)[1] = 0;
sum += oddbyte;
}
/* "Pseudo-header" data */
ptr = (u_short *) &(pip->ip_dst);
ptr = (u_short *) & (pip->ip_dst);
sum += *ptr++;
sum += *ptr;
ptr = (u_short *) &(pip->ip_src);
ptr = (u_short *) & (pip->ip_src);
sum += *ptr++;
sum += *ptr;
sum += htons((u_short) ntcp);
@ -136,35 +131,30 @@ TcpChecksum(struct ip *pip)
sum += (sum >> 16);
/* Return checksum */
return((u_short) ~sum);
return ((u_short) ~ sum);
}
void
DifferentialChecksum(u_short *cksum, u_short *new, u_short *old, int n)
DifferentialChecksum(u_short * cksum, u_short * new, u_short * old, int n)
{
int i;
int accumulate;
accumulate = *cksum;
for (i=0; i<n; i++)
{
for (i = 0; i < n; i++) {
accumulate -= *new++;
accumulate += *old++;
}
if (accumulate < 0)
{
if (accumulate < 0) {
accumulate = -accumulate;
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
accumulate += accumulate >> 16;
*cksum = (u_short) ~accumulate;
}
else
{
*cksum = (u_short) ~ accumulate;
} else {
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
accumulate += accumulate >> 16;
*cksum = (u_short) accumulate;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -88,20 +88,21 @@ int PacketUnaliasOut(char *_ptr, int _maxpacketsize);
/* Port and address redirection functions. */
int PacketAliasAddServer(struct alias_link *_link,
int
PacketAliasAddServer(struct alias_link *_link,
struct in_addr _addr, unsigned short _port);
struct alias_link *
PacketAliasRedirectAddr(struct in_addr _src_addr,
PacketAliasRedirectAddr(struct in_addr _src_addr,
struct in_addr _alias_addr);
int PacketAliasRedirectDynamic(struct alias_link *_link);
void PacketAliasRedirectDelete(struct alias_link *_link);
struct alias_link *
PacketAliasRedirectPort(struct in_addr _src_addr,
PacketAliasRedirectPort(struct in_addr _src_addr,
unsigned short _src_port, struct in_addr _dst_addr,
unsigned short _dst_port, struct in_addr _alias_addr,
unsigned short _alias_port, unsigned char _proto);
struct alias_link *
PacketAliasRedirectProto(struct in_addr _src_addr,
PacketAliasRedirectProto(struct in_addr _src_addr,
struct in_addr _dst_addr, struct in_addr _alias_addr,
unsigned char _proto);
@ -131,26 +132,27 @@ unsigned int
void LibAliasUninit(struct libalias *);
/* Packet Handling functions. */
int LibAliasIn(struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
int LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
/* Port and address redirection functions. */
int LibAliasAddServer(struct libalias *, struct alias_link *_link,
int
LibAliasAddServer(struct libalias *, struct alias_link *_link,
struct in_addr _addr, unsigned short _port);
struct alias_link *
LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
struct in_addr _alias_addr);
int LibAliasRedirectDynamic(struct libalias *, struct alias_link *_link);
void LibAliasRedirectDelete(struct libalias *, struct alias_link *_link);
struct alias_link *
LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
unsigned short _src_port, struct in_addr _dst_addr,
unsigned short _dst_port, struct in_addr _alias_addr,
unsigned short _alias_port, unsigned char _proto);
struct alias_link *
LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
struct in_addr _dst_addr, struct in_addr _alias_addr,
unsigned char _proto);

View File

@ -56,14 +56,15 @@ struct cu_header {
struct oc_header {
u_int16_t client_count; /* Number of client info structs */
u_int32_t seq_no;
char user_name[20];
char reserved[4]; /* flags, version stuff, etc */
char user_name [20];
char reserved [4]; /* flags, version stuff, etc */
};
/* client info structures */
struct client_info {
u_int32_t address; /* Client address */
char reserved[8]; /* Flags, pruning bitfield, packet counts etc */
u_int32_t address;/* Client address */
char reserved [8]; /* Flags, pruning bitfield, packet
* counts etc */
};
void
@ -78,7 +79,7 @@ AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *li
cu = (struct cu_header *)(ud + 1);
if (cu->addr)
cu->addr = (u_int32_t)GetAliasAddress(link).s_addr;
cu->addr = (u_int32_t) GetAliasAddress(link).s_addr;
cu_link = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(link),
ud->uh_dport, 0, IPPROTO_UDP, 1);
@ -109,13 +110,13 @@ AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr origina
end = (char *)ud + ntohs(ud->uh_ulen);
if ((char *)oc <= end) {
if(cu->dest_addr)
cu->dest_addr = (u_int32_t)original_addr.s_addr;
if(ntohs(cu->data_type) == 101)
if (cu->dest_addr)
cu->dest_addr = (u_int32_t) original_addr.s_addr;
if (ntohs(cu->data_type) == 101)
/* Find and change our address */
for(i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
if(ci->address == (u_int32_t)alias_addr.s_addr) {
ci->address = (u_int32_t)original_addr.s_addr;
for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
if (ci->address == (u_int32_t) alias_addr.s_addr) {
ci->address = (u_int32_t) original_addr.s_addr;
break;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -102,10 +102,11 @@ static void NewFtpMessage(struct libalias *la, struct ip *, struct alias_link *,
void
AliasHandleFtpOut(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize /* The maximum size this packet can grow to (including headers) */)
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize /* The maximum size this packet can grow to
(including headers) */ )
{
int hlen, tlen, dlen, pflags;
char *sptr;
@ -113,13 +114,13 @@ int maxpacketsize /* The maximum size this packet can grow to (including header
int ftp_message_type;
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
/* Place string pointer and beginning of data */
sptr = (char *) pip;
sptr = (char *)pip;
sptr += hlen;
/*
@ -153,13 +154,13 @@ int maxpacketsize /* The maximum size this packet can grow to (including header
if (ftp_message_type != FTP_UNKNOWN_MESSAGE)
NewFtpMessage(la, pip, link, maxpacketsize, ftp_message_type);
}
/* Track the msgs which are CRLF term'd for PORT/PASV FW breach */
if (dlen) { /* only if there's data */
sptr = (char *) pip; /* start over at beginning */
tlen = ntohs(pip->ip_len); /* recalc tlen, pkt may have grown */
if (sptr[tlen-2] == '\r' && sptr[tlen-1] == '\n')
sptr = (char *)pip; /* start over at beginning */
tlen = ntohs(pip->ip_len); /* recalc tlen, pkt may
* have grown */
if (sptr[tlen - 2] == '\r' && sptr[tlen - 1] == '\n')
pflags &= ~WAIT_CRLF;
else
pflags |= WAIT_CRLF;
@ -187,24 +188,52 @@ ParseFtpPortCommand(struct libalias *la, char *sptr, int dlen)
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state) {
case -4: if (ch == 'P') state++; else return 0; break;
case -3: if (ch == 'O') state++; else return 0; break;
case -2: if (ch == 'R') state++; else return 0; break;
case -1: if (ch == 'T') state++; else return 0; break;
case -4:
if (ch == 'P')
state++;
else
return 0;
break;
case -3:
if (ch == 'O')
state++;
else
return 0;
break;
case -2:
if (ch == 'R')
state++;
else
return 0;
break;
case -1:
if (ch == 'T')
state++;
else
return 0;
break;
case 0:
if (isspace(ch))
break;
else
state++;
case 1: case 3: case 5: case 7: case 9: case 11:
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 2: case 4: case 6: case 8:
case 2:
case 4:
case 6:
case 8:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',') {
@ -213,7 +242,8 @@ ParseFtpPortCommand(struct libalias *la, char *sptr, int dlen)
} else
return 0;
break;
case 10: case 12:
case 10:
case 12:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',' || state == 12) {
@ -253,12 +283,31 @@ ParseFtpEprtCommand(struct libalias *la, char *sptr, int dlen)
state = -4;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -4: if (ch == 'E') state++; else return 0; break;
case -3: if (ch == 'P') state++; else return 0; break;
case -2: if (ch == 'R') state++; else return 0; break;
case -1: if (ch == 'T') state++; else return 0; break;
switch (state) {
case -4:
if (ch == 'E')
state++;
else
return 0;
break;
case -3:
if (ch == 'P')
state++;
else
return 0;
break;
case -2:
if (ch == 'R')
state++;
else
return 0;
break;
case -1:
if (ch == 'T')
state++;
else
return 0;
break;
case 0:
if (!isspace(ch)) {
@ -278,14 +327,20 @@ ParseFtpEprtCommand(struct libalias *la, char *sptr, int dlen)
else
return 0;
break;
case 3: case 5: case 7: case 9:
case 3:
case 5:
case 7:
case 9:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 4: case 6: case 8: case 10:
case 4:
case 6:
case 8:
case 10:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == '.' || state == 10) {
@ -340,24 +395,46 @@ ParseFtp227Reply(struct libalias *la, char *sptr, int dlen)
state = -3;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -3: if (ch == '2') state++; else return 0; break;
case -2: if (ch == '2') state++; else return 0; break;
case -1: if (ch == '7') state++; else return 0; break;
switch (state) {
case -3:
if (ch == '2')
state++;
else
return 0;
break;
case -2:
if (ch == '2')
state++;
else
return 0;
break;
case -1:
if (ch == '7')
state++;
else
return 0;
break;
case 0:
if (ch == '(')
state++;
break;
case 1: case 3: case 5: case 7: case 9: case 11:
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
if (isdigit(ch)) {
octet = ch - '0';
state++;
} else
return 0;
break;
case 2: case 4: case 6: case 8:
case 2:
case 4:
case 6:
case 8:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',') {
@ -366,7 +443,8 @@ ParseFtp227Reply(struct libalias *la, char *sptr, int dlen)
} else
return 0;
break;
case 10: case 12:
case 10:
case 12:
if (isdigit(ch))
octet = 10 * octet + ch - '0';
else if (ch == ',' || (state == 12 && ch == ')')) {
@ -405,11 +483,25 @@ ParseFtp229Reply(struct libalias *la, char *sptr, int dlen)
state = -3;
for (i = 0; i < dlen; i++) {
ch = sptr[i];
switch (state)
{
case -3: if (ch == '2') state++; else return 0; break;
case -2: if (ch == '2') state++; else return 0; break;
case -1: if (ch == '9') state++; else return 0; break;
switch (state) {
case -3:
if (ch == '2')
state++;
else
return 0;
break;
case -2:
if (ch == '2')
state++;
else
return 0;
break;
case -1:
if (ch == '9')
state++;
else
return 0;
break;
case 0:
if (ch == '(')
@ -419,7 +511,8 @@ ParseFtp229Reply(struct libalias *la, char *sptr, int dlen)
delim = ch;
state++;
break;
case 2: case 3:
case 2:
case 3:
if (ch == delim)
state++;
else
@ -475,8 +568,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
ftp_link = FindUdpTcpOut(la, la->true_addr, GetDestAddress(link),
htons(la->true_port), 0, IPPROTO_TCP, 1);
if (ftp_link != NULL)
{
if (ftp_link != NULL) {
int slen, hlen, tlen, dlen;
struct tcphdr *tc;
@ -486,7 +578,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
#endif
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
@ -502,34 +594,37 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
/* Decompose alias address into quad format */
alias_address = GetAliasAddress(link);
ptr = (u_char *) &alias_address.s_addr;
a1 = *ptr++; a2=*ptr++; a3=*ptr++; a4=*ptr;
ptr = (u_char *) & alias_address.s_addr;
a1 = *ptr++;
a2 = *ptr++;
a3 = *ptr++;
a4 = *ptr;
alias_port = GetAliasPort(ftp_link);
switch (ftp_message_type)
{
switch (ftp_message_type) {
case FTP_PORT_COMMAND:
case FTP_227_REPLY:
/* Decompose alias port into pair format. */
ptr = (char *) &alias_port;
p1 = *ptr++; p2=*ptr;
ptr = (char *)&alias_port;
p1 = *ptr++;
p2 = *ptr;
if (ftp_message_type == FTP_PORT_COMMAND) {
/* Generate PORT command string. */
sprintf(stemp, "PORT %d,%d,%d,%d,%d,%d\r\n",
a1,a2,a3,a4,p1,p2);
a1, a2, a3, a4, p1, p2);
} else {
/* Generate 227 reply string. */
sprintf(stemp,
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
a1,a2,a3,a4,p1,p2);
a1, a2, a3, a4, p1, p2);
}
break;
case FTP_EPRT_COMMAND:
/* Generate EPRT command string. */
sprintf(stemp, "EPRT |1|%d.%d.%d.%d|%d|\r\n",
a1,a2,a3,a4,ntohs(alias_port));
a1, a2, a3, a4, ntohs(alias_port));
break;
case FTP_229_REPLY:
/* Generate 229 reply string. */
@ -542,8 +637,9 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
slen = strlen(stemp);
/* Copy modified buffer into IP packet. */
sptr = (char *) pip; sptr += hlen;
strncpy(sptr, stemp, maxpacketsize-hlen);
sptr = (char *)pip;
sptr += hlen;
strncpy(sptr, stemp, maxpacketsize - hlen);
}
/* Save information regarding modified seq and ack numbers */
@ -552,7 +648,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+slen-dlen);
AddSeq(pip, link, delta + slen - dlen);
}
/* Revise IP header */
@ -570,9 +666,7 @@ NewFtpMessage(struct libalias *la, struct ip *pip,
/* Compute TCP checksum for revised packet */
tc->th_sum = 0;
tc->th_sum = TcpChecksum(pip);
}
else
{
} else {
#ifdef DEBUG
fprintf(stderr,
"PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n");

View File

@ -68,8 +68,9 @@ void
AliasHandleIrcOut(struct libalias *la,
struct ip *pip, /* IP packet to examine */
struct alias_link *link, /* Which link are we on? */
int maxsize /* Maximum size of IP packet including headers */
)
int maxsize /* Maximum size of IP packet including
* headers */
)
{
int hlen, tlen, dlen;
struct in_addr true_addr;
@ -79,52 +80,61 @@ AliasHandleIrcOut(struct libalias *la,
int i; /* Iterator through the source */
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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 if data length is too short - assume an entire PRIVMSG in each packet. */
if (dlen<sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a")-1)
/*
* Return if data length is too short - assume an entire PRIVMSG in
* each packet.
*/
if (dlen < sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a") - 1)
return;
/* Place string pointer at beginning of data */
sptr = (char *) pip;
sptr = (char *)pip;
sptr += hlen;
maxsize -= hlen; /* We're interested in maximum size of data, not packet */
maxsize -= hlen; /* We're interested in maximum size of
* data, not packet */
/* Search for a CTCP command [Note 1] */
for( i=0; i<dlen; i++ ) {
if(sptr[i]=='\001')
for (i = 0; i < dlen; i++) {
if (sptr[i] == '\001')
goto lFOUND_CTCP;
}
return; /* No CTCP commands in */
/* Handle CTCP commands - the buffer may have to be copied */
lFOUND_CTCP:
{
char newpacket[65536]; /* Estimate of maximum packet size :) */
char newpacket[65536]; /* Estimate of maximum packet size
* :) */
int copyat = i; /* Same */
int iCopy = 0; /* How much data have we written to copy-back string? */
int iCopy = 0; /* How much data have we written to
* copy-back string? */
unsigned long org_addr; /* Original IP address */
unsigned short org_port; /* Original source port address */
lCTCP_START:
if( i >= dlen || iCopy >= sizeof(newpacket) )
unsigned short org_port; /* Original source port
* address */
lCTCP_START:
if (i >= dlen || iCopy >= sizeof(newpacket))
goto lPACKET_DONE;
newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start character */
newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start
* character */
/* Start of a CTCP */
if( i+4 >= dlen ) /* Too short for DCC */
if (i + 4 >= dlen) /* Too short for DCC */
goto lBAD_CTCP;
if( sptr[i+0] != 'D' )
if (sptr[i + 0] != 'D')
goto lBAD_CTCP;
if( sptr[i+1] != 'C' )
if (sptr[i + 1] != 'C')
goto lBAD_CTCP;
if( sptr[i+2] != 'C' )
if (sptr[i + 2] != 'C')
goto lBAD_CTCP;
if( sptr[i+3] != ' ' )
if (sptr[i + 3] != ' ')
goto lBAD_CTCP;
/* We have a DCC command - handle it! */
i+= 4; /* Skip "DCC " */
if( iCopy+4 > sizeof(newpacket) )
i += 4; /* Skip "DCC " */
if (iCopy + 4 > sizeof(newpacket))
goto lPACKET_DONE;
newpacket[iCopy++] = 'D';
newpacket[iCopy++] = 'C';
@ -132,54 +142,60 @@ AliasHandleIrcOut(struct libalias *la,
newpacket[iCopy++] = ' ';
DBprintf(("Found DCC\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces\n"));
goto lPACKET_DONE;
}
}
DBprintf(("Transferring command...\n"));
while(sptr[i] != ' ') {
while (sptr[i] != ' ') {
newpacket[iCopy++] = sptr[i];
if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
if (++i >= dlen || iCopy >= sizeof(newpacket)) {
DBprintf(("DCC packet terminated during command\n"));
goto lPACKET_DONE;
}
}
/* Copy _one_ space */
if( i+1 < dlen && iCopy < sizeof(newpacket) )
if (i + 1 < dlen && iCopy < sizeof(newpacket))
newpacket[iCopy++] = sptr[i++];
DBprintf(("Done command - removing spaces\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
goto lPACKET_DONE;
}
}
DBprintf(("Transferring filename...\n"));
while(sptr[i] != ' ') {
while (sptr[i] != ' ') {
newpacket[iCopy++] = sptr[i];
if( ++i >= dlen || iCopy >= sizeof(newpacket) ) {
if (++i >= dlen || iCopy >= sizeof(newpacket)) {
DBprintf(("DCC packet terminated during filename\n"));
goto lPACKET_DONE;
}
}
/* Copy _one_ space */
if( i+1 < dlen && iCopy < sizeof(newpacket) )
if (i + 1 < dlen && iCopy < sizeof(newpacket))
newpacket[iCopy++] = sptr[i++];
DBprintf(("Done filename - removing spaces\n"));
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
goto lPACKET_DONE;
}
@ -188,24 +204,26 @@ AliasHandleIrcOut(struct libalias *la,
DBprintf(("Fetching IP address\n"));
/* Fetch IP address */
org_addr = 0;
while(i<dlen && isdigit(sptr[i])) {
if( org_addr > ULONG_MAX/10UL ) { /* Terminate on overflow */
while (i < dlen && isdigit(sptr[i])) {
if (org_addr > ULONG_MAX / 10UL) { /* Terminate on overflow */
DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
goto lBAD_CTCP;
}
org_addr *= 10;
org_addr += sptr[i++]-'0';
org_addr += sptr[i++] - '0';
}
DBprintf(("Skipping space\n"));
if( i+1 >= dlen || sptr[i] != ' ' ) {
DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i+1, dlen, sptr[i]));
if (i + 1 >= dlen || sptr[i] != ' ') {
DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i + 1, dlen, sptr[i]));
goto lBAD_CTCP;
}
/* Skip any extra spaces (should not occur according to
protocol, but DCC breaks CTCP protocol anyway, so we might
as well play it safe */
while(sptr[i] == ' ') {
if( ++i >= dlen ) {
/*
* Skip any extra spaces (should not occur according to
* protocol, but DCC breaks CTCP protocol anyway, so we
* might as well play it safe
*/
while (sptr[i] == ' ') {
if (++i >= dlen) {
DBprintf(("Packet failure - space overflow.\n"));
goto lPACKET_DONE;
}
@ -213,16 +231,17 @@ AliasHandleIrcOut(struct libalias *la,
DBprintf(("Fetching port number\n"));
/* Fetch source port */
org_port = 0;
while(i<dlen && isdigit(sptr[i])) {
if( org_port > 6554 ) { /* Terminate on overflow (65536/10 rounded up*/
while (i < dlen && isdigit(sptr[i])) {
if (org_port > 6554) { /* Terminate on overflow
* (65536/10 rounded up */
DBprintf(("DCC: port number overflow\n"));
goto lBAD_CTCP;
}
org_port *= 10;
org_port += sptr[i++]-'0';
org_port += sptr[i++] - '0';
}
/* Skip illegal addresses (or early termination) */
if( i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ') ) {
if (i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ')) {
DBprintf(("Bad port termination\n"));
goto lBAD_CTCP;
}
@ -244,16 +263,19 @@ AliasHandleIrcOut(struct libalias *la,
org_port < IPPORT_RESERVED)
goto lBAD_CTCP;
/* Steal the FTP_DATA_PORT - it doesn't really matter, and this
would probably allow it through at least _some_
firewalls. */
/*
* Steal the FTP_DATA_PORT - it doesn't really
* matter, and this would probably allow it through
* at least _some_ firewalls.
*/
dcc_link = FindUdpTcpOut(la, true_addr, destaddr,
true_port, 0,
IPPROTO_TCP, 1);
DBprintf(("Got a DCC link\n"));
if ( dcc_link ) {
if (dcc_link) {
struct in_addr alias_address; /* Address from aliasing */
u_short alias_port; /* Port given by aliasing */
u_short alias_port; /* Port given by
* aliasing */
int n;
#ifndef NO_FW_PUNCH
@ -263,52 +285,58 @@ AliasHandleIrcOut(struct libalias *la,
alias_address = GetAliasAddress(link);
n = snprintf(&newpacket[iCopy],
sizeof(newpacket)-iCopy,
"%lu ", (u_long)htonl(alias_address.s_addr));
if( n < 0 ) {
sizeof(newpacket) - iCopy,
"%lu ", (u_long) htonl(alias_address.s_addr));
if (n < 0) {
DBprintf(("DCC packet construct failure.\n"));
goto lBAD_CTCP;
}
if( (iCopy += n) >= sizeof(newpacket) ) { /* Truncated/fit exactly - bad news */
if ((iCopy += n) >= sizeof(newpacket)) { /* Truncated/fit exactly
* - bad news */
DBprintf(("DCC constructed packet overflow.\n"));
goto lBAD_CTCP;
}
alias_port = GetAliasPort(dcc_link);
n = snprintf(&newpacket[iCopy],
sizeof(newpacket)-iCopy,
"%u", htons(alias_port) );
if( n < 0 ) {
sizeof(newpacket) - iCopy,
"%u", htons(alias_port));
if (n < 0) {
DBprintf(("DCC packet construct failure.\n"));
goto lBAD_CTCP;
}
iCopy += n;
/* Done - truncated cases will be taken care of by lBAD_CTCP */
/*
* Done - truncated cases will be taken
* care of by lBAD_CTCP
*/
DBprintf(("Aliased IP %lu and port %u\n", alias_address.s_addr, (unsigned)alias_port));
}
}
/* An uninteresting CTCP - state entered right after '\001' has
been pushed. Also used to copy the rest of a DCC, after IP
address and port has been handled */
lBAD_CTCP:
for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
/*
* An uninteresting CTCP - state entered right after '\001'
* has been pushed. Also used to copy the rest of a DCC,
* after IP address and port has been handled
*/
lBAD_CTCP:
for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
if(sptr[i] == '\001') {
if (sptr[i] == '\001') {
goto lNORMAL_TEXT;
}
}
goto lPACKET_DONE;
/* Normal text */
lNORMAL_TEXT:
for(; i<dlen && iCopy<sizeof(newpacket); i++,iCopy++) {
lNORMAL_TEXT:
for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
if(sptr[i] == '\001') {
if (sptr[i] == '\001') {
goto lCTCP_START;
}
}
/* Handle the end of a packet */
lPACKET_DONE:
iCopy = iCopy > maxsize-copyat ? maxsize-copyat : iCopy;
memcpy(sptr+copyat, newpacket, iCopy);
lPACKET_DONE:
iCopy = iCopy > maxsize - copyat ? maxsize - copyat : iCopy;
memcpy(sptr + copyat, newpacket, iCopy);
/* Save information regarding modified seq and ack numbers */
{
@ -316,7 +344,7 @@ AliasHandleIrcOut(struct libalias *la,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+copyat+iCopy-dlen);
AddSeq(pip, link, delta + copyat + iCopy - dlen);
}
/* Revise IP header */

View File

@ -70,11 +70,11 @@ struct libalias {
struct in_addr nullAddress; /* Used as a dummy parameter for */
/* some function calls */
LIST_HEAD(, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
/* Lookup table of pointers to */
/* chains of link records. Each */
LIST_HEAD(, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
/* link record is doubly indexed */
/* into input and output lookup */
/* tables. */
@ -95,7 +95,8 @@ struct libalias {
int timeStamp; /* System time in seconds for */
/* current packet */
int lastCleanupTime; /* Last time IncrementalCleanup() */
int lastCleanupTime; /* Last time
* IncrementalCleanup() */
/* was called */
int houseKeepingResidual; /* used by HouseKeeping() */
@ -116,9 +117,12 @@ struct libalias {
/* PacketAliasSetMode on first */
/* setting the PKT_ALIAS_PUNCH_FW */
/* flag. */
int fireWallBaseNum; /* The first firewall entry free for our use */
int fireWallNumNums; /* How many entries can we use? */
int fireWallActiveNum; /* Which entry did we last use? */
int fireWallBaseNum; /* The first firewall entry
* free for our use */
int fireWallNumNums; /* How many entries can we
* use? */
int fireWallActiveNum; /* Which entry did we last
* use? */
char *fireWallField; /* bool array for entries */
#endif
@ -163,55 +167,56 @@ struct libalias {
/* General utilities */
u_short IpChecksum(struct ip *_pip);
u_short TcpChecksum(struct ip *_pip);
void DifferentialChecksum(u_short *_cksum, u_short *_new, u_short *_old,
void
DifferentialChecksum(u_short * _cksum, u_short * _new, u_short * _old,
int _n);
/* Internal data access */
struct alias_link *
FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _id_alias, int _create);
struct alias_link *
FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _id, int _create);
struct alias_link *
FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _ip_id);
struct alias_link *
FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _ip_id);
struct alias_link *
AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
struct alias_link *
FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
struct alias_link *
FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_char _proto);
struct alias_link *
FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_char _proto);
struct alias_link *
FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
struct alias_link *
FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _src_port, u_short _dst_port, u_char _proto, int _create);
struct alias_link *
AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _src_call_id);
struct alias_link *
FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
struct in_addr _dst_addr, u_int16_t _src_call_id);
struct alias_link *
FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _dst_call_id);
struct alias_link *
FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
struct in_addr _dst_addr, u_int16_t _dst_call_id);
struct alias_link *
FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
struct in_addr _alias_addr, u_int16_t _alias_call_id);
struct alias_link *
FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
u_short _src_port, u_short _alias_port, u_char _proto);
struct in_addr
FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
@ -219,7 +224,8 @@ struct in_addr
FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
/* External data access/modification */
int FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
int
FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
u_short _src_port, u_short _dst_port, u_short _port_count,
u_char _proto, u_char _align);
void GetFragmentAddr(struct alias_link *_link, struct in_addr *_src_addr);
@ -228,7 +234,7 @@ void GetFragmentPtr(struct alias_link *_link, char **_fptr);
void SetFragmentPtr(struct alias_link *_link, char *fptr);
void SetStateIn(struct alias_link *_link, int _state);
void SetStateOut(struct alias_link *_link, int _state);
int GetStateIn(struct alias_link *_link);
int GetStateIn (struct alias_link *_link);
int GetStateOut(struct alias_link *_link);
struct in_addr
GetOriginalAddress(struct alias_link *_link);
@ -250,14 +256,16 @@ void SetAckModified(struct alias_link *_link);
int GetAckModified(struct alias_link *_link);
int GetDeltaAckIn(struct ip *_pip, struct alias_link *_link);
int GetDeltaSeqOut(struct ip *_pip, struct alias_link *_link);
void AddSeq(struct ip *_pip, struct alias_link *_link, int _delta);
void SetExpire(struct alias_link *_link, int _expire);
void AddSeq (struct ip *_pip, struct alias_link *_link, int _delta);
void SetExpire (struct alias_link *_link, int _expire);
void ClearCheckNewLink(struct libalias *la);
void SetProtocolFlags(struct alias_link *_link, int _pflags);
int GetProtocolFlags(struct alias_link *_link);
void SetDestCallId(struct alias_link *_link, u_int16_t _cid);
#ifndef NO_FW_PUNCH
void PunchFWHole(struct alias_link *_link);
#endif
/* Housekeeping function */
@ -267,15 +275,18 @@ void HouseKeeping(struct libalias *);
/* lint -save -library Suppress flexelint warnings */
/* FTP routines */
void AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxpacketsize);
/* IRC routines */
void AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxsize);
/* RTSP routines */
void AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
void
AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int _maxpacketsize);
/* PPTP routines */
@ -285,11 +296,13 @@ int AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
int AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
/* NetBIOS routines */
int AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_link,
int
AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short _alias_port);
int AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short *_alias_port,
struct in_addr *_original_address, u_short *_original_port);
int
AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_link,
struct in_addr *_alias_address, u_short * _alias_port,
struct in_addr *_original_address, u_short * _original_port);
/* CUSeeMe routines */
void AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_link);
@ -299,9 +312,11 @@ void AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr
void AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_link);
/* Transparent proxy routines */
int ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
u_short *_proxy_server_port);
void ProxyModify(struct libalias *la, struct alias_link *_link, struct ip *_pip,
int
ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
u_short * _proxy_server_port);
void
ProxyModify(struct libalias *la, struct alias_link *_link, struct ip *_pip,
int _maxpacketsize, int _proxy_type);
enum alias_tcp_state {

View File

@ -80,7 +80,7 @@ typedef struct {
#define OpRefresh 8
typedef struct {
u_short nametrid;
u_short dir:1, opcode:4, nmflags:7, rcode:4;
u_short dir: 1, opcode:4, nmflags:7, rcode:4;
u_short qdcount;
u_short ancount;
u_short nscount;
@ -96,7 +96,9 @@ typedef struct {
#ifdef DEBUG
static void PrintRcode( u_char rcode ) {
static void
PrintRcode(u_char rcode)
{
switch (rcode) {
case FMT_ERR:
@ -112,15 +114,18 @@ static void PrintRcode( u_char rcode ) {
case CFT_ERR:
printf("\nName in conflict error.\n");
default:
printf("\n?%c?=%0x\n", '?', rcode );
printf("\n?%c?=%0x\n", '?', rcode);
}
}
#endif
/* Handling Name field */
static u_char *AliasHandleName ( u_char *p, char *pmax ) {
static u_char *
AliasHandleName(u_char * p, char *pmax)
{
u_char *s;
u_char c;
@ -129,23 +134,23 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
/* Following length field */
if (p == NULL || (char *)p >= pmax)
return(NULL);
return (NULL);
if (*p & 0xc0 ) {
if (*p & 0xc0) {
p = p + 2;
if ((char *)p > pmax)
return(NULL);
return ((u_char *)p);
return (NULL);
return ((u_char *) p);
}
while ( ( *p & 0x3f) != 0x00 ) {
while ((*p & 0x3f) != 0x00) {
s = p + 1;
if ( *p == 0x20 )
if (*p == 0x20)
compress = 1;
else
compress = 0;
/* Get next length field */
p = (u_char *)(p + (*p & 0x3f) + 1);
p = (u_char *) (p + (*p & 0x3f) + 1);
if ((char *)p > pmax) {
p = NULL;
break;
@ -154,15 +159,15 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
printf(":");
#endif
while (s < p) {
if ( compress == 1 ) {
c = (u_char )(((((*s & 0x0f) << 4) | (*(s+1) & 0x0f)) - 0x11));
if (compress == 1) {
c = (u_char) (((((*s & 0x0f) << 4) | (*(s + 1) & 0x0f)) - 0x11));
#ifdef DEBUG
if (isprint( c ) )
printf("%c", c );
if (isprint(c))
printf("%c", c);
else
printf("<0x%02x>", c );
printf("<0x%02x>", c);
#endif
s +=2;
s += 2;
} else {
#ifdef DEBUG
printf("%c", *s);
@ -181,7 +186,7 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
p = NULL;
else
p++;
return ((u_char *)p);
return ((u_char *) p);
}
/*
@ -195,58 +200,61 @@ static u_char *AliasHandleName ( u_char *p, char *pmax ) {
#define DGM_POSITIVE_RES 0x15
#define DGM_NEGATIVE_RES 0x16
int AliasHandleUdpNbt(
int
AliasHandleUdpNbt(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link,
struct in_addr *alias_address,
u_short alias_port
) {
struct udphdr * uh;
)
{
struct udphdr *uh;
NbtDataHeader *ndh;
u_char *p = NULL;
char *pmax;
/* Calculate data length of UDP packet */
uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
pmax = (char *)uh + ntohs( uh->uh_ulen );
uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
pmax = (char *)uh + ntohs(uh->uh_ulen);
ndh = (NbtDataHeader *)((char *)uh + (sizeof (struct udphdr)));
ndh = (NbtDataHeader *) ((char *)uh + (sizeof(struct udphdr)));
if ((char *)(ndh + 1) > pmax)
return(-1);
return (-1);
#ifdef DEBUG
printf("\nType=%02x,", ndh->type );
printf("\nType=%02x,", ndh->type);
#endif
switch ( ndh->type ) {
switch (ndh->type) {
case DGM_DIRECT_UNIQ:
case DGM_DIRECT_GROUP:
case DGM_BROADCAST:
p = (u_char *)ndh + 14;
p = AliasHandleName ( p, pmax ); /* Source Name */
p = AliasHandleName ( p, pmax ); /* Destination Name */
p = (u_char *) ndh + 14;
p = AliasHandleName(p, pmax); /* Source Name */
p = AliasHandleName(p, pmax); /* Destination Name */
break;
case DGM_ERROR:
p = (u_char *)ndh + 11;
p = (u_char *) ndh + 11;
break;
case DGM_QUERY:
case DGM_POSITIVE_RES:
case DGM_NEGATIVE_RES:
p = (u_char *)ndh + 10;
p = AliasHandleName ( p, pmax ); /* Destination Name */
p = (u_char *) ndh + 10;
p = AliasHandleName(p, pmax); /* Destination Name */
break;
}
if (p == NULL || (char *)p > pmax)
p = NULL;
#ifdef DEBUG
printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port) );
printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
#endif
/* Doing an IP address and Port number Translation */
if ( uh->uh_sum != 0 ) {
if (uh->uh_sum != 0) {
int acc;
u_short *sptr;
acc = ndh->source_port;
acc -= alias_port;
sptr = (u_short *) &(ndh->source_ip);
sptr = (u_short *) & (ndh->source_ip);
acc += *sptr++;
acc += *sptr;
sptr = (u_short *) alias_address;
@ -257,11 +265,12 @@ int AliasHandleUdpNbt(
ndh->source_ip = *alias_address;
ndh->source_port = alias_port;
#ifdef DEBUG
printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port) );
printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
fflush(stdout);
#endif
return((p == NULL) ? -1 : 0);
return ((p == NULL) ? -1 : 0);
}
/* Question Section */
#define QS_TYPE_NB 0x0020
#define QS_TYPE_NBSTAT 0x0021
@ -274,29 +283,28 @@ typedef struct {
static u_char *
AliasHandleQuestion(
u_short count,
NBTNsQuestion *q,
NBTNsQuestion * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
while ( count != 0 ) {
while (count != 0) {
/* Name Filed */
q = (NBTNsQuestion *)AliasHandleName((u_char *)q, pmax);
q = (NBTNsQuestion *) AliasHandleName((u_char *) q, pmax);
if (q == NULL || (char *)(q + 1) > pmax) {
q = NULL;
break;
}
/* Type and Class filed */
switch ( ntohs(q->type) ) {
switch (ntohs(q->type)) {
case QS_TYPE_NB:
case QS_TYPE_NBSTAT:
q= q+1;
q = q + 1;
break;
default:
#ifdef DEBUG
printf("\nUnknown Type on Question %0x\n", ntohs(q->type) );
printf("\nUnknown Type on Question %0x\n", ntohs(q->type));
#endif
break;
}
@ -304,7 +312,7 @@ AliasHandleQuestion(
}
/* Set up to out of Question Section */
return ((u_char *)q);
return ((u_char *) q);
}
/* Resource Record */
@ -324,54 +332,53 @@ typedef struct {
#define SizeOfNsRNB 6
typedef struct {
u_short g:1, ont:2, resv:13;
u_short g: 1 , ont:2, resv:13;
struct in_addr addr;
} NBTNsRNB;
static u_char *
AliasHandleResourceNB(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsRNB *nb;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Check out a length */
bcount = ntohs(q->rdlen);
/* Forward to Resource NB position */
nb = (NBTNsRNB *)((u_char *)q + SizeOfNsResource);
nb = (NBTNsRNB *) ((u_char *) q + SizeOfNsResource);
/* Processing all in_addr array */
#ifdef DEBUG
printf("NB rec[%s", inet_ntoa(nbtarg->oldaddr));
printf("->%s, %dbytes] ",inet_ntoa(nbtarg->newaddr ), bcount);
printf("->%s, %dbytes] ", inet_ntoa(nbtarg->newaddr), bcount);
#endif
while ( nb != NULL && bcount != 0 ) {
while (nb != NULL && bcount != 0) {
if ((char *)(nb + 1) > pmax) {
nb = NULL;
break;
}
#ifdef DEBUG
printf("<%s>", inet_ntoa(nb->addr) );
printf("<%s>", inet_ntoa(nb->addr));
#endif
if (!bcmp(&nbtarg->oldaddr,&nb->addr, sizeof(struct in_addr) ) ) {
if ( *nbtarg->uh_sum != 0 ) {
if (!bcmp(&nbtarg->oldaddr, &nb->addr, sizeof(struct in_addr))) {
if (*nbtarg->uh_sum != 0) {
int acc;
u_short *sptr;
sptr = (u_short *) &(nb->addr);
sptr = (u_short *) & (nb->addr);
acc = *sptr++;
acc += *sptr;
sptr = (u_short *) &(nbtarg->newaddr);
sptr = (u_short *) & (nbtarg->newaddr);
acc -= *sptr++;
acc -= *sptr;
ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
}
nb->addr = nbtarg->newaddr;
#ifdef DEBUG
printf("O");
@ -382,14 +389,13 @@ AliasHandleResourceNB(
printf(".");
}
#endif
nb=(NBTNsRNB *)((u_char *)nb + SizeOfNsRNB);
nb = (NBTNsRNB *) ((u_char *) nb + SizeOfNsRNB);
bcount -= SizeOfNsRNB;
}
if (nb == NULL || (char *)(nb + 1) > pmax) {
nb = NULL;
}
return ((u_char *)nb);
return ((u_char *) nb);
}
#define SizeOfResourceA 6
@ -399,18 +405,18 @@ typedef struct {
static u_char *
AliasHandleResourceA(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceA *a;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource A position */
a = (NBTNsResourceA *)( (u_char *)q + sizeof(NBTNsResource) );
a = (NBTNsResourceA *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
@ -418,36 +424,35 @@ AliasHandleResourceA(
/* Processing all in_addr array */
#ifdef DEBUG
printf("Arec [%s", inet_ntoa(nbtarg->oldaddr));
printf("->%s]",inet_ntoa(nbtarg->newaddr ));
printf("->%s]", inet_ntoa(nbtarg->newaddr));
#endif
while ( bcount != 0 ) {
while (bcount != 0) {
if (a == NULL || (char *)(a + 1) > pmax)
return(NULL);
return (NULL);
#ifdef DEBUG
printf("..%s", inet_ntoa(a->addr) );
printf("..%s", inet_ntoa(a->addr));
#endif
if ( !bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr) ) ) {
if ( *nbtarg->uh_sum != 0 ) {
if (!bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr))) {
if (*nbtarg->uh_sum != 0) {
int acc;
u_short *sptr;
sptr = (u_short *) &(a->addr); /* Old */
sptr = (u_short *) & (a->addr); /* Old */
acc = *sptr++;
acc += *sptr;
sptr = (u_short *) &nbtarg->newaddr; /* New */
sptr = (u_short *) & nbtarg->newaddr; /* New */
acc -= *sptr++;
acc -= *sptr;
ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
}
a->addr = nbtarg->newaddr;
}
a++; /*XXXX*/
a++; /* XXXX */
bcount -= SizeOfResourceA;
}
if (a == NULL || (char *)(a + 1) > pmax)
a = NULL;
return ((u_char *)a);
return ((u_char *) a);
}
typedef struct {
@ -456,24 +461,24 @@ typedef struct {
static u_char *
AliasHandleResourceNULL(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNULL *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NULL position */
n = (NBTNsResourceNULL *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
/* Processing all in_addr array */
while ( bcount != 0 ) {
while (bcount != 0) {
if ((char *)(n + 1) > pmax) {
n = NULL;
break;
@ -484,34 +489,34 @@ AliasHandleResourceNULL(
if ((char *)(n + 1) > pmax)
n = NULL;
return ((u_char *)n);
return ((u_char *) n);
}
static u_char *
AliasHandleResourceNS(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNULL *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NULL position */
n = (NBTNsResourceNULL *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
/* Resource Record Name Filed */
q = (NBTNsResource *)AliasHandleName( (u_char *)n, pmax ); /* XXX */
q = (NBTNsResource *) AliasHandleName((u_char *) n, pmax); /* XXX */
if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
return(NULL);
if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
return (NULL);
else
return ((u_char *)n + bcount);
return ((u_char *) n + bcount);
}
typedef struct {
@ -520,78 +525,78 @@ typedef struct {
static u_char *
AliasHandleResourceNBSTAT(
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments *nbtarg)
NBTArguments * nbtarg)
{
NBTNsResourceNBSTAT *n;
u_short bcount;
if (q == NULL || (char *)(q + 1) > pmax)
return(NULL);
return (NULL);
/* Forward to Resource NBSTAT position */
n = (NBTNsResourceNBSTAT *)( (u_char *)q + sizeof(NBTNsResource) );
n = (NBTNsResourceNBSTAT *) ((u_char *) q + sizeof(NBTNsResource));
/* Check out of length */
bcount = ntohs(q->rdlen);
if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
return(NULL);
if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
return (NULL);
else
return ((u_char *)n + bcount);
return ((u_char *) n + bcount);
}
static u_char *
AliasHandleResource(
u_short count,
NBTNsResource *q,
NBTNsResource * q,
char *pmax,
NBTArguments
*nbtarg)
* nbtarg)
{
while ( count != 0 ) {
while (count != 0) {
/* Resource Record Name Filed */
q = (NBTNsResource *)AliasHandleName( (u_char *)q, pmax );
q = (NBTNsResource *) AliasHandleName((u_char *) q, pmax);
if (q == NULL || (char *)(q + 1) > pmax)
break;
#ifdef DEBUG
printf("type=%02x, count=%d\n", ntohs(q->type), count );
printf("type=%02x, count=%d\n", ntohs(q->type), count);
#endif
/* Type and Class filed */
switch ( ntohs(q->type) ) {
switch (ntohs(q->type)) {
case RR_TYPE_NB:
q = (NBTNsResource *)AliasHandleResourceNB(
q = (NBTNsResource *) AliasHandleResourceNB(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_A:
q = (NBTNsResource *)AliasHandleResourceA(
q = (NBTNsResource *) AliasHandleResourceA(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NS:
q = (NBTNsResource *)AliasHandleResourceNS(
q = (NBTNsResource *) AliasHandleResourceNS(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NULL:
q = (NBTNsResource *)AliasHandleResourceNULL(
q = (NBTNsResource *) AliasHandleResourceNULL(
q,
pmax,
nbtarg
);
break;
case RR_TYPE_NBSTAT:
q = (NBTNsResource *)AliasHandleResourceNBSTAT(
q = (NBTNsResource *) AliasHandleResourceNBSTAT(
q,
pmax,
nbtarg
@ -609,21 +614,22 @@ AliasHandleResource(
count--;
}
fflush(stdout);
return ((u_char *)q);
return ((u_char *) q);
}
int AliasHandleUdpNbtNS(
int
AliasHandleUdpNbtNS(
struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link,
struct in_addr *alias_address,
u_short *alias_port,
u_short * alias_port,
struct in_addr *original_address,
u_short *original_port )
u_short * original_port)
{
struct udphdr * uh;
NbtNSHeader * nsh;
u_char * p;
struct udphdr *uh;
NbtNSHeader *nsh;
u_char *p;
char *pmax;
NBTArguments nbtarg;
@ -634,19 +640,19 @@ int AliasHandleUdpNbtNS(
nbtarg.newport = *original_port;
/* Calculate data length of UDP packet */
uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
nbtarg.uh_sum = &(uh->uh_sum);
nsh = (NbtNSHeader *)((char *)uh + (sizeof(struct udphdr)));
p = (u_char *)(nsh + 1);
pmax = (char *)uh + ntohs( uh->uh_ulen );
nsh = (NbtNSHeader *) ((char *)uh + (sizeof(struct udphdr)));
p = (u_char *) (nsh + 1);
pmax = (char *)uh + ntohs(uh->uh_ulen);
if ((char *)(nsh + 1) > pmax)
return(-1);
return (-1);
#ifdef DEBUG
printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
", an=%04x, ns=%04x, ar=%04x, [%d]-->",
nsh->dir ? "Response": "Request",
nsh->dir ? "Response" : "Request",
nsh->nametrid,
nsh->opcode,
nsh->nmflags,
@ -655,50 +661,46 @@ int AliasHandleUdpNbtNS(
ntohs(nsh->ancount),
ntohs(nsh->nscount),
ntohs(nsh->arcount),
(u_char *)p -(u_char *)nsh
(u_char *) p - (u_char *) nsh
);
#endif
/* Question Entries */
if (ntohs(nsh->qdcount) !=0 ) {
if (ntohs(nsh->qdcount) != 0) {
p = AliasHandleQuestion(
ntohs(nsh->qdcount),
(NBTNsQuestion *)p,
(NBTNsQuestion *) p,
pmax,
&nbtarg
);
}
/* Answer Resource Records */
if (ntohs(nsh->ancount) !=0 ) {
if (ntohs(nsh->ancount) != 0) {
p = AliasHandleResource(
ntohs(nsh->ancount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
/* Authority Resource Recodrs */
if (ntohs(nsh->nscount) !=0 ) {
if (ntohs(nsh->nscount) != 0) {
p = AliasHandleResource(
ntohs(nsh->nscount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
/* Additional Resource Recodrs */
if (ntohs(nsh->arcount) !=0 ) {
if (ntohs(nsh->arcount) != 0) {
p = AliasHandleResource(
ntohs(nsh->arcount),
(NBTNsResource *)p,
(NBTNsResource *) p,
pmax,
&nbtarg
);
}
#ifdef DEBUG
PrintRcode(nsh->rcode);
#endif

View File

@ -80,14 +80,14 @@ __FBSDID("$FreeBSD$");
* PPTP definitions
*/
struct grehdr /* Enhanced GRE header. */
{
struct grehdr { /* Enhanced GRE header. */
u_int16_t gh_flags; /* Flags. */
u_int16_t gh_protocol; /* Protocol type. */
u_int16_t gh_length; /* Payload length. */
u_int16_t gh_call_id; /* Call ID. */
u_int32_t gh_seq_no; /* Sequence number (optional). */
u_int32_t gh_ack_no; /* Acknowledgment number (optional). */
u_int32_t gh_ack_no; /* Acknowledgment number
* (optional). */
};
typedef struct grehdr GreHdr;
@ -120,26 +120,26 @@ enum {
};
/* Message structures */
struct pptpMsgHead {
struct pptpMsgHead {
u_int16_t length; /* total length */
u_int16_t msgType; /* PPTP message type */
u_int16_t msgType;/* PPTP message type */
u_int32_t magic; /* magic cookie */
u_int16_t type; /* control message type */
u_int16_t resv0; /* reserved */
};
typedef struct pptpMsgHead *PptpMsgHead;
};
typedef struct pptpMsgHead *PptpMsgHead;
struct pptpCodes {
u_int8_t resCode; /* Result Code */
u_int8_t errCode; /* Error Code */
};
typedef struct pptpCodes *PptpCode;
struct pptpCodes {
u_int8_t resCode;/* Result Code */
u_int8_t errCode;/* Error Code */
};
typedef struct pptpCodes *PptpCode;
struct pptpCallIds {
struct pptpCallIds {
u_int16_t cid1; /* Call ID field #1 */
u_int16_t cid2; /* Call ID field #2 */
};
typedef struct pptpCallIds *PptpCallId;
};
typedef struct pptpCallIds *PptpCallId;
static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
@ -147,8 +147,8 @@ static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
void
AliasHandlePptpOut(struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link) /* The PPTP control link */
{
struct alias_link *link)
{ /* The PPTP control link */
struct alias_link *pptp_link;
PptpCallId cptr;
PptpCode codes;
@ -165,13 +165,19 @@ AliasHandlePptpOut(struct libalias *la,
case PPTP_OutCallReply:
case PPTP_InCallRequest:
case PPTP_InCallReply:
/* Establish PPTP link for address and Call ID found in control message. */
/*
* Establish PPTP link for address and Call ID found in
* control message.
*/
pptp_link = AddPptp(la, GetOriginalAddress(link), GetDestAddress(link),
GetAliasAddress(link), cptr->cid1);
break;
case PPTP_CallClearRequest:
case PPTP_CallDiscNotify:
/* Find PPTP link for address and Call ID found in control message. */
/*
* Find PPTP link for address and Call ID found in control
* message.
*/
pptp_link = FindPptpOutByCallId(la, GetOriginalAddress(link),
GetDestAddress(link),
cptr->cid1);
@ -187,16 +193,18 @@ AliasHandlePptpOut(struct libalias *la,
cptr->cid1 = GetAliasPort(pptp_link);
/* Compute TCP checksum for revised packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
accumulate -= cptr->cid1;
ADJUST_CHECKSUM(accumulate, tc->th_sum);
switch (ctl_type) {
case PPTP_OutCallReply:
case PPTP_InCallReply:
codes = (PptpCode)(cptr + 1);
if (codes->resCode == 1) /* Connection established, */
SetDestCallId(pptp_link, /* note the Peer's Call ID. */
codes = (PptpCode) (cptr + 1);
if (codes->resCode == 1) /* Connection
* established, */
SetDestCallId(pptp_link, /* note the Peer's Call
* ID. */
cptr->cid2);
else
SetExpire(pptp_link, 0); /* Connection refused. */
@ -211,8 +219,8 @@ AliasHandlePptpOut(struct libalias *la,
void
AliasHandlePptpIn(struct libalias *la,
struct ip *pip, /* IP packet to examine/patch */
struct alias_link *link) /* The PPTP control link */
{
struct alias_link *link)
{ /* The PPTP control link */
struct alias_link *pptp_link;
PptpCallId cptr;
u_int16_t *pcall_id;
@ -224,8 +232,7 @@ AliasHandlePptpIn(struct libalias *la,
return;
/* Modify certain PPTP messages */
switch (ctl_type)
{
switch (ctl_type) {
case PPTP_InCallConn:
case PPTP_WanErrorNotify:
case PPTP_SetLinkInfo:
@ -258,14 +265,15 @@ AliasHandlePptpIn(struct libalias *la,
*pcall_id = GetOriginalPort(pptp_link);
/* Compute TCP checksum for modified packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
accumulate -= *pcall_id;
ADJUST_CHECKSUM(accumulate, tc->th_sum);
if (ctl_type == PPTP_OutCallReply || ctl_type == PPTP_InCallReply) {
PptpCode codes = (PptpCode)(cptr + 1);
PptpCode codes = (PptpCode) (cptr + 1);
if (codes->resCode == 1) /* Connection established, */
if (codes->resCode == 1) /* Connection
* established, */
SetDestCallId(pptp_link, /* note the Call ID. */
cptr->cid1);
else
@ -275,24 +283,24 @@ AliasHandlePptpIn(struct libalias *la,
}
static PptpCallId
AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch */
{
AliasVerifyPptp(struct ip *pip, u_int16_t * ptype)
{ /* IP packet to examine/patch */
int hlen, tlen, dlen;
PptpMsgHead hptr;
struct tcphdr *tc;
/* Calculate some lengths */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
/* Verify data length */
if (dlen < (sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
return(NULL);
return (NULL);
/* Move up to PPTP message header */
hptr = (PptpMsgHead)(((char *) pip) + hlen);
hptr = (PptpMsgHead) (((char *)pip) + hlen);
/* Return the control message type */
*ptype = ntohs(hptr->type);
@ -300,7 +308,7 @@ AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch
/* Verify PPTP Control Message */
if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
(ntohl(hptr->magic) != PPTP_MAGIC))
return(NULL);
return (NULL);
/* Verify data length. */
if ((*ptype == PPTP_OutCallReply || *ptype == PPTP_InCallReply) &&
@ -308,7 +316,7 @@ AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch
sizeof(struct pptpCodes)))
return (NULL);
else
return (PptpCallId)(hptr + 1);
return (PptpCallId) (hptr + 1);
}
@ -318,10 +326,10 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
GreHdr *gr;
struct alias_link *link;
gr = (GreHdr *)((char *)pip + (pip->ip_hl << 2));
gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
/* Check GRE header bits. */
if ((ntohl(*((u_int32_t *)gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
return (-1);
link = FindPptpOutByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
@ -330,12 +338,11 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
/* Change source IP address. */
DifferentialChecksum(&pip->ip_sum,
(u_short *)&alias_addr,
(u_short *)&pip->ip_src,
(u_short *) & alias_addr,
(u_short *) & pip->ip_src,
2);
pip->ip_src = alias_addr;
}
return (0);
}
@ -346,10 +353,10 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
GreHdr *gr;
struct alias_link *link;
gr = (GreHdr *)((char *)pip + (pip->ip_hl << 2));
gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
/* Check GRE header bits. */
if ((ntohl(*((u_int32_t *)gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
return (-1);
link = FindPptpInByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
@ -361,11 +368,10 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
/* Restore original IP address. */
DifferentialChecksum(&pip->ip_sum,
(u_short *)&src_addr,
(u_short *)&pip->ip_dst,
(u_short *) & src_addr,
(u_short *) & pip->ip_dst,
2);
pip->ip_dst = src_addr;
}
return (0);
}

View File

@ -84,8 +84,7 @@ __FBSDID("$FreeBSD$");
* A linked list of arbitrary length, based on struct proxy_entry is
* used to store proxy rules.
*/
struct proxy_entry
{
struct proxy_entry {
struct libalias *la;
#define PROXY_TYPE_ENCODE_NONE 1
#define PROXY_TYPE_ENCODE_TCPSTREAM 2
@ -153,7 +152,7 @@ IpMask(int nbits, struct in_addr *mask)
return -1;
imask = 0;
for (i=0; i<nbits; i++)
for (i = 0; i < nbits; i++)
imask = (imask >> 1) + 0x80000000;
mask->s_addr = htonl(imask);
@ -175,8 +174,7 @@ IpPort(char *s, int proto, int *port)
int n;
n = sscanf(s, "%d", port);
if (n != 1)
{
if (n != 1) {
struct servent *se;
if (proto == IPPROTO_TCP)
@ -191,7 +189,6 @@ IpPort(char *s, int proto, int *port)
*port = (u_int) ntohs(se->s_port);
}
return 0;
}
@ -202,8 +199,7 @@ RuleAdd(struct libalias *la, struct proxy_entry *entry)
struct proxy_entry *ptr;
struct proxy_entry *ptr_last;
if (la->proxyList == NULL)
{
if (la->proxyList == NULL) {
la->proxyList = entry;
entry->last = NULL;
entry->next = NULL;
@ -214,19 +210,15 @@ RuleAdd(struct libalias *la, struct proxy_entry *entry)
rule_index = entry->rule_index;
ptr = la->proxyList;
ptr_last = NULL;
while (ptr != NULL)
{
if (ptr->rule_index >= rule_index)
{
if (ptr_last == NULL)
{
while (ptr != NULL) {
if (ptr->rule_index >= rule_index) {
if (ptr_last == NULL) {
entry->next = la->proxyList;
entry->last = NULL;
la->proxyList->last = entry;
la->proxyList = entry;
return;
}
ptr_last->next = entry;
ptr->last = entry;
entry->last = ptr->last;
@ -267,17 +259,14 @@ RuleNumberDelete(struct libalias *la, int rule_index)
err = -1;
ptr = la->proxyList;
while (ptr != NULL)
{
while (ptr != NULL) {
struct proxy_entry *ptr_next;
ptr_next = ptr->next;
if (ptr->rule_index == rule_index)
{
if (ptr->rule_index == rule_index) {
err = 0;
RuleDelete(ptr);
}
ptr = ptr_next;
}
@ -294,21 +283,20 @@ ProxyEncodeTcpStream(struct alias_link *link,
struct tcphdr *tc;
/* Compute pointer to tcp header */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
/* Don't modify if once already modified */
if (GetAckModified (link))
if (GetAckModified(link))
return;
/* Translate destination address and port to string form */
snprintf(buffer, sizeof(buffer) - 2, "[DEST %s %d]",
inet_ntoa(GetProxyAddress (link)), (u_int) ntohs(GetProxyPort (link)));
inet_ntoa(GetProxyAddress(link)), (u_int) ntohs(GetProxyPort(link)));
/* Pad string out to a multiple of two in length */
slen = strlen(buffer);
switch (slen % 2)
{
switch (slen % 2) {
case 0:
strcat(buffer, " \n");
slen += 2;
@ -329,14 +317,14 @@ ProxyEncodeTcpStream(struct alias_link *link,
u_char *p;
hlen = (pip->ip_hl + tc->th_off) << 2;
dlen = ntohs (pip->ip_len) - hlen;
dlen = ntohs(pip->ip_len) - hlen;
/* Modify first packet that has data in it */
if (dlen == 0)
return;
p = (char *) pip;
p = (char *)pip;
p += hlen;
memmove(p + slen, p, dlen);
@ -349,7 +337,7 @@ ProxyEncodeTcpStream(struct alias_link *link,
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
AddSeq(pip, link, delta+slen);
AddSeq(pip, link, delta + slen);
}
/* Update IP header packet length and checksum */
@ -367,7 +355,7 @@ ProxyEncodeTcpStream(struct alias_link *link,
already changed. */
tc->th_sum = 0;
tc->th_sum = TcpChecksum (pip);
tc->th_sum = TcpChecksum(pip);
}
static void
@ -400,10 +388,10 @@ ProxyEncodeIpHeader(struct ip *pip,
option[0] = 0x64; /* class: 3 (reserved), option 4 */
option[1] = OPTION_LEN_BYTES;
memcpy(&option[2], (u_char *) &pip->ip_dst, 4);
memcpy(&option[2], (u_char *) & pip->ip_dst, 4);
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
memcpy(&option[6], (u_char *) &tc->th_sport, 2);
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
memcpy(&option[6], (u_char *) & tc->th_sport, 2);
memcpy(ptr, option, 8);
}
@ -416,7 +404,7 @@ ProxyEncodeIpHeader(struct ip *pip,
sptr = (u_short *) option;
accumulate = 0;
for (i=0; i<OPTION_LEN_INT16; i++)
for (i = 0; i < OPTION_LEN_INT16; i++)
accumulate -= *(sptr++);
sptr = (u_short *) pip;
@ -452,7 +440,7 @@ ProxyEncodeIpHeader(struct ip *pip,
int
ProxyCheck(struct libalias *la, struct ip *pip,
struct in_addr *proxy_server_addr,
u_short *proxy_server_port)
u_short * proxy_server_port)
{
u_short dst_port;
struct in_addr src_addr;
@ -461,19 +449,17 @@ ProxyCheck(struct libalias *la, struct ip *pip,
src_addr = pip->ip_src;
dst_addr = pip->ip_dst;
dst_port = ((struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)))
dst_port = ((struct tcphdr *)((char *)pip + (pip->ip_hl << 2)))
->th_dport;
ptr = la->proxyList;
while (ptr != NULL)
{
while (ptr != NULL) {
u_short proxy_port;
proxy_port = ptr->proxy_port;
if ((dst_port == proxy_port || proxy_port == 0)
&& pip->ip_p == ptr->proto
&& src_addr.s_addr != ptr->server_addr.s_addr)
{
&& src_addr.s_addr != ptr->server_addr.s_addr) {
struct in_addr src_addr_masked;
struct in_addr dst_addr_masked;
@ -481,8 +467,7 @@ ProxyCheck(struct libalias *la, struct ip *pip,
dst_addr_masked.s_addr = dst_addr.s_addr & ptr->dst_mask.s_addr;
if ((src_addr_masked.s_addr == ptr->src_addr.s_addr)
&& (dst_addr_masked.s_addr == ptr->dst_addr.s_addr))
{
&& (dst_addr_masked.s_addr == ptr->dst_addr.s_addr)) {
if ((*proxy_server_port = ptr->server_port) == 0)
*proxy_server_port = dst_port;
*proxy_server_addr = ptr->server_addr;
@ -501,8 +486,7 @@ ProxyModify(struct libalias *la, struct alias_link *link,
int maxpacketsize,
int proxy_type)
{
switch (proxy_type)
{
switch (proxy_type) {
case PROXY_TYPE_ENCODE_IPHDR:
ProxyEncodeIpHeader(pip, maxpacketsize);
break;
@ -571,7 +555,7 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
/* Convert to lower case */
len = strlen(buffer);
for (i=0; i<len; i++)
for (i = 0; i < len; i++)
buffer[i] = tolower((unsigned char)buffer[i]);
/* Set default proxy type */
@ -604,11 +588,9 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
state = STATE_READ_KEYWORD;
token = strsep(&res, " \t");
token_count = 0;
while (token != NULL)
{
while (token != NULL) {
token_count++;
switch (state)
{
switch (state) {
case STATE_READ_KEYWORD:
if (strcmp(token, "type") == 0)
state = STATE_READ_TYPE;
@ -657,14 +639,11 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
while (*p != ':' && *p != 0)
p++;
if (*p != ':')
{
if (*p != ':') {
err = IpAddr(token, &server_addr);
if (err)
return -1;
}
else
{
} else {
*p = ' ';
n = sscanf(token, "%s %s", s, str_server_port);
@ -725,15 +704,12 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
while (*p != '/' && *p != 0)
p++;
if (*p != '/')
{
if (*p != '/') {
IpMask(32, &mask);
err = IpAddr(token, &addr);
if (err)
return -1;
}
else
{
} else {
int nbits;
char s[sizeof(buffer)];
@ -751,13 +727,10 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
return -1;
}
if (state == STATE_READ_SRC)
{
if (state == STATE_READ_SRC) {
src_addr = addr;
src_mask = mask;
}
else
{
} else {
dst_addr = addr;
dst_mask = mask;
}
@ -788,29 +761,23 @@ LibAliasProxyRule(struct libalias *la, const char *cmd)
the string is parsed, because the prototype might not be designated
before the ports (which might be symbolic entries in /etc/services) */
if (strlen(str_port) != 0)
{
if (strlen(str_port) != 0) {
int err;
err = IpPort(str_port, proto, &proxy_port);
if (err)
return -1;
}
else
{
} else {
proxy_port = 0;
}
if (strlen(str_server_port) != 0)
{
if (strlen(str_server_port) != 0) {
int err;
err = IpPort(str_server_port, proto, &server_port);
if (err)
return -1;
}
else
{
} else {
server_port = 0;
}

View File

@ -88,7 +88,7 @@ struct skinny_header {
struct RegisterMessage {
u_int32_t msgId;
char devName[16];
char devName [16];
u_int32_t uid;
u_int32_t instance;
u_int32_t ipAddr;
@ -154,7 +154,10 @@ alias_skinny_startmedia(struct StartMediaTransmission *start_media,
dst.s_addr = start_media->remoteIpAddr;
src.s_addr = localIpAddr;
/* XXX I should probably handle in bound global translations as well. */
/*
* XXX I should probably handle in bound global translations as
* well.
*/
return 0;
}
@ -174,8 +177,8 @@ alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
static int
alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack,
struct ip * pip, struct tcphdr *tc,
struct alias_link *link, u_int32_t *localIpAddr,
struct ip *pip, struct tcphdr *tc,
struct alias_link *link, u_int32_t * localIpAddr,
ConvDirection direction)
{
struct in_addr null_addr;
@ -208,12 +211,12 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
int orig_len, skinny_hdr_len = sizeof(struct skinny_header);
ConvDirection direction;
tc = (struct tcphdr *) ((char *)pip + (pip->ip_hl << 2));
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;
sd = (struct skinny_header *) ((char *)pip + hlen);
sd = (struct skinny_header *)((char *)pip + hlen);
/*
* XXX This direction is reserved for future use. I still need to
@ -234,11 +237,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
orig_len = dlen;
/*
* Skinny packets can contain many messages. We need to loop through
* the packet using len to determine message boundaries. This comes
* into play big time with port messages being in the same packet as
* register messages. Also, open receive channel acks are
* usually buried in a pakcet some 400 bytes long.
* Skinny packets can contain many messages. We need to loop
* through the packet using len to determine message boundaries.
* This comes into play big time with port messages being in the
* same packet as register messages. Also, open receive channel
* acks are usually buried in a pakcet some 400 bytes long.
*/
while (dlen >= skinny_hdr_len) {
len = (sd->len);
@ -253,8 +256,7 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
return;
}
switch (msgId) {
case REG_MSG:
{
case REG_MSG: {
struct RegisterMessage *reg_mesg;
if (len < sizeof(struct RegisterMessage)) {
@ -264,17 +266,17 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
#endif
return;
}
reg_mesg = (struct RegisterMessage *) & sd->msgId;
reg_mesg = (struct RegisterMessage *)&sd->msgId;
#ifdef DEBUG
fprintf(stderr,
"PacketAlias/Skinny: Received a register message");
#endif
alias_skinny_reg_msg(reg_mesg, pip, tc, link, direction);
}
break;
case IP_PORT_MSG:
{
}
case IP_PORT_MSG: {
struct IpPortMessage *port_mesg;
if (len < sizeof(struct IpPortMessage)) {
#ifdef DEBUG
fprintf(stderr,
@ -286,12 +288,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr
"PacketAlias/Skinny: Received ipport message\n");
#endif
port_mesg = (struct IpPortMessage *) & sd->msgId;
port_mesg = (struct IpPortMessage *)&sd->msgId;
alias_skinny_port_msg(port_mesg, pip, tc, link, direction);
}
break;
case OPNRCVCH_ACK:
{
}
case OPNRCVCH_ACK: {
struct OpenReceiveChannelAck *opnrcvchn_ack;
if (len < sizeof(struct OpenReceiveChannelAck)) {
@ -305,12 +306,11 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr,
"PacketAlias/Skinny: Received open rcv channel msg\n");
#endif
opnrcvchn_ack = (struct OpenReceiveChannelAck *) & sd->msgId;
opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, link, &lip, direction);
}
break;
case START_MEDIATX:
{
}
case START_MEDIATX: {
struct StartMediaTransmission *startmedia_tx;
if (len < sizeof(struct StartMediaTransmission)) {
@ -324,15 +324,15 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *link)
fprintf(stderr,
"PacketAlias/Skinny: Received start media trans msg\n");
#endif
startmedia_tx = (struct StartMediaTransmission *) & sd->msgId;
startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
alias_skinny_startmedia(startmedia_tx, pip, tc, link, lip, direction);
}
break;
}
default:
break;
}
/* Place the pointer at the next message in the packet. */
dlen -= len + (skinny_hdr_len - sizeof(msgId));
sd = (struct skinny_header *) (((char *)&sd->msgId) + len);
sd = (struct skinny_header *)(((char *)&sd->msgId) + len);
}
}

View File

@ -155,7 +155,7 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
struct in_addr null_addr;
/* Calculate data length of TCP packet */
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
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;
@ -177,15 +177,14 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (pos < 0) {
break;
}
memcpy (port_newdata, port_data, pos + 1);
memcpy(port_newdata, port_data, pos + 1);
port_newdata += (pos + 1);
p[0] = p[1] = 0;
sport = eport = 0;
state = 0;
for (i = pos; i < port_dlen; i++) {
switch(state) {
switch (state) {
case 0:
if (port_data[i] == '=') {
state++;
@ -218,8 +217,12 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (!links_created) {
links_created = 1;
/* Find an even numbered port number base that
satisfies the contiguous number of ports we need */
/*
* Find an even numbered port
* number base that satisfies the
* contiguous number of ports we
* need
*/
null_addr.s_addr = 0;
if (0 == (salias = FindNewPortGroup(la, null_addr,
FindAliasAddress(la, pip->ip_src),
@ -234,13 +237,21 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
base_alias = ntohs(salias);
for (j = 0; j < RTSP_PORT_GROUP; j++) {
/* Establish link to port found in RTSP packet */
/*
* Establish link
* to port found in
* RTSP packet
*/
rtsp_link = FindRtspOut(la, GetOriginalAddress(link), null_addr,
htons(base_port + j), htons(base_alias + j),
IPPROTO_UDP);
if (rtsp_link != NULL) {
#ifndef NO_FW_PUNCH
/* Punch hole in firewall */
/*
* Punch
* hole in
* firewall
*/
PunchFWHole(rtsp_link);
#endif
} else {
@ -254,7 +265,6 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
}
ealias = htons(base_alias + (RTSP_PORT_GROUP - 1));
}
if (salias && rtsp_link) {
pkt_updated = 1;
@ -273,7 +283,6 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
memcpy(port_newdata, stemp, strlen(stemp));
port_newdata += strlen(stemp);
}
*port_newdata = ';';
port_newdata++;
}
@ -291,13 +300,13 @@ alias_rtsp_out(struct libalias *la, struct ip *pip,
if (!pkt_updated)
return -1;
memcpy (port_newdata, port_data, port_dlen);
memcpy(port_newdata, port_data, port_dlen);
port_newdata += port_dlen;
*port_newdata = '\0';
/* Create new packet */
new_dlen = port_newdata - newdata;
memcpy (data, newdata, new_dlen);
memcpy(data, newdata, new_dlen);
SetAckModified(link);
delta = GetDeltaSeqOut(pip, link);
@ -350,7 +359,7 @@ alias_pna_out(struct libalias *la, struct ip *pip,
/* Punch hole in firewall */
PunchFWHole(pna_links);
#endif
tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
alias_port = GetAliasPort(pna_links);
memcpy(work, &alias_port, 2);
@ -381,7 +390,7 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
tlen = ntohs(pip->ip_len);
dlen = tlen - hlen;
data = (char*)pip;
data = (char *)pip;
data += hlen;
/* When aliasing a client, check for the SETUP request */
@ -399,11 +408,12 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
alias_pna_out(la, pip, link, data, dlen);
}
}
} else {
/* When aliasing a server, check for the 200 reply
Accomodate varying number of blanks between 200 & OK */
/*
* When aliasing a server, check for the 200 reply
* Accomodate varying number of blanks between 200 & OK
*/
if (dlen >= strlen(str200)) {
@ -418,7 +428,7 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *link,
if (parseOk) {
i += strlen(str200); /* skip string found */
while(data[i] == ' ') /* skip blank(s) */
while (data[i] == ' ') /* skip blank(s) */
i++;
if ((dlen - i) >= strlen(okstr)) {

View File

@ -61,33 +61,31 @@ purposes);
#include "alias_local.h"
u_short
LibAliasInternetChecksum(struct libalias *la, u_short *ptr, int nbytes)
LibAliasInternetChecksum(struct libalias *la, u_short * ptr, int nbytes)
{
int sum, oddbyte;
sum = 0;
while (nbytes > 1)
{
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1)
{
if (nbytes == 1) {
oddbyte = 0;
((u_char *) &oddbyte)[0] = *(u_char *) ptr;
((u_char *) &oddbyte)[1] = 0;
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
((u_char *) & oddbyte)[1] = 0;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return(~sum);
return (~sum);
}
u_short
IpChecksum(struct ip *pip)
{
return( PacketAliasInternetChecksum((u_short *) pip,
(pip->ip_hl << 2)) );
return (PacketAliasInternetChecksum((u_short *) pip,
(pip->ip_hl << 2)));
}
@ -102,30 +100,27 @@ TcpChecksum(struct ip *pip)
nhdr = pip->ip_hl << 2;
ntcp = ntohs(pip->ip_len) - nhdr;
tc = (struct tcphdr *) ((char *) pip + nhdr);
tc = (struct tcphdr *)((char *)pip + nhdr);
ptr = (u_short *) tc;
/* Add up TCP header and data */
nbytes = ntcp;
sum = 0;
while (nbytes > 1)
{
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1)
{
if (nbytes == 1) {
oddbyte = 0;
((u_char *) &oddbyte)[0] = *(u_char *) ptr;
((u_char *) &oddbyte)[1] = 0;
((u_char *) & oddbyte)[0] = *(u_char *) ptr;
((u_char *) & oddbyte)[1] = 0;
sum += oddbyte;
}
/* "Pseudo-header" data */
ptr = (u_short *) &(pip->ip_dst);
ptr = (u_short *) & (pip->ip_dst);
sum += *ptr++;
sum += *ptr;
ptr = (u_short *) &(pip->ip_src);
ptr = (u_short *) & (pip->ip_src);
sum += *ptr++;
sum += *ptr;
sum += htons((u_short) ntcp);
@ -136,35 +131,30 @@ TcpChecksum(struct ip *pip)
sum += (sum >> 16);
/* Return checksum */
return((u_short) ~sum);
return ((u_short) ~ sum);
}
void
DifferentialChecksum(u_short *cksum, u_short *new, u_short *old, int n)
DifferentialChecksum(u_short * cksum, u_short * new, u_short * old, int n)
{
int i;
int accumulate;
accumulate = *cksum;
for (i=0; i<n; i++)
{
for (i = 0; i < n; i++) {
accumulate -= *new++;
accumulate += *old++;
}
if (accumulate < 0)
{
if (accumulate < 0) {
accumulate = -accumulate;
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
accumulate += accumulate >> 16;
*cksum = (u_short) ~accumulate;
}
else
{
*cksum = (u_short) ~ accumulate;
} else {
accumulate = (accumulate >> 16) + (accumulate & 0xffff);
accumulate += accumulate >> 16;
*cksum = (u_short) accumulate;
}
}