| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Onlyer ([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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 19 | #include "setup.h"cxx . . $ `<file>/src/d2cs/setup.h`/ |
| 20 | #include "net.h"cxx . . $ `<file>/src/d2cs/net.h`/ |
| 21 | |
| 22 | #include <cctype> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "compat/strerror.h"cxx . . $ `<file>/src/compat/strerror.h`/ |
| 26 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 27 | #include "compat/psock.h"cxx . . $ `<file>/src/compat/psock.h`/ |
| 28 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 29 | |
| 30 | namespace pvpgncxx . . $ pvpgn/ |
| 31 | { |
| 32 | |
| 33 | namespace d2cscxx . . $ pvpgn/d2cs/ |
| 34 | { |
| 35 | |
| 36 | |
| 37 | /* FIXME: use addr.h code to do this and make a separate error |
| 38 | * return path so that 255.255.255.255 isn't an error. |
| 39 | */ |
| 40 | extern unsigned long int net_inet_addrcxx . . $ pvpgn/d2cs/net_inet_addr(751346f8406fd082).(char const * host) |
| 41 | { |
| 42 | struct hostent * hp; |
| 43 | |
| 44 | if (stdcxx . . $ std/::isdigitcxx . . $ std/isdigit(a1851578aea3f541).((int)host[0])) { |
| 45 | return inet_addr(host); |
| 46 | } else { |
| 47 | hp=gethostbyname(host); |
| 48 | if (!hp || !(hp->h_addr_list)) { |
| 49 | return ~0UL; |
| 50 | } |
| 51 | return *(unsigned long *)hp->h_addr_list[0]; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | extern int net_socketcxx . . $ pvpgn/d2cs/net_socket(7864480464b09eea).(int type) |
| 56 | { |
| 57 | int sock; |
| 58 | int val; |
| 59 | int ipproto; |
| 60 | |
| 61 | if (type==PSOCK_SOCK_STREAMcxx . . $ `src/compat/psock.h:192:9`!) { |
| 62 | ipproto = PSOCK_IPPROTO_TCPcxx . . $ `src/compat/psock.h:197:10`!; |
| 63 | } else { |
| 64 | ipproto = PSOCK_IPPROTO_UDPcxx . . $ `src/compat/psock.h:202:10`!; |
| 65 | } |
| 66 | if ((sock=psock_socketcxx . . $ `src/compat/psock.h:287:9`!(PSOCK_PF_INETcxx . . $ `src/compat/psock.h:186:9`!, type, ipproto))<0) { |
| 67 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error creating socket (psock_socket: {})", pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 68 | return -1; |
| 69 | } |
| 70 | val=1; |
| 71 | if (psock_setsockoptcxx . . $ `src/compat/psock.h:289:9`!(sock,PSOCK_SOL_SOCKETcxx . . $ `src/compat/psock.h:208:9`!, PSOCK_SO_KEEPALIVEcxx . . $ `src/compat/psock.h:210:9`!, &val, sizeof(val))<0) { |
| 72 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error set socket option KEEPALIVE (psock_setsockopt: {})",pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 73 | } |
| 74 | if (psock_ctlcxx . . $ psock_ctl(bba9d6e9fb34fad7).(sock,PSOCK_NONBLOCKcxx . . $ `src/compat/psock.h:214:9`!)<0) { |
| 75 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error set socket mode to non-block (psock_ctl: {})",pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 76 | psock_closecxx . . $ `src/compat/psock.h:300:9`!(sock); |
| 77 | return -1; |
| 78 | } |
| 79 | return sock; |
| 80 | } |
| 81 | |
| 82 | extern int net_check_connectedcxx . . $ pvpgn/d2cs/net_check_connected(7864480464b09eea).(int sock) |
| 83 | { |
| 84 | int err; |
| 85 | psock_t_socklencxx . . $ `src/compat/psock.h:273:10`! errlen; |
| 86 | |
| 87 | err = 0; |
| 88 | errlen = sizeof(err); |
| 89 | if (psock_getsockoptcxx . . $ `src/compat/psock.h:288:9`!(sock,PSOCK_SOL_SOCKETcxx . . $ `src/compat/psock.h:208:9`!, PSOCK_SO_ERRORcxx . . $ `src/compat/psock.h:211:9`!, &err, &errlen)<0) { |
| 90 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error get socket option SO_ERROR (psock_getsockopt: {})",pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 91 | return -1; |
| 92 | } |
| 93 | if (errlen && err) |
| 94 | return -1; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | extern int net_listencxx . . $ pvpgn/d2cs/net_listen(1028f34fc8855542).(unsigned int ip, unsigned int port, int type) |
| 100 | { |
| 101 | int sock; |
| 102 | int val; |
| 103 | struct sockaddr_in addr; |
| 104 | int ipproto; |
| 105 | |
| 106 | if (type==PSOCK_SOCK_STREAMcxx . . $ `src/compat/psock.h:192:9`!) { |
| 107 | ipproto = PSOCK_IPPROTO_TCPcxx . . $ `src/compat/psock.h:197:10`!; |
| 108 | } else { |
| 109 | ipproto = PSOCK_IPPROTO_UDPcxx . . $ `src/compat/psock.h:202:10`!; |
| 110 | } |
| 111 | if ((sock=psock_socketcxx . . $ `src/compat/psock.h:287:9`!(PSOCK_PF_INETcxx . . $ `src/compat/psock.h:186:9`!, type, ipproto))<0) { |
| 112 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error create listen socket"); |
| 113 | return -1; |
| 114 | } |
| 115 | val=1; |
| 116 | if (psock_setsockoptcxx . . $ `src/compat/psock.h:289:9`!(sock,PSOCK_SOL_SOCKETcxx . . $ `src/compat/psock.h:208:9`!,PSOCK_SO_REUSEADDRcxx . . $ `src/compat/psock.h:209:9`!,&val,sizeof(int))<0) { |
| 117 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error set socket option SO_REUSEADDR"); |
| 118 | } |
| 119 | stdcxx . . $ std/::memset(&addr,0,sizeof(addr)); |
| 120 | addr.sin_family=PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!; |
| 121 | addr.sin_port=htons(port); |
| 122 | addr.sin_addr.s_addr=htonl(ip); |
| 123 | if (psock_bindcxx . . $ `src/compat/psock.h:292:9`!(sock,(struct sockaddr *)&addr, sizeof(addr))<0) { |
| 124 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error bind listen socket"); |
| 125 | psock_closecxx . . $ `src/compat/psock.h:300:9`!(sock); |
| 126 | return -1; |
| 127 | } |
| 128 | if (psock_listencxx . . $ `src/compat/psock.h:291:9`!(sock,LISTEN_QUEUE)<0) { |
| 129 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error listen socket"); |
| 130 | psock_closecxx . . $ `src/compat/psock.h:300:9`!(sock); |
| 131 | return -1; |
| 132 | } |
| 133 | return sock; |
| 134 | } |
| 135 | |
| 136 | extern int net_send_datacxx . . $ pvpgn/d2cs/net_send_data(d87e05671b9d1f25).(int sock, char * buff, int buffsize, int * pos, int * currsize) |
| 137 | { |
| 138 | int nsend; |
| 139 | |
| 140 | ASSERTcxx . . $ `src/d2cs/setup.h:67:9`!(buff,-1); |
| 141 | ASSERTcxx . . $ `src/d2cs/setup.h:67:9`!(pos,-1); |
| 142 | if (*pos>buffsize) { |
| 143 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] send buffer overflow pos={} buffsize={}",sock,*pos,buffsize); |
| 144 | return -1; |
| 145 | } |
| 146 | if (*currsize>*pos) { |
| 147 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] send buffer error currsize={} pos={}",sock,*currsize,*pos); |
| 148 | return -1; |
| 149 | } |
| 150 | nsend=psock_sendcxx . . $ `src/compat/psock.h:295:9`!(sock,buff+*pos-*currsize,*currsize,0); |
| 151 | if (nsend==0) { |
| 152 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] no data sent (close connection)",sock); |
| 153 | return -1; |
| 154 | } |
| 155 | if (nsend<0) { |
| 156 | if ( |
| 157 | #ifdef PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! |
| 158 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! || |
| 159 | #endif |
| 160 | #ifdef PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! |
| 161 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! || |
| 162 | #endif |
| 163 | #ifdef PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! |
| 164 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! || |
| 165 | #endif |
| 166 | #ifdef PSOCK_NOBUFS |
| 167 | psock_errno()==PSOCK_ENOBUFS || |
| 168 | #endif |
| 169 | #ifdef PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! |
| 170 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`! || |
| 171 | #endif |
| 172 | 0) |
| 173 | return 0; |
| 174 | |
| 175 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] error sent data (closing connection) (psock_send: {})",sock,pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 176 | return -1; |
| 177 | } |
| 178 | *currsize -= nsend; |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | extern int net_recv_datacxx . . $ pvpgn/d2cs/net_recv_data(d87e05671b9d1f25).(int sock, char * buff, int buffsize, int * pos, int * currsize) |
| 183 | { |
| 184 | int nrecv; |
| 185 | |
| 186 | ASSERTcxx . . $ `src/d2cs/setup.h:67:9`!(buff,-1); |
| 187 | ASSERTcxx . . $ `src/d2cs/setup.h:67:9`!(pos,-1); |
| 188 | if (*pos>buffsize) { |
| 189 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] recv buffer overflow pos={} buffsize={}",sock,*pos,buffsize); |
| 190 | return -1; |
| 191 | } |
| 192 | if (*currsize>*pos) { |
| 193 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] recv buffer error currsize={} pos={}",sock,*currsize,*pos); |
| 194 | return -1; |
| 195 | } |
| 196 | if (*pos==buffsize) { |
| 197 | stdcxx . . $ std/::memmove(buff,buff+*pos,*currsize); |
| 198 | *pos=0; |
| 199 | } |
| 200 | nrecv=psock_recvcxx . . $ `src/compat/psock.h:297:9`!(sock,buff+*pos,buffsize-*pos,0); |
| 201 | if (nrecv==0) { |
| 202 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info.,__FUNCTION__,"[{}] remote host closed connection",sock); |
| 203 | return -1; |
| 204 | } |
| 205 | if (nrecv<0) { |
| 206 | if ( |
| 207 | #ifdef PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! |
| 208 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`! || |
| 209 | #endif |
| 210 | #ifdef PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! |
| 211 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! || |
| 212 | #endif |
| 213 | #ifdef PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! |
| 214 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`! || |
| 215 | #endif |
| 216 | #ifdef PSOCK_ENOMEMcxx . . $ `src/compat/psock.h:248:10`! |
| 217 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`! || |
| 218 | #endif |
| 219 | 0) |
| 220 | return 0; |
| 221 | |
| 222 | if ( |
| 223 | #ifdef PSOCK_ENOTCONNcxx . . $ `src/compat/psock.h:236:10`! |
| 224 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_ENOTCONNcxx . . $ `src/compat/psock.h:236:10`! || |
| 225 | #endif |
| 226 | #ifdef PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! |
| 227 | psock_errnocxx . . $ `src/compat/psock.h:286:9`!()==PSOCK_ECONNRESETcxx . . $ `src/compat/psock.h:233:10`! || |
| 228 | #endif |
| 229 | 0) { |
| 230 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] remote host closed connection (psock_recv: {})",sock,pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 231 | return -1; |
| 232 | } |
| 233 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"[{}] recv error (closing connection) (psock_recv: {})",sock,pstrerrorcxx . . $ `src/compat/strerror.h:33:10`!(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 234 | return -1; |
| 235 | } |
| 236 | * currsize += nrecv; |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | } |