| 1 | /* |
| 2 | * Copyright (C) 1998 Mark Baysinger ([email protected]) |
| 3 | * Copyright (C) 1998,1999,2000 Ross Combs ([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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 20 | #include "common/network.h"cxx . . $ `<file>/src/common/network.h`/ |
| 21 | |
| 22 | #include <cerrno> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "compat/socket.h"cxx . . $ `<file>/src/compat/socket.h`/ |
| 26 | #include "compat/recv.h"cxx . . $ `<file>/src/compat/recv.h`/ |
| 27 | #include "compat/send.h"cxx . . $ `<file>/src/compat/send.h`/ |
| 28 | #include "compat/netinet_in.h"cxx . . $ `<file>/src/compat/netinet_in.h`/ |
| 29 | #include "compat/psock.h"cxx . . $ `<file>/src/compat/psock.h`/ |
| 30 | #include "common/packet.h"cxx . . $ `<file>/src/common/packet.h`/ |
| 31 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 32 | #include "common/field_sizes.h"cxx . . $ `<file>/src/common/field_sizes.h`/ |
| 33 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 34 | |
| 35 | |
| 36 | namespace pvpgncxx . . $ pvpgn/ |
| 37 | { |
| 38 | |
| 39 | extern int net_recvcxx . . $ pvpgn/net_recv(80cd8876edb45190).(int sock, void *buff, int len) |
| 40 | { |
| 41 | int res; |
| 42 | |
| 43 | res = psock_recvcxx . . $ `src/compat/psock.h:297:9`!(sock, buff, len, 0); |
| 44 | if (res > 0) return res; |
| 45 | if (!res) return -1; /* connection closed */ |
| 46 | |
| 47 | if ( |
| 48 | #ifdef PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! |
| 49 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! || |
| 50 | #endif |
| 51 | #ifdef PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! |
| 52 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! || |
| 53 | #endif |
| 54 | #ifdef PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! |
| 55 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! || |
| 56 | #endif |
| 57 | #ifdef PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! |
| 58 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! || |
| 59 | #endif |
| 60 | 0) /* try later */ |
| 61 | return 0; |
| 62 | |
| 63 | if ( |
| 64 | #ifdef PSOCK_ENOTCONNcxx . . $ `src/compat/psock.h:236:10`! |
| 65 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_ENOTCONNcxx . . $ `src/compat/psock.h:236:10`! || |
| 66 | #endif |
| 67 | #ifdef PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! |
| 68 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! || |
| 69 | #endif |
| 70 | 0) { |
| 71 | /* eventlog(eventlog_level_debug,__FUNCTION__,"[{}] remote host closed connection (psock_recv: {})",sock,std::strerror(psock_errno())); */ |
| 72 | return -1; /* common error: close connection, but no message */ |
| 73 | } |
| 74 | |
| 75 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "[{}] receive error (closing connection) (psock_recv: {})", sock, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | extern int net_recv_packetcxx . . $ pvpgn/net_recv_packet(364823e0b5ea3b9f).(int sock, t_packetcxx . . $ pvpgn/t_packet# * packet, unsigned int * currsize) |
| 80 | { |
| 81 | int addlen; |
| 82 | unsigned int header_size; |
| 83 | void * temp; |
| 84 | |
| 85 | if (!packet) { |
| 86 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] got NULL packet (closing connection)", sock); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | if (!currsize) { |
| 91 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] got NULL currsize (closing connection)", sock); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if ((header_size = packet_get_header_sizecxx . . $ pvpgn/packet_get_header_size(e4ac9e2b4744cb4b).(packet)) >= MAX_PACKET_SIZEcxx . . $ MAX_PACKET_SIZE.) { |
| 96 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] could not determine header size (closing connection)", sock); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | if (!(temp = packet_get_raw_data_buildcxx . . $ pvpgn/packet_get_raw_data_build(ed324b086ed1b636).(packet, *currsize))) { |
| 101 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] could not obtain raw data pointer at offset {} (closing connection)", sock, *currsize); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | if (*currsize < header_size) |
| 106 | addlen = net_recvcxx . . $ pvpgn/net_recv(80cd8876edb45190).(sock, temp, header_size - *currsize); |
| 107 | else { |
| 108 | unsigned int total_size = packet_get_sizecxx . . $ pvpgn/packet_get_size(e4ac9e2b4744cb4b).(packet); |
| 109 | |
| 110 | if (total_size < header_size) { |
| 111 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_warncxx . . $ pvpgn/eventlog_level_warn., __FUNCTION__, "[{}] corrupted packet received (total_size={} currsize={}) (closing connection)", sock, total_size, *currsize); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | if (*currsize >= total_size) { |
| 116 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_warncxx . . $ pvpgn/eventlog_level_warn., __FUNCTION__, "[{}] more data requested for already complete packet (total_size={} currsize={}) (closing connection)", sock, total_size, *currsize); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | addlen = net_recvcxx . . $ pvpgn/net_recv(80cd8876edb45190).(sock, temp, total_size - *currsize); |
| 121 | } |
| 122 | |
| 123 | if (addlen <= 0) return addlen; |
| 124 | |
| 125 | *currsize += addlen; |
| 126 | |
| 127 | if (*currsize >= header_size && *currsize == packet_get_sizecxx . . $ pvpgn/packet_get_size(e4ac9e2b4744cb4b).(packet)) |
| 128 | return 1; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | extern int net_sendcxx . . $ pvpgn/net_send(a67153b04d1848fa).(int sock, const void *buff, int len) |
| 134 | { |
| 135 | int res; |
| 136 | |
| 137 | res = psock_sendcxx . . $ `src/compat/psock.h:295:9`!(sock, buff, len, 0); |
| 138 | |
| 139 | if (res > 0) return res; |
| 140 | if (!res) return -1; |
| 141 | |
| 142 | if ( |
| 143 | #ifdef PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! |
| 144 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! || |
| 145 | #endif |
| 146 | #ifdef PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! |
| 147 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! || |
| 148 | #endif |
| 149 | #ifdef PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! |
| 150 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! || |
| 151 | #endif |
| 152 | #ifdef PSOCK_ENOBUFScxx . . $ `src/compat/psock.h:245:10`! |
| 153 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_ENOBUFScxx . . $ `src/compat/psock.h:245:10`! || |
| 154 | #endif |
| 155 | #ifdef PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! |
| 156 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() == PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! || |
| 157 | #endif |
| 158 | 0) |
| 159 | return 0; /* try again later */ |
| 160 | |
| 161 | if ( |
| 162 | #ifdef PSOCK_EPIPEcxx . . $ `src/compat/psock.h:239:10`! |
| 163 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() != PSOCK_EPIPEcxx . . $ `src/compat/psock.h:239:10`! && |
| 164 | #endif |
| 165 | #ifdef PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! |
| 166 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!() != PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! && |
| 167 | #endif |
| 168 | 1) eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_debugcxx . . $ pvpgn/eventlog_level_debug., __FUNCTION__, "[{}] could not send data (closing connection) (psock_send: {})", sock, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 169 | |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | extern int net_send_packetcxx . . $ pvpgn/net_send_packet(98ac2114d339f481).(int sock, t_packetcxx . . $ pvpgn/t_packet# const * packet, unsigned int * currsize) |
| 174 | { |
| 175 | unsigned int size; |
| 176 | int addlen; |
| 177 | |
| 178 | if (!packet) { |
| 179 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] got NULL packet (closing connection)", sock); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | if (!currsize) { |
| 184 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] got NULL currsize (closing connection)", sock); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | if ((size = packet_get_sizecxx . . $ pvpgn/packet_get_size(e4ac9e2b4744cb4b).(packet)) < 1) { |
| 189 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "[{}] packet to send is empty (skipping it)", sock); |
| 190 | *currsize = 0; |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | addlen = net_sendcxx . . $ pvpgn/net_send(a67153b04d1848fa).(sock, packet_get_raw_data_constcxx . . $ pvpgn/packet_get_raw_data_const(1f58ab00371793b4).(packet, *currsize), size - *currsize); |
| 195 | |
| 196 | if (addlen <= 0) return addlen; |
| 197 | |
| 198 | *currsize += addlen; |
| 199 | /* sent all data in this packet? */ |
| 200 | if (size == *currsize) |
| 201 | { |
| 202 | *currsize = 0; |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | } |