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