rename src/nepi/ into just nepi/
[nepi.git] / nepi / resources / linux / tcpdump.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy 
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow
23
24 import os
25
26 @clsinit_copy
27 class LinuxTcpdump(LinuxApplication):
28     _rtype = "linux::Tcpdump"
29
30     @classmethod
31     def _register_attributes(cls):
32         A = Attribute("A",
33             "Sets tcpdump -A option. "
34             "Prints each packet (minus its link level header) in ASCII.",
35             type = Types.Bool,
36             default = False,
37             flags = Flags.Design)
38
39         b = Attribute("b",
40             "Sets tcpdump -b option. "
41             "Prints the AS number in BGP packets in ASDOT notation. ",
42             type = Types.Bool,
43             default = False,
44             flags = Flags.Design)
45
46         B = Attribute("B",
47             "Sets tcpdump -B option. "
48             "Sets the operaing system capture buffer size in untils of "
49             "KiB (1024 bytes).",
50             flags = Flags.Design)
51
52         c = Attribute("c",
53             "Sets tcpdump -c option. "
54             "Exists after receiving count packets.",
55             flags = Flags.Design)
56
57         C = Attribute("C",
58             "Sets tcpdump -C option. "
59             "Before writing a raw packet to a savefile, check whether the "
60             "file is currently larger than file_size and, if so, close the "
61             "current  savefile  and  open a new one. "
62             "Savefiles after the first savefile will have the name specified "
63             "with the -w with a number after it, starting at 1 and continuing "
64             "upward. ",
65             flags = Flags.Design)
66
67         d = Attribute("d",
68             "Sets tcpdump -d option. "
69             "Dump the compiled packet-matching code in a human readable form "
70             "to standard output and stop.",
71             type = Types.Bool,
72             default = False,
73             flags = Flags.Design)
74
75         dd = Attribute("dd",
76             "Sets tcpdump -dd option. "
77             "Dump packet-matching code as a C program fragment. ",
78             type = Types.Bool,
79             default = False,
80             flags = Flags.Design)
81
82         ddd = Attribute("ddd",
83             "Sets tcpdump -ddd option. "
84             "Dump packet-matching code as decimal numbers "
85             "(preceded with a count).",
86             type = Types.Bool,
87             default = False,
88             flags = Flags.Design)
89
90         D = Attribute("D",
91             "Sets tcpdump -D option. "
92             "Print the list of the network interfaces available on the system "
93             "and on which tcpdump can capture packets. ",
94             type = Types.Bool,
95             default = False,
96             flags = Flags.Design)
97
98         e = Attribute("e",
99             "Sets tcpdump -e option. "
100             "Print the link-level header on each dump line.",
101             type = Types.Bool,
102             default = False,
103             flags = Flags.Design)
104
105         F =  Attribute("F",
106             "Sets tcpdump -F option. "
107             "Use file as input for the filter expression.",
108             flags = Flags.Design)
109
110         G =  Attribute("G",
111             "Sets tcpdump -G option. "
112             "If specified, rotates the dump file specified with the -w "
113             "option every rotate_seconds seconds. ",
114             type = Types.Bool,
115             default = False,
116             flags = Flags.Design)
117
118         i =  Attribute("i",
119             "Sets tcpdump -i option. "
120             "Listen on interface.  If unspecified, tcpdump searches the "
121             "system interface list for the lowest  numbered, configured "
122             "up interface (excluding loopback). ",
123             flags = Flags.Design)
124
125         I =  Attribute("I",
126             "Sets tcpdump -I option. "
127             "Put the interface in 'monitor mode'; this is supported only "
128             "on IEEE 802.11 Wi-Fi interfaces, and supported only on some "
129             "operating systems. ",
130             type = Types.Bool,
131             default = False,
132             flags = Flags.Design)
133
134         j = Attribute("j",
135             "Sets tcpdump -j option. "
136             "Sets the time stamp type for the capture to tstamp_type. "
137             "The names to use for the time stamp types are given in "
138             "pcap-tstamp-type(7); not all the types listed there will "
139             "necessarily be valid for any given interface.",
140             flags = Flags.Design)
141
142         K = Attribute("K",
143             "Sets tcpdump -K option. "
144             "Don't attempt to verify IP, TCP, or UDP checksums. ",
145             type = Types.Bool,
146             default = False,
147             flags = Flags.Design)
148
149         l = Attribute("l",
150             "Sets tcpdump -l option. "
151             "Make stdout line buffered. ",
152             type = Types.Bool,
153             default = False,
154             flags = Flags.Design)
155
156         U = Attribute("U",
157             "Sets tcpdump -U option. "
158             "Similar to -l in its behavior, but it will cause output to be "
159             "``packet-buffered'', so that the output is written to stdout "
160             "at the end of each packet. ",
161             type = Types.Bool,
162             default = False,
163             flags = Flags.Design)
164
165         n = Attribute("n",
166             "Sets tcpdump -n option. "
167             "Don't convert addresses (i.e., host addresses, port numbers, "
168             "etc.) to names.",
169             type = Types.Bool,
170             default = False,
171             flags = Flags.Design)
172
173         N = Attribute("N",
174             "Sets tcpdump -N option. "
175             "Don't  print domain name qualification of host names. "
176             "E.g., if you give this flag then tcpdump will print ``nic'' " 
177             "instead of ``nic.ddn.mil''.",
178             type = Types.Bool,
179             default = False,
180             flags = Flags.Design)
181
182         S = Attribute("S",
183             "Sets tcpdump -S option. "
184             "Print absolute, rather than relative, TCP sequence numbers.",
185             type = Types.Bool,
186             default = False,
187             flags = Flags.Design)
188
189         s = Attribute("s",
190             "Sets tcpdump -s option. "
191             "Snarf  snaplen bytes of data from each packet rather than "
192             "the default of 65535 bytes. ",
193             flags = Flags.Design)
194
195         T = Attribute("T",
196             "Sets tcpdump -T option. "
197              "Force packets selected by 'expression' to be interpreted the "
198              "specified type.  Currently known types are aodv  (Ad-hoc "
199              "On-demand  Distance Vector protocol), cnfp (Cisco NetFlow "
200              "protocol), rpc (Remote Procedure Call), rtp (Real-Time "
201              "Applications protocol), rtcp (Real-Time Applications control "
202              "protocol), snmp (Simple Network Management Protocol), tftp "
203              "(Trivial  File Transfer Protocol), vat (Visual Audio Tool), "
204              "and wb (distributed White Board).",
205             flags = Flags.Design)
206
207         t = Attribute("t",
208             "Sets tcpdump -t option. "
209             "Don't print a timestamp on each dump line.",
210             type = Types.Bool,
211             default = False,
212             flags = Flags.Design)
213
214         tt = Attribute("tt",
215             "Sets tcpdump -tt option. "
216             "Print an unformatted timestamp on each dump line. ",
217             type = Types.Bool,
218             default = False,
219             flags = Flags.Design)
220
221         ttt = Attribute("ttt",
222             "Sets tcpdump -ttt option. "
223             "Print a delta (micro-second resolution) between current "
224             "and previous line on each dump line.",
225             type = Types.Bool,
226             default = False,
227             flags = Flags.Design)
228
229         tttt = Attribute("tttt",
230             "Sets tcpdump -tttt option. "
231             "Print a timestamp in default format proceeded by date on "
232             "each dump line. ",
233             type = Types.Bool,
234             default = False,
235             flags = Flags.Design)
236
237         ttttt = Attribute("ttttt",
238             "Sets tcpdump -ttttt option. "
239             "Print a delta (micro-second resolution) between current and "
240             "first line on each dump line.",
241             type = Types.Bool,
242             default = False,
243             flags = Flags.Design)
244
245         v = Attribute("v",
246             "Sets tcpdump -v option. "
247             "When  parsing  and printing, produce (slightly more) "
248             "verbose output. ",
249             type = Types.Bool,
250             default = False,
251             flags = Flags.Design)
252
253         vv = Attribute("vv",
254             "Sets tcpdump -vv option. "
255             "Even  more  verbose  output. ",
256             type = Types.Bool,
257             default = False,
258             flags = Flags.Design)
259
260         vvv = Attribute("vvv",
261             "Sets tcpdump -vv option. "
262             "Even  more  verbose  output. ",
263             type = Types.Bool,
264             default = False,
265             flags = Flags.Design)
266
267         w = Attribute("w",
268             "Sets tcpdump -w option. "
269             "Write  the  raw  packets to file rather than parsing "
270             "and printing them out.",
271             type = Types.Bool,
272             default = False,
273             flags = Flags.Design)
274
275         expression = Attribute("expression",
276             "selects  which packets will be dumped.",
277             flags = Flags.Design)
278
279         cls._register_attribute(A)
280         cls._register_attribute(b)
281         cls._register_attribute(B)
282         cls._register_attribute(c)
283         cls._register_attribute(C)
284         cls._register_attribute(d)
285         cls._register_attribute(dd)
286         cls._register_attribute(ddd)
287         cls._register_attribute(D)
288         cls._register_attribute(e)
289         cls._register_attribute(F)
290         cls._register_attribute(G)
291         cls._register_attribute(i)
292         cls._register_attribute(I)
293         cls._register_attribute(j)
294         cls._register_attribute(K)
295         cls._register_attribute(l)
296         cls._register_attribute(U)
297         cls._register_attribute(n)
298         cls._register_attribute(N)
299         cls._register_attribute(S)
300         cls._register_attribute(s)
301         cls._register_attribute(T)
302         cls._register_attribute(t)
303         cls._register_attribute(tt)
304         cls._register_attribute(ttt)
305         cls._register_attribute(tttt)
306         cls._register_attribute(ttttt)
307         cls._register_attribute(v)
308         cls._register_attribute(vv)
309         cls._register_attribute(vvv)
310         cls._register_attribute(w)
311         cls._register_attribute(expression)
312
313     def __init__(self, ec, guid):
314         super(LinuxTcpdump, self).__init__(ec, guid)
315         self._home = "tcpdump-%s" % self.guid
316         self._sudo_kill = True
317
318     def do_deploy(self):
319         if not self.get("command"):
320             self.set("command", self._start_command)
321
322         if not self.get("env"):
323             self.set("env", "PATH=$PATH:/usr/sbin/")
324
325         if not self.get("depends"):
326             self.set("depends", "tcpdump")
327
328         super(LinuxTcpdump, self).do_deploy()
329
330     @property
331     def _start_command(self):
332         args = []
333         args.append("sudo -S tcpdump")
334         if self.get("A") == True:
335             args.append("-A")
336         if self.get("b") == True:
337             args.append("-b")
338         if self.get("B"):
339             args.append("-B %s" % self.get("B"))
340         if self.get("c"):
341             args.append("-c %s" % self.get("c"))
342         if self.get("C"):
343             args.append("-C %s" % self.get("C"))
344         if self.get("d") == True:
345             args.append("-d")
346         if self.get("dd") == True:
347             args.append("-dd")
348         if self.get("ddd") == True:
349             args.append("-ddd")
350         if self.get("D") == True:
351             args.append("-D")
352         if self.get("e") == True:
353             args.append("-e")
354         if self.get("F"):
355             args.append("-F %s" % self.get("F"))
356         if self.get("G") == True:
357             args.append("-G")
358         if self.get("i"):
359             args.append("-i %s" % self.get("i"))
360         if self.get("I") == True:
361             args.append("-I")
362         if self.get("j"):
363             args.append("-j %s" % self.get("j"))
364         if self.get("K") == True:
365             args.append("-K")
366         if self.get("l") == True:
367             args.append("-l")
368         if self.get("U") == True:
369             args.append("-U")
370         if self.get("n") == True:
371             args.append("-n")
372         if self.get("N") == True:
373             args.append("-N")
374         if self.get("S") == True:
375             args.append("-S")
376         if self.get("s"):
377             args.append("-s %s" % self.get("s"))
378         if self.get("T"):
379             args.append("-T %s" % self.get("T"))
380         if self.get("t") == True:
381             args.append("-t")
382         if self.get("tt") == True:
383             args.append("-tt")
384         if self.get("ttt") == True:
385             args.append("-ttt")
386         if self.get("tttt") == True:
387             args.append("-tttt")
388         if self.get("ttttt") == True:
389             args.append("-ttttt")
390         if self.get("v") == True:
391             args.append("-v")
392         if self.get("vv") == True:
393             args.append("-vv")
394         if self.get("vvv") == True:
395             args.append("-vvv")
396         if self.get("w"):
397             args.append("-w %s" % self.get("w"))
398         if self.get("expression"):
399             args.append(self.get("expression"))
400
401         command = " ".join(args)
402
403         return command
404
405     def valid_connection(self, guid):
406         # TODO: Validate!
407         return True
408