tracker.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:6k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 1999  Mark Baysinger (mbaysing@ucsd.edu)
  3.  * Copyright (C) 1999  Ross Combs (rocombs@cs.nmsu.edu)
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; either version 2
  8.  * of the License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  */
  19. #define TRACKER_INTERNAL_ACCESS
  20. #define SERVER_INTERNAL_ACCESS
  21. #include "common/setup_before.h"
  22. #ifdef HAVE_STDDEF_H
  23. # include <stddef.h>
  24. #else
  25. # ifndef NULL
  26. #  define NULL ((void *)0)
  27. # endif
  28. #endif
  29. #ifdef HAVE_STRING_H
  30. # include <string.h>
  31. #else
  32. # ifdef HAVE_STRINGS_H
  33. #  include <strings.h>
  34. # endif
  35. # ifdef HAVE_MEMORY_H
  36. #  include <memory.h>
  37. # endif
  38. #endif
  39. #include "compat/memset.h"
  40. #include <errno.h>
  41. #include "compat/strerror.h"
  42. #ifdef TIME_WITH_SYS_TIME
  43. # include <sys/time.h>
  44. # include <time.h>
  45. #else
  46. # ifdef HAVE_SYS_TIME_H
  47. #  include <sys/time.h>
  48. # else
  49. #  include <time.h>
  50. # endif
  51. #endif
  52. #ifdef HAVE_SYS_UTSNAME_H
  53. # include <sys/utsname.h>
  54. #endif
  55. #include "compat/uname.h"
  56. #ifdef HAVE_SYS_TYPES_H
  57. # include <sys/types.h>
  58. #endif
  59. #ifdef HAVE_SYS_SOCKET_H
  60. # include <sys/socket.h>
  61. #endif
  62. #include "compat/socket.h"
  63. #include "compat/send.h"
  64. #ifdef HAVE_SYS_PARAM_H
  65. # include <sys/param.h>
  66. #endif
  67. #ifdef HAVE_NETINET_IN_H
  68. # include <netinet/in.h>
  69. #endif
  70. #include "compat/netinet_in.h"
  71. #include "compat/psock.h"
  72. #include "common/eventlog.h"
  73. #include "connection.h"
  74. #include "channel.h"
  75. #include "prefs.h"
  76. #include "game.h"
  77. #include "common/version.h"
  78. #include "server.h"
  79. #include "common/addr.h"
  80. #include "common/list.h"
  81. #include "common/tracker.h"
  82. #include "common/setup_after.h"
  83. static t_addrlist * track_servers=NULL;
  84. extern int tracker_set_servers(char const * servers)
  85. {
  86.     t_addr const * addr;
  87.     t_elem const * curr;
  88.     char           temp[32];
  89.     
  90.     if (track_servers && addrlist_destroy(track_servers)<0)
  91.         eventlog(eventlog_level_error,"tracker_set_servers","unable to destroy tracker list");
  92.     
  93.     if (!servers)
  94.      {
  95. track_servers = NULL;
  96. return 0;
  97.      }
  98.    
  99.     if (!(track_servers = addrlist_create(servers,INADDR_LOOPBACK,BNETD_TRACK_PORT)))
  100.     {
  101. eventlog(eventlog_level_error,"tracker_set_servers","could not create tracking server list");
  102. return -1;
  103.     }
  104.     
  105.     LIST_TRAVERSE_CONST(track_servers,curr)
  106.     {
  107. addr = elem_get_data(curr);
  108. if (!addr_get_addr_str(addr,temp,sizeof(temp)))
  109.     strcpy(temp,"x.x.x.x:x");
  110. eventlog(eventlog_level_info,"tracker_set_servers","tracking packets will be sent to %s",temp);
  111.     }
  112.     
  113.     return 0;
  114. }
  115. extern int tracker_send_report(t_addrlist const * laddrs)
  116. {
  117.     t_addr const *     addrl;
  118.     t_elem const *     currl;
  119.     t_addr const *     addrt;
  120.     t_elem const *     currt;
  121.     t_trackpacket      packet;
  122.     struct utsname     utsbuf;
  123.     struct sockaddr_in tempaddr;
  124.     t_laddr_info *     laddr_info;
  125.     char               tempa[64];
  126.     char               tempb[64];
  127.     
  128.     if (addrlist_get_length(track_servers)>0)
  129.     {
  130. packet.packet_version = htons((unsigned short)TRACK_VERSION);
  131. /* packet.port is set below */
  132. packet.flags = 0;
  133. strncpy(packet.server_location,
  134. prefs_get_location(),
  135. sizeof(packet.server_location));
  136. packet.server_location[sizeof(packet.server_location)-1] = '';
  137. strncpy(packet.software,
  138. PVPGN_SOFTWARE,
  139. sizeof(packet.software));
  140. strncpy(packet.version,
  141. PVPGN_VERSION,
  142. sizeof(packet.version));
  143. strncpy(packet.server_desc,
  144. prefs_get_description(),
  145. sizeof(packet.server_desc));
  146. packet.server_desc[sizeof(packet.server_desc)-1] = '';
  147. strncpy(packet.server_url,
  148. prefs_get_url(),
  149. sizeof(packet.server_url));
  150. packet.server_url[sizeof(packet.server_url)-1] = '';
  151. strncpy(packet.contact_name,
  152. prefs_get_contact_name(),
  153. sizeof(packet.contact_name));
  154. packet.contact_name[sizeof(packet.contact_name)-1] = '';
  155. strncpy(packet.contact_email,
  156. prefs_get_contact_email(),
  157. sizeof(packet.contact_email));
  158. packet.contact_email[sizeof(packet.contact_email)-1] = '';
  159. packet.users = htonl(connlist_login_get_length());
  160. packet.channels = htonl(channellist_get_length());
  161. packet.games = htonl(gamelist_get_length());
  162. packet.uptime = htonl(server_get_uptime());
  163. packet.total_logins = htonl(connlist_total_logins());
  164. packet.total_games = htonl(gamelist_total_games());
  165. if (uname(&utsbuf)<0)
  166. {
  167.     eventlog(eventlog_level_warn,"tracker_send_report","could not get platform info (uname: %s)",strerror(errno));
  168.     strncpy(packet.platform,"",sizeof(packet.platform));
  169. }
  170. else
  171. {
  172.     strncpy(packet.platform,
  173.     utsbuf.sysname,
  174.     sizeof(packet.platform));
  175.     packet.platform[sizeof(packet.platform)-1] = '';
  176. }
  177. LIST_TRAVERSE_CONST(laddrs,currl)
  178. {
  179.     addrl = elem_get_data(currl);
  180.     
  181.     if (!(laddr_info = addr_get_data(addrl).p))
  182.     {
  183. eventlog(eventlog_level_error,"tracker_send_report","address data is NULL");
  184. continue;
  185.     }
  186.     if (laddr_info->type!=laddr_type_bnet)
  187. continue; /* don't report IRC, telnet, and other non-game ports */
  188.     
  189.     packet.port = htons(addr_get_port(addrl));
  190.     
  191.     LIST_TRAVERSE_CONST(track_servers,currt)
  192.     {
  193. addrt = elem_get_data(currt);
  194. memset(&tempaddr,0,sizeof(tempaddr));
  195. tempaddr.sin_family = PSOCK_AF_INET; 
  196. tempaddr.sin_port = htons(addr_get_port(addrt));
  197. tempaddr.sin_addr.s_addr = htonl(addr_get_ip(addrt));
  198. if (!addr_get_addr_str(addrl,tempa,sizeof(tempa)))
  199.     strcpy(tempa,"x.x.x.x:x");
  200. if (!addr_get_addr_str(addrt,tempb,sizeof(tempb)))
  201.     strcpy(tempa,"x.x.x.x:x");
  202. /* eventlog(eventlog_level_debug,"tracker_send_report","sending tracking info from %s to %s",tempa,tempb); */
  203. if (psock_sendto(laddr_info->usocket,&packet,sizeof(packet),0,(struct sockaddr *)&tempaddr,(psock_t_socklen)sizeof(tempaddr))<0)
  204.     eventlog(eventlog_level_warn,"tracker_send_report","could not send tracking information from %s to %s (psock_sendto: %s)",tempa,tempb,strerror(errno));
  205.     }
  206. }
  207.     }
  208.     
  209.     return 0;
  210. }