Skip to content

Commit

Permalink
removing StopIteration; fixing some flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
brifordwylie committed Dec 21, 2020
1 parent b022784 commit e4f80f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
12 changes: 7 additions & 5 deletions chains/sources/packet_streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from chains.utils import file_utils, log_utils
logger = log_utils.get_logger()


class PacketStreamer(source.Source):
"""Stream out the packets from the given network interface
Expand Down Expand Up @@ -73,11 +74,11 @@ def read_interface(self):
# snaplen (maximum number of bytes to capture _per_packet_)
# promiscious mode (1 for true)
# timeout (in milliseconds)
self.pcap = pcapy.open_live(self.iface_name, 65536 , 1 , 0)
self.pcap = pcapy.open_live(self.iface_name, 65536, 1, 0)
except OSError:
try:
logger.warning('Could not get promisc mode, turning flag off')
self.pcap = pcapy.open_live(self.iface_name, 65536 , 0 , 0)
self.pcap = pcapy.open_live(self.iface_name, 65536, 0, 0)
except OSError:
log_utils.panic('Could no open interface with any options (may need to be sudo)')

Expand All @@ -94,7 +95,7 @@ def read_interface(self):

# If we don't get a packet header break out of the loop
if not header:
break;
break

# Extract the timestamp from the header and yield the packet
seconds, micro_sec = header.getts()
Expand All @@ -106,12 +107,12 @@ def read_interface(self):
if self.max_packets and _packets >= self.max_packets:
break

# All done so report and raise a StopIteration
# All done so print out a small report
try:
print('Packet stats: %d received, %d dropped, %d dropped by interface' % self.pcap.stats())
except pcapy.PcapError:
print('No stats available...')
raise StopIteration


def test():
"""Open up a test pcap file and stream the packets"""
Expand All @@ -127,5 +128,6 @@ def test():
for packet in streamer.output_stream:
print(packet)


if __name__ == '__main__':
test()
Empty file removed examples/__init__.py
Empty file.
12 changes: 8 additions & 4 deletions examples/simple_packet_print.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from chains.links import packet_meta, reverse_dns, transport_meta
from chains.sinks import packet_printer, packet_summary


def run(iface_name=None, bpf=None, summary=None, max_packets=100):
"""Run the Simple Packet Printer Example"""

Expand All @@ -34,28 +35,31 @@ def run(iface_name=None, bpf=None, summary=None, max_packets=100):
# Pull the chain
printer.pull()


def test():
"""Test the Simple Packet Printer Example"""
from chains.utils import file_utils

# For the test we grab a file, but if you don't specify a
# it will grab from the first active interface
data_path = file_utils.relative_dir(__file__, '../data/http.pcap')
run(iface_name = data_path)
run(iface_name=data_path)


def my_exit():
"""Exit on Signal"""
print('Goodbye...')
sys.exit()


if __name__ == '__main__':

# Collect args from the command line
parser = argparse.ArgumentParser()
parser.add_argument('-bpf', type=str, help='BPF Filter for PacketStream Class')
parser.add_argument('-s','--summary', action="store_true", help='Summary instead of full packet print')
parser.add_argument('-m','--max-packets', type=int, default=100, help='How many packets to process (0 for infinity)')
parser.add_argument('-p','--pcap', type=str, help='Specify a pcap file instead of reading from live network interface')
parser.add_argument('-s', '--summary', action="store_true", help='Summary instead of full packet print')
parser.add_argument('-m', '--max-packets', type=int, default=100, help='How many packets to process (0 for infinity)')
parser.add_argument('-p', '--pcap', type=str, help='Specify a pcap file instead of reading from live network interface')
args, commands = parser.parse_known_args()
if commands:
print('Unrecognized args: %s' % commands)
Expand Down

0 comments on commit e4f80f4

Please sign in to comment.