1
2
3 """Libpcap file format."""
4
5 import sys, time
6 import dpkt
7
8 TCPDUMP_MAGIC = 0xa1b2c3d4L
9 PMUDPCT_MAGIC = 0xd4c3b2a1L
10
11 PCAP_VERSION_MAJOR = 2
12 PCAP_VERSION_MINOR = 4
13
14 DLT_NULL = 0
15 DLT_EN10MB = 1
16 DLT_EN3MB = 2
17 DLT_AX25 = 3
18 DLT_PRONET = 4
19 DLT_CHAOS = 5
20 DLT_IEEE802 = 6
21 DLT_ARCNET = 7
22 DLT_SLIP = 8
23 DLT_PPP = 9
24 DLT_FDDI = 10
25 DLT_PFSYNC = 18
26 DLT_IEEE802_11 = 105
27 DLT_LINUX_SLL = 113
28 DLT_PFLOG = 117
29 DLT_IEEE802_11_RADIO = 127
30
31 if sys.platform.find('openbsd') != -1:
32 DLT_LOOP = 12
33 DLT_RAW = 14
34 else:
35 DLT_LOOP = 108
36 DLT_RAW = 12
37
38 dltoff = { DLT_NULL:4, DLT_EN10MB:14, DLT_IEEE802:22, DLT_ARCNET:6,
39 DLT_SLIP:16, DLT_PPP:4, DLT_FDDI:21, DLT_PFLOG:48, DLT_PFSYNC:4,
40 DLT_LOOP:4, DLT_LINUX_SLL:16 }
41
43 """pcap packet header."""
44 __hdr__ = (
45 ('tv_sec', 'I', 0),
46 ('tv_usec', 'I', 0),
47 ('caplen', 'I', 0),
48 ('len', 'I', 0),
49 )
50
53
55 """pcap file header."""
56 __hdr__ = (
57 ('magic', 'I', TCPDUMP_MAGIC),
58 ('v_major', 'H', PCAP_VERSION_MAJOR),
59 ('v_minor', 'H', PCAP_VERSION_MINOR),
60 ('thiszone', 'I', 0),
61 ('sigfigs', 'I', 0),
62 ('snaplen', 'I', 1500),
63 ('linktype', 'I', 1),
64 )
65
68
70 """Simple pcap dumpfile writer."""
78
94
97
99 """Simple pypcap-compatible pcap file reader."""
100
119
122
125
127 return NotImplementedError
128
131
132 - def dispatch(self, cnt, callback, *args):
133 if cnt > 0:
134 for i in range(cnt):
135 ts, pkt = self.next()
136 callback(ts, pkt, *args)
137 else:
138 for ts, pkt in self:
139 callback(ts, pkt, *args)
140
141 - def loop(self, callback, *args):
143
152
153 if __name__ == '__main__':
154 import unittest
155
158 be = '\xa1\xb2\xc3\xd4\x00\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x60\x00\x00\x00\x01'
159 le = '\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x60\x00\x00\x00\x01\x00\x00\x00'
160 befh = FileHdr(be)
161 lefh = LEFileHdr(le)
162 self.failUnless(befh.linktype == lefh.linktype)
163
164 unittest.main()
165