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

📄 rpcapd.c

📁 winpcap
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (passivemode)
	{
	struct addrinfo *tempaddrinfo;

		// Do the work
		if (sock_initaddress((address[0]) ? address : NULL, port, &mainhints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
		{
			SOCK_ASSERT(errbuf, 1);
			return;
		}

		tempaddrinfo= addrinfo;

		while (tempaddrinfo)
		{
			if ( (sockmain= sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE)) == -1)
			{
				SOCK_ASSERT(errbuf, 1);
				tempaddrinfo= tempaddrinfo->ai_next;
				continue;
			}

#ifdef WIN32
			/* GV we need this to create the thread as detached. */
			/* 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 *) &main_passive, (void *) &sockmain ) )
			{
				SOCK_ASSERT("Error creating the passive child thread", 1);
				pthread_attr_destroy(&detachedAttribute);
				continue;
			}
			pthread_attr_destroy(&detachedAttribute);
#else
			if ( (pid= fork() ) == 0)	// I am the child
			{
				main_passive( (void *) &sockmain);
				return;
			}
#endif
			tempaddrinfo= tempaddrinfo->ai_next;

			// FULVIO (bug)
			// Waits for 100 ms in order to allow the child thread to save the 'sockmain' variable
			// before it gets overwritten by the sock_open, in case we want to open more than one waiting sockets
			// It's a really dirty way to create a mutex
			pthread_suspend(100);
		}

		freeaddrinfo(addrinfo);
	}

	// All the previous calls are no blocking, so the main line of execution goes here
	// and I have to avoid that the program terminates
	while (1)
		pthread_suspend(10*60*1000); // it wakes up every 10 minutes; it seems to me reasonable
}


/*
	\brief Closes gracefully (more or less) the program.

	This function is called:
	- when we're running in console
	- when we're running as a Win32 service (in case we press STOP)

	It is not called when we are running as a daemon on UNIX, since
	we do not define a signal in order to terminate gracefully the daemon.

	This function makes a fast cleanup (it does not clean everything, as 
	you can see from the fact that it uses kill() on UNIX), closes
	the main socket, free winsock resources (on Win32) and exits the
	program.
*/
void main_cleanup(int sign)
{
#ifndef WIN32
	// Sends a KILL signal to all the processes
	// that share the same process group (i.e. kills all the childs)
	kill(0, SIGKILL);
#endif

	SOCK_ASSERT(PROGRAM_NAME " is closing.\n", 1);

	// FULVIO (bug)
	// Here we close only the latest 'sockmain' created; if we opened more than one waiting sockets, 
	// only the latest one is closed correctly.
	if (sockmain)
		closesocket(sockmain);
	sock_cleanup();

	/*
		This code is executed under the following conditions:
		- SIGTERM: we're under UNIX, and the user kills us with 'kill -15' 
		(no matter is we're a daemon or in a console mode)
		- SIGINT: we're in console mode and the user sends us a Ctrl+C 
		(SIGINT signal), no matter if we're UNIX or Win32

		In all these cases, we have to terminate the program.
		The case that still remains is if we're a Win32 service: in this case,
		we're a child thread, and we want just to terminate ourself. This is because
		the exit(0) will be invoked by the main thread, which is blocked waiting that
		all childs terminates. We are forced to call exit from the main thread otherwise
		the Win32 service control manager (SCM) does not work well.
	*/
	if ( (sign == SIGTERM) || (sign == SIGINT) )
		exit(0);
	else
		return;
}



#ifdef linux

void main_cleanup_childs(int sign)
{
pid_t pid;
int stat;

	// For reference, Stevens, pg 128

	while ( (pid= waitpid(-1, &stat, WNOHANG) ) > 0)
		SOCK_ASSERT("Child terminated", 1);

	return;
}

#endif





/*!
	\brief 'true' main of the program.

	It must be in a separate function because:
	- if we're in 'console' mode, we have to put the main thread waiting for a Ctrl+C
	(in order to be able to stop everything)
	- if we're in daemon mode, the main program must terminate and a new child must be 
	created in order to create the daemon

	\param ptr: it keeps the main socket handler (what's called 'sockmain' in the main() ), that
	represents the socket used in the main connection. It is a 'void *' just because pthreads
	want this format.
*/
void main_passive(void *ptr)
{
char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
SOCKET sockctrl;				// keeps the socket ID for this control connection
struct sockaddr_storage from;	// generic sockaddr_storage variable
socklen_t fromlen;				// keeps the length of the sockaddr_storage variable
SOCKET sockmain;

#ifndef WIN32
	pid_t pid;
#endif

	sockmain= *((SOCKET *) ptr);
	// Initialize errbuf
	memset(errbuf, 0, sizeof(errbuf) );

	// main thread loop
	while (1)
	{
#ifdef WIN32
	pthread_t threadId;					// Pthread variable that keeps the thread structures
	pthread_attr_t detachedAttribute;
#endif
	struct daemon_slpars *pars;			// parameters needed by the daemon_serviceloop()

		// Connection creation
		fromlen = sizeof(struct sockaddr_storage);

		sockctrl= accept(sockmain, (struct sockaddr *) &from, &fromlen);
		
		if (sockctrl == -1)
		{
			// The accept() call can return this error when a signal is catched
			// In this case, we have simply to ignore this error code
			// Stevens, pg 124
#ifdef WIN32
			if (WSAGetLastError() == WSAEINTR)
#else
			if (errno == EINTR)
#endif
				continue;

			// Don't check for errors here, since the error can be due to the fact that the thread 
			// has been killed
			sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
			SOCK_ASSERT(errbuf, 1);
			continue;
		}

		// checks if the connecting host is among the ones allowed
		if (sock_check_hostlist(hostlist, RPCAP_HOSTLIST_SEP, &from, errbuf, PCAP_ERRBUF_SIZE) < 0 )
		{
			rpcap_senderror(sockctrl, errbuf, PCAP_ERR_HOSTNOAUTH, NULL);
			sock_close(sockctrl, NULL, 0);
			continue;
		}


#ifdef WIN32
		// in case of passive mode, this variable is deallocated by the daemon_serviceloop()
		pars= (struct daemon_slpars *) malloc ( sizeof(struct daemon_slpars) );
		if (pars == NULL)
		{
			snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
			continue;
		}

		pars->sockctrl= sockctrl;
		pars->activeclose= 0;		// useless in passive mode
		pars->isactive= 0;
		pars->nullAuthAllowed= nullAuthAllowed;

		/* GV we need this to create the thread as detached. */
		/* 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) )
		{
			SOCK_ASSERT("Error creating the child thread", 1);
			pthread_attr_destroy(&detachedAttribute);
			continue;
		}
		pthread_attr_destroy(&detachedAttribute);

#else
		if ( (pid= fork() ) == 0)	// I am the child
		{
			// in case of passive mode, this variable is deallocated by the daemon_serviceloop()
			pars= (struct daemon_slpars *) malloc ( sizeof(struct daemon_slpars) );
			if (pars == NULL)
			{
				snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
				exit(0);
			}

			pars->sockctrl= sockctrl;
			pars->activeclose= 0;		// useless in passive mode
			pars->isactive= 0;
			pars->nullAuthAllowed= nullAuthAllowed;

			// Close the main socket (must be open only in the parent)
			closesocket(sockmain);

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

		// I am the parent
		// Close the childsocket (must be open only in the child)
		closesocket(sockctrl);
#endif

		// loop forever, until interrupted
	}
}




/*!
	\brief 'true' main of the program in case the active mode is turned on.

	It does not have any return value nor parameters.
	This function loops forever trying to connect to the remote host, until the
	daemon is turned down.

	\param ptr: it keeps the 'activepars' parameters. It is a 'void *' just because pthreads
	want this format.
*/
void main_active(void *ptr)
{
char errbuf[PCAP_ERRBUF_SIZE + 1];	// keeps the error string, prior to be printed
SOCKET sockctrl;					// keeps the socket ID for this control connection
struct addrinfo hints;				// temporary struct to keep settings needed to open the new socket
struct addrinfo *addrinfo;			// keeps the addrinfo chain; required to open a new socket
struct active_pars *activepars;
struct daemon_slpars *pars;			// parameters needed by the daemon_serviceloop()


	activepars= (struct active_pars *) ptr;

	// Prepare to open a new server socket
	memset(&hints, 0, sizeof(struct addrinfo));
									// WARNING Currently it supports only ONE socket family among IPv4 and IPv6 
	hints.ai_family = AF_INET;		// PF_UNSPEC to have both IPv4 and IPv6 server
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_family= activepars->ai_family;

	snprintf(errbuf, PCAP_ERRBUF_SIZE, "Connecting to host %s, port %s, using protocol %s",
			activepars->address, activepars->port, (hints.ai_family == AF_INET) ? "IPv4": 
			(hints.ai_family == AF_INET6) ? "IPv6" : "Unspecified");
	SOCK_ASSERT(errbuf, 1);

	// Initialize errbuf
	memset(errbuf, 0, sizeof(errbuf) );

	// Do the work
	if (sock_initaddress(activepars->address, activepars->port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
	{
		SOCK_ASSERT(errbuf, 1);
		return;
	}

	while (1)
	{
	int activeclose;

		if ( (sockctrl= sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == -1)
		{
			SOCK_ASSERT(errbuf, 1);

			snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error connecting to host %s, port %s, using protocol %s",
					activepars->address, activepars->port, (hints.ai_family == AF_INET) ? "IPv4": 
					(hints.ai_family == AF_INET6) ? "IPv6" : "Unspecified" );

			SOCK_ASSERT(errbuf, 1);

			pthread_suspend(RPCAP_ACTIVE_WAIT * 1000);

			continue;
		}

		pars= (struct daemon_slpars *) malloc ( sizeof(struct daemon_slpars) );
		if (pars == NULL)
		{
			snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
			continue;
		}

		pars->sockctrl= sockctrl;
		pars->activeclose= 0;
		pars->isactive= 1;
		pars->nullAuthAllowed= nullAuthAllowed;

		daemon_serviceloop( (void *) pars);

		activeclose= pars->activeclose;

		free(pars);

		// If the connection is closed by the user explicitely, don't try to connect to it again
		// just exit the program
		if (activeclose == 1)
			break;
	}
}

⌨️ 快捷键说明

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