From owner-freebsd-net@FreeBSD.ORG Fri Jul 21 02:47:28 2006 Return-Path: X-Original-To: freebsd-net@freebsd.org Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8652E16A689 for ; Fri, 21 Jul 2006 02:47:28 +0000 (UTC) (envelope-from gnn@neville-neil.com) Received: from mrout2.yahoo.com (mrout2.yahoo.com [216.145.54.172]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2D98D43D55 for ; Fri, 21 Jul 2006 02:47:27 +0000 (GMT) (envelope-from gnn@neville-neil.com) Received: from minion.local.neville-neil.com (proxy7.corp.yahoo.com [216.145.48.98]) by mrout2.yahoo.com (8.13.6/8.13.6/y.out) with ESMTP id k6L2l4T5002390; Thu, 20 Jul 2006 19:47:09 -0700 (PDT) Date: Fri, 21 Jul 2006 11:05:40 +0900 Message-ID: From: gnn@freebsd.org To: "Andrew R. Reiter" In-Reply-To: <20060720104748.R94787@fledge.watson.org> References: <20060720104748.R94787@fledge.watson.org> User-Agent: Wanderlust/2.14.0 (Africa) SEMI/1.14.6 (Maruoka) FLIM/1.14.8 (=?ISO-8859-4?Q?Shij=F2?=) APEL/10.6 Emacs/22.0.50 (i386-apple-darwin8.6.1) MULE/5.0 (SAKAKI) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Cc: freebsd-net@freebsd.org Subject: Re: Packet Construction and Protocol Testing... X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Jul 2006 02:47:28 -0000 At Thu, 20 Jul 2006 10:48:14 -0400 (EDT), Andrew R. Reiter wrote: > > > Aren't there already tools for doing this -- libnet / libdnet that both > have py wrappers? I looked at all those, and more, but they miss an important point. That is, in PCS you define a packet like this (from pcs/packets/ipv4.py): def __init__(self, bytes = None): """ define the fields of an IPv4 packet, from RFC 791 This version does not include options.""" version = pcs.Field("version", 4, default = 4) hlen = pcs.Field("hlen", 4) tos = pcs.Field("tos", 8) length = pcs.Field("length", 16) id = pcs.Field("id", 16) flags = pcs.Field("flags", 3) offset = pcs.Field("offset", 13) ttl = pcs.Field("ttl", 8, default = 64) protocol = pcs.Field("protocol", 8) checksum = pcs.Field("checksum", 16) src = pcs.Field("src", 32) dst = pcs.Field("dst", 32) pcs.Packet.__init__(self, [version, hlen, tos, length, id, flags, offset, ttl, protocol, checksum, src, dst], bytes = bytes) # Description MUST be set after the PCS layer init self.description = "IPv4" which creates a properties in the object to hold the named field. This is what makes it possible to do: ip = ipv4() ip.ttl = 64 ip.src = inet_pton("128.32.1.1") etc. in your program. Also note that the bit lengths can be odd, such as getting the 13 bit offset field. So, PCS is doing all the packing and unpacking of the bytes for you. I intend to put in automatic bounds checking in an upcoming version. There is much more about this in the documentation, docs/pcs.pdf in the package. Future versions will allow import/export to various formats as well, such as XML, so that defining packets will be even easier as will writing tools. Later, George