Changeset 4858

Show
Ignore:
Timestamp:
05/31/09 01:36:46 (4 years ago)
Author:
getxsick
Message:

Implementation of umpa.sniffing.from_file()

You can get list of packets, also call callback func, set filters etc.
This function is not tested yet. to_file() should be implemented first ;)

Also a change in libpcap abstract.
No more open_offline. open_online renamed to open_pcap which handles with both cases.

Location:
branch/UMPA
Files:
5 modified

Legend:

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

    r4849 r4858  
    4242    def test_openlive(self): 
    4343        try: 
    44             obj = pypcap.open_live() 
     44            obj = pypcap.open_pcap() 
    4545            assert obj.device == pcap.lookupdev() 
    46             obj = pypcap.open_live(device="any") # XXX can we use 'any'? 
     46            obj = pypcap.open_pcap(device="any") # XXX can we use 'any'? 
    4747            assert obj.device == "any" 
    4848        except UMPASniffingException: 
     
    6363 
    6464        try: 
    65             p = pypcap.open_live("any", to_ms=100) 
     65            p = pypcap.open_pcap("any", to_ms=100) 
    6666            p.setfilter("src host 1.2.3.4 and src port 99") 
    6767            th = SendPacket(umpa.Packet(IP(source_address="1.2.3.4"), 
     
    8888        th.start() 
    8989        try: 
    90             p = pypcap.open_live("any", to_ms=100) 
     90            p = pypcap.open_pcap("any", to_ms=100) 
    9191            p.setfilter("src host 1.2.3.4 and src port 99") 
    9292            for i in xrange(amount): 
  • branch/UMPA/tests/unit/test_sniffing/test_sniffing_init.py

    r4851 r4858  
    9090                        callback_args=[2,1] ) 
    9191        th.join() 
     92 
     93    def test_from_file(self): 
     94        py.test.skip("implement to_file first") 
  • branch/UMPA/umpa/sniffing/__init__.py

    r4852 r4858  
    1919# along with this library; if not, write to the Free Software Foundation,  
    2020# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
     21 
     22import os.path 
    2123 
    2224import umpa 
     
    7375    """ 
    7476 
    75     session = lpcap.open_live(device, snaplen, promisc, timeout) 
     77    session = lpcap.open_pcap(device, snaplen, promisc, timeout) 
    7678    if filter: 
    7779        session.setfilter(filter) 
     
    134136    """ 
    135137 
    136     session = lpcap.open_live(device, snaplen, promisc, timeout) 
     138    session = lpcap.open_pcap(device, snaplen, promisc, timeout) 
    137139    if filter: 
    138140        session.setfilter(filter) 
     
    146148    return sniff(1, device='any') 
    147149 
    148 def from_file(): 
    149     pass 
     150def from_file(filename, count=0, filter=None, callback=None, 
     151                                                        callback_args=None): 
     152    """ 
     153    Load data from pcap file instead of sniffing online. 
     154 
     155    Call callback for each or return list of packets. 
     156 
     157    @type filename: C{str} 
     158    @param filename: path to a file in pcap format 
     159 
     160    @type count: C{int} 
     161    @param count: number of sniffing packets; 0 means infinity (default: I{0}) 
     162 
     163    @type filter: C{str} 
     164    @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 
     171    """ 
     172 
     173    if os.path.isfile(filename): 
     174        f = lpcap.open_pcap(filename) 
     175    else: 
     176        raise UMPASniffingException("can't open file: %s" % filename) 
     177 
     178    if filter: 
     179        f.setfilter(filter) 
     180    if callback is not None: 
     181        f.loop(count, callback, *callback_args) 
     182    elif count > 0: 
     183        packets = [] 
     184        for i in xrange(count): 
     185            packets.append(f.next()) 
     186    else: 
     187        packets = f.readpkts() 
     188 
     189    return packets 
    150190 
    151191def to_file(): 
  • branch/UMPA/umpa/sniffing/libpcap/_abstract.py

    r4811 r4858  
    4848                    "selected libpcap backend or abstract module") 
    4949 
    50 def open_offline(fname): 
    51     """ 
    52     Open a file in tcpdump format for reading. 
    53     """ 
    54  
    55     raise NotImplementedError("not implemented method for the " 
    56                     "selected libpcap backend or abstract module") 
    57  
    58 class open_live(object): 
     50class open_pcap(object): 
    5951    """ 
    6052    Packet capture descriptor. 
     53 
     54    This is for online and offline capturing. 
    6155    """ 
    6256 
  • branch/UMPA/umpa/sniffing/libpcap/pypcap.py

    r4816 r4858  
    4141    return result 
    4242 
    43 class open_live(open_live): 
     43class open_pcap(open_pcap): 
    4444    def __init__(self, device=None, snaplen=1024, promisc=True, to_ms=0): 
    4545        if device is None: