⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcap.h

📁 winpcap
💻 H
📖 第 1 页 / 共 3 页
字号:
/** \name Basic functions

	This section lists the most important functions exported by the WinPcap library.
 */
//\{ 



/*! \brief Frees an interface list returned by pcap_findalldevs().

       pcap_freealldevs()  is  used  to  free a list allocated by pcap_findalldevs().

\sa pcap_findalldevs()
*/
void pcap_freealldevs(pcap_if_t *alldevsp);


/*! \brief Compiles a packet filter. Converts an high level filtering expression 
(see \ref language) in a program that can be interpreted by the kernel-level
filtering engine.

       pcap_compile() is used to compile the string  str  into  a
       filter  program.   program  is  a pointer to a bpf_program
       struct and is filled in by pcap_compile().  optimize  controls  
	   whether  optimization on the resulting code is performed.  
	   netmask specifies the netmask of the  local  net.
       A   return   of  -1  indicates  an  error  in  which  case
       pcap_geterr() may be used to display the error text.

\sa pcap_open_live(), pcap_setfilter(), pcap_freecode(), pcap_snapshot()
*/
int pcap_compile(pcap_t *p, struct bpf_program *fp, char *str, int optimize, bpf_u_int32 netmask);

/*!\brief Compiles a packet filter without the need of opening an adapter. Converts an high level filtering expression 
(see \ref language) in a program that can be interpreted by the kernel-level filtering engine.

       pcap_compile_nopcap() is similar to pcap_compile() except 
       that  instead  of passing a pcap structure, one passes the
       snaplen and linktype explicitly.  It  is  intended  to  be
       used  for  compiling filters for direct BPF usage, without
       necessarily having called pcap_open().   A  return  of  -1
       indicates   an  error;  the  error  text  is  unavailable.
       (pcap_compile_nopcap()     is     a     wrapper     around
       pcap_open_dead(),  pcap_compile(),  and  pcap_close(); the
       latter three routines can be used directly in order to get
       the error text for a compilation error.)

       Look at the \ref language section for details on the 
       str parameter.

\sa pcap_open_live(), pcap_setfilter(), pcap_freecode(), pcap_snapshot()
*/
int pcap_compile_nopcap(int snaplen_arg, int linktype_arg, struct bpf_program *program, char *buf, int optimize, bpf_u_int32 mask);


/*! \brief Associates a filter to a capture.

       pcap_setfilter()  is used to specify a filter program.  fp
       is a pointer to a bpf_program struct, usually  the  result
       of  a  call to pcap_compile().  -1 is returned on failure,
       in which case pcap_geterr() may be  used  to  display  the
       error text; 0 is returned on success.

\sa pcap_compile(), pcap_compile_nopcap()
*/
int pcap_setfilter(pcap_t *p, struct bpf_program *fp);


/*! \brief Frees a filter.

       pcap_freecode()  is  used  to  free  up  allocated  memory
       pointed to by a bpf_program struct generated by  pcap_compile()  
	   when  that  BPF  program  is no longer needed, for
       example after it has been made the filter  program  for  a
       pcap structure by a call to pcap_setfilter().

\sa pcap_compile(), pcap_compile_nopcap()
*/
void pcap_freecode(struct bpf_program *fp);


/*!	\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);


/*! \brief Opens a file to write the network traffic.

  pcap_dump_open()  is  called  to  open  a "savefile" for
  writing. fname  is the  name  of  the  file  to  open. 
  The name "-" in a synonym for  stdout. If NULL is returned,
  pcap_geterr() can be used to get the error text.

\sa pcap_dump_close(), pcap_file(), pcap_dump ()
*/
pcap_dumper_t *pcap_dump_open(pcap_t *p, const char *fname);


/*!\brief Saves a packet to disk.

       pcap_dump() outputs a packet to  the  "savefile"  opened
       with  pcap_dump_open().   Note  that its calling arguments
       are suitable for use with pcap_dispatch() or  pcap_loop().
       If   called  directly,  the  user  parameter  is  of  type
       pcap_dumper_t as returned by pcap_dump_open().

\sa pcap_dump_open(), pcap_dump_close(), pcap_dispatch(), pcap_loop()
*/
void pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp);

/*! \brief
       closes the files associated with p and deallocates resources.

\sa pcap_open_live(), pcap_open_offline(), pcap_open_dead()
*/
void pcap_close(pcap_t *p);


/*! \brief
       pcap_dump_close() closes the "savefile".

\sa pcap_dump_open(), pcap_dump()
*/
void pcap_dump_close(pcap_dumper_t *p);


/*! \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);


//\}
// End of basic functions









/** \name Miscellaneous functions

	This section lists the helper functions exported by the WinPcap library.
 */
//\{ 


/*! \brief Switches between blocking and nonblocking mode.

       pcap_setnonblock() puts a capture descriptor, opened  with
       pcap_open_live(),  into "non-blocking" mode, or takes it
       out of "non-blocking" mode,  depending  on  whether  the
       nonblock  argument  is non-zero or zero.  It has no effect
       on "savefiles".  If there is an error,  -1  is  returned
       and errbuf is filled in with an appropriate error message;
       otherwise, 0 is returned.  In  "non-blocking"  mode,  an
       attempt to read from the capture descriptor with pcap_dispatch() 
	   will, if no packets are currently available to  be
       read,  return  0  immediately rather than blocking waiting
       for packets to arrive.  pcap_loop() and  pcap_next()  will
       not work in "non-blocking" mode.

\sa pcap_getnonblock(), pcap_dispatch()
*/
int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);


/*! \brief Gets the "non-blocking" state of an interface.

       pcap_getnonblock()  returns  the  current "non-blocking"
       state of the capture descriptor; it always  returns  0  on
       "savefiles".   If  there is an error, -1 is returned and
       errbuf is filled in with an appropriate error message.

\sa pcap_setnonblock()
*/
int pcap_getnonblock(pcap_t *p, char *errbuf);


/*! \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()
*/
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> Sets the minumum amount of data received by the kernel in a single call.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -