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

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 1999,2000,2001  Ross Combs (rocombs@cs.nmsu.edu)
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #include "common/setup_before.h"
  19. #include "common/packet.h"
  20. #include "common/udp_protocol.h"
  21. #include "common/eventlog.h"
  22. #include "connection.h"
  23. #include "common/queue.h"
  24. #include "common/bn_type.h"
  25. #include "common/addr.h"
  26. #include "udptest_send.h"
  27. #include "handle_udp.h"
  28. #include "common/setup_after.h"
  29. extern int handle_udp_packet(int usock, unsigned int src_addr, unsigned short src_port, t_packet const * const packet)
  30. {
  31.     if (!packet)
  32.     {
  33.         eventlog(eventlog_level_error,"handle_udp_packet","[%d] got NULL packet",usock);
  34.         return -1;
  35.     }
  36.     if (packet_get_class(packet)!=packet_class_udp)
  37.     {
  38.         eventlog(eventlog_level_error,"handle_udp_packet","[%d] got bad packet (class %d)",usock,(int)packet_get_class(packet));
  39.         return -1;
  40.     }
  41.     
  42.     switch (packet_get_type(packet))
  43.     {
  44.     case SERVER_UDPTEST: /* we might get these if a client is running on the same machine as us */
  45. if (packet_get_size(packet)<sizeof(t_server_udptest))
  46. {
  47.     eventlog(eventlog_level_error,"handle_udp_packet","[%d] got bad UDPTEST packet (expected %u bytes, got %u)",usock,sizeof(t_server_udptest),packet_get_size(packet));
  48.     return -1;
  49. }
  50. eventlog(eventlog_level_debug,"handle_udp_packet","[%d] got UDPTEST packet from %s (myself?)",usock,addr_num_to_addr_str(src_addr,src_port));
  51. return 0;
  52.     case CLIENT_UDPPING:
  53. if (packet_get_size(packet)<sizeof(t_client_udpping))
  54. {
  55.     eventlog(eventlog_level_error,"handle_udp_packet","[%d] got bad UDPPING packet (expected %u bytes, got %u)",usock,sizeof(t_client_udpping),packet_get_size(packet));
  56.     return -1;
  57. }
  58. eventlog(eventlog_level_debug,"handle_udp_packet","[%d] got udpping unknown1=%u",usock,bn_int_get(packet->u.client_udpping.unknown1));
  59. return 0;
  60.     case CLIENT_SESSIONADDR1:
  61. if (packet_get_size(packet)<sizeof(t_client_sessionaddr1))
  62. {
  63.     eventlog(eventlog_level_error,"handle_udp_packet","[%d] got bad SESSIONADDR1 packet (expected %u bytes, got %u)",usock,sizeof(t_client_sessionaddr1),packet_get_size(packet));
  64.     return -1;
  65. }
  66. {
  67.     t_connection * c;
  68.     
  69.     if (!(c = connlist_find_connection_by_sessionkey(bn_int_get(packet->u.client_sessionaddr1.sessionkey))))
  70.     {
  71. eventlog(eventlog_level_error,"handle_udp_packet","[%d] address not set (no connection with session key 0x%08x)",usock,bn_int_get(packet->u.client_sessionaddr1.sessionkey));
  72. return -1;
  73.     }
  74.     
  75.     if (conn_get_game_addr(c)!=src_addr || conn_get_game_port(c)!=src_port)
  76. eventlog(eventlog_level_info,"handle_udp_packet","[%d][%d] SESSIONADDR1 set new UDP address to %s",conn_get_socket(c),usock,addr_num_to_addr_str(src_addr,src_port));
  77.     
  78.     conn_set_game_socket(c,usock);
  79.     conn_set_game_addr(c,src_addr);
  80.     conn_set_game_port(c,src_port);
  81.     
  82.     udptest_send(c);
  83. }
  84. return 0;
  85.     case CLIENT_SESSIONADDR2:
  86. if (packet_get_size(packet)<sizeof(t_client_sessionaddr2))
  87. {
  88.     eventlog(eventlog_level_error,"handle_udp_packet","[%d] got bad SESSIONADDR2 packet (expected %u bytes, got %u)",usock,sizeof(t_client_sessionaddr2),packet_get_size(packet));
  89.     return -1;
  90. }
  91. {
  92.     t_connection * c;
  93.     
  94.     if (!(c = connlist_find_connection_by_sessionnum(bn_int_get(packet->u.client_sessionaddr2.sessionnum))))
  95.     {
  96. eventlog(eventlog_level_error,"handle_udp_packet","[%d] address not set (no connection with session number %u)",usock,bn_int_get(packet->u.client_sessionaddr2.sessionnum));
  97. return -1;
  98.     }
  99.     if (conn_get_sessionkey(c)!=bn_int_get(packet->u.client_sessionaddr2.sessionkey))
  100.     {
  101. eventlog(eventlog_level_error,"handle_udp_packet","[%d][%d] address not set (expected session key 0x%08x, got 0x%08x)",conn_get_socket(c),usock,conn_get_sessionkey(c),bn_int_get(packet->u.client_sessionaddr2.sessionkey));
  102. return -1;
  103.     }
  104.     
  105.     if (conn_get_game_addr(c)!=src_addr || conn_get_game_port(c)!=src_port)
  106. eventlog(eventlog_level_info,"handle_udp_packet","[%d][%d] SESSIONADDR2 set new UDP address to %s",conn_get_socket(c),usock,addr_num_to_addr_str(src_addr,src_port));
  107.     
  108.     conn_set_game_socket(c,usock);
  109.     conn_set_game_addr(c,src_addr);
  110.     conn_set_game_port(c,src_port);
  111.     
  112.     udptest_send(c);
  113. }
  114. return 0;
  115. case CLIENT_SEARCH_LAN_GAMES: //added by Spider
  116. {
  117.   eventlog(eventlog_level_debug,__FUNCTION__,"[%d] got SEARCH_LAN_GAMES packet from %s",usock,addr_num_to_addr_str(src_addr,src_port));
  118.   return 0;
  119. }
  120.     default:
  121. eventlog(eventlog_level_error,"handle_udp_packet","[%d] got unknown udp packet type 0x%04x, len %u from %s",usock,(unsigned int)packet_get_type(packet),packet_get_size(packet),addr_num_to_addr_str(src_addr,src_port));
  122.     }
  123.     
  124.     return 0;
  125. }