📄 pcap.h
字号:
\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);
/*! \brief
closes the files associated with p and deallocates resources.
\sa pcap_open_live(), pcap_open_offline(), pcap_open_dead()
*/
\fn void pcap_close(pcap_t *p);
/*! \brief
pcap_dump_close() closes the "savefile".
\sa pcap_dump_open(), pcap_dump()
*/
\fn void pcap_dump_close(pcap_dumper_t *p);
/*! \brief <b>Win32 Specific.</b> Sets the size of the kernel buffer associated with an adapter.
\e dim specifies the size of the buffer in bytes.
The return value is 0 when the call succeeds, -1 otherwise. If an old buffer was already created
with a previous call to pcap_setbuff(), it is deleted and its content is discarded.
pcap_open_live() creates a 1 MByte buffer by default.
\sa pcap_open_live(), pcap_loop(), pcap_dispatch()
*/
\fn int pcap_setbuff(pcap_t *p, int dim);
/*! \brief <b>Win32 Specific.</b> Sets the working mode of the interface p to mode.
Valid values for mode are
MODE_CAPT (default capture mode) and MODE_STAT (statistical mode). See the tutorial "\ref wpcap_tut9"
for details about statistical mode.
*/
int pcap_setmode(pcap_t *p, int mode);
/*! \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> Sets the minumum amount of data received by the kernel in a single call.
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 <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.
\sa pcap_open_live()
*/
HANDLE pcap_getevent(pcap_t *p);
/*! \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);
/*! \brief Read a packet from an interface or from an offline capture.
This function is used to retrieve the next available packet, bypassing the callback method traditionally
provided by libpcap.
pcap_next_ex fills the pkt_header and pkt_data parameters (see pcap_handler()) with the pointers to the
header and to the data of the next captured packet.
The return value can be:
- 1 if the packet has been read without problems
- 0 if the timeout set with pcap_open_live() has elapsed. In this case pkt_header and pkt_data don't point to a valid packet
- -1 if an error occurred
- -2 if EOF was reached reading from an offline capture
\sa pcap_open_live(), pcap_loop(), pcap_dispatch(), pcap_handler()
*/
int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, u_char **pkt_data);
/*! \brief <b>Win32 Specific.</b> Saves a capture to file.
pcap_live_dump() dumps the network traffic from an interface to
a file. Using this function the dump is performed at kernel level, therefore it is more efficient than using
pcap_dump().
The parameters of this function are an interface descriptor (obtained with pcap_open_live()), a string with
the name of the dump file, the maximum size of the file (in bytes) and the maximum number of packets that the file
will contain. Setting maxsize or maxpacks to 0 means no limit. When maxsize or maxpacks are reached,
the dump ends.
pcap_live_dump() is non-blocking, threfore it returns immediately. pcap_live_dump_ended() can be used to
check the status of the dump process or to wait until it is finished. pcap_close() can instead be used to
end the dump process.
Note that when one of the two limits is reached, the dump is stopped, but the file remains opened. In order
to correctly flush the data and put the file in a consistent state, the adapter must be closed with
pcap_close().
\sa pcap_live_dump_ended(), pcap_open_live(), pcap_close(), pcap_dump_open(), pcap_dump()
*/
int pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks);
/*! \brief <b>Win32 Specific.</b> Returns the status of the kernel dump process, i.e. tells if one of the limits defined with pcap_live_dump() has been reached.
pcap_live_dump_ended() informs the user about the limits that were set with a previous call to
pcap_live_dump() on the interface pointed by p: if the return value is nonzero, one of the limits has been
reched and the dump process is currently stopped.
If sync is nonzero, the function blocks until the dump is finished, otherwise it returns immediately.
\warning if the dump process has no limits (i.e. if the maxsize and maxpacks arguments of pcap_live_dump()
were both 0), the dump process will never stop, therefore setting sync to TRUE will block the application
on this call forever.
\sa pcap_live_dump()
*/
int pcap_live_dump_ended(pcap_t *p, int sync);
/*! \brief<b> Win32 Specific.</b> Returns statistics on current capture.
pcap_stats_ex() extends the pcap_stats() allowing to return more statistical parameters than the old call.
One of the advantages of this new call is that the pcap_stat structure is not allocated by the user; instead,
it is returned back by the system. This allow to extend the pcap_stat structure without affecting backward compatibility
on older applications. These will simply check at the values of the members at the beginning of the structure,
while only newest applications are able to read new statistical values, which are appended in tail.
To be sure not to read a piece of mamory which has not been allocated by the system, the variable pcap_stat_size
will return back the size of the structure pcap_stat allocated by the system.
\param p: pointer to the pcap_t currently in use.
\param pcap_stat_size: pointer to an integer that will contain (when the function returns back) the size of the
structure pcap_stat as it has been allocated by the system.
\return: a pointer to a pcap_stat structure, that will contain the statistics related to the current device.
The return value is NULL in case of errors, and the error text can be obtained with pcap_perror() or pcap_geterr().
\warning pcap_stats_ex() is supported only on live captures, not on "savefiles"; no statistics are stored in
"savefiles", so no statistics are available when reading from a "savefile".
\sa pcap_stats()
*/
struct pcap_stat *pcap_stats_ex(pcap_t *p, int *pcap_stat_size);
/*! \brief Prototype of the callback function that receives the packets.
When pcap_dispatch() or pcap_loop() are called by the user, the packets are passed to the application
by means of this callback. user is a user-defined parameter that contains the state of the
capture session, it corresponds to the \e user parameter of pcap_dispatch() and pcap_loop(). pkt_header is
the header associated by the capture driver to the packet. It is NOT a protocol header. pkt_data
points to the data of the packet, including the protocol headers.
*/
typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *pkt_header,
const u_char *pkt_data);
/*! \brief<b> Win32 Specific.</b> It returns if a given filter applies to an offline packet.
This function is used to apply a filter to a packet that is currently in memory.
This process does not need to open an adapter; we need just to create the proper filter (by settings
parameters like the snapshot length, or the link-layer type) by means of the pcap_compile_nopcap().
The current API of libpcap does not allow to receive a packet and to filter the packet after it has been
received. However, this can be useful in case you want to filter packets in the application, instead of into
the receiving process. This function allows you to do the job.
\param prog: bpf program (created with the pcap_compile_nopcap() )
\param header: header of the packet that has to be filtered
\param pkt_data: buffer containing the packet, in network-byte order.
\return the length of the bytes that are currently available into the packet if the packet satisfies the filter,
0 otherwise.
*/
bool pcap_offline_filter(struct bpf_program *prog, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*@}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -