DoxigAlpha

parseIpAndPort

Parse an IP address which may include a port. For IPv4, this is just written address:port. For IPv6, RFC 3986 defines this as an "IP literal", and the port is differentiated from the address by surrounding the address part in brackets '[addr]:port'. Even if the port is not given, the brackets are mandatory.

Function parameters

Parameters

#
str:[]const u8

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#
tcpConnectToHost
All memory allocated with `allocator` will be freed before this function returns.
getAddressList
Call `AddressList.deinit` on the result.

Error sets in this namespace

Error Sets

#

= switch (native_os) { .windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false, .wasi => false, else => true, }

Values

#
has_unix_sockets
= switch (native_os) { .windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false, .wasi => false, else => true, }

Source

Implementation

#
pub fn parseIpAndPort(str: []const u8) error{ InvalidAddress, InvalidPort }!Address {
    if (str.len == 0) return error.InvalidAddress;
    if (str[0] == '[') {
        const addr_end = std.mem.indexOfScalar(u8, str, ']') orelse
            return error.InvalidAddress;
        const addr_str = str[1..addr_end];
        const port: u16 = p: {
            if (addr_end == str.len - 1) break :p 0;
            if (str[addr_end + 1] != ':') return error.InvalidAddress;
            break :p parsePort(str[addr_end + 2 ..]) orelse return error.InvalidPort;
        };
        return parseIp6(addr_str, port) catch error.InvalidAddress;
    } else {
        if (std.mem.indexOfScalar(u8, str, ':')) |idx| {
            // hold off on `error.InvalidPort` since `error.InvalidAddress` might make more sense
            const port: ?u16 = parsePort(str[idx + 1 ..]);
            const addr = parseIp4(str[0..idx], port orelse 0) catch return error.InvalidAddress;
            if (port == null) return error.InvalidPort;
            return addr;
        } else {
            return parseIp4(str, 0) catch error.InvalidAddress;
        }
    }
}