| 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 | |
|---|
| 22 | def findalldevs(): |
|---|
| 23 | """ |
|---|
| 24 | Return list of network devices. |
|---|
| 25 | |
|---|
| 26 | These devices can be opened with open_live(). |
|---|
| 27 | |
|---|
| 28 | @note: There may be network devices that cannot be opened |
|---|
| 29 | with open_live() by process calling findalldevs(), because |
|---|
| 30 | e.g. that process might not have sufficent priviliges to open them |
|---|
| 31 | for capturing |
|---|
| 32 | |
|---|
| 33 | @return: list of network devices |
|---|
| 34 | """ |
|---|
| 35 | |
|---|
| 36 | raise NotImplementedError("not implemented method for the " |
|---|
| 37 | "selected libpcap backend or abstract module") |
|---|
| 38 | |
|---|
| 39 | def lookupdev(): |
|---|
| 40 | """ |
|---|
| 41 | Return the name of the first network device that is suitable for |
|---|
| 42 | packet capture |
|---|
| 43 | |
|---|
| 44 | @return: name of the device |
|---|
| 45 | """ |
|---|
| 46 | |
|---|
| 47 | raise NotImplementedError("not implemented method for the " |
|---|
| 48 | "selected libpcap backend or abstract module") |
|---|
| 49 | |
|---|
| 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): |
|---|
| 59 | """ |
|---|
| 60 | Packet capture descriptor. |
|---|
| 61 | """ |
|---|
| 62 | |
|---|
| 63 | def __init__(self, device=None, snaplen=1024, promisc=True, to_ms=0): |
|---|
| 64 | """ |
|---|
| 65 | @type device: C{str} |
|---|
| 66 | @param device: network device for capturing; |
|---|
| 67 | if None try to use "any" if suitable or first found. |
|---|
| 68 | |
|---|
| 69 | @type snaplen: C{int} |
|---|
| 70 | @param snaplen: maximum number of bytes to capture |
|---|
| 71 | |
|---|
| 72 | @type promisc: C{bool} |
|---|
| 73 | @param promisc: set promiscuous mode |
|---|
| 74 | |
|---|
| 75 | @type to_ms: C{int} |
|---|
| 76 | @param to_ms: read timeout in miliseconds |
|---|
| 77 | """ |
|---|
| 78 | |
|---|
| 79 | raise NotImplementedError("not implemented method for the " |
|---|
| 80 | "selected libpcap backend or abstract module") |
|---|
| 81 | |
|---|
| 82 | def dispatch(self, cnt, callback, *user): |
|---|
| 83 | """ |
|---|
| 84 | Collect and process packets. Return if cnt or to_ms |
|---|
| 85 | (see the constructur param) is occured. |
|---|
| 86 | |
|---|
| 87 | @type cnt: C{int} |
|---|
| 88 | @param cnt: maximum number of packets to process before returning |
|---|
| 89 | |
|---|
| 90 | @type callback: C{func} |
|---|
| 91 | @param callback: function to be called for captured packets |
|---|
| 92 | |
|---|
| 93 | @param user: additional arguments for callback functions |
|---|
| 94 | """ |
|---|
| 95 | |
|---|
| 96 | raise NotImplementedError("not implemented method for the " |
|---|
| 97 | "selected libpcap backend or abstract module") |
|---|
| 98 | |
|---|
| 99 | def loop(self, cnt, callback, *user): |
|---|
| 100 | """ |
|---|
| 101 | Collect and process packets. Return if cnt is occured. |
|---|
| 102 | |
|---|
| 103 | @type cnt: C{int} |
|---|
| 104 | @param cnt: maximum number of packets to process before returning |
|---|
| 105 | |
|---|
| 106 | @type callback: C{func} |
|---|
| 107 | @param callback: function to be called for captured packets |
|---|
| 108 | |
|---|
| 109 | @param user: additional arguments for callback functions |
|---|
| 110 | """ |
|---|
| 111 | raise NotImplementedError("not implemented method for the " |
|---|
| 112 | "selected libpcap backend or abstract module") |
|---|
| 113 | |
|---|
| 114 | def next(self): |
|---|
| 115 | """ |
|---|
| 116 | Collect and return the first captured packet. |
|---|
| 117 | |
|---|
| 118 | @return: captured packet. |
|---|
| 119 | """ |
|---|
| 120 | |
|---|
| 121 | raise NotImplementedError("not implemented method for the " |
|---|
| 122 | "selected libpcap backend or abstract module") |
|---|
| 123 | |
|---|
| 124 | def setfilter(self, filter): |
|---|
| 125 | """ |
|---|
| 126 | Specify a filter. |
|---|
| 127 | |
|---|
| 128 | If compiling it's needed, it's done also. |
|---|
| 129 | |
|---|
| 130 | @type filter: C{str} |
|---|
| 131 | @param filter: filter string in BPF format (see pcap manual) |
|---|
| 132 | """ |
|---|
| 133 | |
|---|
| 134 | raise NotImplementedError("not implemented method for the " |
|---|
| 135 | "selected libpcap backend or abstract module") |
|---|
| 136 | |
|---|
| 137 | def dump_open(self): |
|---|
| 138 | raise NotImplementedError("not implemented method for the " |
|---|
| 139 | "selected libpcap backend or abstract module") |
|---|