diff options
Diffstat (limited to 'main/common/tcpstuff.c')
-rw-r--r-- | main/common/tcpstuff.c | 202 |
1 files changed, 102 insertions, 100 deletions
diff --git a/main/common/tcpstuff.c b/main/common/tcpstuff.c index 8122637..f43e60a 100644 --- a/main/common/tcpstuff.c +++ b/main/common/tcpstuff.c @@ -1,7 +1,7 @@ /************************************************************************** * * Copyright (c) 2013 Alcatel-Lucent - * + * * Alcatel Lucent licenses this file to You under the Apache License, * Version 2.0 (the "License"); you may not use this file except in * compliance with the License. A copy of the License is contained the @@ -43,126 +43,128 @@ #include "stddefs.h" /* SendTcpConnectionReset(): - * Called in response to any incoming TCP request. - * It is used to let the sender know that the TCP connection is being - * refused. + * Called in response to any incoming TCP request. + * It is used to let the sender know that the TCP connection is being + * refused. */ int SendTcpConnectionReset(struct ether_header *re) { - short datalen, flags; - ulong ackno; - struct ether_header *te; - struct ip *ti, *ri; - struct tcphdr *ttcp, *rtcp; - - ri = (struct ip *) (re + 1); - datalen = ecs(ri->ip_len) - ((ri->ip_vhl & 0x0f) * 4); - - te = EtherCopy(re); - ti = (struct ip *) (te + 1); - ti->ip_vhl = ri->ip_vhl; - ti->ip_tos = ri->ip_tos; - ti->ip_len = ri->ip_len; - ti->ip_id = ri->ip_id; - ti->ip_off = ecs(IP_DONTFRAG); - ti->ip_ttl = ri->ip_ttl/2; - ti->ip_p = ri->ip_p; - memcpy((char *)&(ti->ip_src.s_addr),(char *)&(ri->ip_dst.s_addr), - sizeof(struct in_addr)); - memcpy((char *)&(ti->ip_dst.s_addr),(char *)&(ri->ip_src.s_addr), - sizeof(struct in_addr)); - - ttcp = (struct tcphdr *) (ti + 1); - rtcp = (struct tcphdr *) (ri + 1); - flags = ecs(rtcp->flags); - flags &= ~TCP_FLAGMASK; - flags |= (TCP_ACK | TCP_RESET); - ttcp->flags = ecs(flags); - ttcp->sport = rtcp->dport; - ttcp->dport = rtcp->sport; - memset((char *)&ttcp->seqno,0,4); - memcpy((char *)&ackno,(char *)&rtcp->seqno,4); - self_ecl(ackno); - ackno+=1; - self_ecl(ackno); - memcpy((char *)&ttcp->ackno,(char *)&ackno,4); - - ttcp->windowsize = 0; - ttcp->urgentptr = 0; - memcpy((char *)(ttcp+1),(char *)(rtcp+1),datalen-sizeof(struct tcphdr)); - - ipChksum(ti); /* compute checksum of ip hdr: (3rd Edition Comer pg 100) */ - tcpChksum(ti); /* Compute TCP checksum */ - - sendBuffer(sizeof(struct ether_header) + ecs(ti->ip_len)); - return(0); + short datalen, flags; + ulong ackno; + struct ether_header *te; + struct ip *ti, *ri; + struct tcphdr *ttcp, *rtcp; + + ri = (struct ip *)(re + 1); + datalen = ecs(ri->ip_len) - ((ri->ip_vhl & 0x0f) * 4); + + te = EtherCopy(re); + ti = (struct ip *)(te + 1); + ti->ip_vhl = ri->ip_vhl; + ti->ip_tos = ri->ip_tos; + ti->ip_len = ri->ip_len; + ti->ip_id = ri->ip_id; + ti->ip_off = ecs(IP_DONTFRAG); + ti->ip_ttl = ri->ip_ttl/2; + ti->ip_p = ri->ip_p; + memcpy((char *)&(ti->ip_src.s_addr),(char *)&(ri->ip_dst.s_addr), + sizeof(struct in_addr)); + memcpy((char *)&(ti->ip_dst.s_addr),(char *)&(ri->ip_src.s_addr), + sizeof(struct in_addr)); + + ttcp = (struct tcphdr *)(ti + 1); + rtcp = (struct tcphdr *)(ri + 1); + flags = ecs(rtcp->flags); + flags &= ~TCP_FLAGMASK; + flags |= (TCP_ACK | TCP_RESET); + ttcp->flags = ecs(flags); + ttcp->sport = rtcp->dport; + ttcp->dport = rtcp->sport; + memset((char *)&ttcp->seqno,0,4); + memcpy((char *)&ackno,(char *)&rtcp->seqno,4); + self_ecl(ackno); + ackno+=1; + self_ecl(ackno); + memcpy((char *)&ttcp->ackno,(char *)&ackno,4); + + ttcp->windowsize = 0; + ttcp->urgentptr = 0; + memcpy((char *)(ttcp+1),(char *)(rtcp+1),datalen-sizeof(struct tcphdr)); + + ipChksum(ti); /* compute checksum of ip hdr: (3rd Edition Comer pg 100) */ + tcpChksum(ti); /* Compute TCP checksum */ + + sendBuffer(sizeof(struct ether_header) + ecs(ti->ip_len)); + return(0); } -/* tcpChksum(): - * Compute the checksum of the TCP packet. - * The incoming pointer is to an ip header, the tcp header after that ip - * header is directly populated with the result. Similar to the udp - * checksum calculation. This one is mandatory. +/* tcpChksum(): + * Compute the checksum of the TCP packet. + * The incoming pointer is to an ip header, the tcp header after that ip + * header is directly populated with the result. Similar to the udp + * checksum calculation. This one is mandatory. */ void tcpChksum(struct ip *ihdr) { - register int i; - register ushort *sp; - register ulong t; - short len; - struct tcphdr *thdr; - struct UdpPseudohdr pseudohdr; - - thdr = (struct tcphdr *)(ihdr+1); - thdr->tcpcsum = 0; - - /* Start by building the pseudo header: */ - memcpy((char *)&pseudohdr.ip_src.s_addr,(char *)&ihdr->ip_src.s_addr,4); - memcpy((char *)&pseudohdr.ip_dst.s_addr,(char *)&ihdr->ip_dst.s_addr,4); - pseudohdr.zero = 0; - pseudohdr.proto = ihdr->ip_p; - len = ecs(ihdr->ip_len) - sizeof(struct ip); - pseudohdr.ulen = ecs(len); - - /* Get checksum of pseudo header: */ - sp = (ushort *) &pseudohdr; - for (i=0,t=0;i<(sizeof(struct UdpPseudohdr)/sizeof(ushort));i++,sp++) - t += *sp; - - /* If length is odd, pad and add one. */ - if (len & 1) { - uchar *ucp; - ucp = (uchar *)thdr; - ucp[len] = 0; - len++; - } - len >>= 1; - - sp = (ushort *) thdr; - for (i=0;i<len;i++,sp++) - t += *sp; - t = (t & 0xffff) + (t >> 16); - thdr->tcpcsum = ~t; + register int i; + register ushort *sp; + register ulong t; + short len; + struct tcphdr *thdr; + struct UdpPseudohdr pseudohdr; + + thdr = (struct tcphdr *)(ihdr+1); + thdr->tcpcsum = 0; + + /* Start by building the pseudo header: */ + memcpy((char *)&pseudohdr.ip_src.s_addr,(char *)&ihdr->ip_src.s_addr,4); + memcpy((char *)&pseudohdr.ip_dst.s_addr,(char *)&ihdr->ip_dst.s_addr,4); + pseudohdr.zero = 0; + pseudohdr.proto = ihdr->ip_p; + len = ecs(ihdr->ip_len) - sizeof(struct ip); + pseudohdr.ulen = ecs(len); + + /* Get checksum of pseudo header: */ + sp = (ushort *) &pseudohdr; + for(i=0,t=0; i<(sizeof(struct UdpPseudohdr)/sizeof(ushort)); i++,sp++) { + t += *sp; + } + + /* If length is odd, pad and add one. */ + if(len & 1) { + uchar *ucp; + ucp = (uchar *)thdr; + ucp[len] = 0; + len++; + } + len >>= 1; + + sp = (ushort *) thdr; + for(i=0; i<len; i++,sp++) { + t += *sp; + } + t = (t & 0xffff) + (t >> 16); + thdr->tcpcsum = ~t; } void tcperrmsg(char *msg) { - printf("TCP error: %s\n",msg); + printf("TCP error: %s\n",msg); } void processTCP(struct ether_header *ehdr,ushort size) { - //struct ip *ipp; - //struct tcphdr *tcpp; + //struct ip *ipp; + //struct tcphdr *tcpp; - //ipp = (struct ip *)(ehdr + 1); - //tcpp = (struct tcphdr *)((char *)ipp + IP_HLEN(ipp)); - SendTcpConnectionReset(ehdr); - return; + //ipp = (struct ip *)(ehdr + 1); + //tcpp = (struct tcphdr *)((char *)ipp + IP_HLEN(ipp)); + SendTcpConnectionReset(ehdr); + return; } #endif |