Prefer fixed local Port for Discovery

This commit is contained in:
Florian Märkl 2021-01-10 16:01:37 +01:00
commit 9ab84e6054
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 39 additions and 16 deletions

View file

@ -25,6 +25,8 @@ extern "C" {
#define CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS4 "00020020" #define CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS4 "00020020"
#define CHIAKI_DISCOVERY_PORT_PS5 9302 #define CHIAKI_DISCOVERY_PORT_PS5 9302
#define CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS5 "00030010" #define CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS5 "00030010"
#define CHIAKI_DISCOVERY_PORT_LOCAL_MIN 9303
#define CHIAKI_DISCOVERY_PORT_LOCAL_MAX 9319
typedef enum chiaki_discovery_cmd_t typedef enum chiaki_discovery_cmd_t
{ {

View file

@ -152,6 +152,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
return CHIAKI_ERR_NETWORK; return CHIAKI_ERR_NETWORK;
} }
// First try CHIAKI_DISCOVERY_PORT_LOCAL_MIN..<MAX, then 0 (random)
uint16_t port = CHIAKI_DISCOVERY_PORT_LOCAL_MIN;
int r;
while(true)
{
memset(&discovery->local_addr, 0, sizeof(discovery->local_addr)); memset(&discovery->local_addr, 0, sizeof(discovery->local_addr));
discovery->local_addr.sa_family = family; discovery->local_addr.sa_family = family;
if(family == AF_INET6) if(family == AF_INET6)
@ -163,16 +168,32 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
#ifndef __SWITCH__ #ifndef __SWITCH__
addr->sin6_addr = anyaddr; addr->sin6_addr = anyaddr;
#endif #endif
addr->sin6_port = htons(0); addr->sin6_port = htons(port);
} }
else // AF_INET else // AF_INET
{ {
struct sockaddr_in *addr = (struct sockaddr_in *)&discovery->local_addr; struct sockaddr_in *addr = (struct sockaddr_in *)&discovery->local_addr;
addr->sin_addr.s_addr = htonl(INADDR_ANY); addr->sin_addr.s_addr = htonl(INADDR_ANY);
addr->sin_port = htons(0); addr->sin_port = htons(port);
}
r = bind(discovery->socket, &discovery->local_addr, sizeof(discovery->local_addr));
if(r >= 0 || !port)
break;
if(port == CHIAKI_DISCOVERY_PORT_LOCAL_MAX)
{
port = 0;
CHIAKI_LOGI(discovery->log, "Discovery failed to bind port %u, trying random",
(unsigned int)port);
}
else
{
port++;
CHIAKI_LOGI(discovery->log, "Discovery failed to bind port %u, trying one higher",
(unsigned int)port);
}
} }
int r = bind(discovery->socket, &discovery->local_addr, sizeof(discovery->local_addr));
if(r < 0) if(r < 0)
{ {
CHIAKI_LOGE(discovery->log, "Discovery failed to bind"); CHIAKI_LOGE(discovery->log, "Discovery failed to bind");

View file

@ -238,7 +238,7 @@ static void discovery_service_host_received(ChiakiDiscoveryHost *host, void *use
if(service->hosts_count == service->options.hosts_max) if(service->hosts_count == service->options.hosts_max)
{ {
CHIAKI_LOGE(service->log, "Discovery Service received new host, but no space available"); CHIAKI_LOGE(service->log, "Discovery Service received new host, but no space available");
goto r2con; goto rzcon;
} }
CHIAKI_LOGI(service->log, "Discovery Service detected new host with id %s", host->host_id); CHIAKI_LOGI(service->log, "Discovery Service detected new host with id %s", host->host_id);
@ -279,7 +279,7 @@ static void discovery_service_host_received(ChiakiDiscoveryHost *host, void *use
if(change) if(change)
discovery_service_report_state(service); discovery_service_report_state(service);
r2con: rzcon:
chiaki_mutex_unlock(&service->state_mutex); chiaki_mutex_unlock(&service->state_mutex);
} }