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_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_strerror(), rpcap_checkmsg(), rpcap_createhdr(), RPCAP_MSG_AUTH_REQ, RPCAP_MSG_CLOSE, RPCAP_MSG_ERROR, RPCAP_RMTAUTH_NULL, RPCAP_RMTAUTH_PWD, snprintf, SOCK_ASSERT, sock_discard(), 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(), rpcap_createhdr(), 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(), rpcap_findalldevs_if::desclen, pcap_if::description, pcap_if::flags, rpcap_findalldevs_if::flags, rpcap_findalldevs_if::naddr, pcap_if::name, rpcap_findalldevs_if::namelen, pcap_if::next, pcap_findalldevs(), pcap_freealldevs(), rpcap_createhdr(), RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), and uint16. Referenced by daemon_serviceloop().

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

| Definition at line 1345 of file daemon.c. References rpcap_stats::ifdrop, rpcap_stats::ifrecv, rpcap_stats::krnldrop, pcap_stats(), pcap_stat::ps_drop, pcap_stat::ps_ifdrop, pcap_stat::ps_recv, rpcap_createhdr(), RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), and rpcap_stats::svrcapt. 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 rpcap_stats::ifdrop, rpcap_stats::ifrecv, rpcap_stats::krnldrop, rpcap_createhdr(), RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_send(), and rpcap_stats::svrcapt. 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 rpcap_openreply::linktype, pcap_close(), pcap_open(), pcap_t, rpcap_createhdr(), RPCAP_NETBUF_SIZE, rpcap_senderror(), sock_bufferize(), sock_discard(), sock_recv(), sock_send(), and rpcap_openreply::tzoff. 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_slpars::activeclose, daemon_checkauth(), daemon_endcapture(), daemon_findalldevs(), daemon_getstats(), daemon_getstatsnopcap(), daemon_opensource(), daemon_setsampling(), daemon_startcapture(), daemon_updatefilter(), errbuf, daemon_slpars::isactive, daemon_slpars::nullAuthAllowed, PCAP_BUF_SIZE, pcap_close(), 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_TIMEOUT_INIT, RPCAP_TIMEOUT_RUNTIME, SOCK_ASSERT, sock_close(), sock_discard(), sock_geterror(), sock_recv(), and daemon_slpars::sockctrl. 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, rpcap_createhdr(), rpcap_senderror(), sock_discard(), 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 rpcap_startcapreply::bufsize, daemon_unpackapplyfilter(), rpcap_sampling::method, PCAP_BUF_SIZE, pcap_close(), pcap_open(), pcap_t, rpcap_startcapreply::portdata, rpcap_createhdr(), RPCAP_NETBUF_SIZE, rpcap_senderror(), RPCAP_STARTCAPREQ_FLAG_DGRAM, RPCAP_STARTCAPREQ_FLAG_SERVEROPEN, snprintf, sock_bufferize(), sock_close(), sock_discard(), sock_geterror(), sock_initaddress(), sock_open(), sock_recv(), sock_send(), SOCKET, and rpcap_sampling::value. Referenced by daemon_serviceloop().

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

| Definition at line 1424 of file daemon.c. References rpcap_pkthdr::caplen, pcap_pkthdr::caplen, errbuf, pcap_pkthdr::len, rpcap_pkthdr::len, rpcap_pkthdr::npkt, PCAP_ERRBUF_SIZE, pcap_geterr(), pcap_next_ex(), pcap_t, rpcap_createhdr(), rpcap_senderror(), snprintf, SOCK_ASSERT, sock_bufferize(), sock_send(), rpcap_pkthdr::timestamp_sec, rpcap_pkthdr::timestamp_usec, and pcap_pkthdr::ts.

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_len, bpf_insn::code, bpf_insn::jf, bpf_insn::jt, bpf_insn::k, pcap_setfilter(), pcap_strerror(), RPCAP_UPDATEFILTER_BPF, snprintf, 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(), rpcap_createhdr(), 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.