Skip to content

feat(socket): Addrinfo + Socket.getaddrinfo + sockaddr pack/unpack - #199

Merged
tannevaled merged 1 commit into
mainfrom
feat/socket-addrinfo
Jul 6, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/socket-addrinfo

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Extends rbgo's socket transport (require "socket") with the name-resolution and address utilities the connected/datagram socket work (#173 / #192) deferred, all backed by Go's net package (CGO=0).

Surface

Socket.getaddrinfo(host, port[, family, socktype, protocol, flags]) → array of MRI-shape tuples [afamily_string, port, host, addr, pfamily, socktype, protocol], resolved through net.Resolver. No reverse lookup (host == numeric addr), keeping results hermetic. family / socktype accept an Integer (Socket::AF_INET), a Symbol (:INET), or a String ("AF_INET"); a numeric port String is parsed and a service-name String is looked up.

Addrinfo value class:

  • Addrinfo.tcp(host, port) / .udp(host, port) / .ip(host) / .getaddrinfo(...) / .new(sockaddr[, family, socktype, protocol]) — sockaddr is either a packed sockaddr String or a [afamily, port, host, addr] array (MRI accepts both).
  • instance: ip_address / ip_port / afamily / pfamily / socktype / protocol / ipv4? / ipv6? / to_sockaddr (alias to_s) / inspect (#<Addrinfo: 127.0.0.1:8080 TCP>, IPv6 bracketed).

sockaddr pack/unpack:

  • Socket.pack_sockaddr_in(port, host) / sockaddr_in and Socket.unpack_sockaddr_in(sa) → [port, host] (IPv4 + IPv6).
  • Socket.pack_sockaddr_un(path) / sockaddr_un and Socket.unpack_sockaddr_un(sa) — AF_UNIX, non-Windows only; the Windows build installs NotImplementedError stubs, mirroring the UNIXSocket transport.

Endianness

The sockaddr layout is the Linux one (no BSD sa_len byte): the family field is written in the host's native byte order (a C sa_family_t is a host-endian short) while the port and address are network byte order, exactly the C semantics. pack and unpack share the native order for the family field, so the [port, host] round-trip is byte-exact on every arch. Verified on big-endian s390x under emulation.

Testing

The whole surface is exercised through the rbgo engine against loopback-resolvable inputs (127.0.0.1 / ::1 / localhost). The resolver and service-name port lookup are reached only for non-numeric inputs and are driven through injectable seams (resolveIPs / lookupPort), so the suite never touches live DNS. 100% coverage of the new code, including every error and platform arm.

Confined to internal/vm/socket*.go (+ one classOf case in object_model.go).

🤖 Generated with Claude Code

Extend rbgo's socket transport (require "socket") with the name-resolution
and address utilities the connected/datagram socket work (#173/#192)
deferred, all backed by Go's net package (no cgo):

- Socket.getaddrinfo(host, port[, family, socktype, protocol, flags]) ->
  MRI's [afamily, port, host, addr, pfamily, socktype, protocol] tuples,
  resolved through net.Resolver with no reverse lookup (hermetic).
- Addrinfo value class: .tcp / .udp / .ip / .getaddrinfo / .new (packed
  sockaddr String or [afamily, port, host, addr] array), plus
  ip_address / ip_port / afamily / pfamily / socktype / protocol /
  ipv4? / ipv6? / to_sockaddr / inspect.
- sockaddr pack/unpack: Socket.pack_sockaddr_in / sockaddr_in /
  unpack_sockaddr_in (IPv4 + IPv6) and pack_sockaddr_un / sockaddr_un /
  unpack_sockaddr_un (AF_UNIX, non-Windows; NotImplementedError stubs on
  Windows, mirroring the UNIXSocket transport).

The sockaddr layout is the Linux one (no BSD sa_len byte): the family
field is host native byte order (a C sa_family_t) while port and address
are network byte order, so the [port, host] round-trip is byte-exact on
every arch. Verified on big-endian s390x under emulation.

Tests exercise the whole surface through the rbgo engine against
loopback-resolvable inputs (127.0.0.1 / ::1 / localhost); the resolver and
service-name port lookup are driven through injectable seams (resolveIPs /
lookupPort) so the suite never touches live DNS. 100% coverage of the new
code, including every error and platform arm.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 08a46b8 into main Jul 6, 2026
9 checks passed
@tannevaled
tannevaled deleted the feat/socket-addrinfo branch July 6, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant