| 1 | /* |
| 2 | * Copyright (C) 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_send.h" |
| 20 | |
| 21 | #include <cstring> |
| 22 | #include <cstdio> |
| 23 | |
| 24 | #include "compat/psock.h" |
| 25 | #include "compat/strerror.h" |
| 26 | #include "common/packet.h" |
| 27 | #include "common/eventlog.h" |
| 28 | #include "common/bn_type.h" |
| 29 | #include "common/hexdump.h" |
| 30 | #include "common/addr.h" |
| 31 | #include "common/tag.h" |
| 32 | #include "common/setup_after.h" |
| 33 | |
| 34 | |
| 35 | extern std::FILE * hexstrm; /* from main.c */ |
| 36 | |
| 37 | namespace pvpgn |
| 38 | { |
| 39 | |
| 40 | namespace bnetd |
| 41 | { |
| 42 | |
| 43 | extern int udptest_send(t_connection const * c) |
| 44 | { |
| 45 | t_packet * upacket; |
| 46 | struct sockaddr_in caddr; |
| 47 | unsigned int tries, successes; |
| 48 | |
| 49 | std::memset(&caddr, 0, sizeof(caddr)); |
| 50 | caddr.sin_family = PSOCK_AF_INET; |
| 51 | caddr.sin_port = htons(conn_get_game_port(c)); |
| 52 | caddr.sin_addr.s_addr = htonl(conn_get_game_addr(c)); |
| 53 | |
| 54 | for (tries = successes = 0; successes != 2 && tries < 5; tries++) |
| 55 | { |
| 56 | if (!(upacket = packet_create(packet_class_udp))) |
| 57 | { |
| 58 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not allocate memory for packet", conn_get_socket(c)); |
| 59 | continue; |
| 60 | } |
| 61 | packet_set_size(upacket, sizeof(t_server_udptest)); |
| 62 | packet_set_type(upacket, SERVER_UDPTEST); |
| 63 | bn_int_tag_set(&upacket->u.server_udptest.bnettag, BNETTAG); |
| 64 | |
| 65 | if (hexstrm) |
| 66 | { |
| 67 | std::fprintf(hexstrm, "%d: send class=%s[0x%02x] type=%s[0x%04x] ", |
| 68 | conn_get_game_socket(c), |
| 69 | packet_get_class_str(upacket), (unsigned int)packet_get_class(upacket), |
| 70 | packet_get_type_str(upacket, packet_dir_from_server), packet_get_type(upacket)); |
| 71 | std::fprintf(hexstrm, "from=%s ", |
| 72 | addr_num_to_addr_str(conn_get_game_addr(c), conn_get_game_port(c))); |
| 73 | std::fprintf(hexstrm, "to=%s ", |
| 74 | addr_num_to_addr_str(ntohl(caddr.sin_addr.s_addr), ntohs(caddr.sin_port))); |
| 75 | std::fprintf(hexstrm, "length=%u\n", |
| 76 | packet_get_size(upacket)); |
| 77 | hexdump(hexstrm, packet_get_raw_data(upacket, 0), packet_get_size(upacket)); |
| 78 | } |
| 79 | |
| 80 | if (psock_sendto(conn_get_game_socket(c), |
| 81 | packet_get_raw_data_const(upacket, 0), packet_get_size(upacket), |
| 82 | 0, (struct sockaddr *)&caddr, (psock_t_socklen)sizeof(caddr)) != (int)packet_get_size(upacket)) |
| 83 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] failed to send UDPTEST to {} (attempt {}) (psock_sendto: {})", conn_get_socket(c), addr_num_to_addr_str(ntohl(caddr.sin_addr.s_addr), conn_get_game_port(c)), tries + 1, pstrerror(psock_errno())); |
| 84 | else |
| 85 | successes++; |
| 86 | |
| 87 | packet_del_ref(upacket); |
| 88 | } |
| 89 | |
| 90 | if (successes != 2) |
| 91 | return -1; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | } |