Philipp Moll | 55e2dd9 | 2020-02-20 09:59:27 +0100 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015-2020, The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | from minindn.apps.application import Application |
| 25 | from mininet.log import debug |
| 26 | |
| 27 | |
| 28 | class Tshark(Application): |
| 29 | """ |
| 30 | Logging utility to dump network traffic of a node to a PCAP file. |
| 31 | |
| 32 | The app is based on the command line tool tshark and requires tshark to be installed on the system. |
| 33 | """ |
| 34 | |
| 35 | def __init__(self, node, logFolder="./", singleLogFile=False): |
| 36 | """ |
| 37 | :param logFolder Folder, where PCAP files are stored. |
| 38 | :param singleLogFile Single PCAP file per node, or individual PCAP for each interface |
| 39 | """ |
| 40 | |
| 41 | Application.__init__(self, node) |
| 42 | |
| 43 | self.logFolder = logFolder |
| 44 | self.singleLogFile = singleLogFile |
| 45 | |
| 46 | # Create logfile folder in case it does not exist |
| 47 | node.cmd('mkdir -p {}'.format(self.logFolder)) |
| 48 | |
| 49 | def start(self): |
| 50 | # Start capturing traffic with Tshark. |
| 51 | debug("[{0}] Starting tshark logging\n".format(self.node.name)) |
| 52 | |
| 53 | if self.singleLogFile: |
| 54 | interfaces = ["-i " + intf for intf in self.node.intfNames()] |
| 55 | ndnDumpOutputFile = "{}/{}-interfaces.pcap".format(self.logFolder, self.node.name) |
| 56 | self.node.cmd("tshark {} -w {} -q &".format(" ".join(interfaces), ndnDumpOutputFile)) |
| 57 | else: |
| 58 | for intf in self.node.intfNames(): |
| 59 | ndnDumpOutputFile = "{}/{}.pcap".format(self.logFolder, intf) |
| 60 | self.node.cmd("tshark -i {} -w {} -q &".format(intf, ndnDumpOutputFile)) |