Skip to content

Commit

Permalink
align GOOS=tamago support go go1.22.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abarisani committed Apr 8, 2024
1 parent 8566423 commit 1e756f6
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 62 deletions.
76 changes: 47 additions & 29 deletions src/net/dnsclient_tamago.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package net
import (
"context"
"errors"
"internal/bytealg"
"io"
"time"

Expand Down Expand Up @@ -198,7 +199,9 @@ func (r *Resolver) exchange(ctx context.Context, server string, q dnsmessage.Que

// checkHeader performs basic sanity checks on the header.
func checkHeader(p *dnsmessage.Parser, h dnsmessage.Header) error {
if h.RCode == dnsmessage.RCodeNameError {
rcode := extractExtendedRCode(*p, h)

if rcode == dnsmessage.RCodeNameError {
return errNoSuchHost
}

Expand All @@ -209,17 +212,17 @@ func checkHeader(p *dnsmessage.Parser, h dnsmessage.Header) error {

// libresolv continues to the next server when it receives
// an invalid referral response. See golang.org/issue/15434.
if h.RCode == dnsmessage.RCodeSuccess && !h.Authoritative && !h.RecursionAvailable && err == dnsmessage.ErrSectionDone {
if rcode == dnsmessage.RCodeSuccess && !h.Authoritative && !h.RecursionAvailable && err == dnsmessage.ErrSectionDone {
return errLameReferral
}

if h.RCode != dnsmessage.RCodeSuccess && h.RCode != dnsmessage.RCodeNameError {
if rcode != dnsmessage.RCodeSuccess && rcode != dnsmessage.RCodeNameError {
// None of the error codes make sense
// for the query we sent. If we didn't get
// a name error and we didn't get success,
// the server is behaving incorrectly or
// having temporary trouble.
if h.RCode == dnsmessage.RCodeServerFailure {
if rcode == dnsmessage.RCodeServerFailure {
return errServerTemporarilyMisbehaving
}
return errServerMisbehaving
Expand All @@ -246,6 +249,23 @@ func skipToAnswer(p *dnsmessage.Parser, qtype dnsmessage.Type) error {
}
}

// extractExtendedRCode extracts the extended RCode from the OPT resource (EDNS(0))
// If an OPT record is not found, the RCode from the hdr is returned.
func extractExtendedRCode(p dnsmessage.Parser, hdr dnsmessage.Header) dnsmessage.RCode {
p.SkipAllAnswers()
p.SkipAllAuthorities()
for {
ahdr, err := p.AdditionalHeader()
if err != nil {
return hdr.RCode
}
if ahdr.Type == dnsmessage.TypeOPT {
return ahdr.ExtendedRCode(hdr.RCode)
}
p.SkipAdditional()
}
}

// Do a lookup for a single name, which must be rooted
// (otherwise answer will not find the answers).
func (r *Resolver) tryOneName(ctx context.Context, cfg *dnsConfig, name string, qtype dnsmessage.Type) (dnsmessage.Parser, string, error) {
Expand Down Expand Up @@ -385,10 +405,6 @@ func avoidDNS(name string) bool {

// nameList returns a list of names for sequential DNS queries.
func (conf *dnsConfig) nameList(name string) []string {
if avoidDNS(name) {
return nil
}

// Check name length (see isDomainName).
l := len(name)
rooted := l > 0 && name[l-1] == '.'
Expand All @@ -398,27 +414,31 @@ func (conf *dnsConfig) nameList(name string) []string {

// If name is rooted (trailing dot), try only that name.
if rooted {
if avoidDNS(name) {
return nil
}
return []string{name}
}

hasNdots := count(name, '.') >= conf.ndots
hasNdots := bytealg.CountString(name, '.') >= conf.ndots
name += "."
l++

// Build list of search choices.
names := make([]string, 0, 1+len(conf.search))
// If name has enough dots, try unsuffixed first.
if hasNdots {
if hasNdots && !avoidDNS(name) {
names = append(names, name)
}
// Try suffixes that are not too long (see isDomainName).
for _, suffix := range conf.search {
if l+len(suffix) <= 254 {
names = append(names, name+suffix)
fqdn := name + suffix
if !avoidDNS(fqdn) && len(fqdn) <= 254 {
names = append(names, fqdn)
}
}
// Try unsuffixed, if not tried first above.
if !hasNdots {
if !hasNdots && !avoidDNS(name) {
names = append(names, name)
}
return names
Expand Down Expand Up @@ -520,7 +540,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
h, err := result.p.AnswerHeader()
if err != nil && err != dnsmessage.ErrSectionDone {
lastErr = &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: name,
Server: result.server,
}
Expand All @@ -533,7 +553,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
a, err := result.p.AResource()
if err != nil {
lastErr = &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: name,
Server: result.server,
}
Expand All @@ -548,7 +568,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
aaaa, err := result.p.AAAAResource()
if err != nil {
lastErr = &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: name,
Server: result.server,
}
Expand All @@ -563,7 +583,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
c, err := result.p.CNAMEResource()
if err != nil {
lastErr = &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: name,
Server: result.server,
}
Expand All @@ -576,7 +596,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
default:
if err := result.p.SkipAnswer(); err != nil {
lastErr = &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: name,
Server: result.server,
}
Expand Down Expand Up @@ -612,16 +632,14 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co
return addrs, cname, nil
}

// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME.
func (r *Resolver) goLookupCNAME(ctx context.Context, host string, conf *dnsConfig) (string, error) {
_, cname, err := r.goLookupIPCNAME(ctx, "CNAME", host, conf)
return cname.String(), err
}

// goLookupPTR is the native Go implementation of LookupAddr.
// Used only if cgoLookupPTR refuses to handle the request (that is,
// only if cgoLookupPTR is the stub in cgo_stub.go).
// Normally we let cgo use the C library resolver instead of depending
// on our lookup code, so that Go and C get the same answers.
func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig) ([]string, error) {
names := lookupStaticAddr(addr)
if len(names) > 0 {
return names, nil
}
arpa, err := reverseaddr(addr)
if err != nil {
return nil, err
Expand All @@ -638,7 +656,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig
}
if err != nil {
return nil, &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: addr,
Server: server,
}
Expand All @@ -647,7 +665,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig
err := p.SkipAnswer()
if err != nil {
return nil, &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: addr,
Server: server,
}
Expand All @@ -657,7 +675,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig
ptr, err := p.PTRResource()
if err != nil {
return nil, &DNSError{
Err: "cannot marshal DNS message",
Err: errCannotUnmarshalDNSMessage.Error(),
Name: addr,
Server: server,
}
Expand Down
19 changes: 9 additions & 10 deletions src/net/lookup_tamago.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,36 @@ func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string,
return
}

func (r *Resolver) lookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error) {
ips, _, err := r.goLookupIPCNAME(ctx, network, name, getSystemDNSConfig())
func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) {
ips, _, err := r.goLookupIPCNAME(ctx, network, host, getSystemDNSConfig())
return ips, err
}

func (*Resolver) lookupPort(ctx context.Context, network, service string) (port int, err error) {
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
return goLookupPort(network, service)
}

func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
_, cname, err := r.goLookupIPCNAME(ctx, "CNAME", name, getSystemDNSConfig())
return cname.String(), err
return r.goLookupCNAME(ctx, name, getSystemDNSConfig())
}

func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (cname string, srvs []*SRV, err error) {
func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
return r.goLookupSRV(ctx, service, proto, name)
}

func (r *Resolver) lookupMX(ctx context.Context, name string) (mxs []*MX, err error) {
func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
return r.goLookupMX(ctx, name)
}

func (r *Resolver) lookupNS(ctx context.Context, name string) (nss []*NS, err error) {
func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) {
return r.goLookupNS(ctx, name)
}

func (r *Resolver) lookupTXT(ctx context.Context, name string) (txts []string, err error) {
func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
return r.goLookupTXT(ctx, name)
}

func (r *Resolver) lookupAddr(ctx context.Context, addr string) (ptrs []string, err error) {
func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
return r.goLookupPTR(ctx, addr, getSystemDNSConfig())
}

Expand Down
2 changes: 1 addition & 1 deletion src/net/sockopt_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build js || wasip1 || tamago
//go:build js || wasip1

package net

Expand Down
37 changes: 37 additions & 0 deletions src/net/sockopt_tamago.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build tamago

package net

import "syscall"

func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
return nil
}

func setDefaultListenerSockopts(s int) error {
return nil
}

func setDefaultMulticastSockopts(s int) error {
return nil
}

func setReadBuffer(fd *netFD, bytes int) error {
return syscall.ENOPROTOOPT
}

func setWriteBuffer(fd *netFD, bytes int) error {
return syscall.ENOPROTOOPT
}

func setKeepAlive(fd *netFD, keepalive bool) error {
return syscall.ENOPROTOOPT
}

func setLinger(fd *netFD, sec int) error {
return syscall.ENOPROTOOPT
}
6 changes: 6 additions & 0 deletions src/os/exec/lp_tamago.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ func LookPath(file string) (string, error) {
// tamago can not execute processes, so act as if there are no executables at all.
return "", &Error{file, ErrNotFound}
}

// lookExtensions is a no-op on non-Windows platforms, since
// they do not restrict executables to specific extensions.
func lookExtensions(path, dir string) (string, error) {
return path, nil
}
17 changes: 0 additions & 17 deletions src/os/file_open_tamago.go

This file was deleted.

2 changes: 1 addition & 1 deletion src/os/file_open_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix || (js && wasm)
//go:build unix || (js && wasm) || tamago

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/stat_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build (js && wasm)
//go:build js && wasm

package os

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/lock_tamago.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const (
passive_spin = 1
)

func mutexContended(l *mutex) bool {
return false
}

func lock(l *mutex) {
lockWithRank(l, getLockRank(l))
}
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/os_tamago_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func osinit() {
initBloc()
}

func readRandom(r []byte) int {
initRNG()
getRandomData(r)
return len(r)
}

func signame(sig uint32) string {
return ""
}
Expand All @@ -123,7 +129,6 @@ func checkgoarm() {

//go:nosplit
func cputicks() int64 {
// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
return nanotime()
}
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/os_tamago_riscv64.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ func osinit() {
initBloc()
}

func readRandom(r []byte) int {
initRNG()
getRandomData(r)
return len(r)
}

func signame(sig uint32) string {
return ""
}

//go:nosplit
func cputicks() int64 {
// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
return nanotime()
}
Expand Down
Loading

0 comments on commit 1e756f6

Please sign in to comment.