diff --git a/dll/network.cpp b/dll/network.cpp index e7c5b2d..d9d94d8 100644 --- a/dll/network.cpp +++ b/dll/network.cpp @@ -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 *custom_broadcasts) +static bool send_broadcasts(sock_t sock, uint16 port, char *data, unsigned long length, std::vector *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 Networking::resolve_ip(std::string dns) +std::set Networking::resolve_ip(std::string dns) { run_at_startup(); - std::set ips; + std::set 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 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 *custom_broadcasts, bool disable_sockets) +Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set *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::setbegin(), 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(); diff --git a/dll/network.h b/dll/network.h index 5bb3e62..4231055 100644 --- a/dll/network.h +++ b/dll/network.h @@ -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 ids; uint32 appid; std::chrono::high_resolution_clock::time_point last_broadcast; - std::vector custom_broadcasts; + std::vector custom_broadcasts; std::vector 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 resolve_ip(std::string dns); - Networking(CSteamID id, uint32 appid, uint16 port, std::set *custom_broadcasts, bool disable_sockets); + static std::set resolve_ip(std::string dns); + Networking(CSteamID id, uint32 appid, uint16 port, std::set *custom_broadcasts, bool disable_sockets); void addListenId(CSteamID id); void setAppID(uint32 appid); void Run(); diff --git a/dll/settings.h b/dll/settings.h index f813cd9..de4b66b 100644 --- a/dll/settings.h +++ b/dll/settings.h @@ -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 custom_broadcasts; + std::set custom_broadcasts; //stats std::map getStats() { return stats; } diff --git a/dll/settings_parser.cpp b/dll/settings_parser.cpp index f369cd6..3168cf5 100644 --- a/dll/settings_parser.cpp +++ b/dll/settings_parser.cpp @@ -28,7 +28,7 @@ static void consume_bom(std::ifstream &input) } } -static void load_custom_broadcasts(std::string broadcasts_filepath, std::set &custom_broadcasts) +static void load_custom_broadcasts(std::string broadcasts_filepath, std::set &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 ips = Networking::resolve_ip(line); + std::set 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 custom_broadcasts; + std::set 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);