Show
Ignore:
Timestamp:
06/02/09 11:15:41 (4 years ago)
Author:
getxsick
Message:

umpa.sniffing.from_file() splitted to 2 functions (from_file() and
from_file_loop()). Now API is more unified (the same is for sniff()
and sniff_loop()).
add failing test and fix for the bug (handling func arguments (callback_args))

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branch/UMPA/umpa/sniffing/__init__.py

    r4858 r4870  
    106106 
    107107def sniff_loop(count=0, filter=None, device=None, timeout=0, snaplen=1024, 
    108                         promisc=True, callback=None, callback_args=None): 
     108                            promisc=True, callback=None, callback_args=None): 
    109109    """ 
    110110    Sniff packets and call a callback function for each. 
     
    136136    """ 
    137137 
     138    if callback_args is None: 
     139        callback_args = [] 
     140 
    138141    session = lpcap.open_pcap(device, snaplen, promisc, timeout) 
    139142    if filter: 
     
    148151    return sniff(1, device='any') 
    149152 
    150 def from_file(filename, count=0, filter=None, callback=None, 
    151                                                         callback_args=None): 
     153def from_file(filename, count=0, filter=None): 
    152154    """ 
    153155    Load data from pcap file instead of sniffing online. 
     
    163165    @type filter: C{str} 
    164166    @param filter: BPF filter 
    165  
    166     @type callback: C{func} 
    167     @param callback: function with (timestamp, pkt, *callback_args) prototype 
    168  
    169     @type callback_args: C{list} 
    170     @param callback_args: additional arguments for callback function 
    171167    """ 
    172168 
     
    178174    if filter: 
    179175        f.setfilter(filter) 
    180     if callback is not None: 
    181         f.loop(count, callback, *callback_args) 
    182     elif count > 0: 
     176    if count > 0: 
    183177        packets = [] 
    184178        for i in xrange(count): 
     
    189183    return packets 
    190184 
     185def from_file_loop(filename, count=0, filter=None, callback=None, 
     186                                                callback_args=None): 
     187    """ 
     188    Load data from pcap file instead of sniffing online. 
     189 
     190    Call callback for each or return list of packets. 
     191 
     192    @type filename: C{str} 
     193    @param filename: path to a file in pcap format 
     194 
     195    @type count: C{int} 
     196    @param count: number of sniffing packets; 0 means infinity (default: I{0}) 
     197 
     198    @type filter: C{str} 
     199    @param filter: BPF filter 
     200 
     201    @type callback: C{func} 
     202    @param callback: function with (timestamp, pkt, *callback_args) prototype 
     203 
     204    @type callback_args: C{list} 
     205    @param callback_args: additional arguments for callback function 
     206    """ 
     207 
     208    if callback_args is None: 
     209        callback_args = [] 
     210 
     211    if os.path.isfile(filename): 
     212        f = lpcap.open_pcap(filename) 
     213    else: 
     214        raise UMPASniffingException("can't open file: %s" % filename) 
     215 
     216    if filter: 
     217        f.setfilter(filter) 
     218    f.loop(count, callback, *callback_args) 
     219 
    191220def to_file(): 
    192221    pass