Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

# daemon.c File Reference

`#include <pcap.h>`
`#include <pcap-int.h>`
`#include <errno.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <pthread.h>`
`#include "pcap-remote.h"`
`#include "daemon.h"`
`#include "sockutils.h"`

Go to the source code of this file.

## Functions
int | daemon_checkauth (SOCKET sockctrl, int nullAuthAllowed, char *errbuf)
| It checks if the authentication credentials supplied by the user are valid.
int | daemon_AuthUserPwd (char *username, char *password, char *errbuf)
int | daemon_findalldevs (SOCKET sockctrl, char *errbuf)
int | daemon_opensource (SOCKET sockctrl, char *source, int srclen, uint32 plen, char *errbuf)
pcap_t * | daemon_startcapture (SOCKET sockctrl, pthread_t *threaddata, char *source, int active, struct rpcap_sampling *samp_param, uint32 plen, char *errbuf)
int | daemon_endcapture (pcap_t *fp, pthread_t *threaddata, char *errbuf)
int | daemon_updatefilter (pcap_t *fp, uint32 plen)
int | daemon_unpackapplyfilter (pcap_t *fp, unsigned int *nread, int *plen, char *errbuf)
int | daemon_getstats (pcap_t *fp)
int | daemon_getstatsnopcap (SOCKET sockctrl, unsigned int ifdrops, unsigned int ifrecv, unsigned int krnldrop, unsigned int svrcapt, char *errbuf)
int | daemon_setsampling (SOCKET sockctrl, struct rpcap_sampling *samp_param, int plen, char *errbuf)
| Received the sampling parameters from remote host and it stores in the pcap_t structure.
void | daemon_seraddr (struct sockaddr_storage *sockaddrin, struct sockaddr_storage *sockaddrout)
| It serializes a network address.
void * | daemon_thrdatamain (void *ptr)
void | daemon_serviceloop (void *ptr)
| Main serving funtion This function is the one which does the job. It is the main() of the child thread, which is created as soon as a new connection is accepted.
void | pthread_suspend (int msec)
| Suspends a pthread for msec milliseconds.

## Function Documentation

int daemon_AuthUserPwd | ( | char * | username,
|  | char * | password,
|  | char * | errbuf
| ) |

| Definition at line 614 of file daemon.c. References PCAP_ERRBUF_SIZE, pcap_strerror(), and snprintf. Referenced by daemon_checkauth().

int daemon_checkauth | ( | SOCKET | sockctrl,
|  | int | nullAuthAllowed,
|  | char * | errbuf
| ) |

  | It checks if the authentication credentials supplied by the user are valid. This function is called each time the rpcap daemon starts a new serving thread. It reads the authentication message from the network and it checks that the user information are valid. Parameters:
  | sockctrl: | the socket if of the control connection.
  | nullAuthAllowed: | '1' if the NULL authentication is allowed.
  | errbuf: | a user-allocated buffer in which the error message (if one) has to be written.
Returns: '0' if everything is fine, '-1' if an unrecoverable error occurred. The error message is returned in the 'errbuf' variable. '-2' is returned in case the authentication failed or in case of a recoverable error (like wrong version). In that case, 'errbuf' keeps the reason of the failure. This provides a way to know that the connection does not have to be closed. In case the message is a 'CLOSE' or an 'ERROR', it returns -3. The error can be due to a connection refusal in active mode, since this host cannot be allowed to connect to the remote peer. Definition at line 459 of file daemon.c. References daemon_AuthUserPwd(), nullAuthAllowed, PCAP_ERRBUF_SIZE, pcap_strerror(), rpcap_checkmsg(), rpcap_createhdr(), RPCAP_MSG_AUTH_REPLY, RPCAP_MSG_AUTH_REQ, RPCAP_MSG_CLOSE, RPCAP_MSG_ERROR, RPCAP_RMTAUTH_NULL, RPCAP_RMTAUTH_PWD, snprintf, SOCK_ASSERT, sock_discard(), SOCK_RECEIVEALL_YES, sock_recv(), and sock_send(). Referenced by daemon_serviceloop().

int daemon_endcapture | ( | pcap_t * | fp,
|  | pthread_t * | threaddata,
|  | char * | errbuf
| ) |

| Definition at line 1168 of file daemon.c. References pcap_close(), PCAP_ERRBUF_SIZE, rpcap_createhdr(), RPCAP_MSG_ENDCAP_REPLY, sock_close(), sock_send(), and SOCKET. Referenced by daemon_serviceloop().

int daemon_findalldevs | ( | SOCKET | sockctrl,
|  | char * | errbuf
| ) |

| Definition at line 721 of file daemon.c. References address, pcap_if::addresses, daemon_seraddr(), pcap_if::description, pcap_if::flags, pcap_if::name, pcap_if::next, PCAP_ERR_FINDALLIF, PCAP_ERR_NOREMOTEIF, PCAP_ERRBUF_SIZE, pcap_findalldevs(), pcap_freealldevs(), rpcap_createhdr(), RPCAP_MSG_FINDALLIF_REPLY, RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), SOCKBUF_BUFFERIZE, SOCKBUF_CHECKONLY, and uint16. Referenced by daemon_serviceloop().

int daemon_getstats | ( | pcap_t * | fp | ) |

| Definition at line 1345 of file daemon.c. References PCAP_ERR_GETSTATS, PCAP_ERRBUF_SIZE, pcap_stats(), pcap_stat::ps_drop, pcap_stat::ps_ifdrop, pcap_stat::ps_recv, rpcap_createhdr(), RPCAP_MSG_STATS_REPLY, RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), SOCKBUF_CHECKONLY, and uint16. Referenced by daemon_serviceloop().

int daemon_getstatsnopcap | ( | SOCKET | sockctrl,
|  | unsigned int | ifdrops,
|  | unsigned int | ifrecv,
|  | unsigned int | krnldrop,
|  | unsigned int | svrcapt,
|  | char * | errbuf
| ) |

| Definition at line 1386 of file daemon.c. References PCAP_ERR_GETSTATS, PCAP_ERRBUF_SIZE, rpcap_createhdr(), RPCAP_MSG_STATS_REPLY, RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), SOCKBUF_CHECKONLY, and uint16. Referenced by daemon_serviceloop().

int daemon_opensource | ( | SOCKET | sockctrl,
|  | char * | source,
|  | int | srclen,
|  | uint32 | plen,
|  | char * | errbuf
| ) |

| Definition at line 857 of file daemon.c. References pcap_close(), PCAP_ERR_OPEN, PCAP_ERRBUF_SIZE, pcap_open(), PCAP_SRC_IF_STRING, pcap_t, rpcap_createhdr(), RPCAP_MSG_OPEN_REPLY, RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_discard(), SOCK_RECEIVEALL_YES, sock_recv(), sock_send(), and SOCKBUF_CHECKONLY. Referenced by daemon_serviceloop().

void daemon_seraddr | ( | struct sockaddr_storage * | sockaddrin,
|  | struct sockaddr_storage * | sockaddrout
| ) |

  | It serializes a network address. It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format that can be used to be sent on the network. Basically, it applies all the hton() conversion required to the input variable. Parameters:
  | sockaddrin: | a 'sockaddr_storage' pointer to the variable that has to be serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
  | sockaddrout: | a 'sockaddr_storage' pointer to the variable that will contain the serialized data. This variable has to be allocated by the user.
Returns: None Warning: This function supports only AF_INET and AF_INET6 address families. Definition at line 1534 of file daemon.c. Referenced by daemon_findalldevs().

void daemon_serviceloop | ( | void * | ptr | ) |

  | Main serving funtion This function is the one which does the job. It is the main() of the child thread, which is created as soon as a new connection is accepted. Parameters:
  | ptr: | a void pointer that keeps the reference of the 'pthread_chain' value corrisponding to this thread. This variable is casted into a 'pthread_chain' value in order to retrieve the socket we're currently using, the therad ID, and some pointers to the previous and next elements into this struct.
Returns: None. Definition at line 97 of file daemon.c. References daemon_checkauth(), daemon_endcapture(), daemon_findalldevs(), daemon_getstats(), daemon_getstatsnopcap(), daemon_opensource(), daemon_setsampling(), daemon_startcapture(), daemon_updatefilter(), PCAP_BUF_SIZE, pcap_close(), PCAP_ERR_AUTH, PCAP_ERR_ENDCAPTURE, PCAP_ERR_INITTIMEOUT, PCAP_ERR_NETW, PCAP_ERR_RUNTIMETIMEOUT, PCAP_ERR_UPDATEFILTER, PCAP_ERR_WRONGMSG, PCAP_ERR_WRONGVER, PCAP_ERRBUF_SIZE, pcap_stats(), pcap_t, pcap_stat::ps_drop, pcap_stat::ps_ifdrop, pcap_stat::ps_recv, pthread_suspend(), rpcap_checkmsg(), RPCAP_MSG_CLOSE, RPCAP_MSG_ENDCAP_REQ, RPCAP_MSG_ERROR, RPCAP_MSG_FINDALLIF_REQ, RPCAP_MSG_OPEN_REQ, RPCAP_MSG_SETSAMPLING_REQ, RPCAP_MSG_STARTCAP_REQ, RPCAP_MSG_STATS_REQ, RPCAP_MSG_UPDATEFILTER_REQ, rpcap_senderror(), RPCAP_SUSPEND_WRONGAUTH, RPCAP_TIMEOUT_INIT, RPCAP_TIMEOUT_RUNTIME, SOCK_ASSERT, sock_close(), sock_discard(), sock_geterror(), SOCK_RECEIVEALL_YES, and sock_recv(). Referenced by main_active(), and main_passive().

int daemon_setsampling | ( | SOCKET | sockctrl,
|  | struct rpcap_sampling * | samp_param,
|  | int | plen,
|  | char * | errbuf
| ) |

| Received the sampling parameters from remote host and it stores in the pcap_t structure. Definition at line 1306 of file daemon.c. References rpcap_sampling::method, PCAP_ERR_SETSAMPLING, PCAP_ERRBUF_SIZE, rpcap_createhdr(), RPCAP_MSG_SETSAMPLING_REPLY, rpcap_senderror(), sock_discard(), SOCK_RECEIVEALL_YES, sock_recv(), sock_send(), and rpcap_sampling::value. Referenced by daemon_serviceloop().

pcap_t * daemon_startcapture | ( | SOCKET | sockctrl,
|  | pthread_t * | threaddata,
|  | char * | source,
|  | int | active,
|  | struct rpcap_sampling * | samp_param,
|  | uint32 | plen,
|  | char * | errbuf
| ) |

| Definition at line 942 of file daemon.c. References daemon_thrdatamain(), daemon_unpackapplyfilter(), rpcap_startcapreq::flags, rpcap_sampling::method, PCAP_BUF_SIZE, pcap_close(), PCAP_ERR_OPEN, PCAP_ERR_STARTCAPTURE, PCAP_ERRBUF_SIZE, pcap_open(), PCAP_OPENFLAG_PROMISCUOUS, pcap_t, rpcap_createhdr(), RPCAP_MSG_STARTCAP_REPLY, RPCAP_NETBUF_SIZE, rpcap_senderror(), RPCAP_STARTCAPREQ_FLAG_DGRAM, RPCAP_STARTCAPREQ_FLAG_PROMISC, RPCAP_STARTCAPREQ_FLAG_SERVEROPEN, snprintf, sock_bufferize(), sock_close(), sock_discard(), sock_geterror(), sock_initaddress(), sock_open(), SOCK_RECEIVEALL_YES, sock_recv(), sock_send(), SOCKBUF_CHECKONLY, SOCKET, SOCKOPEN_CLIENT, SOCKOPEN_SERVER, and rpcap_sampling::value. Referenced by daemon_serviceloop().

void * daemon_thrdatamain | ( | void * | ptr | ) |

| Definition at line 1424 of file daemon.c. References PCAP_ERR_READEX, PCAP_ERRBUF_SIZE, pcap_geterr(), pcap_next_ex(), pcap_t, rpcap_createhdr(), RPCAP_MSG_PACKET, RPCAP_NETBUF_SIZE, rpcap_senderror(), snprintf, SOCK_ASSERT, sock_bufferize(), sock_send(), SOCKBUF_BUFFERIZE, SOCKBUF_CHECKONLY, and uint16. Referenced by daemon_startcapture().

int daemon_unpackapplyfilter | ( | pcap_t * | fp,
|  | unsigned int * | nread,
|  | int * | plen,
|  | char * | errbuf
| ) |

| Definition at line 1199 of file daemon.c. References bpf_program::bf_insns, bpf_program::bf_len, bpf_insn::code, PCAP_ERRBUF_SIZE, pcap_setfilter(), pcap_strerror(), RPCAP_UPDATEFILTER_BPF, snprintf, SOCK_RECEIVEALL_YES, and sock_recv(). Referenced by daemon_startcapture(), and daemon_updatefilter().

int daemon_updatefilter | ( | pcap_t * | fp,
|  | uint32 | plen
| ) |

| Definition at line 1263 of file daemon.c. References daemon_unpackapplyfilter(), PCAP_ERR_UPDATEFILTER, PCAP_ERRBUF_SIZE, rpcap_createhdr(), RPCAP_MSG_UPDATEFILTER_REPLY, rpcap_senderror(), sock_discard(), and sock_send(). Referenced by daemon_serviceloop().

void pthread_suspend | ( | int | msec | ) |

| Suspends a pthread for msec milliseconds. This function is provided since pthreads do not have a suspend() call. Definition at line 1573 of file daemon.c. Referenced by daemon_serviceloop(), main_active(), and main_startup().

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.