Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addrinfo: fix address ordering #68

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions systems/unix/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/stealthrocket/wasi-go"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -916,7 +917,7 @@ func (s *System) SockRemoteAddress(ctx context.Context, fd wasi.FD) (wasi.Socket
}

func (s *System) SockAddressInfo(ctx context.Context, name, service string, hints wasi.AddressInfo, results []wasi.AddressInfo) (int, wasi.Errno) {
if cap(results) == 0 {
if len(results) == 0 {
return 0, wasi.EINVAL
}
// TODO: support AI_ADDRCONFIG, AI_CANONNAME, AI_V4MAPPED, AI_V4MAPPED_CFG, AI_ALL
Expand Down Expand Up @@ -1008,10 +1009,8 @@ func (s *System) SockAddressInfo(ctx context.Context, name, service string, hint
return 0, wasi.ECANCELED // TODO: better errors on name resolution failure
}

if len(ips) > cap(results) {
ips = ips[:cap(results)]
}
results = results[:0]
addrs := make([]wasi.AddressInfo, 0, 16)

for _, ip := range ips {
var addr wasi.AddressInfo
if ipv4 := ip.To4(); ipv4 != nil {
Expand All @@ -1029,9 +1028,14 @@ func (s *System) SockAddressInfo(ctx context.Context, name, service string, hint
copy(inet6Addr.Addr[:], ip)
addr.Address = &inet6Addr
}
results = append(results, addr)
addrs = append(addrs, addr)
}
return len(results), wasi.ESUCCESS

slices.SortStableFunc(addrs, func(a1, a2 wasi.AddressInfo) bool {
return a1.Family < a2.Family
})

return copy(results, addrs), wasi.ESUCCESS
}

func (s *System) Close(ctx context.Context) error {
Expand Down