Custom Broadcasts improvement to add support for specifying ports

This commit is contained in:
ptremor 2021-05-30 19:18:19 -03:00
parent faf15e5d88
commit 02195f5636
4 changed files with 36 additions and 18 deletions

View file

@ -308,7 +308,7 @@ static int receive_packet(sock_t sock, IP_PORT *ip_port, char *data, unsigned lo
return -1;
}
static bool send_broadcasts(sock_t sock, uint16 port, char *data, unsigned long length, std::vector<uint32_t> *custom_broadcasts)
static bool send_broadcasts(sock_t sock, uint16 port, char *data, unsigned long length, std::vector<IP_PORT> *custom_broadcasts)
{
static std::chrono::high_resolution_clock::time_point last_get_broadcast_info;
if (number_broadcasts < 0 || check_timedout(last_get_broadcast_info, 60.0)) {
@ -338,11 +338,8 @@ static bool send_broadcasts(sock_t sock, uint16 port, char *data, unsigned long
* This is useful in cases of undetected network interfaces
*/
PRINT_DEBUG("start custom broadcasts\n");
IP_PORT custom_targeted_broadcast;
custom_targeted_broadcast.port = port;
for(auto &ip : *custom_broadcasts) {
custom_targeted_broadcast.ip = ip;
send_packet_to(sock, custom_targeted_broadcast, data, length);
for(auto &addr : *custom_broadcasts) {
send_packet_to(sock, addr, data, length);
}
PRINT_DEBUG("end custom broadcasts\n");
@ -487,12 +484,20 @@ static void socket_timeouts(struct TCP_Socket &socket, double extra_time)
}
}
std::set<uint32> Networking::resolve_ip(std::string dns)
std::set<IP_PORT> Networking::resolve_ip(std::string dns)
{
run_at_startup();
std::set<uint32> ips;
std::set<IP_PORT> ips;
struct addrinfo* result = NULL;
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);
}
if (getaddrinfo(dns.c_str(), NULL, NULL, &result) == 0) {
for (struct addrinfo *res = result; res != NULL; res = res->ai_next) {
PRINT_DEBUG("%u %u\n", res->ai_addrlen, res->ai_family);
@ -500,7 +505,10 @@ std::set<uint32> Networking::resolve_ip(std::string dns)
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
uint32 ip;
memcpy(&ip, &ipv4->sin_addr, sizeof(ip));
ips.insert(ntohl(ip));
IP_PORT addr;
addr.ip = ntohl(ip);
addr.port = port;
ips.insert(addr);
}
}
}
@ -733,7 +741,7 @@ bool Networking::handle_low_level_udp(Common_Message *msg, IP_PORT ip_port)
#define NUM_TCP_WAITING 128
Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set<uint32_t> *custom_broadcasts, bool disable_sockets)
Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set<IP_PORT> *custom_broadcasts, bool disable_sockets)
{
tcp_port = udp_port = port;
own_ip = 0x7F000001;
@ -749,7 +757,11 @@ Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set<uint32_t
}
if (custom_broadcasts) {
std::transform(custom_broadcasts->begin(), custom_broadcasts->end(), std::back_inserter(this->custom_broadcasts), [](uint32 ip) {return htonl(ip);});
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);
}
}
run_at_startup();

View file

@ -38,6 +38,10 @@ typedef int sock_t;
struct IP_PORT {
uint32 ip;
uint16 port;
bool operator <(const IP_PORT& other) const
{
return (ip < other.ip) || (ip == other.ip && port < other.port);
}
};
struct Network_Callback {
@ -103,7 +107,7 @@ class Networking {
std::vector<CSteamID> ids;
uint32 appid;
std::chrono::high_resolution_clock::time_point last_broadcast;
std::vector<uint32_t> custom_broadcasts;
std::vector<IP_PORT> custom_broadcasts;
std::vector<struct TCP_Socket> accepted;
std::recursive_mutex mutex;
@ -120,8 +124,8 @@ class Networking {
public:
//NOTE: for all functions ips/ports are passed/returned in host byte order
//ex: 127.0.0.1 should be passed as 0x7F000001
static std::set<uint32> resolve_ip(std::string dns);
Networking(CSteamID id, uint32 appid, uint16 port, std::set<uint32_t> *custom_broadcasts, bool disable_sockets);
static std::set<IP_PORT> resolve_ip(std::string dns);
Networking(CSteamID id, uint32 appid, uint16 port, std::set<IP_PORT> *custom_broadcasts, bool disable_sockets);
void addListenId(CSteamID id);
void setAppID(uint32 appid);
void Run();

View file

@ -20,6 +20,8 @@
#include "base.h"
struct IP_PORT;
struct DLC_entry {
AppId_t appID;
std::string name;
@ -125,7 +127,7 @@ public:
bool createUnknownLeaderboards() { return create_unknown_leaderboards; }
//custom broadcasts
std::set<uint32> custom_broadcasts;
std::set<IP_PORT> custom_broadcasts;
//stats
std::map<std::string, Stat_config> getStats() { return stats; }

View file

@ -28,7 +28,7 @@ static void consume_bom(std::ifstream &input)
}
}
static void load_custom_broadcasts(std::string broadcasts_filepath, std::set<uint32> &custom_broadcasts)
static void load_custom_broadcasts(std::string broadcasts_filepath, std::set<IP_PORT> &custom_broadcasts)
{
PRINT_DEBUG("Broadcasts file path: %s\n", broadcasts_filepath.c_str());
std::ifstream broadcasts_file(utf8_decode(broadcasts_filepath));
@ -36,7 +36,7 @@ static void load_custom_broadcasts(std::string broadcasts_filepath, std::set<uin
if (broadcasts_file.is_open()) {
std::string line;
while (std::getline(broadcasts_file, line)) {
std::set<uint32> ips = Networking::resolve_ip(line);
std::set<IP_PORT> ips = Networking::resolve_ip(line);
custom_broadcasts.insert(ips.begin(), ips.end());
}
}
@ -204,7 +204,7 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
// Custom broadcasts
std::set<uint32> custom_broadcasts;
std::set<IP_PORT> custom_broadcasts;
load_custom_broadcasts(local_storage->get_global_settings_path() + "custom_broadcasts.txt", custom_broadcasts);
load_custom_broadcasts(Local_Storage::get_game_settings_path() + "custom_broadcasts.txt", custom_broadcasts);