diff --git a/include/BetterSpades/network.h b/include/BetterSpades/network.h index d651112..7e944c6 100644 --- a/include/BetterSpades/network.h +++ b/include/BetterSpades/network.h @@ -27,14 +27,17 @@ #define SETBIT(dest, bit, value) { dest &= MASKOFF(bit); dest |= (value << bit); } +enum Version { VER075, VER076, UNKNOWN }; +typedef struct { char ip[32]; int port; enum Version version; } Address; + const char * network_reason_disconnect(int code); unsigned int network_ping(void); void network_send(int id, void * data, int len); void network_updateColor(void); void network_disconnect(void); -int network_identifier_split(char * addr, char * ip_out, int * port_out); -int network_connect(char * ip, int port); +int network_identifier_split(char * addr, Address *); +int network_connect(Address *); int network_connect_string(char * addr); int network_update(void); int network_status(void); diff --git a/src/hud.c b/src/hud.c index 746c0fc..dbe6373 100644 --- a/src/hud.c +++ b/src/hud.c @@ -2407,10 +2407,10 @@ static void hud_serverlist_render(mu_Context * ctx, float scale) { strncpy(serverlist[k].country, json_object_get_string(s, "country"), sizeof(serverlist[k].country) - 1); - int port; - char ip[32]; - if (network_identifier_split(serverlist[k].identifier, ip, &port)) - ping_check(ip, port, serverlist[k].identifier); + Address addr; + + if (network_identifier_split(serverlist[k].identifier, &addr)) + ping_check(addr.ip, addr.port, serverlist[k].identifier); player_count += serverlist[k].current; } diff --git a/src/network.c b/src/network.c index cb89135..7f4d92e 100644 --- a/src/network.c +++ b/src/network.c @@ -1103,46 +1103,64 @@ int network_connect_sub(char * ip, int port, int version) { return 0; } -int network_connect(char * ip, int port) { - log_info("Connecting to %s at port %i", ip, port); +int network_connect(Address * addr) { + log_info("Connecting to %s at port %i", addr->ip, addr->port); if (network_connected) network_disconnect(); - if (network_connect_sub(ip, port, VERSION_075)) return 1; - if (network_connect_sub(ip, port, VERSION_076)) return 1; + switch (addr->version) { + case VER075: { + if (network_connect_sub(addr->ip, addr->port, VERSION_075)) return 1; + network_connected = 0; return 0; + } - network_connected = 0; - return 0; + case VER076: { + if (network_connect_sub(addr->ip, addr->port, VERSION_076)) return 1; + network_connected = 0; return 0; + } + + default: { + if (network_connect_sub(addr->ip, addr->port, VERSION_075)) return 1; + if (network_connect_sub(addr->ip, addr->port, VERSION_076)) return 1; + network_connected = 0; return 0; + } + } } -int network_identifier_split(char * addr, char * ip_out, int * port_out) { - char * ip_start = strstr(addr, "aos://") + 6; - if ((size_t) ip_start <= 6) return 0; +int network_identifier_split(char * str, Address * addr) { + while (*str && isspace(*str)) str++; // skip trailing whitespace - char * port_start = strchr(ip_start, ':'); - *port_out = port_start ? strtoul(port_start + 1, NULL, 10) : 32887; + if (strstr(str, "aos://") != str) return 0; + str += 6; // skip that “aos://” prefix - if (strchr(ip_start, '.')) { - if (port_start) { - strncpy(ip_out, ip_start, port_start - ip_start); - ip_out[port_start - ip_start] = 0; - } else { - strcpy(ip_out, ip_start); - } + char * colon = strchr(str, ':'); + addr->port = colon ? strtoul(colon + 1, NULL, 10) : 32887; + + size_t len = strlen(str), iplen = colon ? colon - str : len; + + if (memchr(str, '.', iplen)) { + strncpy(addr->ip, str, iplen); + addr->ip[iplen] = 0; } else { - unsigned int ip = strtoul(ip_start, NULL, 10); - sprintf(ip_out, "%i.%i.%i.%i", ip & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255); + unsigned int ip = strtoul(str, NULL, 10); + sprintf(addr->ip, "%i.%i.%i.%i", ip & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255); } + if (strcmp(str + len - 5, ":0.75") == 0) + addr->version = VER075; + else if (strcmp(str + len - 5, ":0.76") == 0) + addr->version = VER076; + else addr->version = UNKNOWN; + return 1; } -int network_connect_string(char * addr) { - char ip[32]; int port; +int network_connect_string(char * str) { + Address addr; - if (!network_identifier_split(addr, ip, &port)) + if (!network_identifier_split(str, &addr)) return 0; - return network_connect(ip, port); + return network_connect(&addr); } int network_update() {