| 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" |
| 19 | #include "udptest.h" |
| 20 | |
| 21 | #include <cstdio> |
| 22 | #include <cerrno> |
| 23 | #include <cstring> |
| 24 | #include <ctime> |
| 25 | |
| 26 | #include "compat/psock.h" |
| 27 | #include "common/packet.h" |
| 28 | #include "common/bn_type.h" |
| 29 | #include "common/tag.h" |
| 30 | #include "common/setup_after.h" |
| 31 | |
| 32 | |
| 33 | #ifdef CLIENTDEBUG |
| 34 | #define dprintf printf |
| 35 | #else |
| 36 | #define dprintf if (0) printf |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | namespace pvpgn |
| 41 | { |
| 42 | |
| 43 | namespace client |
| 44 | { |
| 45 | |
| 46 | extern int client_udptest_setup(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 | std::fprintf(stderr, "got NULL progname\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | if ((lsock = psock_socket(PF_INET, SOCK_DGRAM, PSOCK_IPPROTO_UDP)) < 0) |
| 59 | { |
| 60 | std::fprintf(stderr, "%s: could not create UDP socket (psock_socket: %s)\n", progname, std::strerror(psock_errno())); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | if (psock_ctl(lsock, PSOCK_NONBLOCK) < 0) |
| 65 | std::fprintf(stderr, "%s: could not set UDP socket to non-blocking mode (psock_ctl: %s)\n", progname, std::strerror(psock_errno())); |
| 66 | |
| 67 | for (lsock_port = BNETD_MIN_TEST_PORT; lsock_port <= BNETD_MAX_TEST_PORT; lsock_port++) |
| 68 | { |
| 69 | std::memset(&laddr, 0, sizeof(laddr)); |
| 70 | laddr.sin_family = PSOCK_AF_INET; |
| 71 | laddr.sin_port = htons(lsock_port); |
| 72 | laddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 73 | if (psock_bind(lsock, (struct sockaddr *)&laddr, (psock_t_socklen)sizeof(laddr)) == 0) |
| 74 | break; |
| 75 | |
| 76 | if (lsock_port == BNETD_MIN_TEST_PORT) |
| 77 | dprintf("Could not bind to standard UDP port %hu, trying others. (psock_bind: %s)\n", BNETD_MIN_TEST_PORT, std::strerror(psock_errno())); |
| 78 | } |
| 79 | if (lsock_port > BNETD_MAX_TEST_PORT) |
| 80 | { |
| 81 | std::fprintf(stderr, "%s: could not bind to any UDP port %hu through %hu (psock_bind: %s)\n", progname, BNETD_MIN_TEST_PORT, BNETD_MAX_TEST_PORT, std::strerror(psock_errno())); |
| 82 | psock_close(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_recv(char const * progname, int lsock, unsigned short lsock_port, unsigned int timeout) |
| 94 | { |
| 95 | int len; |
| 96 | unsigned int count; |
| 97 | t_packet * rpacket; |
| 98 | std::time_t start; |
| 99 | |
| 100 | if (!progname) |
| 101 | { |
| 102 | std::fprintf(stderr, "%s: got NULL progname\n", progname); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | if (!(rpacket = packet_create(packet_class_bnet))) |
| 107 | { |
| 108 | std::fprintf(stderr, "%s: could not create packet\n", progname); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | start = std::time(NULL); |
| 113 | count = 0; |
| 114 | while (start + (std::time_t)timeout >= std::time(NULL)) /* timeout after a few seconds from last packet */ |
| 115 | { |
| 116 | if ((len = psock_recv(lsock, packet_get_raw_data_build(rpacket, 0), MAX_PACKET_SIZE, 0)) < 0) |
| 117 | { |
| 118 | if (psock_errno() != PSOCK_EAGAIN && psock_errno() != PSOCK_EWOULDBLOCK) |
| 119 | std::fprintf(stderr, "%s: failed to receive UDPTEST on port %hu (psock_recv: %s)\n", progname, lsock_port, std::strerror(psock_errno())); |
| 120 | continue; |
| 121 | } |
| 122 | packet_set_size(rpacket, len); |
| 123 | |
| 124 | if (packet_get_type(rpacket) != SERVER_UDPTEST) |
| 125 | { |
| 126 | dprintf("Got unexpected UDP packet type %u on port %hu\n", packet_get_type(rpacket), lsock_port); |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | if (bn_int_tag_eq(rpacket->u.server_udptest.bnettag, BNETTAG) < 0) |
| 131 | { |
| 132 | std::fprintf(stderr, "%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 = std::time(NULL); |
| 141 | } |
| 142 | |
| 143 | packet_destroy(rpacket); |
| 144 | |
| 145 | if (count < 2) |
| 146 | { |
| 147 | std::printf("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 | } |