2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
28 class LinuxTcpdump(LinuxApplication):
29 _rtype = "LinuxTcpdump"
32 def _register_attributes(cls):
34 "Sets tcpdump -A option. "
35 "Prints each packet (minus its link level header) in ASCII.",
41 "Sets tcpdump -b option. "
42 "Prints the AS number in BGP packets in ASDOT notation. ",
48 "Sets tcpdump -B option. "
49 "Sets the operaing system capture buffer size in untils of "
54 "Sets tcpdump -c option. "
55 "Exists after receiving count packets.",
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 "
69 "Sets tcpdump -d option. "
70 "Dump the compiled packet-matching code in a human readable form "
71 "to standard output and stop.",
77 "Sets tcpdump -dd option. "
78 "Dump packet-matching code as a C program fragment. ",
83 ddd = Attribute("ddd",
84 "Sets tcpdump -ddd option. "
85 "Dump packet-matching code as decimal numbers "
86 "(preceded with a count).",
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. ",
100 "Sets tcpdump -e option. "
101 "Print the link-level header on each dump line.",
104 flags = Flags.Design)
107 "Sets tcpdump -F option. "
108 "Use file as input for the filter expression.",
109 flags = Flags.Design)
112 "Sets tcpdump -G option. "
113 "If specified, rotates the dump file specified with the -w "
114 "option every rotate_seconds seconds. ",
117 flags = Flags.Design)
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)
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. ",
133 flags = Flags.Design)
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)
144 "Sets tcpdump -K option. "
145 "Don't attempt to verify IP, TCP, or UDP checksums. ",
148 flags = Flags.Design)
151 "Sets tcpdump -l option. "
152 "Make stdout line buffered. ",
155 flags = Flags.Design)
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. ",
164 flags = Flags.Design)
167 "Sets tcpdump -n option. "
168 "Don't convert addresses (i.e., host addresses, port numbers, "
172 flags = Flags.Design)
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''.",
181 flags = Flags.Design)
184 "Sets tcpdump -S option. "
185 "Print absolute, rather than relative, TCP sequence numbers.",
188 flags = Flags.Design)
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)
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)
209 "Sets tcpdump -t option. "
210 "Don't print a timestamp on each dump line.",
213 flags = Flags.Design)
216 "Sets tcpdump -tt option. "
217 "Print an unformatted timestamp on each dump line. ",
220 flags = Flags.Design)
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.",
228 flags = Flags.Design)
230 tttt = Attribute("tttt",
231 "Sets tcpdump -tttt option. "
232 "Print a timestamp in default format proceeded by date on "
236 flags = Flags.Design)
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.",
244 flags = Flags.Design)
247 "Sets tcpdump -v option. "
248 "When parsing and printing, produce (slightly more) "
252 flags = Flags.Design)
255 "Sets tcpdump -vv option. "
256 "Even more verbose output. ",
259 flags = Flags.Design)
261 vvv = Attribute("vvv",
262 "Sets tcpdump -vv option. "
263 "Even more verbose output. ",
266 flags = Flags.Design)
269 "Sets tcpdump -w option. "
270 "Write the raw packets to file rather than parsing "
271 "and printing them out.",
274 flags = Flags.Design)
276 expression = Attribute("expression",
277 "selects which packets will be dumped.",
278 flags = Flags.Design)
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)
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
320 if not self.get("command"):
321 self.set("command", self._start_command)
323 if not self.get("env"):
324 self.set("env", "PATH=$PATH:/usr/sbin/")
326 if not self.get("depends"):
327 self.set("depends", "tcpdump")
329 super(LinuxTcpdump, self).do_deploy()
332 def _start_command(self):
334 args.append("sudo -S tcpdump")
335 if self.get("A") == True:
337 if self.get("b") == True:
340 args.append("-B %s" % self.get("B"))
342 args.append("-c %s" % self.get("c"))
344 args.append("-C %s" % self.get("C"))
345 if self.get("d") == True:
347 if self.get("dd") == True:
349 if self.get("ddd") == True:
351 if self.get("D") == True:
353 if self.get("e") == True:
356 args.append("-F %s" % self.get("F"))
357 if self.get("G") == True:
360 args.append("-i %s" % self.get("i"))
361 if self.get("I") == True:
364 args.append("-j %s" % self.get("j"))
365 if self.get("K") == True:
367 if self.get("l") == True:
369 if self.get("U") == True:
371 if self.get("n") == True:
373 if self.get("N") == True:
375 if self.get("S") == True:
378 args.append("-s %s" % self.get("s"))
380 args.append("-T %s" % self.get("T"))
381 if self.get("t") == True:
383 if self.get("tt") == True:
385 if self.get("ttt") == True:
387 if self.get("tttt") == True:
389 if self.get("ttttt") == True:
390 args.append("-ttttt")
391 if self.get("v") == True:
393 if self.get("vv") == True:
395 if self.get("vvv") == True:
398 args.append("-w %s" % self.get("w"))
399 if self.get("expression"):
400 args.append(self.get("expression"))
402 command = " ".join(args)
406 def valid_connection(self, guid):