Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
add data transfer port
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyangc3 committed Jan 19, 2017
1 parent ea593f0 commit f7df3b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
26 changes: 18 additions & 8 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ 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);
struct rpcap_sampling *samp_param, uint32 plen, char *errbuf, char *data_port);
int daemon_endcapture(pcap_t *fp, pthread_t *threaddata, char *errbuf);

int daemon_updatefilter(pcap_t *fp, uint32 plen);
Expand Down Expand Up @@ -89,12 +89,14 @@ void *daemon_thrdatamain(void *ptr);
\param 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
value in order to retrieve the socket we're currently using, the thread ID, and
some pointers to the previous and next elements into this struct.
\param data_port: the server listening port for data transmission in passive mode.
\return None.
*/
void daemon_serviceloop( void *ptr )
void daemon_serviceloop( void *ptr, char *data_port)
{
char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printed
char source[PCAP_BUF_SIZE]; // keeps the string that contains the interface to open
Expand Down Expand Up @@ -294,7 +296,7 @@ int retval; // select() return value

case RPCAP_MSG_STARTCAP_REQ:
{
fp= daemon_startcapture(pars->sockctrl, &threaddata, source, pars->isactive, &samp_param, ntohl(header.plen), errbuf);
fp= daemon_startcapture(pars->sockctrl, &threaddata, source, pars->isactive, &samp_param, ntohl(header.plen), errbuf, data_port);

if (fp == NULL)
SOCK_ASSERT(errbuf, 1);
Expand Down Expand Up @@ -939,7 +941,8 @@ struct rpcap_openreply *openreply; // open reply message
\param plen: the length of the current message (needed in order to be able
to discard excess data in the message, if present)
*/
pcap_t *daemon_startcapture(SOCKET sockctrl, pthread_t *threaddata, char *source, int active, struct rpcap_sampling *samp_param, 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, char *data_port)
{
char portdata[PCAP_BUF_SIZE]; // temp variable needed to derive the data port
char peerhost[PCAP_BUF_SIZE]; // temp variable needed to derive the host name of our peer
Expand Down Expand Up @@ -1035,9 +1038,16 @@ int serveropen_dp; // keeps who is going to open the data connection
{
hints.ai_flags = AI_PASSIVE;

// Let's the server socket pick up a free network port for us
if (sock_initaddress(NULL, "0", &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;
if (data_port)
{
if (sock_initaddress(NULL, data_port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;
}
else
{ // Let's the server socket pick up a free network port for us
if (sock_initaddress(NULL, "0", &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;
}

if ( (sockdata= sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errbuf, PCAP_ERRBUF_SIZE)) == -1)
goto error;
Expand Down
2 changes: 1 addition & 1 deletion daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct daemon_slpars
};


void daemon_serviceloop( void *ptr );
void daemon_serviceloop( void *ptr, char *data_port );

void pthread_suspend(int msec);

Expand Down
13 changes: 9 additions & 4 deletions rpcapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int passivemode= 1; //!< '1' if we want to run in passive mode as well
struct addrinfo mainhints; //!< temporary struct to keep settings needed to open the new socket
char address[MAX_LINE + 1]; //!< keeps the network address (either numeric or literal) to bind to
char port[MAX_LINE + 1]; //!< keeps the network port to bind to
char data_port[MAX_LINE + 1]; //!< keeps the network port to transfer data

extern char *optarg; // for getopt()

Expand Down Expand Up @@ -92,6 +93,7 @@ void printusage()
" -b <address>: the address to bind to (either numeric or literal).\n"
" Default: it binds to all local IPv4 addresses\n"
" -p <port>: the port to bind to. Default: it binds to port " RPCAP_DEFAULT_NETPORT "\n"
" -d <data port>: the port to transfer data.\n"
" -4: use only IPv4 (default both IPv4 and IPv6 waiting sockets are used)\n"
" -l <host_list>: a file that keeps the list of the hosts which are allowed\n"
" to connect to this server (if more than one, list them one per line).\n"
Expand Down Expand Up @@ -148,7 +150,7 @@ char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printe
mainhints.ai_socktype = SOCK_STREAM;

// Getting the proper command line options
while ((retval = getopt(argc, argv, "b:dhp:4l:na:s:f:v")) != -1)
while ((retval = getopt(argc, argv, "b:dhp:d:4l:na:s:f:v")) != -1)
{
switch (retval)
{
Expand All @@ -158,6 +160,9 @@ char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printe
case 'p':
strncpy(port, optarg, MAX_LINE);
break;
case 'd':
strncpy(data_port, optarg, MAX_LINE);
break;
case '4':
mainhints.ai_family = PF_INET; // IPv4 server only
break;
Expand Down Expand Up @@ -590,7 +595,7 @@ SOCKET sockmain;
/* GV otherwise, the thread handle is not destroyed */
pthread_attr_init(&detachedAttribute);
pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);
if ( pthread_create( &threadId, &detachedAttribute, (void *) &daemon_serviceloop, (void *) pars) )
if ( pthread_create( &threadId, &detachedAttribute, (void *) &daemon_serviceloop, (void *) pars), data_port )
{
SOCK_ASSERT("Error creating the child thread", 1);
pthread_attr_destroy(&detachedAttribute);
Expand All @@ -617,7 +622,7 @@ SOCKET sockmain;
// Close the main socket (must be open only in the parent)
closesocket(sockmain);

daemon_serviceloop( (void *) pars);
daemon_serviceloop( (void *) pars, data_port);
exit(0);
}

Expand Down Expand Up @@ -708,7 +713,7 @@ struct daemon_slpars *pars; // parameters needed by the daemon_serviceloop()
pars->isactive= 1;
pars->nullAuthAllowed= nullAuthAllowed;

daemon_serviceloop( (void *) pars);
daemon_serviceloop( (void *) pars, data_port);

activeclose= pars->activeclose;

Expand Down

0 comments on commit f7df3b8

Please sign in to comment.