📄 pcap.h
字号:
\param type: pointer to an integer, which is used to return the code corrisponding to the
selected source. The code will be one defined in the \link remote_source_ID Source identification
Codes \endlink.<br>
In case the source string does not exists (i.e. 'source == NULL') or it is empty
('*source == NULL'), it returns PCAP_SRC_IF_LOCAL (i.e. you are ready to
call pcap_open_live() ). This behavior is kept only for compatibility with older
applications (e.g. tcpdump); therefore we suggest to move to the new syntax for sources.<br>
This parameter can be NULL in case the user is not interested in that.
\param host: user-allocated buffer (of size PCAP_BUF_SIZE) that is used to return
the host name on which the capture has to be started.
This value is meaningful only in case of remote capture; otherwise, the returned
string will be empty ("").
This parameter can be NULL in case the user is not interested in that.
\param port: user-allocated buffer (of size PCAP_BUF_SIZE) that is used to return
the port that has to be used by the RPCAP protocol to contact the other host.
This value is meaningful only in case of remote capture and if the user wants to use
a non-standard port; otherwise, the returned string will be empty ("").
In case of remote capture, an emply string means "use the standard RPCAP port".
This parameter can be NULL in case the user is not interested in that.
\param name: user-allocated buffer (of size PCAP_BUF_SIZE) that is used to return
the source name, without the source prefix.
If the name does not exist (for example because source contains 'rpcap://' that means
'default local adapter'), it returns NULL.
This parameter can be NULL in case the user is not interested in that.
\param errbuf: pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
that will contain the error message (in case there is one).
This parameter can be NULL in case the user is not interested in that.
\return '0' if everything is fine, '-1' if some errors occurred. The requested values
(host name, network port, type of the source) are returned into the proper variables
passed by reference.
*/
int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char *name, char *errbuf);
/*! \brief Open a generic source in order to capture / send (WinPcap only) traffic.
The pcap_open() replaces all the pcap_open_xxx() functions with a single call.
This function hides the differences between the different pcap_open_xxx() functions
so that the programmer does not have to manage different opening function.
In this way, the 'true' open function is decided according to the source type,
which is included into the source string (in the form of source prefix).
This function can rely on the pcap_createsrcstr() to create the string that keeps
the capture device according to the new syntax, and the pcap_parsesrcstr() for the
other way round.
\param source: zero-terminated string containing the source name to open.
The source name has to include the format prefix according to the new
\link remote_source_string Source Specification Syntax\endlink and it cannot be NULL.<br>
On on Linux systems with 2.2 or later kernels, a device argument of "any"
(i.e. rpcap://any) can be used to capture packets from all interfaces.
<br>
In order to makes the source syntax easier, please remember that:
- the adapters returned by the pcap_findalldevs_ex() can be used immediately by the pcap_open()
- in case the user wants to pass its own source string to the pcap_open(), the
pcap_createsrcstr() helps in creating the correct source identifier.
\param snaplen: length of the packet that has to be retained.
For each packet received by the filter, only the first 'snaplen' bytes are stored
in the buffer and passed to the user application. For instance, snaplen equal to
100 means that only the first 100 bytes of each packet are stored.
\param flags: keeps several flags that can be needed for capturing packets.
The allowed flags are defined in the \link remote_open_flags pcap_open() flags \endlink.
\param read_timeout: 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 waits 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.
\param auth: a pointer to a 'struct pcap_rmtauth' that keeps the information required to
authenticate the user on a remote machine. In case this is not a remote capture, this
pointer can be set to NULL.
\param errbuf: a pointer to a user-allocated buffer which will contain the error
in case this function fails. The pcap_open() and findalldevs() are the only two
functions which have this parameter, since they do not have (yet) a pointer to a
pcap_t structure, which reserves space for the error string. Since these functions
do not have (yet) a pcap_t pointer (the pcap_t pointer is NULL in case of errors),
they need an explicit 'errbuf' variable.
'errbuf' may also be set to warning text when pcap_open_live() succeds;
to detect this case the caller should store a zero-length string in
'errbuf' before calling pcap_open_live() and display the warning to the user
if 'errbuf' is no longer a zero-length string.
\return A pointer to a 'pcap_t' which can be used as a parameter to the following
calls (pcap_compile() and so on) and that specifies an opened WinPcap session. In case of
problems, it returns NULL and the 'errbuf' variable keeps the error message.
\warning The source cannot be larger than PCAP_BUF_SIZE.
\warning The following formats are not allowed as 'source' strings:
- rpcap:// [to open the first local adapter]
- rpcap://hostname/ [to open the first remote adapter]
*/
pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf);
/*! \brief Define a sampling method for packet capture.
This function allows applying a sampling method to the packet capture process.
The currently sampling methods (and the way to set them) are described into the
struct pcap_samp. In other words, the user must set the appropriate parameters
into it; these will be applied as soon as the capture starts.
\warning Sampling parameters <strong>cannot</strong> be changed when a capture is
active. These parameters must be applied <strong>before</strong> starting the capture.
If they are applied when the capture is in progress, the new settings are ignored.
\warning Sampling works only when capturing data on Win32 or reading from a file.
It has not been implemented on other platforms. Sampling works on remote machines
provided that the probe (i.e. the capturing device) is a Win32 workstation.
*/
struct pcap_samp *pcap_setsampling(pcap_t *p);
/*! \brief Block until a network connection is accepted (active mode only).
This function has been defined to allow the client dealing with the 'active mode'.
In other words, in the 'active mode' the server opens the connection toward the
client, so that the client has to open a socket in order to wait for connections.
When a new connection is accepted, the RPCAP protocol starts as usual; the only
difference is that the connection is initiated by the server.
This function accepts only ONE connection, then it closes the waiting socket. This means
that if some error occurs, the application has to call it again in order to accept another
connection.
This function returns when a new connection (coming from a valid host 'connectinghost')
is accepted; it returns error otherwise.
\param address: a string that keeps the network address we have to bind to;
usually it is NULL (it means 'bind on all local addresses').
\param port: a string that keeps the network port on which we have to bind to; usually
it is NULL (it means 'bind on the predefined port', i.e. RPCAP_DEFAULT_NETPORT_ACTIVE).
\param hostlist: a string that keeps the host name of the host from whom we are
expecting a connection; it can be NULL (it means 'accept connection from everyone').
Host names are separated by a whatever character in the RPCAP_HOSTLIST_SEP list.
\param connectinghost: a user-allocated buffer that will contain the name of the host
is trying to connect to us.
This variable must be at least RPCAP_HOSTLIST_SIZE bytes..
\param auth: a pointer to a pcap_rmtauth structure. This pointer keeps the information
required to authenticate the RPCAP connection to the remote host.
\param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
that will contain the error message (in case there is one).
\return The SOCKET identifier of the new control connection if everything is fine,
a negative number if some errors occurred. The error message is returned into the errbuf variable.
In case it returns '-1', this means 'everything is fine', but the host cannot be admitted.
In case it returns '-2', in means 'unrecoverable error' (for example it is not able to bind the
socket, or something like that).
In case it returns '-3', it means 'authentication failed'. The authentication check is performed
only if the connecting host is among the ones that are allowed to connect to this host.
The host that is connecting to us is returned into the hostlist variable, which ust be allocated
by the user. This variable contains the host name both in case the host is allowed,
and in case the connection is refused.
\warning Although this function returns the socket established by the new control connection,
this value should not be used. This value will be stored into some libpcap internal
variables and it will be managed automatically by the library. In other words, all the
following calls to findalldevs() and pcap_open() will check if the host is among one that
already has a control connection in place; if so, that one will be used.
\warning This function has several problems if used inside a thread, which is stopped
when this call is blocked into the accept(). In this case, the socket on which we accept
connections is not freed (thread termination is a very dirty job), so that we are no
longer able to accept other connections until the program (i.e. the process) stops.
In order to solve the problem, call the pcap_remoteact_cleanup().
*/
SOCKET pcap_remoteact_accept(const char *address, const char *port, const char *hostlist, char *connectinghost, struct pcap_rmtauth *auth, char *errbuf);
/*! \brief Drop an active connection (active mode only).
This function has been defined to allow the client dealing with the 'active mode'.
This function closes an active connection that is still in place and it purges
the host name from the 'activeHost' list.
From this point on, the client will not have any connection with that host in place.
\param host: a string that keeps the host name of the host for which we want to
close the active connection.
\param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
that will contain the error message (in case there is one).
\return '0' if everything is fine, '-1' if some errors occurred. The error message is
returned into the errbuf variable.
*/
int pcap_remoteact_close(const char *host, char *errbuf);
/*! \brief Clean the socket that is currently used in waiting active connections.
This function does a very dirty job. The fact is that is the waiting socket is not
freed if the pcap_remoteaccept() is killed inside a new thread. This function is
able to clean the socket in order to allow the next calls to pcap_remoteact_accept() to work.
This function is useful *only* if you launch pcap_remoteact_accept() inside a new thread,
and you stops (not very gracefully) the thread (for example because the user changed idea,
and it does no longer want to wait for an active connection).
So, basically, the flow should be the following:
- launch a new thread
- call the pcap_remoteact_accept
- if this new thread is killed, call pcap_remoteact_cleanup().
This function has no effects in other cases.
\return None.
*/
void pcap_remoteact_cleanup();
/*! \brief Return the hostname of the host that have an active connection with us (active mode only).
This function has been defined to allow the client dealing with the 'active mode'.
This function returns the list of hosts that are currently having an active connection
with us. This function is useful in order to delete an active connection that is still
in place.
\param hostlist: a user-allocated string that will keep the list of host that are
currently connected with us.
\param sep: the character that has to be sued as a separator between the hosts (',' for example).
\param size: size of the hostlist buffer.
\param errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE)
that will contain the error message (in case there is one).
\return '0' if everything is fine, '-1' if some errors occurred. The error message is
returned into the errbuf variable.
*/
int pcap_remoteact_list(char *hostlist, char sep, int size, char *errbuf);
//\}
// End of Windows-specific extensions
/*@}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -