From 9bc975eb07b0f080e8e4cc5290731f187f5b2547 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 26 Sep 2024 02:18:52 -0300 Subject: [PATCH] net: allow ipv6 address with brackets (fix #22313) (#22316) --- vlib/net/util.v | 18 ++++++++++++------ vlib/net/utils_test.v | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 vlib/net/utils_test.v diff --git a/vlib/net/util.v b/vlib/net/util.v index 75276a3d4550ed..0f36b2fb51729d 100644 --- a/vlib/net/util.v +++ b/vlib/net/util.v @@ -1,11 +1,12 @@ module net -const socket_max_port = u16(0xFFFF) - -// validate_port checks whether a port is valid -// and returns the port or an error +// validate_port checks whether a port is valid and returns the port or an error. +// The valid ports numbers are between 0 and 0xFFFF. +// For TCP, port number 0 is reserved and cannot be used, while for UDP, the source port +// is optional and a value of zero means no port. +// See also https://en.wikipedia.org/wiki/Port_%28computer_networking%29 . pub fn validate_port(port int) !u16 { - if port <= socket_max_port { + if port >= 0 && port <= 0xFFFF { return u16(port) } else { return err_port_out_of_range @@ -15,11 +16,16 @@ pub fn validate_port(port int) !u16 { // split_address splits an address into its host name and its port pub fn split_address(addr string) !(string, u16) { port := addr.all_after_last(':').int() - address := addr.all_before_last(':') + mut address := addr.all_before_last(':') // TODO(emily): Maybe do some more checking here // to validate ipv6 address sanity? + // RFC4038 - allow [::1]:port + if address.len > 0 && address[0] == `[` && address[address.len - 1] == `]` { + address = address[1..address.len - 1] + } + p := validate_port(port)! return address, p } diff --git a/vlib/net/utils_test.v b/vlib/net/utils_test.v new file mode 100644 index 00000000000000..e80ebd6e35aee4 --- /dev/null +++ b/vlib/net/utils_test.v @@ -0,0 +1,25 @@ +import net + +fn test_validate() { + assert net.validate_port(0)! == 0 + assert net.validate_port(1)! == 1 + assert net.validate_port(0xFFFF)! == 0xFFFF + if _ := net.validate_port(0xFFFF + 1) { + assert false + } else { + assert true + } + if x := net.validate_port(-2) { + dump(x) + assert false + } else { + assert true + } +} + +fn test_resolve() { + x := net.resolve_addrs_fuzzy('[::1]:10093', .udp)! + assert x.len > 0 + assert x[0].str() == '[::1]:10093' + assert x[0].port()! == 10093 +}