PERFORCE change 142591 for review

Victor Hugo Bilouro bilouro at FreeBSD.org
Fri May 30 17:11:06 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=142591

Change 142591 by bilouro at bilouro_tcptest on 2008/05/30 17:10:51

	This version is correctly sending SYN and receiving the SYN+ACK.

Affected files ...

.. //depot/projects/soc2008/bilouro_tcptest/src/scripts/tcpconnect.py#2 edit

Differences ...

==== //depot/projects/soc2008/bilouro_tcptest/src/scripts/tcpconnect.py#2 (text+ko) ====

@@ -1,7 +1,7 @@
-from pcs.packets.ipv4 import *
-from pcs.packets.tcp import *
-from pcs.packets.ethernet import *
-from pcs import *
+from pcs.packets import ipv4
+from pcs.packets import tcp
+from pcs.packets import ethernet
+import pcs
 
 def main():
 
@@ -38,15 +38,15 @@
     
     (options, args) = parser.parse_args()
 
-##todo: create syn. test exately equals a tcp dump.
-##todo: wait with timer the answer possibilities (in this a simple ack-syn)
-##todo: send an acknoledgment establishing the connection
+    ##todo: create syn. test exately equals a tcp dump. DONE
+    ##todo: wait with timer the answer possibilities (in this a simple ack-syn) DONE
+    ##todo: send an acknoledgment establishing the connection
 
-#
-# creating a durty-code ether-ip-tcp-syn packet 
-#
+    #
+    # creating a durty-code ether-ip-tcp-syn packet 
+    #
 
-    ip = ipv4()
+    ip = ipv4.ipv4()
 
     ip.version = 4                 # 0100 max 1111 15 *caution* :)
     ip.hlen = 5                 # 0101 0 ->  min 0101 (20 bytes) 
@@ -55,18 +55,12 @@
     ip.flags = 0                 #study on book
     ip.offset = 0
     ip.ttl = 64
-    ip.protocol = IPPROTO_TCP                 #1 ICMP #6 TCP
-    ip.src = inet_atol(options.ip_source)
-    ip.dst = inet_atol(options.ip_target)
-    #print ip.calc_checksum()
-    #print int(ip.calc_checksum())
-    #print ip_cksum(ip)
-    #ip.checksum = int(ip.calc_checksum())
-    ip.checksum = 63328 
-    #ip.checksum = ip_cksum(ip)
-    print ip.checksum
+    ip.protocol = pcs.IPPROTO_TCP                 #1 ICMP #6 TCP
+    ip.src = pcs.inet_atol(options.ip_source)
+    ip.dst = pcs.inet_atol(options.ip_target)
+    
 
-    tcppkt = tcp()
+    tcppkt = tcp.tcp()
 
     tcppkt.sport = int(options.source_port )
     tcppkt.dport = int(options.destination_port)
@@ -83,27 +77,28 @@
     tcppkt.urg_point = 0
     #tcppkt.options
 
+    tcppkt.checksum = tcp_cksum(tcppkt , ip)
+
     ip.length = len(ip.bytes) + len(tcppkt.bytes) 
 
-    #tcppkt.checksum = tcp_cksum(tcppkt , ip)
-    tcppkt.checksum = 954
+    # important, only calcs the ip checksum after fill length field
+    ip.checksum = ip_cksum(ip)
 
-
-    ether = ethernet()
-    ether.src = ether_atob(options.ether_source)
-    ether.dst = ether_atob(options.ether_destination)
+    ether = ethernet.ethernet()
+    ether.src = ethernet.ether_atob(options.ether_source)
+    ether.dst = ethernet.ether_atob(options.ether_destination)
     ether.type = 0x800
 
-    packet = Chain([ether, ip, tcppkt])
+    packet = pcs.Chain([ether, ip, tcppkt])
     
-    output = PcapConnector(options.interface)
+    output = pcs.PcapConnector(options.interface)
 
     out = output.write(packet.bytes, len(packet.bytes))
 
     reply = output.read()
     reply = output.read()
 
-    packet = ethernet(reply)
+    packet = ethernet.ethernet(reply)
     print "\n---------------------------------"
     print packet 
     print packet.data
@@ -111,16 +106,16 @@
     print packet.data.data
     print "---------------------------------"
 
-
-def tcp_cksum(self, ip, data = ""):
+def tcp_cksum(self, ip, data = ""):  #TODO: add this method to pcs tcp.py
     """return tcpv4 checksum"""
     import struct
     total = 0
-    tmpip = pseudoipv4()
+    
+    tmpip = ipv4.pseudoipv4()
     tmpip.src = ip.src
     tmpip.dst = ip.dst
     tmpip.reserved = 0 
-    tmpip.protocol = IPPROTO_TCP
+    tmpip.protocol = pcs.IPPROTO_TCP
     tmpip.length = len(self.getbytes()) + len(data)
     pkt = tmpip.getbytes() + self.getbytes() + data
     if len(pkt) % 2 == 1:
@@ -131,21 +126,26 @@
     total += total >> 16
     return  ~total & 0xffff
 
-def ip_cksum(self):
+def ip_cksum(self):    #TODO: solve the self problem, may be adding another arg
     """calculate the IPv4 checksum over a packet
 
     returns the calculated checksum
     """
+    import struct
     total = 0
-    packet = ipv4(self.bytes)
+    packet = ipv4.ipv4(self.bytes)
     packet.checksum = 0
     bytes = packet.bytes
+
     if len(bytes) % 2 == 1:
         bytes += "\0"
+
     for i in range(len(bytes)/2):
         total += (struct.unpack("!H", bytes[2*i:2*i+2])[0])
+
     total = (total >> 16) + (total & 0xffff)
     total += total >> 16
+
     return ~total & 0xffff
 
 


More information about the p4-projects mailing list