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

📄 bntrackd.c

📁 打魔兽战网的都知道他是什么
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 1999  Mark Baysinger (mbaysing@ucsd.edu) * Copyright (C) 2000  Ross Combs (rocombs@cs.nmsu.edu) *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#define TRACKER_INTERNAL_ACCESS#include "common/setup_before.h"#include <stdio.h>#ifdef HAVE_STDDEF_H# include <stddef.h>#else# ifndef NULL#  define NULL ((void *)0)# endif#endif#ifdef STDC_HEADERS# include <stdlib.h>#else# ifdef HAVE_MALLOC_H#  include <malloc.h># endif#endif#include "compat/exitstatus.h"#ifdef HAVE_STRING_H# include <string.h>#else# ifdef HAVE_STRINGS_H#  include <strings.h># endif#endif#ifdef HAVE_MEMORY_H# include <memory.h>#endif#include "compat/memset.h"#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#include "compat/stdfileno.h"#include <errno.h>#include "compat/strerror.h"#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# ifdef HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#ifdef HAVE_SYS_TYPES_H# include <sys/types.h>#endif#ifdef HAVE_SYS_SOCKET_H# include <sys/socket.h>#endif#include "compat/socket.h"#include "compat/send.h"#ifdef HAVE_SYS_PARAM_H# include <sys/param.h>#endif#ifdef HAVE_NETINET_IN_H# include <netinet/in.h>#endif#include "compat/netinet_in.h"#ifdef HAVE_ARPA_INET_H# include <arpa/inet.h>#endif#include "compat/inet_ntoa.h"#include "compat/psock.h"#include "common/list.h"#include "common/version.h"#include "common/eventlog.h"#include "common/util.h"#include "common/tracker.h"#include "common/xalloc.h"#include "common/setup_after.h"/****************************************************************************** * TYPES *****************************************************************************/typedef struct{    struct in_addr address;    time_t         updated;    t_trackpacket  info;} t_server;typedef struct{    int            foreground;    int            debug;    int            XML_mode;    unsigned int   expire;    unsigned int   update;    unsigned short port;    char const *   outfile;    char const *   pidfile;    char const *   logfile;    char const *   process;} t_prefs;/****************************************************************************** * STATIC FUNCTION PROTOTYPES *****************************************************************************/static int server_process(int sockfd);static void usage(char const * progname);static void getprefs(int argc, char * argv[]);static void fixup_str(char * str);/****************************************************************************** * GLOBAL VARIABLES *****************************************************************************/static t_prefs prefs;extern int main(int argc, char * argv[]){    int sockfd;        if (argc<1 || !argv || !argv[0])    {        fprintf(stderr,"bad arguments\n");        return STATUS_FAILURE;    }        getprefs(argc,argv);        if (!prefs.debug)        eventlog_del_level("debug");    if (prefs.logfile)    {	eventlog_set(stderr);        if (eventlog_open(prefs.logfile)<0)	{            eventlog(eventlog_level_fatal,__FUNCTION__,"could not use file \"%s\" for the eventlog (exiting)",prefs.logfile);	    return STATUS_FAILURE;	}    }    #ifdef DO_DAEMONIZE    if (!prefs.foreground)    {	switch (fork())	{	case -1:	    eventlog(eventlog_level_error,__FUNCTION__,"could not fork (fork: %s)\n",pstrerror(errno));	    return STATUS_FAILURE;	case 0: /* child */	    break;	default: /* parent */	    return STATUS_SUCCESS;	}		close(STDINFD);	close(STDOUTFD);	close(STDERRFD);	# ifdef HAVE_SETPGID	if (setpgid(0,0)<0)	{	    eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgid: %s)\n",pstrerror(errno));	    return STATUS_FAILURE;	}# else#  ifdef HAVE_SETPGRP#   ifdef SETPGRP_VOID	if (setpgrp()<0)	{	    eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgrp: %s)\n",pstrerror(errno));	    return STATUS_FAILURE;	}#   else	if (setpgrp(0,0)<0)	{	    eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setpgrp: %s)\n",pstrerror(errno));	    return STATUS_FAILURE;	}#   endif#  else#   ifdef HAVE_SETSID	if (setsid()<0)	{	    eventlog(eventlog_level_error,__FUNCTION__,"could not create new process group (setsid: %s)\n",pstrerror(errno));	    return STATUS_FAILURE;	}#   else#    error "One of setpgid(), setpgrp(), or setsid() is required"#   endif#  endif# endif    }#endif        if (prefs.pidfile)    {#ifdef HAVE_GETPID        FILE * fp;        if (!(fp = fopen(prefs.pidfile,"w")))        {            eventlog(eventlog_level_error,__FUNCTION__,"unable to open pid file \"%s\" for writing (fopen: %s)",prefs.pidfile,pstrerror(errno));            prefs.pidfile = NULL;        }        else        {            fprintf(fp,"%u",(unsigned int)getpid());            if (fclose(fp)<0)                eventlog(eventlog_level_error,__FUNCTION__,"could not close pid file \"%s\" after writing (fclose: %s)",prefs.pidfile,pstrerror(errno));        }#else        eventlog(eventlog_level_warn,__FUNCTION__,"no getpid() system call, do not use the -P or the --pidfile option");        prefs.pidfile = NULL;#endif    }    #ifdef HAVE_GETPID    eventlog(eventlog_level_info,__FUNCTION__,"bntrackd version "PVPGN_VERSION" process %u",(unsigned int)getpid());#else    eventlog(eventlog_level_info,__FUNCTION__,"bntrackd version "PVPGN_VERSION);#endif        if (psock_init()<0)    {        eventlog(eventlog_level_error,__FUNCTION__,"could not initialize socket functions");        return STATUS_FAILURE;    }        /* create the socket */    if ((sockfd = psock_socket(PSOCK_PF_INET,PSOCK_SOCK_DGRAM,PSOCK_IPPROTO_UDP))<0)    {	eventlog(eventlog_level_error,__FUNCTION__,"could not create UDP listen socket (psock_socket: %s)\n",pstrerror(psock_errno()));	return STATUS_FAILURE;    }        {	struct sockaddr_in servaddr;		/* bind the socket to correct port and interface */	memset(&servaddr,0,sizeof(servaddr));	servaddr.sin_family = PSOCK_AF_INET;	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);	servaddr.sin_port = htons(prefs.port);	if (psock_bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)	{	    eventlog(eventlog_level_error,__FUNCTION__,"could not bind to UDP port %hu (psock_bind: %s)\n",prefs.port,pstrerror(psock_errno()));	    return STATUS_FAILURE;	}    }        if (server_process(sockfd)<0)	return STATUS_FAILURE;    return STATUS_SUCCESS;}static int server_process(int sockfd){    t_list *           serverlist_head;    t_elem *           curr;    t_server *         server;    struct sockaddr_in cliaddr;    t_psock_fd_set     rfds;    struct timeval     tv;    time_t             last;    FILE *             outfile;    psock_t_socklen    len;    t_trackpacket      packet;        if (!(serverlist_head = list_create()))    {	eventlog(eventlog_level_error,__FUNCTION__,"could not create server list");	return -1;    }        /* the main loop */    last = time(NULL) - prefs.update;    for (;;)    {	/* time to dump our list to disk and call the process command */	/* (I'm making the assumption that this won't take very long.) */	if (last+(signed)prefs.update<time(NULL))	{	    last = time(NULL);	    	    if (!(outfile = fopen(prefs.outfile,"w")))	    {		eventlog(eventlog_level_error,__FUNCTION__,"unable to open file \"%s\" for writing (fopen: %s)",prefs.outfile,pstrerror(errno));		continue;	    }	    	    LIST_TRAVERSE(serverlist_head,curr)	    {		server = elem_get_data(curr);				if (server->updated+(signed)prefs.expire<last)		{		    list_remove_elem(serverlist_head,&curr);		    xfree(server);		}		else		{		   if (prefs.XML_mode == 1)		  {		    fprintf(outfile,"<server>\n\t<address>%s</address>\n",inet_ntoa(server->address));		    fprintf(outfile,"\t<port>%hu</port>\n",(unsigned short)ntohs(server->info.port));		    fprintf(outfile,"\t<location>%s</location>\n",server->info.server_location);		    fprintf(outfile,"\t<software>%s</software>\n",server->info.software);		    fprintf(outfile,"\t<version>%s</version>\n",server->info.version);		    fprintf(outfile,"\t<users>%lu</users>\n",(unsigned long)ntohl(server->info.users));		    fprintf(outfile,"\t<channels>%lu</channels>\n",(unsigned long)ntohl(server->info.channels));		    fprintf(outfile,"\t<games>%lu</games>\n",(unsigned long)ntohl(server->info.games));		    fprintf(outfile,"\t<description>%s</description>\n",server->info.server_desc);		    fprintf(outfile,"\t<platform>%s</platform>\n",server->info.platform);		    fprintf(outfile,"\t<url>%s</url>\n",server->info.server_url);		    fprintf(outfile,"\t<contact_name>%s</contact_name>\n",server->info.contact_name);		    fprintf(outfile,"\t<contact_email>%s</contact_email>\n",server->info.contact_email);		    fprintf(outfile,"\t<uptime>%lu</uptime>\n",(unsigned long)ntohl(server->info.uptime));		    fprintf(outfile,"\t<total_games>%lu</total_games>\n",(unsigned long)ntohl(server->info.total_games));		    fprintf(outfile,"\t<logins>%lu</logins>\n",(unsigned long)ntohl(server->info.total_logins));		    fprintf(outfile,"</server>\n");		  }		  else		  { 		    fprintf(outfile,"%s\n##\n",inet_ntoa(server->address));		    fprintf(outfile,"%hu\n##\n",(unsigned short)ntohs(server->info.port));		    fprintf(outfile,"%s\n##\n",server->info.server_location);		    fprintf(outfile,"%s\n##\n",server->info.software);		    fprintf(outfile,"%s\n##\n",server->info.version);		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.users));		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.channels));		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.games));		    fprintf(outfile,"%s\n##\n",server->info.server_desc);		    fprintf(outfile,"%s\n##\n",server->info.platform);		    fprintf(outfile,"%s\n##\n",server->info.server_url);		    fprintf(outfile,"%s\n##\n",server->info.contact_name);		    fprintf(outfile,"%s\n##\n",server->info.contact_email);		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.uptime));		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.total_games));		    fprintf(outfile,"%lu\n##\n",(unsigned long)ntohl(server->info.total_logins));		    fprintf(outfile,"###\n");		  }		}	    }            if (fclose(outfile)<0)                eventlog(eventlog_level_error,__FUNCTION__,"could not close output file \"%s\" after writing (fclose: %s)",prefs.outfile,pstrerror(errno));	    	    if (prefs.process[0]!='\0')		system(prefs.process);	}		/* select socket to operate on */	PSOCK_FD_ZERO(&rfds);	PSOCK_FD_SET(sockfd,&rfds);	tv.tv_sec = BNTRACKD_GRANULARITY;	tv.tv_usec = 0;	switch (psock_select(sockfd+1,&rfds,NULL,NULL,&tv))        {        case -1: /* error */            if (#ifdef PSOCK_EINTR                errno!=PSOCK_EINTR &&#endif                1)                eventlog(eventlog_level_error,__FUNCTION__,"select failed (select: %s)",pstrerror(errno));        case 0: /* timeout and no sockets ready */            continue;        }		/* New tracking packet */	if (PSOCK_FD_ISSET(sockfd,&rfds))	{	    	    len = sizeof(cliaddr);	    if (psock_recvfrom(sockfd,&packet,sizeof(packet),0,(struct sockaddr *)&cliaddr,&len)>=0)	    {		

⌨️ 快捷键说明

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