File: encap-pkt.py

package info (click to toggle)
python-libtrace 1.6%2Bgit20161027-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,124 kB
  • ctags: 1,357
  • sloc: ansic: 6,890; python: 3,228; makefile: 70; sh: 49
file content (53 lines) | stat: -rwxr-xr-x 1,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python

# Thu, 13 Mar 14 (PDT)
# encap-pkt.py: Test Packet methods and ip.new 
# Copyright (C) 2015, Nevil Brownlee, U Auckland | WAND

from plt_testing import *

t = get_example_trace('anon-v4.pcap')
#t = get_rlt_example_file('icmp-sample.pcap')

def compare_objects(a, b, msg):
   if a.data != b.data:  # Compare bytearrays
       print_data("a =", 5, a.data)
       print_data("b =", 5, b.data)
       print ">>> %s <<<" % msg
       sys.exit()

n = nip = 0;  offset = 12
for pkt in t:
    n += 1
    ip = pkt.ip
    if not ip:
        continue
    print "%5d:" % (n),
    print_ip(ip, offset)
    l3ip = plt.ip(pkt.layer3.data);
    compare_objects(ip, l3ip, "ip : layer3")

    tcp = pkt.tcp;  udp = pkt.udp;  icmp = pkt.icmp
    print "      ",
    if tcp:
        print_tcp(tcp, offset)
        ntcp = plt.tcp(ip)
        compare_objects(tcp, ntcp, "tcp : new tcp")
    elif udp:
        print_udp(udp, offset)
        nudp = plt.udp(ip)
        compare_objects(udp, nudp, "udp : new udp")
    elif icmp:
        print_icmp(icmp, offset)
        nicmp = plt.icmp(ip)
        compare_objects(icmp, nicmp, "icmp : new icmp")
    else:
        margin = ' ' * offset
        print "Unknown: proto=%d" % (ip.proto)
    print

    nip += 1
    if nip >= 15:
       break 

t.close