| 1 | /* |
| 2 | * Copyright (C) 1999,2000 Ross Combs ([email protected]) |
| 3 | * Copyright (C) 1999,2000 Rob Crittenden ([email protected]) |
| 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 | #include "common/setup_before.h" |
| 20 | #include "handle_file.h" |
| 21 | |
| 22 | #include "compat/psock.h" |
| 23 | #include "common/eventlog.h" |
| 24 | #include "common/packet.h" |
| 25 | #include "common/bn_type.h" |
| 26 | |
| 27 | #include "connection.h" |
| 28 | #include "file.h" |
| 29 | #include "common/setup_after.h" |
| 30 | |
| 31 | |
| 32 | namespace pvpgn |
| 33 | { |
| 34 | |
| 35 | namespace bnetd |
| 36 | { |
| 37 | |
| 38 | extern int handle_file_packet(t_connection * c, t_packet const * const packet) |
| 39 | { |
| 40 | if (!c) |
| 41 | { |
| 42 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(c)); |
| 43 | return -1; |
| 44 | } |
| 45 | if (!packet) |
| 46 | { |
| 47 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet", conn_get_socket(c)); |
| 48 | return -1; |
| 49 | } |
| 50 | /* REMOVED BY UNDYING SOULZZ 4/3/02 */ |
| 51 | /* |
| 52 | if (packet_get_class(packet)!=packet_class_file) |
| 53 | { |
| 54 | eventlog(eventlog_level_error,__FUNCTION__,"[%d] got bad packet (class %d)",conn_get_socket(c),(int)packet_get_class(packet)); |
| 55 | return -1; |
| 56 | } |
| 57 | */ |
| 58 | switch (conn_get_state(c)) |
| 59 | { |
| 60 | case conn_state_connected: |
| 61 | switch (packet_get_type(packet)) |
| 62 | { |
| 63 | case CLIENT_FILE_REQ: |
| 64 | { |
| 65 | char const * rawname; |
| 66 | |
| 67 | if (!(rawname = packet_get_str_const(packet, sizeof(t_client_file_req), MAX_FILENAME_STR))) |
| 68 | { |
| 69 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad FILE_REQ (missing or too long filename)", conn_get_socket(c)); |
| 70 | |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | file_send(c, rawname, |
| 75 | bn_int_get(packet->u.client_file_req.adid), |
| 76 | bn_int_get(packet->u.client_file_req.extensiontag), |
| 77 | bn_int_get(packet->u.client_file_req.startoffset), |
| 78 | 1); |
| 79 | } |
| 80 | break; |
| 81 | |
| 82 | case CLIENT_FILE_REQ2: |
| 83 | { |
| 84 | t_packet * rpacket = NULL; |
| 85 | if ((rpacket = packet_create(packet_class_raw))) { |
| 86 | packet_set_size(rpacket, sizeof(t_server_file_unknown1)); |
| 87 | bn_int_set(&rpacket->u.server_file_unknown1.unknown, 0xdeadbeef); |
| 88 | conn_push_outqueue(c, rpacket); |
| 89 | packet_del_ref(rpacket); |
| 90 | } |
| 91 | conn_set_state(c, conn_state_pending_raw); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | default: |
| 96 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown file packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet)); |
| 97 | |
| 98 | break; |
| 99 | } |
| 100 | break; |
| 101 | |
| 102 | case conn_state_pending_raw: |
| 103 | switch (packet_get_type(packet)) |
| 104 | { |
| 105 | case CLIENT_FILE_REQ3: |
| 106 | { |
| 107 | char rawname[MAX_FILENAME_STR] = {}; |
| 108 | |
| 109 | psock_recv(conn_get_socket(c), rawname, MAX_FILENAME_STR, 0); |
| 110 | file_send(c, rawname, 0, 0, 0, 1); |
| 111 | } |
| 112 | break; |
| 113 | |
| 114 | default: |
| 115 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown file packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet)); |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | break; |
| 120 | |
| 121 | default: |
| 122 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown file connection state {}", conn_get_socket(c), (int)conn_get_state(c)); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | } |