| 1 | /* |
| 2 | * Copyright (C) 1999,2000 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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 19 | #include "udptest.h"cxx . . $ `<file>/src/client/udptest.h`/ |
| 20 | |
| 21 | #include <cstdio> |
| 22 | #include <cerrno> |
| 23 | #include <cstring> |
| 24 | #include <ctime> |
| 25 | |
| 26 | #include "compat/psock.h"cxx . . $ `<file>/src/compat/psock.h`/ |
| 27 | #include "common/packet.h"cxx . . $ `<file>/src/common/packet.h`/ |
| 28 | #include "common/bn_type.h"cxx . . $ `<file>/src/common/bn_type.h`/ |
| 29 | #include "common/tag.h"cxx . . $ `<file>/src/common/tag.h`/ |
| 30 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 31 | |
| 32 | |
| 33 | #ifdef CLIENTDEBUGcxx . . $ `src/common/setup_before.h:49:9`! |
| 34 | #define dprintfcxx . . $ `src/client/udptest.cpp:34:9`! printf |
| 35 | #else |
| 36 | #define dprintf if (0) printf |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | namespace pvpgncxx . . $ pvpgn/ |
| 41 | { |
| 42 | |
| 43 | namespace clientcxx . . $ pvpgn/client/ |
| 44 | { |
| 45 | |
| 46 | extern int client_udptest_setupcxx . . $ pvpgn/client/client_udptest_setup(c9bce2f4bcd31d4).(char const * progname, unsigned short * lsock_port_ret) |
| 47 | { |
| 48 | int lsock; |
| 49 | struct sockaddr_in laddr; |
| 50 | unsigned short lsock_port; |
| 51 | |
| 52 | if (!progname) |
| 53 | { |
| 54 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "got NULL progname\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | if ((lsock = psock_socketcxx . . $ `src/compat/psock.h:287:9`!(PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!, SOCK_DGRAMcxx . . $ `<external>/5956532346368192945/socket_type.h:31:9`!, PSOCK_IPPROTO_UDPcxx . . $ `src/compat/psock.h:202:10`!)) < 0) |
| 59 | { |
| 60 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create UDP socket (psock_socket: %s)\n", progname, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | if (psock_ctlcxx . . $ psock_ctl(bba9d6e9fb34fad7).(lsock, PSOCK_NONBLOCKcxx . . $ `src/compat/psock.h:214:9`!) < 0) |
| 65 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not set UDP socket to non-blocking mode (psock_ctl: %s)\n", progname, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 66 | |
| 67 | for (lsock_port = BNETD_MIN_TEST_PORTcxx . . $ BNETD_MIN_TEST_PORT.; lsock_port <= BNETD_MAX_TEST_PORTcxx . . $ BNETD_MAX_TEST_PORT.; lsock_port++) |
| 68 | { |
| 69 | stdcxx . . $ std/::memset(&laddr, 0, sizeof(laddr)); |
| 70 | laddr.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!; |
| 71 | laddr.sin_port = htons(lsock_port); |
| 72 | laddr.sin_addr.s_addr = htonl(INADDR_ANYcxx . . $ `<external>/9581057695539925927/in.h:198:9`!); |
| 73 | if (psock_bindcxx . . $ `src/compat/psock.h:292:9`!(lsock, (struct sockaddr *)&laddr, (psock_t_socklencxx . . $ `src/compat/psock.h:273:10`!)sizeof(laddr)) == 0) |
| 74 | break; |
| 75 | |
| 76 | if (lsock_port == BNETD_MIN_TEST_PORTcxx . . $ BNETD_MIN_TEST_PORT.) |
| 77 | dprintfcxx . . $ `src/client/udptest.cpp:34:9`!("Could not bind to standard UDP port %hu, trying others. (psock_bind: %s)\n", BNETD_MIN_TEST_PORTcxx . . $ BNETD_MIN_TEST_PORT., stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 78 | } |
| 79 | if (lsock_port > BNETD_MAX_TEST_PORTcxx . . $ BNETD_MAX_TEST_PORT.) |
| 80 | { |
| 81 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not bind to any UDP port %hu through %hu (psock_bind: %s)\n", progname, BNETD_MIN_TEST_PORTcxx . . $ BNETD_MIN_TEST_PORT., BNETD_MAX_TEST_PORTcxx . . $ BNETD_MAX_TEST_PORT., stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 82 | psock_closecxx . . $ `src/compat/psock.h:300:9`!(lsock); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | if (lsock_port_ret) |
| 87 | *lsock_port_ret = lsock_port; |
| 88 | |
| 89 | return lsock; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | extern int client_udptest_recvcxx . . $ pvpgn/client/client_udptest_recv(4c1d61b0332639d9).(char const * progname, int lsock, unsigned short lsock_port, unsigned int timeout) |
| 94 | { |
| 95 | int len; |
| 96 | unsigned int count; |
| 97 | t_packetcxx . . $ pvpgn/t_packet# * rpacket; |
| 98 | stdcxx . . $ std/::time_tcxx . . $ std/time_t# start; |
| 99 | |
| 100 | if (!progname) |
| 101 | { |
| 102 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: got NULL progname\n", progname); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | if (!(rpacket = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_bnetcxx . . $ pvpgn/packet_class_bnet.))) |
| 107 | { |
| 108 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create packet\n", progname); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | start = stdcxx . . $ std/::timecxx . . $ std/time(a7cd306b2d326b09).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 113 | count = 0; |
| 114 | while (start + (stdcxx . . $ std/::time_tcxx . . $ std/time_t#)timeout >= stdcxx . . $ std/::timecxx . . $ std/time(a7cd306b2d326b09).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) /* timeout after a few seconds from last packet */ |
| 115 | { |
| 116 | if ((len = psock_recvcxx . . $ `src/compat/psock.h:297:9`!(lsock, packet_get_raw_data_build(rpacket, 0), MAX_PACKET_SIZE, 0)) < 0) |
| 117 | { |
| 118 | if (psock_errnocxx . . $ `src/compat/psock.h:286:9`!() != PSOCK_EAGAINcxx . . $ `src/compat/psock.h:230:10`! && psock_errnocxx . . $ `src/compat/psock.h:286:9`!() != PSOCK_EWOULDBLOCKcxx . . $ `src/compat/psock.h:218:10`!) |
| 119 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: failed to receive UDPTEST on port %hu (psock_recv: %s)\n", progname, lsock_port, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!())); |
| 120 | continue; |
| 121 | } |
| 122 | packet_set_sizecxx . . $ pvpgn/packet_set_size(a12d8724e4c46208).(rpacket, len); |
| 123 | |
| 124 | if (packet_get_typecxx . . $ pvpgn/packet_get_type(e4ac9e2b4744cb4b).(rpacket) != SERVER_UDPTESTcxx . . $ `src/common/udp_protocol.h:52:9`!) |
| 125 | { |
| 126 | dprintfcxx . . $ `src/client/udptest.cpp:34:9`!("Got unexpected UDP packet type %u on port %hu\n", packet_get_typecxx . . $ pvpgn/packet_get_type(e4ac9e2b4744cb4b).(rpacket), lsock_port); |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | if (bn_int_tag_eq(rpacket->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..server_udptestcxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#server_udptest..bnettagcxx . . $ pvpgn/$anonymous_type_68de0d28359c8d56_2#bnettag., BNETTAGcxx . . $ `src/common/tag.h:181:9`!) < 0) |
| 131 | { |
| 132 | stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: got bad UDPTEST packet on port %hu\n", progname, lsock_port); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | count++; |
| 137 | if (count >= 2) |
| 138 | break; |
| 139 | |
| 140 | start = stdcxx . . $ std/::timecxx . . $ std/time(a7cd306b2d326b09).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 141 | } |
| 142 | |
| 143 | packet_destroycxx . . $ pvpgn/packet_destroy(5beaf79da73358e2).(rpacket); |
| 144 | |
| 145 | if (count < 2) |
| 146 | { |
| 147 | stdcxx . . $ std/::printfcxx . . $ std/printf(581fddd72a8db8fc).("Only received %d UDP packets on port %hu. Connection may be slow or firewalled.\n", count, lsock_port); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } |