2019-04-14 00:21:56 +08:00
|
|
|
/* Copyright (C) 2019 Mr Goldberg
|
|
|
|
This file is part of the Goldberg Emulator
|
|
|
|
|
|
|
|
The Goldberg Emulator is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Goldberg Emulator is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the Goldberg Emulator; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
2023-12-27 15:21:59 +08:00
|
|
|
#include "dll/network.h"
|
|
|
|
#include "dll/dll.h"
|
2019-04-14 00:21:56 +08:00
|
|
|
|
|
|
|
#define MAX_BROADCASTS 16
|
|
|
|
static int number_broadcasts = -1;
|
|
|
|
static IP_PORT broadcasts[MAX_BROADCASTS];
|
2020-05-01 01:36:59 +08:00
|
|
|
static uint32_t lower_range_ips[MAX_BROADCASTS];
|
|
|
|
static uint32_t upper_range_ips[MAX_BROADCASTS];
|
2019-04-14 00:21:56 +08:00
|
|
|
|
|
|
|
#define BROADCAST_INTERVAL 5.0
|
|
|
|
#define HEARTBEAT_TIMEOUT 20.0
|
|
|
|
#define USER_TIMEOUT 20.0
|
|
|
|
|
2022-09-03 16:21:08 +08:00
|
|
|
#define MAX_UDP_SIZE 16384
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
|
2020-01-09 07:26:42 +08:00
|
|
|
//windows xp support
|
|
|
|
static int
|
|
|
|
inet_pton4(const char *src, uint32_t *dst)
|
|
|
|
{
|
|
|
|
static const char digits[] = "0123456789";
|
|
|
|
int saw_digit, octets, ch;
|
|
|
|
u_char tmp[sizeof(uint32_t)], *tp;
|
|
|
|
|
|
|
|
saw_digit = 0;
|
|
|
|
octets = 0;
|
|
|
|
*(tp = tmp) = 0;
|
|
|
|
while ((ch = *src++) != '\0') {
|
|
|
|
const char *pch;
|
|
|
|
|
|
|
|
if ((pch = strchr(digits, ch)) != NULL) {
|
|
|
|
size_t nx = *tp * 10 + (pch - digits);
|
|
|
|
|
|
|
|
if (nx > 255)
|
|
|
|
return (0);
|
|
|
|
*tp = (u_char) nx;
|
|
|
|
if (! saw_digit) {
|
|
|
|
if (++octets > 4)
|
|
|
|
return (0);
|
|
|
|
saw_digit = 1;
|
|
|
|
}
|
|
|
|
} else if (ch == '.' && saw_digit) {
|
|
|
|
if (octets == 4)
|
|
|
|
return (0);
|
|
|
|
*++tp = 0;
|
|
|
|
saw_digit = 0;
|
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (octets < 4)
|
|
|
|
return (0);
|
|
|
|
memcpy(dst, tmp, sizeof(uint32_t));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
static void get_broadcast_info(uint16 port)
|
|
|
|
{
|
|
|
|
number_broadcasts = 0;
|
|
|
|
|
|
|
|
IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
|
|
|
|
unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
|
|
|
|
|
|
|
if (pAdapterInfo == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
|
|
|
|
|
|
|
|
if (pAdapterInfo == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
|
|
|
IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
|
|
|
|
|
|
|
|
while (pAdapter) {
|
2020-01-09 07:26:42 +08:00
|
|
|
uint32_t iface_ip = 0, subnet_mask = 0;
|
2019-04-14 00:21:56 +08:00
|
|
|
|
2020-01-09 07:26:42 +08:00
|
|
|
if (inet_pton4(pAdapter->IpAddressList.IpMask.String, &subnet_mask) == 1
|
|
|
|
&& inet_pton4(pAdapter->IpAddressList.IpAddress.String, &iface_ip) == 1) {
|
2019-04-14 00:21:56 +08:00
|
|
|
IP_PORT *ip_port = &broadcasts[number_broadcasts];
|
2019-09-20 18:52:56 +08:00
|
|
|
uint32 broadcast_ip = iface_ip | ~subnet_mask;
|
|
|
|
ip_port->ip = broadcast_ip;
|
2019-04-14 00:21:56 +08:00
|
|
|
ip_port->port = port;
|
2020-05-01 01:36:59 +08:00
|
|
|
lower_range_ips[number_broadcasts] = iface_ip & subnet_mask;
|
|
|
|
upper_range_ips[number_broadcasts] = broadcast_ip;
|
2019-04-14 00:21:56 +08:00
|
|
|
number_broadcasts++;
|
|
|
|
|
|
|
|
if (number_broadcasts >= MAX_BROADCASTS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pAdapter = pAdapter->Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pAdapterInfo) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
|
|
|
static void get_broadcast_info(uint16 port)
|
|
|
|
{
|
|
|
|
/* Not sure how many platforms this will run on,
|
|
|
|
* so it's wrapped in __linux for now.
|
|
|
|
* Definitely won't work like this on Windows...
|
|
|
|
*/
|
|
|
|
number_broadcasts = 0;
|
|
|
|
sock_t sock = 0;
|
|
|
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Configure ifconf for the ioctl call. */
|
|
|
|
struct ifreq i_faces[MAX_BROADCASTS];
|
|
|
|
memset(i_faces, 0, sizeof(struct ifreq) * MAX_BROADCASTS);
|
|
|
|
|
|
|
|
struct ifconf ifconf;
|
|
|
|
ifconf.ifc_buf = (char *)i_faces;
|
|
|
|
ifconf.ifc_len = sizeof(i_faces);
|
|
|
|
|
|
|
|
if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
|
|
|
|
close(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ifconf.ifc_len is set by the ioctl() to the actual length used;
|
|
|
|
* on usage of the complete array the call should be repeated with
|
|
|
|
* a larger array, not done (640kB and 16 interfaces shall be
|
|
|
|
* enough, for everybody!)
|
|
|
|
*/
|
|
|
|
int i, count = ifconf.ifc_len / sizeof(struct ifreq);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
/* there are interfaces with are incapable of broadcast */
|
|
|
|
if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* moot check: only AF_INET returned (backwards compat.) */
|
|
|
|
if (i_faces[i].ifr_broadaddr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct sockaddr_in *sock4 = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
|
|
|
|
|
|
|
|
if (number_broadcasts >= MAX_BROADCASTS) {
|
|
|
|
close(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IP_PORT *ip_port = &broadcasts[number_broadcasts];
|
|
|
|
ip_port->ip = sock4->sin_addr.s_addr;
|
|
|
|
|
|
|
|
if (ip_port->ip == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_port->port = port;
|
|
|
|
number_broadcasts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool is_socket_valid(sock_t sock)
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
|
|
|
|
if (sock == (sock_t)INVALID_SOCKET || sock == (sock_t)~0) {
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (sock < 0) {
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_tcp_socket_valid(struct TCP_Socket &socket)
|
|
|
|
{
|
|
|
|
return is_socket_valid(socket.sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_socket_nonblocking(sock_t sock)
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
u_long mode = 1;
|
|
|
|
return (ioctlsocket(sock, FIONBIO, &mode) == 0);
|
|
|
|
#else
|
|
|
|
return (fcntl(sock, F_SETFL, O_NONBLOCK, 1) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-03 16:20:03 +08:00
|
|
|
static bool disable_nagle(sock_t sock)
|
|
|
|
{
|
|
|
|
int set = 1;
|
|
|
|
return (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&set, sizeof(set)) == 0);
|
|
|
|
}
|
2019-04-14 00:21:56 +08:00
|
|
|
|
|
|
|
static void kill_socket(sock_t sock)
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
|
|
|
close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kill_tcp_socket(struct TCP_Socket &socket)
|
|
|
|
{
|
|
|
|
if (is_socket_valid(socket.sock)) {
|
|
|
|
kill_socket(socket.sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
socket = TCP_Socket();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool initialed;
|
|
|
|
static void run_at_startup()
|
|
|
|
{
|
|
|
|
if (initialed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
WSADATA wsaData;
|
2024-02-18 04:22:43 +08:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Networking WSAStartup error");
|
2019-04-14 00:21:56 +08:00
|
|
|
return;
|
2024-02-18 04:22:43 +08:00
|
|
|
}
|
2020-01-03 03:10:01 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
//hack: the game Full Mojo Rampage calls WSACleanup on startup so we call WSAStartup a few times so it doesn't get deallocated.
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
|
|
}
|
2019-04-14 00:21:56 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#endif
|
|
|
|
initialed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_last_error()
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
return WSAGetLastError();
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reset the wsa error code so that games don't get confused.
|
|
|
|
static void reset_last_error()
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
WSASetLastError(0);
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_packet_to(sock_t sock, IP_PORT ip_port, char *data, unsigned long length)
|
|
|
|
{
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("send: %lu %hhu.%hhu.%hhu.%hhu:%hu", length, ((unsigned char *)&ip_port.ip)[0], ((unsigned char *)&ip_port.ip)[1], ((unsigned char *)&ip_port.ip)[2], ((unsigned char *)&ip_port.ip)[3], htons(ip_port.port));
|
2019-04-14 00:21:56 +08:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
|
|
|
|
|
2024-05-27 20:33:45 +08:00
|
|
|
#if defined(STEAM_WIN32)
|
2024-05-27 20:41:03 +08:00
|
|
|
size_t addrsize = sizeof(struct sockaddr_in);
|
2024-05-27 20:33:45 +08:00
|
|
|
#else
|
|
|
|
socklen_t addrsize = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2019-04-14 00:21:56 +08:00
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_addr.s_addr = ip_port.ip;
|
|
|
|
addr4->sin_port = ip_port.port;
|
2024-05-27 20:33:45 +08:00
|
|
|
|
|
|
|
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, addrsize);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int receive_packet(sock_t sock, IP_PORT *ip_port, char *data, unsigned long max_length)
|
|
|
|
{
|
2024-02-29 04:41:04 +08:00
|
|
|
struct sockaddr_storage addr{};
|
2019-04-14 00:21:56 +08:00
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
int addrlen = sizeof(addr);
|
|
|
|
#else
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int ret = recvfrom(sock, (char *) data, max_length, 0, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
if (ret >= 0) {
|
|
|
|
struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
|
|
|
|
ip_port->ip = addr_in->sin_addr.s_addr;
|
|
|
|
ip_port->port = addr_in->sin_port;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 06:18:19 +08:00
|
|
|
static bool send_broadcasts(sock_t sock, uint16 port, char *data, unsigned long length, std::vector<IP_PORT> *custom_broadcasts)
|
2019-04-14 00:21:56 +08:00
|
|
|
{
|
|
|
|
static std::chrono::high_resolution_clock::time_point last_get_broadcast_info;
|
|
|
|
if (number_broadcasts < 0 || check_timedout(last_get_broadcast_info, 60.0)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("get_broadcast_info");
|
2019-04-14 00:21:56 +08:00
|
|
|
get_broadcast_info(port);
|
2022-05-01 10:23:42 +08:00
|
|
|
std::vector<uint32_t> lower_range(lower_range_ips, lower_range_ips + number_broadcasts), upper_range(upper_range_ips, upper_range_ips + number_broadcasts);
|
|
|
|
for(auto &addr : *custom_broadcasts) {
|
|
|
|
lower_range.push_back(addr.ip);
|
|
|
|
upper_range.push_back(addr.ip);
|
|
|
|
}
|
|
|
|
|
2024-05-23 23:32:53 +08:00
|
|
|
set_whitelist_ips(lower_range.data(), upper_range.data(), static_cast<unsigned int>(lower_range.size()));
|
2019-04-14 00:21:56 +08:00
|
|
|
last_get_broadcast_info = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
IP_PORT main_broadcast;
|
|
|
|
main_broadcast.ip = INADDR_BROADCAST;
|
|
|
|
main_broadcast.port = port;
|
|
|
|
int ret = send_packet_to(sock, main_broadcast, data, length);
|
|
|
|
|
|
|
|
if (!number_broadcasts)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < number_broadcasts; i++) {
|
|
|
|
ret = send_packet_to(sock, broadcasts[i], data, length);
|
|
|
|
IP_PORT ip_port = broadcasts[i];
|
|
|
|
}
|
|
|
|
|
2019-04-22 05:25:37 +08:00
|
|
|
/**
|
|
|
|
* Custom targeted clients server broadcaster
|
|
|
|
*
|
|
|
|
* Sends to custom IPs the broadcast packet
|
|
|
|
* This is useful in cases of undetected network interfaces
|
|
|
|
*/
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("start custom broadcasts");
|
2021-05-31 06:18:19 +08:00
|
|
|
for(auto &addr : *custom_broadcasts) {
|
|
|
|
send_packet_to(sock, addr, data, length);
|
2019-04-22 05:25:37 +08:00
|
|
|
}
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("end custom broadcasts");
|
2019-04-22 05:25:37 +08:00
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffers_set(sock_t sock)
|
|
|
|
{
|
|
|
|
int n = 1024 * 1024;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&n, sizeof(n));
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&n, sizeof(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bind_socket(sock_t sock, uint16 port)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage addr = {};
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
|
2024-05-27 20:33:45 +08:00
|
|
|
#if defined(STEAM_WIN32)
|
2024-05-27 20:41:03 +08:00
|
|
|
size_t addrsize = sizeof(struct sockaddr_in);
|
2024-05-27 20:33:45 +08:00
|
|
|
#else
|
|
|
|
socklen_t addrsize = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2019-04-14 00:21:56 +08:00
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_port = htons(port);
|
|
|
|
addr4->sin_addr.s_addr = 0;
|
|
|
|
|
2024-05-27 20:33:45 +08:00
|
|
|
return !bind(sock, (struct sockaddr *)&addr, addrsize);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool socket_reuseaddr(sock_t sock)
|
|
|
|
{
|
|
|
|
int set = 1;
|
|
|
|
return (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&set, sizeof(set)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect_socket(sock_t sock, IP_PORT ip_port)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
|
2024-05-27 20:41:03 +08:00
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
size_t addrsize = sizeof(struct sockaddr_in);
|
|
|
|
#else
|
|
|
|
socklen_t addrsize = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2019-04-14 00:21:56 +08:00
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_addr.s_addr = ip_port.ip;
|
|
|
|
addr4->sin_port = ip_port.port;
|
|
|
|
|
2024-05-27 20:41:03 +08:00
|
|
|
connect(sock, (struct sockaddr *)&addr, addrsize);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int receive_buffer_amount(sock_t sock)
|
|
|
|
{
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
unsigned long count = 0;
|
|
|
|
ioctlsocket(sock, FIONREAD, &count);
|
|
|
|
#else
|
|
|
|
int count = 0;
|
|
|
|
ioctl(sock, FIONREAD, &count);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void send_tcp_pending(struct TCP_Socket &socket)
|
|
|
|
{
|
2019-06-18 05:08:23 +08:00
|
|
|
size_t buf_size = socket.send_buffer.size();
|
|
|
|
if (buf_size == 0) return;
|
|
|
|
|
2024-05-23 23:32:53 +08:00
|
|
|
int len = send(socket.sock, &(socket.send_buffer[0]), static_cast<int>(buf_size), MSG_NOSIGNAL);
|
2019-04-14 00:21:56 +08:00
|
|
|
if (len <= 0) return;
|
|
|
|
|
|
|
|
socket.send_buffer.erase(socket.send_buffer.begin(), socket.send_buffer.begin() + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_buffer_tcp(struct TCP_Socket &socket, Common_Message *msg)
|
|
|
|
{
|
2024-05-23 23:32:53 +08:00
|
|
|
uint32 size = static_cast<uint32>(msg->ByteSizeLong()), old_size = static_cast<uint32>(socket.send_buffer.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
socket.send_buffer.resize(old_size + sizeof(uint32) + size);
|
|
|
|
memcpy(&(socket.send_buffer[old_size]), &size, sizeof(size));
|
|
|
|
msg->SerializeToArray(&(socket.send_buffer[old_size + sizeof(uint32)]), size);
|
|
|
|
|
|
|
|
send_tcp_pending(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long peek_buffer_tcp(struct TCP_Socket &socket)
|
|
|
|
{
|
|
|
|
uint32 length;
|
|
|
|
if (socket.recv_buffer.size() < sizeof(length)) return 0;
|
|
|
|
|
|
|
|
memcpy(&length, &(socket.recv_buffer[0]), sizeof(length));
|
|
|
|
if (sizeof(length) + length > socket.recv_buffer.size()) return 0;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool unbuffer_tcp(struct TCP_Socket &socket, Common_Message *msg)
|
|
|
|
{
|
|
|
|
uint32 l = peek_buffer_tcp(socket);
|
|
|
|
if (!l) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->ParseFromArray(&(socket.recv_buffer[sizeof(uint32)]), l)) {
|
|
|
|
socket.recv_buffer.erase(socket.recv_buffer.begin(), socket.recv_buffer.begin() + sizeof(l) + l);
|
|
|
|
return true;
|
|
|
|
} else {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("BAD TCP DATA %u %zu %zu %hhu", l, socket.recv_buffer.size(), sizeof(uint32), *((char *)&(socket.recv_buffer[sizeof(uint32)])));
|
2019-04-14 00:21:56 +08:00
|
|
|
kill_tcp_socket(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool recv_tcp(struct TCP_Socket &socket)
|
|
|
|
{
|
|
|
|
if (is_socket_valid(socket.sock)) {
|
2024-05-27 20:33:45 +08:00
|
|
|
unsigned int size = receive_buffer_amount(socket.sock), old_size = static_cast<uint32>(socket.recv_buffer.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
int len;
|
|
|
|
socket.recv_buffer.resize(old_size + size);
|
|
|
|
if (size > 0) {
|
|
|
|
len = recv(socket.sock, &(socket.recv_buffer[old_size]), size, MSG_NOSIGNAL);
|
|
|
|
socket.received_data = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void socket_timeouts(struct TCP_Socket &socket, double extra_time)
|
|
|
|
{
|
|
|
|
if (check_timedout(socket.last_heartbeat_sent, HEARTBEAT_TIMEOUT / 2.0)) {
|
|
|
|
Common_Message msg;
|
|
|
|
msg.set_allocated_low_level(new Low_Level());
|
|
|
|
msg.mutable_low_level()->set_type(Low_Level::HEARTBEAT);
|
|
|
|
send_buffer_tcp(socket, &msg);
|
|
|
|
socket.last_heartbeat_sent = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_timedout(socket.last_heartbeat_received, HEARTBEAT_TIMEOUT + extra_time)) {
|
|
|
|
kill_tcp_socket(socket);
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP SOCKET HEARTBEAT TIMEOUT");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 06:18:19 +08:00
|
|
|
std::set<IP_PORT> Networking::resolve_ip(std::string dns)
|
2019-05-09 03:43:25 +08:00
|
|
|
{
|
2019-05-09 04:31:23 +08:00
|
|
|
run_at_startup();
|
2021-05-31 06:18:19 +08:00
|
|
|
std::set<IP_PORT> ips;
|
2019-05-09 04:31:23 +08:00
|
|
|
struct addrinfo* result = NULL;
|
2019-05-09 03:43:25 +08:00
|
|
|
|
2021-05-31 06:18:19 +08:00
|
|
|
uint16 port = 0;
|
|
|
|
|
|
|
|
auto port_sindex = dns.find(":", 0);
|
|
|
|
if (port_sindex != std::string::npos) {
|
|
|
|
port = (uint16)atoi(dns.substr(port_sindex + 1).c_str());
|
|
|
|
dns = dns.substr(0, port_sindex);
|
|
|
|
}
|
|
|
|
|
2019-05-09 03:43:25 +08:00
|
|
|
if (getaddrinfo(dns.c_str(), NULL, NULL, &result) == 0) {
|
|
|
|
for (struct addrinfo *res = result; res != NULL; res = res->ai_next) {
|
2023-12-21 09:20:32 +08:00
|
|
|
// cast to size_t because on Linux 'ai_addrlen' is defined as unsigned int and clang complains
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("%zu %u", (size_t)res->ai_addrlen, res->ai_family);
|
2019-05-09 03:43:25 +08:00
|
|
|
if (res->ai_family == AF_INET) {
|
|
|
|
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
|
|
|
|
uint32 ip;
|
|
|
|
memcpy(&ip, &ipv4->sin_addr, sizeof(ip));
|
2021-05-31 06:18:19 +08:00
|
|
|
IP_PORT addr;
|
|
|
|
addr.ip = ntohl(ip);
|
|
|
|
addr.port = port;
|
|
|
|
ips.insert(addr);
|
2019-05-09 03:43:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips;
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
void Networking::do_callbacks_message(Common_Message *msg)
|
|
|
|
{
|
|
|
|
if (msg->has_network() || msg->has_network_old()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_network");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_NETWORKING, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_lobby()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_lobby");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_LOBBY, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_lobby_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_lobby_messages");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_LOBBY, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_gameserver()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_gameserver");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_GAMESERVER, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_friend_()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_friend_");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_FRIEND, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_auth_ticket()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_auth_ticket");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_AUTH_TICKET, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_friend_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_friend_messages");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_FRIEND_MESSAGES, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->has_networking_sockets()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_networking_sockets");
|
2019-04-14 00:21:56 +08:00
|
|
|
run_callbacks(CALLBACK_ID_NETWORKING_SOCKETS, msg);
|
|
|
|
}
|
2019-08-03 18:58:48 +08:00
|
|
|
|
|
|
|
if (msg->has_steam_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_steam_messages");
|
2019-08-03 18:58:48 +08:00
|
|
|
run_callbacks(CALLBACK_ID_STEAM_MESSAGES, msg);
|
|
|
|
}
|
2021-02-17 01:32:22 +08:00
|
|
|
|
|
|
|
if (msg->has_networking_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_networking_messages");
|
2021-02-17 01:32:22 +08:00
|
|
|
run_callbacks(CALLBACK_ID_NETWORKING_MESSAGES, msg);
|
|
|
|
}
|
2024-03-23 13:22:47 +08:00
|
|
|
|
|
|
|
if (msg->has_gameserver_stats_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_gameserver_stats");
|
2024-03-23 13:22:47 +08:00
|
|
|
run_callbacks(CALLBACK_ID_GAMESERVER_STATS, msg);
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:00:05 +08:00
|
|
|
if (msg->has_leaderboards_messages()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("has_leaderboards_messages");
|
2024-03-30 14:00:05 +08:00
|
|
|
run_callbacks(CALLBACK_ID_LEADERBOARDS_STATS, msg);
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::handle_tcp(Common_Message *msg, struct TCP_Socket &socket)
|
|
|
|
{
|
|
|
|
socket.last_heartbeat_received = std::chrono::high_resolution_clock::now();
|
|
|
|
if (msg->has_low_level()) {
|
|
|
|
switch (msg->low_level().type()) {
|
|
|
|
case Low_Level::DISCONNECT:
|
|
|
|
|
|
|
|
break;
|
|
|
|
case Low_Level::HEARTBEAT:
|
|
|
|
//socket.last_heartbeat_received = std::chrono::high_resolution_clock::now();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_callbacks_message(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid)
|
|
|
|
{
|
2024-03-30 13:55:56 +08:00
|
|
|
auto conn = std::find_if(connections.begin(), connections.end(), [&search_id, appid](struct Connection const& conn) {
|
|
|
|
if (appid && (conn.appid != appid)) return false;
|
2019-04-14 00:21:56 +08:00
|
|
|
|
2024-03-30 13:55:56 +08:00
|
|
|
for (const auto &id: conn.ids) {
|
|
|
|
if (search_id == id) return true;
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (connections.end() != conn)
|
|
|
|
return &(*conn);
|
|
|
|
|
2024-03-30 13:55:56 +08:00
|
|
|
return nullptr;
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::add_id_connection(struct Connection *connection, CSteamID steam_id)
|
|
|
|
{
|
|
|
|
if (!connection) return false;
|
|
|
|
|
|
|
|
auto id = std::find(connection->ids.begin(), connection->ids.end(), steam_id);
|
|
|
|
if (id != connection->ids.end())
|
|
|
|
return false;
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ADDED ID %llu", (uint64)steam_id.ConvertToUint64());
|
2019-04-14 00:21:56 +08:00
|
|
|
connection->ids.push_back(steam_id);
|
|
|
|
if (connection->connected) {
|
|
|
|
run_callback_user(steam_id, true, connection->appid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Connection *Networking::new_connection(CSteamID search_id, uint32 appid)
|
|
|
|
{
|
|
|
|
Connection *conn = find_connection(search_id, appid);
|
|
|
|
if (conn && conn->appid == appid) return NULL;
|
|
|
|
|
|
|
|
struct Connection connection;
|
|
|
|
connection.ids.push_back(search_id);
|
|
|
|
connection.appid = appid;
|
|
|
|
connection.last_received = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ADDED ID %llu", (uint64)search_id.ConvertToUint64());
|
2019-04-14 00:21:56 +08:00
|
|
|
connections.push_back(connection);
|
|
|
|
return &(connections[connections.size() - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::handle_announce(Common_Message *msg, IP_PORT ip_port)
|
|
|
|
{
|
|
|
|
Connection *conn = find_connection((uint64)msg->source_id(), msg->announce().appid());
|
|
|
|
if (!conn || conn->appid != msg->announce().appid()) {
|
|
|
|
conn = new_connection((uint64)msg->source_id(), msg->announce().appid());
|
|
|
|
if (!conn) return false;
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("new connection created: user %llu, appid %u", (uint64)msg->source_id(), msg->announce().appid());
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Handle Announce: %u, " "%" PRIu64 ", %u, %u", conn->appid, msg->source_id(), msg->announce().appid(), msg->announce().type());
|
2019-04-14 00:21:56 +08:00
|
|
|
conn->tcp_ip_port = ip_port;
|
|
|
|
conn->tcp_ip_port.port = htons(msg->announce().tcp_port());
|
|
|
|
conn->appid = msg->announce().appid();
|
|
|
|
|
|
|
|
for (int i = 0; i < msg->announce().ids_size(); ++i) {
|
|
|
|
add_id_connection(conn, (uint64) msg->announce().ids(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < msg->announce().peers_size(); ++i) {
|
|
|
|
CSteamID search_id((uint64)msg->announce().peers(i).id());
|
|
|
|
auto id_temp = std::find(ids.begin(), ids.end(), search_id);
|
|
|
|
if (id_temp != ids.end()) {
|
|
|
|
own_ip = ntohl(msg->announce().peers(i).ip());
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection *conn = find_connection((uint64)msg->announce().peers(i).id(), msg->announce().peers(i).appid());
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("%p %u %u " "%" PRIu64 "", conn, conn ? conn->appid : (uint32)0, msg->announce().peers(i).appid(), msg->announce().peers(i).id());
|
2019-04-14 00:21:56 +08:00
|
|
|
if (!conn || conn->appid != msg->announce().peers(i).appid()) {
|
|
|
|
Common_Message msg_ = create_announce(true);
|
|
|
|
|
|
|
|
size_t size = msg_.ByteSizeLong();
|
|
|
|
char *buffer = new char[size];
|
2024-05-23 23:32:53 +08:00
|
|
|
msg_.SerializeToArray(buffer, static_cast<int>(size));
|
2019-04-14 00:21:56 +08:00
|
|
|
IP_PORT ipp;
|
|
|
|
ipp.ip = msg->announce().peers(i).ip();
|
|
|
|
ipp.port = htons(msg->announce().peers(i).udp_port());
|
2024-05-23 23:32:53 +08:00
|
|
|
send_packet_to(udp_socket, ipp, buffer, static_cast<unsigned long>(size));
|
2019-04-14 00:21:56 +08:00
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->last_received = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2024-05-27 20:33:45 +08:00
|
|
|
if (msg->announce().type() == Announce::PING) {
|
2019-04-14 00:21:56 +08:00
|
|
|
Common_Message msg = create_announce(false);
|
|
|
|
size_t size = msg.ByteSizeLong();
|
|
|
|
char *buffer = new char[size];
|
2024-05-23 23:32:53 +08:00
|
|
|
msg.SerializeToArray(buffer, static_cast<int>(size));
|
|
|
|
send_packet_to(udp_socket, ip_port, buffer, static_cast<unsigned long>(size));
|
2019-04-14 00:21:56 +08:00
|
|
|
delete[] buffer;
|
|
|
|
|
|
|
|
//send ping packet if not pinged
|
|
|
|
if (!conn->udp_pinged) {
|
|
|
|
Common_Message msg = create_announce(true);
|
|
|
|
size_t size = msg.ByteSizeLong();
|
|
|
|
char *buffer = new char[size];
|
2024-05-23 23:32:53 +08:00
|
|
|
msg.SerializeToArray(buffer, static_cast<int>(size));
|
|
|
|
send_packet_to(udp_socket, ip_port, buffer, static_cast<unsigned long>(size));
|
2019-04-14 00:21:56 +08:00
|
|
|
delete[] buffer;
|
|
|
|
}
|
2024-05-27 20:33:45 +08:00
|
|
|
} else if (msg->announce().type() == Announce::PONG) {
|
2019-04-14 00:21:56 +08:00
|
|
|
conn->udp_ip_port = ip_port;
|
|
|
|
conn->udp_pinged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::handle_low_level_udp(Common_Message *msg, IP_PORT ip_port)
|
|
|
|
{
|
|
|
|
//TODO: connection appid
|
|
|
|
struct Connection *connection = find_connection((uint64)msg->source_id());
|
|
|
|
if (!connection)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (msg->low_level().type()) {
|
|
|
|
case Low_Level::DISCONNECT:
|
|
|
|
|
|
|
|
break;
|
|
|
|
case Low_Level::HEARTBEAT:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NUM_TCP_WAITING 128
|
|
|
|
|
2021-05-31 06:18:19 +08:00
|
|
|
Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set<IP_PORT> *custom_broadcasts, bool disable_sockets)
|
2019-04-14 00:21:56 +08:00
|
|
|
{
|
|
|
|
tcp_port = udp_port = port;
|
|
|
|
own_ip = 0x7F000001;
|
|
|
|
last_run = std::chrono::high_resolution_clock::now();
|
|
|
|
this->appid = appid;
|
2019-10-06 03:39:50 +08:00
|
|
|
|
|
|
|
if (disable_sockets) {
|
|
|
|
enabled = false;
|
|
|
|
udp_socket = -1;
|
|
|
|
tcp_socket = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 03:43:25 +08:00
|
|
|
if (custom_broadcasts) {
|
2021-05-31 06:18:19 +08:00
|
|
|
std::transform(custom_broadcasts->begin(), custom_broadcasts->end(), std::back_inserter(this->custom_broadcasts), [](IP_PORT addr) {addr.ip = htonl(addr.ip); addr.port = htons(addr.port); return addr; });
|
|
|
|
for (auto& addr : this->custom_broadcasts) {
|
|
|
|
if (addr.port == htons(0))
|
|
|
|
addr.port = htons(port);
|
|
|
|
}
|
2019-05-09 03:43:25 +08:00
|
|
|
}
|
2019-04-14 00:21:56 +08:00
|
|
|
|
2019-10-06 03:39:50 +08:00
|
|
|
run_at_startup();
|
2024-05-23 23:32:53 +08:00
|
|
|
sock_t sock = static_cast<sock_t>(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UDP socket: %u", sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
if (is_socket_valid(sock) && set_socket_nonblocking(sock)) {
|
|
|
|
int broadcast = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast));
|
|
|
|
//socket_reuseaddr(sock);
|
|
|
|
|
|
|
|
buffers_set(sock);
|
|
|
|
for (unsigned i = 0; i < 1000; ++i) {
|
|
|
|
udp_port = port + i;
|
|
|
|
if (bind_socket(sock, udp_port)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UDP successful");
|
2019-04-14 00:21:56 +08:00
|
|
|
udp_socket = sock;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
//clear the error
|
|
|
|
int error = 0;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_socket_valid(udp_socket)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UDP: could not bind socket");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UDP: could not initialize %i", get_last_error());
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
2024-05-23 23:32:53 +08:00
|
|
|
sock = static_cast<sock_t>(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP socket: %u", sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
if (is_socket_valid(sock) && set_socket_nonblocking(sock)) {
|
|
|
|
buffers_set(sock);
|
|
|
|
//socket_reuseaddr(sock);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 1000; ++i) {
|
|
|
|
tcp_port = port + i;
|
|
|
|
if (bind_socket(sock, tcp_port)) {
|
|
|
|
if ((listen(sock, NUM_TCP_WAITING) == 0)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP successful");
|
2019-04-14 00:21:56 +08:00
|
|
|
tcp_socket = sock;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int error = 0;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP listen error %i", error);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int error = 0;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_socket_valid(udp_socket)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP: could not bind or listen");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP: could not initialize %i", get_last_error());
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
2024-02-18 04:22:43 +08:00
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL) == 0) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("CURL successful");
|
2023-08-21 11:58:55 +08:00
|
|
|
} else {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("CURL: could not initialize");
|
2023-08-21 11:58:55 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
if (is_socket_valid(udp_socket) && is_socket_valid(tcp_socket)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Networking initialized successfully on udp: %u tcp: %u", udp_port, tcp_port);
|
2019-04-14 00:21:56 +08:00
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ADDED ID %llu", (uint64)id.ConvertToUint64());
|
2019-04-14 00:21:56 +08:00
|
|
|
ids.push_back(id);
|
|
|
|
|
|
|
|
reset_last_error();
|
|
|
|
}
|
|
|
|
|
2022-08-10 15:24:29 +08:00
|
|
|
Networking::~Networking()
|
|
|
|
{
|
|
|
|
for (auto &c : connections) {
|
|
|
|
kill_tcp_socket(c.tcp_socket_incoming);
|
|
|
|
kill_tcp_socket(c.tcp_socket_outgoing);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &c : accepted) {
|
|
|
|
kill_tcp_socket(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
kill_socket(udp_socket);
|
|
|
|
kill_socket(tcp_socket);
|
2023-08-21 11:58:55 +08:00
|
|
|
|
|
|
|
curl_global_cleanup();
|
2022-08-10 15:24:29 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
Common_Message Networking::create_announce(bool request)
|
|
|
|
{
|
|
|
|
Announce *announce = new Announce();
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ids length %zu", ids.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
if (request) {
|
|
|
|
announce->set_type(Announce::PING);
|
|
|
|
} else {
|
|
|
|
announce->set_type(Announce::PONG);
|
|
|
|
for (auto &conn: connections) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Connection %u %llu %u", conn.udp_pinged, conn.ids[0].ConvertToUint64(), conn.appid);
|
2019-04-14 00:21:56 +08:00
|
|
|
if (conn.udp_pinged) {
|
|
|
|
Announce_Other_Peers *peer = announce->add_peers();
|
|
|
|
peer->set_id(conn.ids[0].ConvertToUint64());
|
|
|
|
peer->set_ip(conn.udp_ip_port.ip);
|
|
|
|
peer->set_udp_port(ntohs(conn.udp_ip_port.port));
|
|
|
|
peer->set_appid(conn.appid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
announce->set_tcp_port(tcp_port);
|
|
|
|
announce->set_appid(this->appid);
|
|
|
|
for (auto &id : ids) announce->add_ids(id.ConvertToUint64());
|
|
|
|
Common_Message msg;
|
|
|
|
msg.set_allocated_announce(announce);
|
|
|
|
msg.set_source_id(ids[0].ConvertToUint64());
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::send_announce_broadcasts()
|
|
|
|
{
|
|
|
|
Common_Message msg = create_announce(true);
|
|
|
|
|
|
|
|
size_t size = msg.ByteSizeLong();
|
2024-04-06 13:49:07 +08:00
|
|
|
std::vector<char> buffer(size);
|
2024-05-23 23:32:53 +08:00
|
|
|
msg.SerializeToArray(&buffer[0], static_cast<int>(size));
|
|
|
|
send_broadcasts(udp_socket, htons(DEFAULT_PORT), &buffer[0], static_cast<unsigned long>(size), &this->custom_broadcasts);
|
2019-04-14 00:21:56 +08:00
|
|
|
if (udp_port != DEFAULT_PORT) {
|
2024-05-23 23:32:53 +08:00
|
|
|
send_broadcasts(udp_socket, htons(udp_port), &buffer[0], static_cast<unsigned long>(size), &this->custom_broadcasts);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
last_broadcast = std::chrono::high_resolution_clock::now();
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("sent broadcasts");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::Run()
|
|
|
|
{
|
|
|
|
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
|
|
|
|
double time_extra = std::chrono::duration_cast<std::chrono::duration<double>>(now - last_run).count();
|
|
|
|
last_run = now;
|
|
|
|
|
|
|
|
if (!enabled || ids.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
//PRINT_DEBUG("%lf", time_extra);
|
|
|
|
// PRINT_DEBUG_ENTRY();
|
2019-04-14 00:21:56 +08:00
|
|
|
if (check_timedout(last_broadcast, BROADCAST_INTERVAL)) {
|
|
|
|
send_announce_broadcasts();
|
|
|
|
}
|
|
|
|
|
|
|
|
IP_PORT ip_port;
|
2022-09-03 16:21:08 +08:00
|
|
|
char data[MAX_UDP_SIZE];
|
2019-04-14 00:21:56 +08:00
|
|
|
int len;
|
|
|
|
|
2024-02-28 03:17:53 +08:00
|
|
|
if (query_alive && is_socket_valid(query_socket)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RECV Source Query");
|
2024-02-28 03:17:53 +08:00
|
|
|
Steam_Client* client = get_steam_client();
|
|
|
|
sockaddr_in addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
while ((len = receive_packet(query_socket, &ip_port, data, sizeof(data))) >= 0) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("requesting Source Query server info from Steam_GameServer");
|
2024-02-28 03:17:53 +08:00
|
|
|
client->steam_gameserver->HandleIncomingPacket(data, len, htonl(ip_port.ip), htons(ip_port.port));
|
|
|
|
len = client->steam_gameserver->GetNextOutgoingPacket(data, sizeof(data), &ip_port.ip, &ip_port.port);
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("sending Source Query server info");
|
2024-02-28 03:17:53 +08:00
|
|
|
addr.sin_addr.s_addr = htonl(ip_port.ip);
|
|
|
|
addr.sin_port = htons(ip_port.port);
|
|
|
|
sendto(query_socket, data, len, 0, (sockaddr*)&addr, sizeof(addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RECV UDP");
|
2019-04-14 00:21:56 +08:00
|
|
|
while((len = receive_packet(udp_socket, &ip_port, data, sizeof(data))) >= 0) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("recv %i %hhu.%hhu.%hhu.%hhu:%hu", len,
|
|
|
|
((unsigned char *)&ip_port.ip)[0], ((unsigned char *)&ip_port.ip)[1], ((unsigned char *)&ip_port.ip)[2], ((unsigned char *)&ip_port.ip)[3], htons(ip_port.port));
|
2019-04-14 00:21:56 +08:00
|
|
|
Common_Message msg;
|
|
|
|
if (msg.ParseFromArray(data, len)) {
|
|
|
|
if (msg.source_id()) {
|
|
|
|
if (msg.has_announce()) {
|
|
|
|
handle_announce(&msg, ip_port);
|
2024-04-06 13:49:07 +08:00
|
|
|
} else if (msg.has_low_level()) {
|
2019-04-14 00:21:56 +08:00
|
|
|
handle_low_level_udp(&msg, ip_port);
|
2024-04-06 13:49:07 +08:00
|
|
|
} else {
|
2019-04-14 00:21:56 +08:00
|
|
|
msg.set_source_ip(ntohl(ip_port.ip));
|
|
|
|
msg.set_source_port(ntohs(ip_port.port));
|
|
|
|
do_callbacks_message(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RECV LOCAL %zu", local_send.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
std::vector<Common_Message> local_send_copy = local_send;
|
|
|
|
local_send.clear();
|
|
|
|
|
|
|
|
for (auto & m: local_send_copy) {
|
|
|
|
m.set_source_ip(ntohl(own_ip));
|
|
|
|
m.set_source_port(ntohs(udp_port));
|
|
|
|
do_callbacks_message(&m);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
int addrlen = sizeof(addr);
|
|
|
|
#else
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
sock_t sock;
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ACCEPTING");
|
2024-05-23 23:32:53 +08:00
|
|
|
while (is_socket_valid(sock = static_cast<sock_t>(accept(tcp_socket, (struct sockaddr *)&addr, &addrlen)))) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ACCEPT SOCKET %u", sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
int addrlen = sizeof(addr);
|
|
|
|
#else
|
|
|
|
socklen_t addrlen = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
|
|
|
|
ip_port.ip = addr_in->sin_addr.s_addr;
|
|
|
|
ip_port.port = addr_in->sin_port;
|
|
|
|
struct TCP_Socket socket;
|
|
|
|
if (set_socket_nonblocking(sock)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("SET NONBLOCK");
|
2022-09-03 16:20:03 +08:00
|
|
|
disable_nagle(sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
socket.sock = sock;
|
|
|
|
socket.received_data = true;
|
|
|
|
socket.last_heartbeat_received = std::chrono::high_resolution_clock::now();
|
|
|
|
accepted.push_back(socket);
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP ACCEPTED %u", sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ACCEPTED %zu", accepted.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
auto conn = std::begin(accepted);
|
|
|
|
while (conn != std::end(accepted)) {
|
|
|
|
bool deleted = false;
|
|
|
|
recv_tcp(*conn);
|
|
|
|
Common_Message msg;
|
|
|
|
if (unbuffer_tcp(*conn, &msg)) {
|
|
|
|
if (msg.source_id()) {
|
|
|
|
Connection *connection = find_connection((uint64)msg.source_id());
|
|
|
|
if (connection) {
|
|
|
|
kill_tcp_socket(connection->tcp_socket_incoming);
|
|
|
|
connection->tcp_socket_incoming = *conn;
|
|
|
|
conn = accepted.erase(conn);
|
|
|
|
deleted = true;
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP REPLACED");
|
2019-04-14 00:21:56 +08:00
|
|
|
//TODO: add other ids?
|
|
|
|
} else {
|
|
|
|
//Don't allow connection from unknown
|
|
|
|
//Connection *conn = Networking::new_connection(msg.source_id());
|
|
|
|
kill_tcp_socket(*conn);
|
|
|
|
conn = accepted.erase(conn);
|
|
|
|
deleted = true;
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP UNKNOWN");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!deleted && check_timedout(conn->last_heartbeat_received, HEARTBEAT_TIMEOUT + time_extra)) {
|
|
|
|
kill_tcp_socket(*conn);
|
|
|
|
conn = accepted.erase(conn);
|
|
|
|
deleted = true;
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("TCP TIMEOUT");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!deleted){
|
|
|
|
++conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("CONNECTIONS %zu", connections.size());
|
2019-04-14 00:21:56 +08:00
|
|
|
for (auto &conn: connections) {
|
|
|
|
if (!is_tcp_socket_valid(conn.tcp_socket_outgoing)) {
|
2024-05-23 23:32:53 +08:00
|
|
|
sock = static_cast<sock_t>(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
2019-04-14 00:21:56 +08:00
|
|
|
if (is_socket_valid(sock) && set_socket_nonblocking(sock)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("NEW SOCKET %u %u", sock, conn.tcp_socket_outgoing.sock);
|
2022-09-03 16:20:03 +08:00
|
|
|
disable_nagle(sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
connect_socket(sock, conn.tcp_ip_port);
|
|
|
|
conn.tcp_socket_outgoing.sock = sock;
|
|
|
|
conn.tcp_socket_outgoing.last_heartbeat_received = std::chrono::high_resolution_clock::now();
|
|
|
|
Common_Message msg;
|
|
|
|
msg.set_source_id(ids[0].ConvertToUint64());
|
|
|
|
send_buffer_tcp(conn.tcp_socket_outgoing, &msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RUN SOCKET1 %u %u", conn.tcp_socket_outgoing.sock, conn.tcp_socket_incoming.sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
recv_tcp(conn.tcp_socket_outgoing);
|
|
|
|
recv_tcp(conn.tcp_socket_incoming);
|
|
|
|
|
|
|
|
if (conn.tcp_socket_incoming.received_data || conn.tcp_socket_outgoing.received_data) {
|
|
|
|
if (!conn.connected) {
|
|
|
|
//reconnect the connection if it has the right appid
|
|
|
|
if (conn.appid == this->appid || conn.appid == LOBBY_CONNECT_APPID) {
|
|
|
|
for (auto &c: connections) {
|
|
|
|
if (&c == &conn) continue;
|
|
|
|
if (c.appid != this->appid) continue;
|
|
|
|
for (auto &steam_id : conn.ids) {
|
|
|
|
auto i = std::find(c.ids.begin(), c.ids.end(), steam_id);
|
|
|
|
if (i != c.ids.end()) {
|
|
|
|
c.ids.erase(i);
|
|
|
|
run_callback_user(steam_id, false, c.appid);
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("REMOVE OLD CONNECTION ID");
|
2019-04-14 00:21:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &steam_id : conn.ids) run_callback_user(steam_id, true, conn.appid);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.connected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RUN SOCKET2 %u %u", conn.tcp_socket_outgoing.sock, conn.tcp_socket_incoming.sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
send_tcp_pending(conn.tcp_socket_outgoing);
|
|
|
|
send_tcp_pending(conn.tcp_socket_incoming);
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RUN SOCKET3 %u %u", conn.tcp_socket_outgoing.sock, conn.tcp_socket_incoming.sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
Common_Message msg;
|
|
|
|
while (unbuffer_tcp(conn.tcp_socket_outgoing, &msg)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UNBUFFER SOCKET");
|
2019-04-14 00:21:56 +08:00
|
|
|
msg.set_source_ip(ntohl(conn.tcp_ip_port.ip)); //TODO: get from tcp socket
|
|
|
|
handle_tcp(&msg, conn.tcp_socket_outgoing);
|
|
|
|
conn.last_received = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (unbuffer_tcp(conn.tcp_socket_incoming, &msg)) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("UNBUFFER SOCKET");
|
2019-04-14 00:21:56 +08:00
|
|
|
msg.set_source_ip(ntohl(conn.tcp_ip_port.ip)); //TODO: get from tcp socket
|
|
|
|
handle_tcp(&msg, conn.tcp_socket_incoming);
|
|
|
|
conn.last_received = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("RUN SOCKET4 %u %u", conn.tcp_socket_outgoing.sock, conn.tcp_socket_incoming.sock);
|
2019-04-14 00:21:56 +08:00
|
|
|
socket_timeouts(conn.tcp_socket_outgoing, time_extra);
|
|
|
|
socket_timeouts(conn.tcp_socket_incoming, time_extra);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto conn = std::begin(connections);
|
|
|
|
while (conn != std::end(connections)) {
|
|
|
|
if (check_timedout(conn->last_received, USER_TIMEOUT + time_extra)) {
|
|
|
|
if (conn->connected) for (auto &steam_id : conn->ids) run_callback_user(steam_id, false, conn->appid);
|
|
|
|
kill_tcp_socket(conn->tcp_socket_outgoing);
|
|
|
|
kill_tcp_socket(conn->tcp_socket_incoming);
|
|
|
|
conn = connections.erase(conn);
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("USER TIMEOUT");
|
2019-04-14 00:21:56 +08:00
|
|
|
} else {
|
|
|
|
++conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &conn: connections) {
|
|
|
|
if (!(conn.tcp_socket_incoming.received_data || conn.tcp_socket_outgoing.received_data)) {
|
|
|
|
if (conn.connected) for (auto &steam_id : conn.ids) run_callback_user(steam_id, false, conn.appid);
|
|
|
|
conn.connected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reset_last_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::addListenId(CSteamID id)
|
|
|
|
{
|
2019-10-06 03:39:50 +08:00
|
|
|
if (!enabled) return;
|
2019-04-14 00:21:56 +08:00
|
|
|
auto i = std::find(ids.begin(), ids.end(), id);
|
|
|
|
if (i != ids.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("ADDED ID %llu", (uint64)id.ConvertToUint64());
|
2019-04-14 00:21:56 +08:00
|
|
|
ids.push_back(id);
|
|
|
|
send_announce_broadcasts();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::setAppID(uint32 appid)
|
|
|
|
{
|
|
|
|
this->appid = appid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::sendToIPPort(Common_Message *msg, uint32 ip, uint16 port, bool reliable)
|
|
|
|
{
|
2022-05-30 03:46:31 +08:00
|
|
|
bool is_local_ip = ((ip >> 24) == 0x7F);
|
|
|
|
uint32_t local_ip = getIP(ids.front());
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("%X %u %X", ip, is_local_ip, local_ip);
|
2019-04-14 00:21:56 +08:00
|
|
|
//TODO: actually send to ip/port
|
|
|
|
for (auto &conn: connections) {
|
2022-05-30 03:46:31 +08:00
|
|
|
if (ntohl(conn.tcp_ip_port.ip) == ip || (is_local_ip && ntohl(conn.tcp_ip_port.ip) == local_ip)) {
|
2019-04-14 00:21:56 +08:00
|
|
|
for (auto &steam_id : conn.ids) {
|
|
|
|
msg->set_dest_id(steam_id.ConvertToUint64());
|
|
|
|
sendTo(msg, reliable, &conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-03 22:04:04 +08:00
|
|
|
uint32 Networking::getIP(CSteamID id)
|
|
|
|
{
|
|
|
|
Connection *conn = find_connection(id, this->appid);
|
|
|
|
if (conn) {
|
|
|
|
return ntohl(conn->tcp_ip_port.ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
bool Networking::sendTo(Common_Message *msg, bool reliable, Connection *conn)
|
|
|
|
{
|
2019-10-06 03:39:50 +08:00
|
|
|
if (!enabled) return false;
|
|
|
|
|
2022-05-08 04:37:09 +08:00
|
|
|
size_t size = msg->ByteSizeLong();
|
2022-09-03 16:21:08 +08:00
|
|
|
if (size >= MAX_UDP_SIZE) reliable = true; //too big for UDP
|
2022-05-08 04:37:09 +08:00
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
bool ret = false;
|
|
|
|
CSteamID dest_id((uint64)msg->dest_id());
|
|
|
|
if (std::find(ids.begin(), ids.end(), dest_id) != ids.end()) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("sending to self");
|
2019-04-14 00:21:56 +08:00
|
|
|
if (!conn) {
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("local send");
|
2019-04-14 00:21:56 +08:00
|
|
|
local_send.push_back(*msg);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn) {
|
|
|
|
conn = find_connection(dest_id, this->appid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret && conn) {
|
|
|
|
if (reliable || !conn->udp_pinged) {
|
|
|
|
if (conn->tcp_socket_incoming.received_data) {
|
|
|
|
send_buffer_tcp(conn->tcp_socket_incoming, msg);
|
|
|
|
ret = true;
|
|
|
|
} else if (conn->tcp_socket_outgoing.received_data) {
|
|
|
|
send_buffer_tcp(conn->tcp_socket_outgoing, msg);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-30 13:55:56 +08:00
|
|
|
std::vector<char> buffer(size, 0);
|
2024-05-23 23:32:53 +08:00
|
|
|
msg->SerializeToArray(&buffer[0], static_cast<int>(size));
|
|
|
|
send_packet_to(udp_socket, conn->udp_ip_port, &buffer[0], static_cast<unsigned long>(size));
|
2019-04-14 00:21:56 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reset_last_error();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::sendToAllIndividuals(Common_Message *msg, bool reliable)
|
|
|
|
{
|
|
|
|
for (auto &conn: connections) {
|
|
|
|
for (auto &steam_id : conn.ids) {
|
|
|
|
if (steam_id.BIndividualAccount()) {
|
|
|
|
msg->set_dest_id(steam_id.ConvertToUint64());
|
|
|
|
sendTo(msg, reliable, &conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-01 03:17:20 +08:00
|
|
|
bool Networking::sendToAllGameservers(Common_Message *msg, bool reliable)
|
|
|
|
{
|
|
|
|
for (auto &conn: connections) {
|
|
|
|
for (auto &steam_id : conn.ids) {
|
|
|
|
if (steam_id.BGameServerAccount()) {
|
|
|
|
msg->set_dest_id(steam_id.ConvertToUint64());
|
|
|
|
sendTo(msg, reliable, &conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
bool Networking::sendToAll(Common_Message *msg, bool reliable)
|
|
|
|
{
|
|
|
|
for (auto &conn: connections) {
|
|
|
|
for (auto &steam_id : conn.ids) {
|
|
|
|
msg->set_dest_id(steam_id.ConvertToUint64());
|
|
|
|
sendTo(msg, reliable, &conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::run_callbacks(Callback_Ids id, Common_Message *msg)
|
|
|
|
{
|
|
|
|
for (auto &cb : callbacks[id].callbacks) {
|
2024-03-23 13:22:47 +08:00
|
|
|
uint64 callback_allowed_steamid = cb.steam_id.ConvertToUint64();
|
|
|
|
uint64 message_destination_steamid = msg->dest_id();
|
|
|
|
if (callback_allowed_steamid == 0 || // callback wants to receive all messages (callback for broadcast)
|
|
|
|
message_destination_steamid == 0 || // message was broadcasted to all (broadcast message)
|
|
|
|
callback_allowed_steamid == message_destination_steamid) { // callback destination is the same as the message destination
|
2019-04-14 00:21:56 +08:00
|
|
|
cb.message_callback(cb.object, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::run_callback_user(CSteamID steam_id, bool online, uint32 appid)
|
|
|
|
{
|
|
|
|
//only give callbacks for right game accounts
|
|
|
|
if (steam_id.BIndividualAccount() && appid != this->appid && appid != LOBBY_CONNECT_APPID) return;
|
|
|
|
|
2024-03-30 13:55:56 +08:00
|
|
|
Common_Message msg{};
|
2019-04-14 00:21:56 +08:00
|
|
|
msg.set_source_id(steam_id.ConvertToUint64());
|
|
|
|
msg.set_allocated_low_level(new Low_Level());
|
|
|
|
if (online) {
|
|
|
|
msg.mutable_low_level()->set_type(Low_Level::CONNECT);
|
|
|
|
} else {
|
|
|
|
msg.mutable_low_level()->set_type(Low_Level::DISCONNECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_callbacks(CALLBACK_ID_USER_STATUS, &msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::setCallback(Callback_Ids id, CSteamID steam_id, void (*message_callback)(void *object, Common_Message *msg), void *object)
|
|
|
|
{
|
|
|
|
if (id >= CALLBACK_IDS_MAX) return false;
|
|
|
|
|
2024-03-23 13:22:47 +08:00
|
|
|
struct Network_Callback nc{};
|
2019-04-14 00:21:56 +08:00
|
|
|
nc.message_callback = message_callback;
|
|
|
|
nc.object = object;
|
|
|
|
nc.steam_id = steam_id;
|
|
|
|
|
|
|
|
callbacks[id].callbacks.push_back(nc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-23 13:22:47 +08:00
|
|
|
void Networking::rmCallback(Callback_Ids id, CSteamID steam_id, void (*message_callback)(void *object, Common_Message *msg), void *object)
|
|
|
|
{
|
|
|
|
if (id >= CALLBACK_IDS_MAX) return;
|
|
|
|
|
|
|
|
auto &target_cb = callbacks[id].callbacks;
|
|
|
|
auto itrm = std::remove_if(
|
|
|
|
target_cb.begin(),
|
|
|
|
target_cb.end(),
|
|
|
|
[=, &steam_id](const struct Network_Callback &item) {
|
|
|
|
return item.message_callback == message_callback &&
|
|
|
|
item.object == object &&
|
|
|
|
item.steam_id == steam_id;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
target_cb.erase(itrm, target_cb.end());
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:21:56 +08:00
|
|
|
uint32 Networking::getOwnIP()
|
|
|
|
{
|
|
|
|
return own_ip;
|
|
|
|
}
|
2024-02-28 03:17:53 +08:00
|
|
|
|
|
|
|
void Networking::startQuery(IP_PORT ip_port)
|
|
|
|
{
|
|
|
|
if (ip_port.port <= 1024)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!query_alive)
|
|
|
|
{
|
|
|
|
if (ip_port.port == MASTERSERVERUPDATERPORT_USEGAMESOCKETSHARE)
|
|
|
|
{
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Source Query in Shared Mode");
|
2024-02-28 03:17:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int retry = 0;
|
|
|
|
constexpr auto max_retry = 10;
|
|
|
|
|
|
|
|
while (retry++ < max_retry)
|
|
|
|
{
|
2024-05-23 23:32:53 +08:00
|
|
|
query_socket = static_cast<sock_t>(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
|
2024-02-28 03:17:53 +08:00
|
|
|
if (is_socket_valid(query_socket))
|
|
|
|
break;
|
|
|
|
if (retry > max_retry)
|
|
|
|
{
|
|
|
|
reset_last_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retry = 0;
|
|
|
|
|
2024-02-29 04:41:04 +08:00
|
|
|
sockaddr_in addr{};
|
2024-02-28 03:17:53 +08:00
|
|
|
addr.sin_addr.s_addr = htonl(ip_port.ip);
|
|
|
|
addr.sin_port = htons(ip_port.port);
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
while (retry++ < max_retry)
|
|
|
|
{
|
|
|
|
int res = bind(query_socket, (sockaddr*)&addr, sizeof(sockaddr_in));
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
set_socket_nonblocking(query_socket);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retry >= max_retry)
|
|
|
|
{
|
|
|
|
kill_socket(query_socket);
|
|
|
|
query_socket = -1;
|
|
|
|
reset_last_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 04:41:04 +08:00
|
|
|
char str_ip[16]{};
|
2024-02-28 03:17:53 +08:00
|
|
|
inet_ntop(AF_INET, &(addr.sin_addr), str_ip, 16);
|
|
|
|
|
2024-04-06 13:49:07 +08:00
|
|
|
PRINT_DEBUG("Started query server on %s:%d", str_ip, htons(addr.sin_port));
|
2024-02-28 03:17:53 +08:00
|
|
|
}
|
|
|
|
query_alive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::shutDownQuery()
|
|
|
|
{
|
|
|
|
query_alive = false;
|
|
|
|
kill_socket(query_socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Networking::isQueryAlive()
|
|
|
|
{
|
|
|
|
return query_alive;
|
|
|
|
}
|