From 6643bae82370e68f27625aeb91eeb0882864febb Mon Sep 17 00:00:00 2001 From: IWASE Yusuke Date: Mon, 30 May 2016 15:24:42 +0900 Subject: pcaplib: Reduce Pylint warnings Signed-off-by: IWASE Yusuke Signed-off-by: FUJITA Tomonori --- ryu/lib/pcaplib.py | 206 ++++++++++++++++++++++++++++------------------------- 1 file changed, 109 insertions(+), 97 deletions(-) diff --git a/ryu/lib/pcaplib.py b/ryu/lib/pcaplib.py index 03e02027..b11c0b35 100644 --- a/ryu/lib/pcaplib.py +++ b/ryu/lib/pcaplib.py @@ -1,3 +1,18 @@ +# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + """ Parsing libpcap and reading/writing PCAP file. Reference source: http://wiki.wireshark.org/Development/LibpcapFileFormat @@ -66,7 +81,6 @@ Sample usage of reading PCAP files: """ -import six import struct import sys import time @@ -103,43 +117,56 @@ class PcapFileHdr(object): +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ File Format """ - _FILE_HDR_FMT = None + _FILE_HDR_FMT = '4sHHIIII' + _FILE_HDR_FMT_BIG_ENDIAN = '>' + _FILE_HDR_FMT + _FILE_HDR_FMT_LITTLE_ENDIAN = '<' + _FILE_HDR_FMT + FILE_HDR_SIZE = struct.calcsize(_FILE_HDR_FMT) + + # Magic Number field is used to detect the file format itself and + # the byte ordering. + MAGIC_NUMBER_IDENTICAL = b'\xa1\xb2\xc3\xd4' # Big Endian + MAGIC_NUMBER_SWAPPED = b'\xd4\xc3\xb2\xa1' # Little Endian - def __init__(self, magic=b'\xd4\xc3\xb2\xa1', version_major=2, + def __init__(self, magic=MAGIC_NUMBER_SWAPPED, version_major=2, version_minor=4, thiszone=0, sigfigs=0, snaplen=0, - linktype=0): + network=0): self.magic = magic self.version_major = version_major self.version_minor = version_minor self.thiszone = thiszone self.sigfigs = sigfigs self.snaplen = snaplen - self.linktype = linktype + self.network = network @classmethod def parser(cls, buf): - if buf[:4] == b'\xa1\xb2\xc3\xd4': + magic_buf = buf[:4] + if magic_buf == cls.MAGIC_NUMBER_IDENTICAL: # Big Endian - cls._FILE_HDR_FMT = '>IHHIIII' - byteorder = '>' - elif buf[:4] == b'\xd4\xc3\xb2\xa1': + fmt = cls._FILE_HDR_FMT_BIG_ENDIAN + byteorder = 'big' + elif magic_buf == cls.MAGIC_NUMBER_SWAPPED: # Little Endian - cls._FILE_HDR_FMT = 'IHHIIII') - self._f.write(str(p)) + self.snaplen = snaplen + self.network = network + self._write_pcap_file_hdr() - def _write_pkt_hdr(self, ts, buf_str_len): + def _write_pcap_file_hdr(self): + pcap_file_hdr = PcapFileHdr(snaplen=self.snaplen, + network=self.network) + self._f.write(pcap_file_hdr.serialize()) + + def _write_pkt_hdr(self, ts, buf_len): sec = int(ts) - if sec == 0: - usec = 0 - else: - usec = int(ts * 1e6) % int(ts) - - if sys.byteorder == 'little': - # usec = int(ts * 1e6) % int(ts) - # old_usec = int((float(ts) - int(ts)) * 1e6) - pc_pkt_hdr = PcapPktHdr(ts_sec=sec, - ts_usec=usec, - incl_len=buf_str_len, - orig_len=buf_str_len) - p = pc_pkt_hdr.serialize(fmt='IIII') - self._f.write(str(p)) + usec = int(round(ts % 1, 6) * 1e6) if sec != 0 else 0 + + pc_pkt_hdr = PcapPktHdr(ts_sec=sec, ts_usec=usec, + incl_len=buf_len, orig_len=buf_len) + + self._f.write(pc_pkt_hdr.serialize()) def write_pkt(self, buf, ts=None): - if ts is None: - ts = time.time() + ts = time.time() if ts is None else ts + + # Check the max length of captured packets + buf_len = len(buf) + if buf_len > self.snaplen: + buf_len = self.snaplen + buf = buf[:self.snaplen] + + self._write_pkt_hdr(ts, buf_len) - buf_str = six.binary_type(buf) - buf_str_len = len(buf_str) - self._write_pkt_hdr(ts, buf_str_len) - self._f.write(buf_str) + self._f.write(buf) def __del__(self): self._f.close() -- cgit v1.2.3