From c2d09135f7dcc27a7f0fdba60ba57ddadebe3e50 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 7 Jan 2024 21:17:59 +0100 Subject: [PATCH] port: net: probably fix ipv6 address parsing --- port/src/net/net.c | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/port/src/net/net.c b/port/src/net/net.c index a0349bac3..350aac3c7 100644 --- a/port/src/net/net.c +++ b/port/src/net/net.c @@ -84,27 +84,20 @@ static s32 netParseAddr(ENetAddress *out, const char *str) char *host = tmp; char *port = NULL; - - if (tmp[0] == '[') { - // ipv6 with port: [ADDR]:PORT - host = tmp + 1; // skip [ - port = strrchr(host, ']'); // find ] - if (port) { - if (port[1] != ':' || !isdigit(port[2])) { - return false; - } - *port = '\0'; // terminate ip - port += 2; // skip ]: - } - } else { - // ipv4 or hostname - port = strrchr(host, ':'); - if (port) { - if (!isdigit(port[1])) { - return false; - } - *port = '\0'; // terminate address - ++port; // skip : + char *colon = strrchr(tmp, ':'); + + // if there is a : in the address string, there could be a port value + // otherwise it's an ip or hostname with default port + if (colon > tmp) { + if (tmp[0] == '[' && colon[-1] == ']' && isdigit(colon[1])) { + // ipv6 with port: [ADDR]:PORT + colon[-1] = '\0'; // terminate ip + host = tmp + 1; // skip [ + port = colon + 1; // skip : + } else if (isdigit(colon[1]) && strchr(host, ':') == colon) { + // ipv4 or hostname with port + colon[0] = '\0'; // terminate ip + port = colon + 1; // skip : } }