| 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" |
| 20 | #include "common/network.h" |
| 21 | |
| 22 | #include <cerrno> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "compat/socket.h" |
| 26 | #include "compat/recv.h" |
| 27 | #include "compat/send.h" |
| 28 | #include "compat/netinet_in.h" |
| 29 | #include "compat/psock.h" |
| 30 | #include "common/packet.h" |
| 31 | #include "common/eventlog.h" |
| 32 | #include "common/field_sizes.h" |
| 33 | #include "common/setup_after.h" |
| 34 | |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | extern int net_recv(int sock, void *buff, int len) |
| 40 | { |
| 41 | int res; |
| 42 | |
| 43 | res = psock_recv(sock, buff, len, 0); |
| 44 | if (res > 0) return res; |
| 45 | if (!res) return -1; /* connection closed */ |
| 46 | |
| 47 | if ( |
| 48 | #ifdef PSOCK_EINTR |
| 49 | psock_errno() == PSOCK_EINTR || |
| 50 | #endif |
| 51 | #ifdef PSOCK_EAGAIN |
| 52 | psock_errno() == PSOCK_EAGAIN || |
| 53 | #endif |
| 54 | #ifdef PSOCK_EWOULDBLOCK |
| 55 | psock_errno() == PSOCK_EWOULDBLOCK || |
| 56 | #endif |
| 57 | #ifdef PSOCK_ENOMEM |
| 58 | psock_errno() == PSOCK_ENOMEM || |
| 59 | #endif |
| 60 | 0) /* try later */ |
| 61 | return 0; |
| 62 | |
| 63 | if ( |
| 64 | #ifdef PSOCK_ENOTCONN |
| 65 | psock_errno() == PSOCK_ENOTCONN || |
| 66 | #endif |
| 67 | #ifdef PSOCK_ECONNRESET |
| 68 | psock_errno() == PSOCK_ECONNRESET || |
| 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 | eventlog(eventlog_level_debug, __FUNCTION__, "[{}] receive error (closing connection) (psock_recv: {})", sock, std::strerror(psock_errno())); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | extern int net_recv_packet(int sock, t_packet * packet, unsigned int * currsize) |
| 80 | { |
| 81 | int addlen; |
| 82 | unsigned int header_size; |
| 83 | void * temp; |
| 84 | |
| 85 | if (!packet) { |
| 86 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet (closing connection)", sock); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | if (!currsize) { |
| 91 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL currsize (closing connection)", sock); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if ((header_size = packet_get_header_size(packet)) >= MAX_PACKET_SIZE) { |
| 96 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not determine header size (closing connection)", sock); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | if (!(temp = packet_get_raw_data_build(packet, *currsize))) { |
| 101 | eventlog(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_recv(sock, temp, header_size - *currsize); |
| 107 | else { |
| 108 | unsigned int total_size = packet_get_size(packet); |
| 109 | |
| 110 | if (total_size < header_size) { |
| 111 | eventlog(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 | eventlog(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_recv(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_size(packet)) |
| 128 | return 1; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | extern int net_send(int sock, const void *buff, int len) |
| 134 | { |
| 135 | int res; |
| 136 | |
| 137 | res = psock_send(sock, buff, len, 0); |
| 138 | |
| 139 | if (res > 0) return res; |
| 140 | if (!res) return -1; |
| 141 | |
| 142 | if ( |
| 143 | #ifdef PSOCK_EINTR |
| 144 | psock_errno() == PSOCK_EINTR || |
| 145 | #endif |
| 146 | #ifdef PSOCK_EAGAIN |
| 147 | psock_errno() == PSOCK_EAGAIN || |
| 148 | #endif |
| 149 | #ifdef PSOCK_EWOULDBLOCK |
| 150 | psock_errno() == PSOCK_EWOULDBLOCK || |
| 151 | #endif |
| 152 | #ifdef PSOCK_ENOBUFS |
| 153 | psock_errno() == PSOCK_ENOBUFS || |
| 154 | #endif |
| 155 | #ifdef PSOCK_ENOMEM |
| 156 | psock_errno() == PSOCK_ENOMEM || |
| 157 | #endif |
| 158 | 0) |
| 159 | return 0; /* try again later */ |
| 160 | |
| 161 | if ( |
| 162 | #ifdef PSOCK_EPIPE |
| 163 | psock_errno() != PSOCK_EPIPE && |
| 164 | #endif |
| 165 | #ifdef PSOCK_ECONNRESET |
| 166 | psock_errno() != PSOCK_ECONNRESET && |
| 167 | #endif |
| 168 | 1) eventlog(eventlog_level_debug, __FUNCTION__, "[{}] could not send data (closing connection) (psock_send: {})", sock, std::strerror(psock_errno())); |
| 169 | |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | extern int net_send_packet(int sock, t_packet const * packet, unsigned int * currsize) |
| 174 | { |
| 175 | unsigned int size; |
| 176 | int addlen; |
| 177 | |
| 178 | if (!packet) { |
| 179 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet (closing connection)", sock); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | if (!currsize) { |
| 184 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL currsize (closing connection)", sock); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | if ((size = packet_get_size(packet)) < 1) { |
| 189 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet to send is empty (skipping it)", sock); |
| 190 | *currsize = 0; |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | addlen = net_send(sock, packet_get_raw_data_const(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 | } |