| 1 | /* |
| 2 | * Copyright (C) 1999,2000,2001 Ross Combs ([email protected]) |
| 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 "handle_udp.h" |
| 20 | |
| 21 | #include "common/eventlog.h" |
| 22 | #include "common/packet.h" |
| 23 | #include "common/addr.h" |
| 24 | #include "common/bn_type.h" |
| 25 | |
| 26 | #include "connection.h" |
| 27 | #include "udptest_send.h" |
| 28 | #include "common/setup_after.h" |
| 29 | |
| 30 | namespace pvpgn |
| 31 | { |
| 32 | |
| 33 | namespace bnetd |
| 34 | { |
| 35 | |
| 36 | extern int handle_udp_packet(int usock, unsigned int src_addr, unsigned short src_port, t_packet const * const packet) |
| 37 | { |
| 38 | if (!packet) |
| 39 | { |
| 40 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet", usock); |
| 41 | return -1; |
| 42 | } |
| 43 | if (packet_get_class(packet) != packet_class_udp) |
| 44 | { |
| 45 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad packet (class {})", usock, (int)packet_get_class(packet)); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | switch (packet_get_type(packet)) |
| 50 | { |
| 51 | case SERVER_UDPTEST: /* we might get these if a client is running on the same machine as us */ |
| 52 | if (packet_get_size(packet) < sizeof(t_server_udptest)) |
| 53 | { |
| 54 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UDPTEST packet (expected {} bytes, got {})", usock, sizeof(t_server_udptest), packet_get_size(packet)); |
| 55 | return -1; |
| 56 | } |
| 57 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got UDPTEST packet from {} (myself?)", usock, addr_num_to_addr_str(src_addr, src_port)); |
| 58 | return 0; |
| 59 | |
| 60 | case CLIENT_UDPPING: |
| 61 | if (packet_get_size(packet) < sizeof(t_client_udpping)) |
| 62 | { |
| 63 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UDPPING packet (expected {} bytes, got {})", usock, sizeof(t_client_udpping), packet_get_size(packet)); |
| 64 | return -1; |
| 65 | } |
| 66 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got udpping unknown1={}", usock, bn_int_get(packet->u.client_udpping.unknown1)); |
| 67 | return 0; |
| 68 | |
| 69 | case CLIENT_SESSIONADDR1: |
| 70 | if (packet_get_size(packet) < sizeof(t_client_sessionaddr1)) |
| 71 | { |
| 72 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad SESSIONADDR1 packet (expected {} bytes, got {})", usock, sizeof(t_client_sessionaddr1), packet_get_size(packet)); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | { |
| 77 | t_connection * c; |
| 78 | |
| 79 | if (!(c = connlist_find_connection_by_sessionkey(bn_int_get(packet->u.client_sessionaddr1.sessionkey)))) |
| 80 | { |
| 81 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] address not set (no connection with session key 0x{:08x})", usock, bn_int_get(packet->u.client_sessionaddr1.sessionkey)); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | if (conn_get_game_addr(c) != src_addr || conn_get_game_port(c) != src_port) |
| 86 | eventlog(eventlog_level_info, __FUNCTION__, "[{}][{}] SESSIONADDR1 set new UDP address to {}", conn_get_socket(c), usock, addr_num_to_addr_str(src_addr, src_port)); |
| 87 | |
| 88 | conn_set_game_socket(c, usock); |
| 89 | conn_set_game_addr(c, src_addr); |
| 90 | conn_set_game_port(c, src_port); |
| 91 | |
| 92 | udptest_send(c); |
| 93 | } |
| 94 | return 0; |
| 95 | |
| 96 | case CLIENT_SESSIONADDR2: |
| 97 | if (packet_get_size(packet) < sizeof(t_client_sessionaddr2)) |
| 98 | { |
| 99 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad SESSIONADDR2 packet (expected {} bytes, got {})", usock, sizeof(t_client_sessionaddr2), packet_get_size(packet)); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | t_connection * c; |
| 105 | |
| 106 | if (!(c = connlist_find_connection_by_sessionnum(bn_int_get(packet->u.client_sessionaddr2.sessionnum)))) |
| 107 | { |
| 108 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] address not set (no connection with session number {})", usock, bn_int_get(packet->u.client_sessionaddr2.sessionnum)); |
| 109 | return -1; |
| 110 | } |
| 111 | if (conn_get_sessionkey(c) != bn_int_get(packet->u.client_sessionaddr2.sessionkey)) |
| 112 | { |
| 113 | eventlog(eventlog_level_error, __FUNCTION__, "[{}][{}] 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)); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | if (conn_get_game_addr(c) != src_addr || conn_get_game_port(c) != src_port) |
| 118 | eventlog(eventlog_level_info, __FUNCTION__, "[{}][{}] SESSIONADDR2 set new UDP address to {}", conn_get_socket(c), usock, addr_num_to_addr_str(src_addr, src_port)); |
| 119 | |
| 120 | conn_set_game_socket(c, usock); |
| 121 | conn_set_game_addr(c, src_addr); |
| 122 | conn_set_game_port(c, src_port); |
| 123 | |
| 124 | udptest_send(c); |
| 125 | } |
| 126 | return 0; |
| 127 | |
| 128 | case CLIENT_SEARCH_LAN_GAMES: //added by Spider |
| 129 | { |
| 130 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got SEARCH_LAN_GAMES packet from {}", usock, addr_num_to_addr_str(src_addr, src_port)); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | default: |
| 135 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got unknown udp packet type 0x{:04x}, len {} from {}", usock, packet_get_type(packet), packet_get_size(packet), addr_num_to_addr_str(src_addr, src_port)); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | } |