root/umpa/branches/link-layer-integration/umit/umpa/utils/libpcap/wrappers/pypcap.py @ 5721

Revision 5721, 4.4 kB (checked in by kosma, 3 years ago)

UMPAPcapException: modify utils.libpcap to use UMPAPcapException

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2009 Adriano Monteiro Marques.
5#
6# Author: Bartosz SKOWRON <getxsick at gmail dot com>
7#
8# This library is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published
10# by the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16# License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this library; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22import pcap
23
24from umit.umpa.utils.libpcap.wrappers._abstract import *
25from umit.umpa.utils.exceptions import UMPAPcapException
26
27# see umit.umpa.utils.libpcap.wrappers._abstract for docstrings
28
29if not hasattr(pcap, 'pcap'):
30    raise UMPAPcapException("the installed libpcap library doesn't look like pypcap")
31
32def lookupdev():
33    try:
34        result = pcap.lookupdev()
35    except OSError, msg:
36        raise UMPAPcapException(msg)
37    return result
38
39def findalldevs():
40    try:
41        result = pcap.findalldevs()
42    except OSError, msg:
43        raise UMPAPcapException(msg)
44    return result
45
46class open_pcap(open_pcap):
47    def __init__(self, device=None, snaplen=1024, promisc=True, to_ms=0,
48                                                            immediate=False):
49        if device is None:
50            self.device = lookupdev()
51        else:
52            self.device = device
53        self.snaplen = snaplen
54        self.promisc = promisc
55        self.to_ms = to_ms
56
57        try:
58            self._pcap = pcap.pcap(self.device, self.snaplen, self.promisc,
59                                                self.to_ms, immediate=immediate)
60        except OSError, msg:
61            raise UMPAPcapException(msg)
62
63    def __iter__(self):
64        return self
65
66    def dispatch(self, cnt, callback, *user):
67        return self._pcap.dispatch(cnt, callback, *user)
68
69    def loop(self, cnt, callback, *user):
70        return self._pcap.loop(cnt, callback, *user)
71
72    def next(self):
73        return self._pcap.next()
74
75    def setfilter(self, filter):
76        self._pcap.setfilter(filter)
77
78    def datalink(self):
79        return self._pcap.datalink()
80
81    def sendpacket(self, buf):
82        try:
83            self._pcap.sendpacket(buf) == 0
84        except OSError, msg:
85            raise UMPAPcapException(msg)
86
87        return len(buf)
88
89class dumper(dumper):
90    def __init__(self, p=None, fname=None, open=True):
91        if p is not None:
92            # XXX dirty hack
93            # the next line has a dirty hack and looks like some design problems
94            # feel free to propose better solution or send a patch
95            #
96            # anyway, Joao Medeiros proposed to put a quote here:
97            # "In Python, there isn't much of an idea of "Private"
98            # Python's philosophy is "We're all consenting adults here."
99            # Thus, there isn't much of an idea of "friends" either.
100            # In Java terminology, on a technical level everybody is already
101            # friends with everybody else already." by Jeremy Bowers
102            self._pcap = p._pcap
103        else:
104            self._pcap = None
105        self.fname = fname
106        self._dump = pcap.dump(self._pcap, self.fname, open)
107
108    def open(self, p=None, fname=None):
109        if p is not None:
110            # XXX dirty hack - same as above
111            self._pcap = p._pcap
112        if fname is not None:
113            self.fname = fname
114
115        try:
116            self._dump.open(self._pcap, self.fname)
117        except OSError, msg:
118            raise UMPAPcapException(msg)
119
120    def dump(self):
121        if not self._dump:
122            raise UMPAPcapException("not dump file is opened. "
123                                        "use open() first.")
124        self._dump.dump()
125
126    def flush(self):
127        if not self._dump:
128            raise UMPAPcapException("not dump file is opened. "
129                                        "use open() first.")
130        self._dump.flush()
131
132    def close(self):
133        if not self._dump:
134            raise UMPAPcapException("not dump file is opened. "
135                                        "use open() first.")
136        self._dump.close()
Note: See TracBrowser for help on using the browser.