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 version 2 as
7 # published by the Free Software Foundation;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
27 class LinuxTcpdump(LinuxApplication):
28 _rtype = "linux::Tcpdump"
31 def _register_attributes(cls):
33 "Sets tcpdump -A option. "
34 "Prints each packet (minus its link level header) in ASCII.",
40 "Sets tcpdump -b option. "
41 "Prints the AS number in BGP packets in ASDOT notation. ",
47 "Sets tcpdump -B option. "
48 "Sets the operaing system capture buffer size in untils of "
53 "Sets tcpdump -c option. "
54 "Exists after receiving count packets.",
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 "
68 "Sets tcpdump -d option. "
69 "Dump the compiled packet-matching code in a human readable form "
70 "to standard output and stop.",
76 "Sets tcpdump -dd option. "
77 "Dump packet-matching code as a C program fragment. ",
82 ddd = Attribute("ddd",
83 "Sets tcpdump -ddd option. "
84 "Dump packet-matching code as decimal numbers "
85 "(preceded with a count).",
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. ",
99 "Sets tcpdump -e option. "
100 "Print the link-level header on each dump line.",
103 flags = Flags.Design)
106 "Sets tcpdump -F option. "
107 "Use file as input for the filter expression.",
108 flags = Flags.Design)
111 "Sets tcpdump -G option. "
112 "If specified, rotates the dump file specified with the -w "
113 "option every rotate_seconds seconds. ",
116 flags = Flags.Design)
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)
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. ",
132 flags = Flags.Design)
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)
143 "Sets tcpdump -K option. "
144 "Don't attempt to verify IP, TCP, or UDP checksums. ",
147 flags = Flags.Design)
150 "Sets tcpdump -l option. "
151 "Make stdout line buffered. ",
154 flags = Flags.Design)
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. ",
163 flags = Flags.Design)
166 "Sets tcpdump -n option. "
167 "Don't convert addresses (i.e., host addresses, port numbers, "
171 flags = Flags.Design)
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''.",
180 flags = Flags.Design)
183 "Sets tcpdump -S option. "
184 "Print absolute, rather than relative, TCP sequence numbers.",
187 flags = Flags.Design)
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)
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)
208 "Sets tcpdump -t option. "
209 "Don't print a timestamp on each dump line.",
212 flags = Flags.Design)
215 "Sets tcpdump -tt option. "
216 "Print an unformatted timestamp on each dump line. ",
219 flags = Flags.Design)
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.",
227 flags = Flags.Design)
229 tttt = Attribute("tttt",
230 "Sets tcpdump -tttt option. "
231 "Print a timestamp in default format proceeded by date on "
235 flags = Flags.Design)
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.",
243 flags = Flags.Design)
246 "Sets tcpdump -v option. "
247 "When parsing and printing, produce (slightly more) "
251 flags = Flags.Design)
254 "Sets tcpdump -vv option. "
255 "Even more verbose output. ",
258 flags = Flags.Design)
260 vvv = Attribute("vvv",
261 "Sets tcpdump -vv option. "
262 "Even more verbose output. ",
265 flags = Flags.Design)
268 "Sets tcpdump -w option. "
269 "Write the raw packets to file rather than parsing "
270 "and printing them out.",
273 flags = Flags.Design)
275 expression = Attribute("expression",
276 "selects which packets will be dumped.",
277 flags = Flags.Design)
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)
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
319 if not self.get("command"):
320 self.set("command", self._start_command)
322 if not self.get("env"):
323 self.set("env", "PATH=$PATH:/usr/sbin/")
325 if not self.get("depends"):
326 self.set("depends", "tcpdump")
328 super(LinuxTcpdump, self).do_deploy()
331 def _start_command(self):
333 args.append("sudo -S tcpdump")
334 if self.get("A") == True:
336 if self.get("b") == True:
339 args.append("-B %s" % self.get("B"))
341 args.append("-c %s" % self.get("c"))
343 args.append("-C %s" % self.get("C"))
344 if self.get("d") == True:
346 if self.get("dd") == True:
348 if self.get("ddd") == True:
350 if self.get("D") == True:
352 if self.get("e") == True:
355 args.append("-F %s" % self.get("F"))
356 if self.get("G") == True:
359 args.append("-i %s" % self.get("i"))
360 if self.get("I") == True:
363 args.append("-j %s" % self.get("j"))
364 if self.get("K") == True:
366 if self.get("l") == True:
368 if self.get("U") == True:
370 if self.get("n") == True:
372 if self.get("N") == True:
374 if self.get("S") == True:
377 args.append("-s %s" % self.get("s"))
379 args.append("-T %s" % self.get("T"))
380 if self.get("t") == True:
382 if self.get("tt") == True:
384 if self.get("ttt") == True:
386 if self.get("tttt") == True:
388 if self.get("ttttt") == True:
389 args.append("-ttttt")
390 if self.get("v") == True:
392 if self.get("vv") == True:
394 if self.get("vvv") == True:
397 args.append("-w %s" % self.get("w"))
398 if self.get("expression"):
399 args.append(self.get("expression"))
401 command = " ".join(args)
405 def valid_connection(self, guid):