Common Documentation

This page contains the Common Package documentation.

The commontest Module

class netbench.common.commontest.AdvancedTests(methodName='runTest')

Bases: unittest.TestCase

Contains more advanced tests of the common part of the ANT library

setUp()
Sets up the environment for the tests. Packet are loaded from the test.pcap file into the object of self.traffic and packet object is created.
testCount()
Counts the number of the packets in the file. In test.pcap there should be 306 packets.
testUDP()
Tests if the netbench parse UDP packet correctly. Test that the values for the first UDP packet are the same as in wireshark
class netbench.common.commontest.BasicTests(methodName='runTest')

Bases: unittest.TestCase

Basics tests of the common part of the ANT library.

testFile()
Tries to open existing pcap file
testTraffic()
Tests if it is possible to create object of the Traffic class
testnonexistentFile()
Tries to open nonexisting file without .pcap suffix
testnonexistentFile1()
Tries to open nonexisting file with .pcap suffix.

The traffic Module

class netbench.common.traffic.Traffic

Bases: object

Class for parsing files with packets

next_packet(packet)
Returns next packet in the file.
set_file(filename)
Opens file with packets.
set_filter(filterstring)

The hash Module

Module providing several classes for hash functions.

class netbench.common.hash.Hash(seed)

Bases: object

Very slow Python hash of high quality

hash(key)

Compute Python hash of the key.

key: string

class netbench.common.hash.JenkinsHash(seed=0)

Bases: object

Bob Jenkins hash, as described at http://burtleburtle.net/bob/hash/doobs.html

get_last()
Return last hash result.
get_seed()
Return seed of the hash function.
hash(key)

Compute Jenkins Hash of the key.

key: list of integers, one for each byte.

set_seed(seed)

Set seed of the hash function.

seed: integer seed of the hash function.

class netbench.common.hash.SimpleHash(seed)

Bases: object

Very fast hash of poor quality

hash(key)

Compute Python hash of the key.

key: list of one byte integers

The packet Module

class netbench.common.packet.Packet

Bases: object

Class for Packet storage and packet header parsing.

Parsed packet can contain this fields:

ProtoHeader fields ———– ————

ethernet typelength
src_mac dst_mac

vlan type ipv4 version

header_len tos total_len id flags fragment_offset ttl protocol checksum src_addr dst_addr options
tcp src_port
dst_port data_offset tcp_flags tcp_window
udp src_port
dst_port udp_len udp_checksum
decode_ipv4(s, ipheader)
Dissector for IPv4 header
decode_l2(s, l2header)
Dissector for Ethernet header
decode_tcp(s, tcpheader)
Dissector for TCP header
decode_udp(s, udpheader)
Dissector for UDP header
decode_vlan(s, vlanheader)
Dissector for 802.1Q Virtual LAN header. It currently decodes next header type only.
dumphex()
Print out raw data of a packet in hexadecimal format.
get_packet_header()
Returns packet header
get_payload(type)
Returns payload of a packet belonging to a given protocol layer.
parse(level=10)

Parse packet header up to a specified level.

level
Specifies the maximum level a packet header should be parsed to.

The packetheader Module

class netbench.common.packetheader.PacketHeader(header_list=None)

Bases: object

Class for providing access to protocol headers

get_header(type, occurence=0)

Returns ProtoHeader of given type.

type
Name of protocol, for example: tcp.
occurence
Order of protocol in case two protocols are the same, e.g., IP in IP
get_headers_type()
Returns list of protocols in packet header.
iterfields()
Returns iterator through values of all fields in all headers.
push_header(protoheader)
Add protocol header into a list of headers
netbench.common.packetheader.int2bytelist(integer, bytes=4)

Convert integer number into list of one-byte numbers, [0] is MSB

integer: integer to convert.

netbench.common.packetheader.ipv4str2bytelist(ipstr)

Convert IP in string format x.x.x.x into list of numbers, one number for each byte, [0] is MSB.

ipstr: IPv4 addres in traditional text notation with dots.

netbench.common.packetheader.ipv4str2int(ipstr)

Convert IP in string format x.x.x.x into an integer,

ipstr: IPv4 addres in traditional text notation with dots.

The pcapfile Module

exception netbench.common.pcapfile.NotIndexedError
Bases: exceptions.Exception
exception netbench.common.pcapfile.NotOpenedError
Bases: exceptions.Exception
class netbench.common.pcapfile.PcapFile(filename=None)

Simple parser of pcap files. Extracts raw packet data from a given file.

You can open file by passing its name to constructor or by calling open() method.

For sequential reading of packets, call next_packet() method in a loop. It always returns next packet if the file (an instance of Packet class), or None if there aren’t any packets left.

To enable random access to packets, you must call index() method first. Than you can access packets in the file using indexes (ie. pcap[i]). Note that file is not loaded to memory by calling index(), only positions of packets are saved, so every access means reading from disk.

Writing of pcap files is not implemented (yet).

close()
Closes the file.
index()
Reads whole file and save positions of all packets. Also counts number of packets in the file.
next()
Returns next packet in the file. Raises StopIteration exception when EOF is reached. This method is called by Python to iterate in “for” loop.
next_packet()
Returns next packet in the file. Returns None when EOF is reached.
open(filename, mode='r')
Opens a given file in a given mode. Default (and only currently supported) mode is ‘r’, which means reading.
rewind()
Rewinds file to the begining so next read returns the first packet.

The protoheader Module

class netbench.common.protoheader.ProtoHeader(headertype, header_fields=None)

Bases: object

Class providing access to fields of protocol header

get_field(name)
Returns field of a given name
get_header_type()
Returns name of protocol header
set_field(name, value)
Set field of a given name to a given value

The headerfields Module

class netbench.common.headerfields.DstPort
Bases: netbench.common.headerfields.Port
class netbench.common.headerfields.Eth_DstMac
Bases: netbench.common.headerfields.Eth_Mac
class netbench.common.headerfields.Eth_Mac
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.Eth_SrcMac
Bases: netbench.common.headerfields.Eth_Mac
class netbench.common.headerfields.Eth_TypeLength
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.HeaderField
Bases: long
class netbench.common.headerfields.IP4_Address
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_Checksum
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_DstAddress
Bases: netbench.common.headerfields.IP4_Address
class netbench.common.headerfields.IP4_Flags
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_FragmentOffset
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_HeaderLen
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_ID
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_Options
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_Protocol

Bases: netbench.common.headerfields.HeaderField

toStr()
class netbench.common.headerfields.IP4_SrcAddress
Bases: netbench.common.headerfields.IP4_Address
class netbench.common.headerfields.IP4_TTL
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_ToS
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_TotalLen
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.IP4_Version
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.Port
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.SrcPort
Bases: netbench.common.headerfields.Port
class netbench.common.headerfields.TCP_DataOffset
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.TCP_DstPort
Bases: netbench.common.headerfields.DstPort
class netbench.common.headerfields.TCP_Flags
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.TCP_SrcPort
Bases: netbench.common.headerfields.SrcPort
class netbench.common.headerfields.TCP_Window
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.UDP_Checksum
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.UDP_DstPort
Bases: netbench.common.headerfields.DstPort
class netbench.common.headerfields.UDP_Length
Bases: netbench.common.headerfields.HeaderField
class netbench.common.headerfields.UDP_SrcPort
Bases: netbench.common.headerfields.SrcPort
class netbench.common.headerfields.VLAN_Type
Bases: netbench.common.headerfields.HeaderField