📄 pcap.h
字号:
pcap_setmintocopy() changes the minimum amount of data in the kernel buffer that causes a read from
the application to return (unless the timeout expires). If the value of \e size is large, the kernel
is forced to wait the arrival of several packets before copying the data to the user. This guarantees
a low number of system calls, i.e. low processor usage, and is a good setting for applications like
packet-sniffers and protocol analyzers. Vice versa, in presence of a small value for this variable,
the kernel will copy the packets as soon as the application is ready to receive them. This is useful
for real time applications that need the best responsiveness from the kernel.
\sa pcap_open_live(), pcap_loop(), pcap_dispatch()
*/
int pcap_setmintocopy(pcap_t *p, int size);
/*! \brief Returns the link layer of an adapter.
pcap_datalink() returns the link layer type; link layer
types it can return include:
- DLT_NULL
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
Note that "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.
- DLT_EN10MB
Ethernet (10Mb, 100Mb, 1000Mb, and up)
- DLT_IEEE802
IEEE 802.5 Token Ring
- DLT_ARCNET
ARCNET
SLIP; the link layer header contains, in order:
a 1-byte flag, which is 0 for packets
received by the machine and 1 for packets
sent by the machine;
a 1-byte field, the upper 4 bits of which
indicate the type of packet, as per RFC
1144:
- 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;
for a total of 16 bytes; the uncompressed IP
datagram follows the header
- DLT_PPP
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
- DLT_FDDI
FDDI
- DLT_ATM_RFC1483
RFC 1483 LLC/SNAP-encapsulated ATM; the packet
begins with an IEEE 802.2 LLC header
- DLT_RAW
raw IP; the packet begins with an IP header
- DLT_PPP_SERIAL
PPP in HDLC-like framing, as per RFC 1662, or
Cisco PPP with HDLC framing, as per section
or 0x8F for Cisco PPP with HDLC framing
- DLT_PPP_ETHER
PPPoE; the packet begins with a PPPoE header, as
per RFC 2516
- DLT_C_HDLC
Cisco PPP with HDLC framing, as per section
4.3.1 of RFC 1547
- DLT_IEEE802_11
IEEE 802.11 wireless LAN
- DLT_LOOP
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
Note that, if a "savefile" is being read,
those PF_ values are not necessarily those of
the machine reading the capture file.
- DLT_LINUX_SLL
Linux "cooked" capture encapsulation; the link
layer header contains, in order:
a 2-byte "packet type", in network byte
order, which is one of:
- 0 packet was sent to us by somebody
else
- 1 packet was broadcast by somebody
else
- 2 packet was multicast, but not
broadcast, by somebody else
- 3 packet was sent by somebody else
to somebody else
- 4 packet was sent by us
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);
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.
- DLT_LTALK
Apple LocalTalk; the packet begins with an
AppleTalk LLAP header
*/
int pcap_datalink(pcap_t *p);
/*! \brief Returns the dimension of the packet portion (in bytes) that is delivered to the application.
pcap_snapshot() returns the snapshot length specified when
pcap_open_live was called.
\sa pcap_open_live(), pcap_compile(), pcap_compile_nopcap()
*/
int pcap_snapshot(pcap_t *p);
/*! \brief
returns true if the current savefile
uses a different byte order than the current system.
*/
int pcap_is_swapped(pcap_t *p);
/*! \brief
returns the major version number of the pcap library used to write the savefile.
\sa pcap_minor_version()
*/
int pcap_major_version(pcap_t *p);
/*! \brief
returns the minor version number of the pcap library used to write the savefile.
\sa pcap_major_version()
*/
int pcap_minor_version(pcap_t *p);
/*! \brief <b>Win32 Specific.</b> Returns the handle of the event associated with the interface p.
This event can be passed to functions like WaitForSingleObject() or WaitForMultipleObjects() to wait
until the driver's buffer contains some data without performing a read.
We disourage the use of this function because it is not portable.
\sa pcap_open_live()
*/
HANDLE pcap_getevent(pcap_t *p);
/*! \brief
prints the text of the last pcap library
error on stderr, prefixed by prefix.
\sa pcap_geterr()
*/
void pcap_perror(pcap_t *p, char *prefix);
/*! \brief
returns the error text pertaining to the
last pcap library error.
\note the pointer it returns will no longer point to a valid
error message string after the pcap_t passed to it is closed;
you must use or copy the string before closing the pcap_t.
\sa pcap_perror()
*/
char *pcap_geterr(pcap_t *p);
/*! \brief
pcap_strerror() is provided in case strerror() isn't
available.
\sa pcap_perror(), pcap_geterr()
*/
char *pcap_strerror(int error);
//\}
// End of miscellaneous functions
/** \name Sending functions
This section lists the functions that are available for sending raw packets on the network.
These functions are WinPcap specific.
*/
//\{
/*! \brief <b>Win32 Specific.</b> Sends a raw packet.
This function allows to send a raw packet to the network. p is the interface that
will be used to send the packet, buf contains the data of the packet to send (including the various
protocol headers), size is the dimension of the buffer pointed by buf, i.e. the size of the packet to send.
The MAC CRC doesn't need to be included, because it is transparently calculated and added by the network
interface driver.
The return value is 0 if the packet is succesfully sent, -1 otherwise.
\sa pcap_open_live()
*/
int pcap_sendpacket(pcap_t *p, u_char *buf, int size);
/*! \brief <b>Win32 Specific.</b> Allocate a send queue.
This function allocates a send queue, i.e. a buffer containing a set of raw packets that will be transimtted
on the network with pcap_sendqueue_transmit().
memsize is the size, in bytes, of the queue, therefore it determines the maximum amount of data that the
queue will contain.
Use pcap_sendqueue_queue() to insert packets in the queue.
\sa pcap_sendqueue_queue(), pcap_sendqueue_transmit(), pcap_sendqueue_destroy()
*/
pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
/*! \brief <b>Win32 Specific.</b> Destroy a send queue.
Deletes a send queue and frees all the memory associated with it.
\sa pcap_sendqueue_alloc(), pcap_sendqueue_queue(), pcap_sendqueue_transmit()
*/
void pcap_sendqueue_destroy(pcap_send_queue* queue);
/*! \brief <b>Win32 Specific.</b> Add a packet to a send queue.
pcap_sendqueue_queue() adds a packet at the end of the send queue pointed by the queue parameter.
pkt_header points to a pcap_pkthdr structure with the timestamp and the length of the packet, pkt_data
points to a buffer with the data of the packet.
The pcap_pkthdr structure is the same used by WinPcap and libpcap to store the packets in a file,
therefore sending a capture file is straightforward.
'Raw packet' means that the sending application will have to include the protocol headers, since every packet
is sent to the network 'as is'. The CRC of the packets needs not to be calculated, because it will be
transparently added by the network interface.
\sa pcap_sendqueue_alloc(), pcap_sendqueue_transmit(), pcap_sendqueue_destroy()
*/
int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
/*! \brief <b>Win32 Specific.</b> Sends a queue of raw packets to the network.
This function transmits the content of a queue to the wire. p is a
pointer to the adapter on which the packets will be sent, queue points to a pcap_send_queue structure
containing the packets to send (see pcap_sendqueue_alloc() and pcap_sendqueue_queue()), sync determines if the
send operation must be synchronized: if it is non-zero, the packets are sent respecting the timestamps,
otherwise they are sent as fast as possible.
The return value is the amount of bytes actually sent. If it is smaller than the size parameter, an
error occurred during the send. The error can be caused by a driver/adapter problem or by an inconsistent/bogus
send queue.
\note Using this function is more efficient than issuing a series of pcap_sendpacket(), because the packets are
buffered in the kernel driver, so the number of context switches is reduced. Therefore, expect a better
throughput when using pcap_sendqueue_transmit.
\note When Sync is set to TRUE, the packets are synchronized in the kernel with a high precision timestamp.
This requires a non-negligible amount of CPU, but allows normally to send the packets with a precision of some
microseconds (depending on the accuracy of the performance counter of the machine). Such a precision cannot
be reached sending the packets with pcap_sendpacket().
\sa pcap_sendqueue_alloc(), pcap_sendqueue_queue(), pcap_sendqueue_destroy()
*/
u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);
//\}
// End of sending functions
/*@}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -