From owner-p4-projects@FreeBSD.ORG Fri May 30 17:11:06 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 760A61065687; Fri, 30 May 2008 17:11:06 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1C6C2106567D for ; Fri, 30 May 2008 17:11:06 +0000 (UTC) (envelope-from bilouro@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 190FF8FC1A for ; Fri, 30 May 2008 17:11:06 +0000 (UTC) (envelope-from bilouro@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m4UHB5n3057949 for ; Fri, 30 May 2008 17:11:05 GMT (envelope-from bilouro@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id m4UHB5E2057947 for perforce@freebsd.org; Fri, 30 May 2008 17:11:05 GMT (envelope-from bilouro@FreeBSD.org) Date: Fri, 30 May 2008 17:11:05 GMT Message-Id: <200805301711.m4UHB5E2057947@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to bilouro@FreeBSD.org using -f From: Victor Hugo Bilouro To: Perforce Change Reviews Cc: Subject: PERFORCE change 142591 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 May 2008 17:11:06 -0000 http://perforce.freebsd.org/chv.cgi?CH=142591 Change 142591 by bilouro@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