Module pcap :: Class pcap
[hide private]
[frames] | no frames]

Class pcap

object --+
         |
        pcap

pcap object is a wrapper for the type `pcap_t' defined in PCAP library. Its methods are all PCAP library functions with prototype `type pcap_func(pcap_t *,...)'. Method names are functions names without `pcap_' prefix.

Instance Methods [hide private]
 
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
a new object with type S, a subtype of T
__new__(T, S, ...)
None
activate()
activate() activate a capture handle.
None
breakloop()
breakloop() force a dispatch() or loop() call to return.
boolean
can_set_rfmon()
can_set_rfmon() check whether monitor mode can be set for a not- yet-activated capture handle.
bpf_program object
compile(str, optimize=..., netmask=...)
compile() compile a filter expression.
int
datalink()
datalink() get the link-layer header type.
int
dispatch(cnt, callback, user=...)
dispatch() process packets from a live capture or savefile.
pcap_dumper object
dump_fopen(fp)
dump_fopen() open a file to which to write packets.
pcap_dumper object
dump_open(fname)
dump_open() open a file to which to write packets.
file object | None
file()
file() get the file object associated to standard I/O stream for a savefile being read.
int
fileno()
fileno() get the file descriptor for a live capture.
int
get_selectable_fd()
get_selectable_fd() get a file descriptor on which a select() can be done for a live capture.
boolean
getnonblock()
getnonblock() get the state of non-blocking mode on a capture device.
int
inject(pkt)
inject() transmit a packet.
boolean
is_swapped()
is_swapped() find out whether a savefile has the native byte order.
(int, int,...)
list_datalinks()
list_datalinks() get a list of link-layer header types supported by a capture device.
None
loop(cnt, callback, user=...)
loop() process packets from a live capture or savefile.
int
major_version()
major_version() get the version number of a savefile.
int
minor_version()
minor_version() get the version number of a savefile.
(dict, str) | None
next()
next() read the next packet from a pcap object.
(int, dict, str)
next_ex()
next_ex() read the next packet from a pcap object.
None
sendpacket(pkt)
inject() transmit a packet.
None
set_buffer_size(buffer_size)
set_buffer_size() set the buffer size for a not-yet-activated capture handle.
None
set_promisc(boolean)
set_promisc() set promiscuous mode for a not-yet-activated capture handle.
None
set_rfmon(boolean)
set_rfmon() set monitor mode for a not-yet-activated capture handle.
None
set_snaplen(snaplen)
set_snaplen() set the snapshot length for a not-yet-activated capture handle.
None
set_timeout(to_ms)
set_timeout() set the read timeout for a not-yet-activated capture handle.
 
setdirection(...)
setdirection(pcap.PCAP_D_IN | pcap.PCAP_D_OUT | pcap.PCAP_D_INOUT) -> None
None
setfilter(fp)
setfilter() set the filter.
None
setnonblock(boolean)
setnonblock() set the state of non-blocking mode on a capture device.
int
snapshot()
snapshot() get the snapshot length.
dict
stats()
stats() get capture statistics.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(...)
(Constructor)

 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

activate()

 

activate() activate a capture handle. It is a wrapper for pcap_activate() Packet Capture library routine.

Returns: None

breakloop()

 

breakloop() force a dispatch() or loop() call to return. It is a wrapper for pcap_breakloop() Packet Capture library routine.

Returns: None

can_set_rfmon()

 

can_set_rfmon() check whether monitor mode can be set for a not- yet-activated capture handle. It is a wrapper for pcap_can_set_rfmon() Packet Capture library routine.

Returns: boolean

compile(str, optimize=..., netmask=...)

 

compile() compile a filter expression. It is a wrapper for pcap_compile() Packet Capture library routine.

Default values for parameters `optimize' and `netmask' are respectively True and 0.

Returns: bpf_program object

datalink()

 

datalink() get the link-layer header type. It is a wrapper for pcap_datalink() Packet Capture library routine.

Returns: int

dispatch(cnt, callback, user=...)

 

dispatch() process packets from a live capture or savefile. It is a wrapper for pcap_dispatch() Packet Capture library routine. See `loop()' method.

Returns: int

dump_fopen(fp)

 

dump_fopen() open a file to which to write packets. It is a wrapper for pcap_dump_fopen() Packet Capture library routine.

Argument `fp' is a file object. File object `fp' should have been opened with mode `wb'.

Returns: pcap_dumper object

dump_open(fname)

 

dump_open() open a file to which to write packets. It is a wrapper for pcap_dump_open() Packet Capture library routine.

Returns: pcap_dumper object

file()

 

file() get the file object associated to standard I/O stream for a savefile being read. It is a wrapper for pcap_file() Packet Capture library routine.

Note: return None if pcap object was not opened with `pcap_offline()'

Returns: file object | None

fileno()

 

fileno() get the file descriptor for a live capture. It is a wrapper for pcap_fileno() Packet Capture library routine.

Returns: int

get_selectable_fd()

 

get_selectable_fd() get a file descriptor on which a select() can be done for a live capture. It is a wrapper for pcap_get_selectable_fd() Packet Capture library routine.

Returns: int

getnonblock()

 

getnonblock() get the state of non-blocking mode on a capture device. It is a wrapper for pcap_getnonblock() Packet Capture library routine.

Returns: boolean

inject(pkt)

 

inject() transmit a packet. It is a wrapper for pcap_inject() Packet Capture library routine.

Returns: int

is_swapped()

 

is_swapped() find out whether a savefile has the native byte order. It is a wrapper for pcap_is_swapped() Packet Capture library routine.

Returns: boolean

list_datalinks()

 

list_datalinks() get a list of link-layer header types supported by a capture device. It is a wrapper for pcap_list_datalinks() Packet Capture library routine.

Returns: (int, int,...)

loop(cnt, callback, user=...)

 
loop() process packets from a live capture or savefile. It is a wrapper
for pcap_loop() Packet Capture library routine.

Argument `callback' is a function or an instance method which takes
exactly 3 arguments. First one is the third argument of `loop' (`user')
which is passed by `loop' to `callback'. It can be any Python object
and it's default value is None. Second argument is a wrapper for
`struct pcap_pkthdr' defined in PCAP library. It is a dictionary of
the following form:
  {'caplen': <int>, # length of portion present
   'ts': {'tv_sec': <int>, 'tv_usec': <int>}, # time stamp
   'len': <int> # length of this packet (off wire)
  }
Last argument of `callback' is a Python string containing the first
`caplen' bytes of captured packet.

Returns: None

major_version()

 

major_version() get the version number of a savefile. It is a wrapper for pcap_major_version() Packet Capture library routine.

Returns: int

minor_version()

 

minor_version() get the version number of a savefile. It is a wrapper for pcap_minor_version() Packet Capture library routine.

Returns: int

next()

 

next() read the next packet from a pcap object. It is a wrapper for pcap_next() Packet Capture library routine.

Returned value is a 2-tuple. First element is a Python dictionary which is a wrapper for `struct pcap_pkthdr' defined in PCAP library (see `loop()' documentation for a description of this dictionary) and second element is a Python string containing the captured packet.

next() returns None when pcap_next() returned NULL.

Returns: (dict, str) | None

next_ex()

 

next_ex() read the next packet from a pcap object. It is a wrapper for pcap_next_ex() Packet Capture library routine.

Returned value is a 3-tuple. First element is a Python integer which is the integer returned by PCAP library function pcap_next_ex().

If this integer is 1 (no error), second element is a Python dictionary which is a wrapper for `struct pcap_pkthdr' defined in PCAP library (see `loop()' documentation for a description of this dictionary) and last element is a Python string containing the captured packet.

When pcap_next_ex() returns a value different from one (error occured), second element is setted to None and last element is a Python string containing the corresponding error message.

Returns: (int, dict, str)

sendpacket(pkt)

 

inject() transmit a packet. It is a wrapper for pcap_sendpacket() Packet Capture library routine.

Returns: None

set_buffer_size(buffer_size)

 

set_buffer_size() set the buffer size for a not-yet-activated capture handle. It is a wrapper for pcap_set_buffer_size() Packet Capture library routine.

Returns: None

set_promisc(boolean)

 

set_promisc() set promiscuous mode for a not-yet-activated capture handle. It is a wrapper for pcap_set_promisc() Packet Capture library routine.

Returns: None

set_rfmon(boolean)

 

set_rfmon() set monitor mode for a not-yet-activated capture handle. It is a wrapper for pcap_set_rfmon() Packet Capture library routine.

Returns: None

set_snaplen(snaplen)

 

set_snaplen() set the snapshot length for a not-yet-activated capture handle. It is a wrapper for pcap_set_snaplen() Packet Capture library routine.

Returns: None

set_timeout(to_ms)

 

set_timeout() set the read timeout for a not-yet-activated capture handle. It is a wrapper for pcap_set_timeout() Packet Capture library routine.

Returns: None

setdirection(...)

 
setdirection(pcap.PCAP_D_IN | pcap.PCAP_D_OUT | pcap.PCAP_D_INOUT)
   -> None

setdirection() set the direction for which packets will be captured.
It is a wrapper for pcap_setdirection() Packet Capture library routine.

setfilter(fp)

 

setfilter() set the filter. It is a wrapper for pcap_setfilter() Packet Capture library routine.

Argument `fp' is a bpf_program object, usually the result of a call to compile().

Returns: None

setnonblock(boolean)

 

setnonblock() set the state of non-blocking mode on a capture device. It is a wrapper for pcap_setnonblock() Packet Capture library routine.

Returns: None

snapshot()

 

snapshot() get the snapshot length. It is a wrapper for pcap_snapshot() Packet Capture library routine.

Returns: int

stats()

 
stats() get capture statistics. It is a wrapper for pcap_stats()
Packet Capture library routine.

Returned dictionary has following format:
  {'ps_recv': <int>, # number of packets received
   'ps_drop': <int>  # number of packets dropped
  }

Returns: dict