Copyright © 2003 CORE SECURITY TECHNOLOGIES
|Revision $Revision: 1.2 $||$Date: 2003/10/23 17:24:27 $||$Author: jkohen $|
Table of Contents
open_live is used to obtain a packet capture descriptor to look at packets on the network. device is a string that specifies the network device to open; on Linux systems with 2.2 or later kernels, a device argument of any or NULL can be used to capture packets from all interfaces. snaplen specifies the maximum number of bytes to capture. promisc specifies if the interface is to be put into promiscuous mode. (Note that even if this parameter is false, the interface could well be in promiscuous mode for some other reason.) For now, this doesn't work on the any device; if an argument of any or NULL is supplied, the promisc flag is ignored. to_ms specifies the read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it wait for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don't, the read timeout is ignored.
findalldevs constructs a list of network devices that can be opened with open_live. (Note that there may be network devices that cannot be opened with open_live, because, for example, that process might not have sufficient privileges to open them for capturing; if so, those devices will not appear on the list.)
dispatch is used to collect and process packets. maxcant specifies the maximum number of packets to process before returning. This is not a minimum number; when reading a live capture, only one bufferful of packets is read at a time, so fewer than maxcant packets may be processed. A cnt of -1 processes all the packets received in one buffer when reading a live capture, or all the packets in the file when reading a savefile. callback specifies a routine to be called with two arguments: a Pkthdr instance describing the data passed and the data itself.
The number of packets read is returned. 0 is returned if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a read timeout that starts before any packets arrive, the timeout expires before any packets arrive, or if the file descriptor for the capture device is in non–blocking mode and no packets were available to be read) or if no more packets are available in a savefile.
When reading a live capture, dispatch will not necessarily return when the read times out; on some platforms, the read timeout isn't supported, and, on other platforms, the timer doesn't start until at least one packet arrives. This means that the read timeout should not be used in, for example, an interactive application, to allow the packet capture loop to poll for user input periodically, as there's no guarantee that dispatch will return after the timeout expires.
loop is similar to dispatch except it keeps reading packets until maxcant packets are processed or an error occurs. It does not return when live read timeouts occur. Rather, specifying a non–zero read timeout to open_live and then calling dispatch allows the reception and processing of any packets that arrive when the timeout occurs. A negative maxcant causes loop to loop forever (or at least until an error occurs). 0 is returned if maxcant is exhausted.
datalink returns the link layer type; link layer types it can return include:
BSD loopback encapsulation; the link layer header is a 4–byte field, in host byte order, containing a PF_ value from socket.h for the network–layer protocol of the packet.
“host byte order” is the byte order of the machine on which the packets are captured, and the PF_ values are for the OS of the machine on which the packets are captured; if a live capture is being done, “host byte order” is the byte order of the machine capturing the packets, and the PF_ values are those of the OS of the machine capturing the packets, but if a savefile is being read, the byte order and PF_ values are not necessarily those of the machine reading the capture file.
Ethernet (10Mb, 100Mb, 1000Mb, and up)
IEEE 802.5 Token Ring
a 1–byte flag, which is 0 for packets received by the machine and 1 for packets sent by the machine.
0x40; an unmodified IP datagram (TYPE_IP)
0x70; an uncompressed–TCP/IP datagram (UNCOMPRESSED_TCP), with that byte being the first byte of the raw IP header on the wire, containing the connection number in the protocol field
0x80; a compressed–TCP/IP datagram (COMPRESSED_TCP), with that byte being the first byte of the compressed TCP/IP datagram header
for UNCOMPRESSED_TCP, the rest of the modified IP header, and for COMPRESSED_TCP, the compressed TCP/IP datagram header
PPP; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC–like framing, with the PPP header following those two bytes, otherwise it's PPP without framing, and the packet begins with the PPP header.
RFC 1483 LLC/SNAP–encapsulated ATM; the packet begins with an IEEE 802.2 LLC header.
Raw IP; the packet begins with an IP header.
PPP in HDLC–like framing, as per RFC 1662, or Cisco PPP with HDLC framing, as per section §4.3.1 of RFC 1547; the first byte will be 0xFF for PPP in HDLC–like framing, and will be 0x0F or 0x8F for Cisco PPP with HDLC framing.
PPPoE; the packet begins with a PPPoE header, as per RFC 2516.
Cisco PPP with HDLC framing, as per section § 4.3.1 of RFC 1547.
IEEE 802.11 wireless LAN.
OpenBSD loopback encapsulation; the link layer header is a 4–byte field, in network byte order, containing a PF_ value from OpenBSD's socket.h for the network–layer protocol of the packet.
a 2–byte field, in network byte order, containing a Linux ARPHRD_ value for the link layer device type.
a 2–byte field, in network byte order, containing the length of the link layer address of the sender of the packet (which could be 0).
an 8–byte field containing that number of bytes of the link layer header (if there are more than 8 bytes, only the first 8 are present).
a 2–byte field containing an Ethernet protocol type, in network byte order, or containing 1 for Novell 802.3 frames without an 802.2 LLC header or 4 for frames beginning with an 802.2 LLC header.
Apple LocalTalk; the packet begins with an AppleTalk LLAP header.
getnonblock returns the current non–blocking state of the capture descriptor; it always returns 0 on savefiles.
setnonblock puts a capture descriptor, opened with open_live, into non–blocking mode, or takes it out of non–blocking mode, depending on whether the state argument is non–zero or zero. It has no effect on savefiles. In non–blocking mode, an attempt to read from the capture descriptor with dispatch will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. loop and next will not work in non–blocking mode.
Table of Contents
Table of Contents
getts, getcaplen and getlen return the timestamp, capture length and total length fields of the packet header, respectively.
Timestamp is a tuple with two elements: the number of seconds since the Epoch, and the amount of microseconds past the current second. The capture length is the number of bytes of the packet that are available from the capture. Finally, total length gives the length of the packet, in bytes (which might be more than the number of bytes available from the capture, if the length of the packet is larger than the maximum number of bytes to capture).
Table of Contents