ish icon indicating copy to clipboard operation
ish copied to clipboard

accept tcp [::]:9999 function not implemented

Open Aquarian-Age opened this issue 2 years ago • 6 comments

The file (web service) compiled with Golang on the PC Linux system cannot achieve tcp connection

None of the following three can be connected

http.ListenAndServe("127.0.0.1:9999", nil)
http.ListenAndServe("localhost:9999", nil)
http.ListenAndServe("0.0.0.0:9999", nil)

CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w" -trimpath -o sjqm-jqh-i386

thumbnail_image

Aquarian-Age avatar Aug 29 '22 12:08 Aquarian-Age

Upvote. Same problem occurs to me on the latest App Store version 1.2.3

I was trying to get a syncthing client with go 1.20 compiled along running on iSH, whose web-gui depends on net.http. And the server was just unable to accept any connections.

The syncthing in alpinelinux repo works just fine (which I don’t understand why, they should be all using the same net.http), despite some random deadlocks.

Shihira avatar Sep 04 '22 11:09 Shihira

I have the same issue when I was using cloudflared access to host an access to tunnel...

and I'm using App Store version 1.2.3

$ ./cloudflared access tcp --host
name [some-private-domain] --url 0.0.0.0:2038
2022-10-15T04:54:15Z INF Start Websocket listener host=0.0.0.0: 2038
2022-10-15T04:54:15Z ERR Error on Websocket listener error="accept tcp [::]: 2038: accept: function not implemented"
accept tcp [::]: 2038: accept: function not implemented

ljcucc avatar Oct 15 '22 04:10 ljcucc

The following Go code replaces the default internal/poll.AcceptFunc function with a C binding of the native accept function to work around an implementation issue :

package main

/*
#cgo CFLAGS: -Wall
#include <arpa/inet.h>

int native_accept(int server_fd, struct sockaddr_in *address,
                  socklen_t *addrlen) {
  return accept(server_fd, (struct sockaddr *)address, addrlen);
}
*/
import "C"

import (
	"fmt"
	"net"
	"net/http"
	"syscall"
	"unsafe"
)

//go:linkname pollAcceptFunc internal/poll.AcceptFunc
var pollAcceptFunc func(int) (int, syscall.Sockaddr, error)

//go:linkname syscallAnyToSockaddr syscall.anyToSockaddr
func syscallAnyToSockaddr(rsa *syscall.RawSockaddrAny) (syscall.Sockaddr, error)

func init() {
	pollAcceptFunc = Accept
}

// Replace [internal/poll.AcceptFunc] to fix `accept: function not implemented`
// error on iSH
func Accept(fd int) (nfd int, sa syscall.Sockaddr, err error) {
	var addr syscall.RawSockaddrAny
	var addrLen C.socklen_t = syscall.SizeofSockaddrAny

	newFd, err := C.native_accept(
		C.int(fd),
		(*C.struct_sockaddr_in)(unsafe.Pointer(&addr)),
		&addrLen,
	)
	if newFd < 0 {
		return -1, nil, err
	}

	sa, err = syscallAnyToSockaddr(&addr)
	if err != nil {
		return -1, nil, err
	}

	return int(newFd), sa, nil
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("Hello World!"))
	})

	l, err := net.Listen("tcp", "0.0.0.0:8080")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Listening on %s\n", l.Addr())

	err = http.Serve(l, mux)
	if err != nil {
		panic(err)
	}
}

birros avatar May 31 '24 22:05 birros