Changeset 4870

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))

Location:
branch/UMPA
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branch/UMPA/tests/unit/test_sniffing/test_sniffing_init.py

    r4858 r4870  
    7575            if args[0] > args[1]: 
    7676                raise UMPASniffingException("test") 
     77         
     78        def cbk2(ts, pkt, *args): 
     79            print pkt 
     80             
     81        th = SendPacket(umpa.Packet(IP(source_address="1.2.3.6"), 
     82                                    TCP(source_port=99))) 
     83        th.start() 
     84        umpa.sniffing.sniff_loop(1, filter="src 1.2.3.6", device='any', 
     85                                            callback=cbk, callback_args=[1,2]) 
     86        th.join() 
    7787 
    7888        th = SendPacket(umpa.Packet(IP(source_address="1.2.3.6"), 
     
    8090        th.start() 
    8191        umpa.sniffing.sniff_loop(1, filter="src 1.2.3.6", device='any', 
    82                                             callback=cbk, callback_args=[1,2]) 
     92                                                            callback=cbk2) 
    8393        th.join() 
    8494 
     
    93103    def test_from_file(self): 
    94104        py.test.skip("implement to_file first") 
     105 
     106    def test_from_file_loop(self): 
     107        py.test.skip("implement to_file first") 
  • 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