1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright 2023 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 wasip1
package net
import (
"os"
"syscall"
_ "unsafe" // for go:linkname
)
func fileListener(f *os.File) (Listener, error) {
filetype, err := fd_fdstat_get_type(f.PollFD().Sysfd)
if err != nil {
return nil, err
}
net, err := fileListenNet(filetype)
if err != nil {
return nil, err
}
pfd := f.PollFD().Copy()
fd := newPollFD(net, pfd)
if err := fd.init(); err != nil {
pfd.Close()
return nil, err
}
return newFileListener(fd), nil
}
func fileConn(f *os.File) (Conn, error) {
filetype, err := fd_fdstat_get_type(f.PollFD().Sysfd)
if err != nil {
return nil, err
}
net, err := fileConnNet(filetype)
if err != nil {
return nil, err
}
pfd := f.PollFD().Copy()
fd := newPollFD(net, pfd)
if err := fd.init(); err != nil {
pfd.Close()
return nil, err
}
return newFileConn(fd), nil
}
func filePacketConn(f *os.File) (PacketConn, error) {
return nil, syscall.ENOPROTOOPT
}
func fileListenNet(filetype syscall.Filetype) (string, error) {
switch filetype {
case syscall.FILETYPE_SOCKET_STREAM:
return "tcp", nil
case syscall.FILETYPE_SOCKET_DGRAM:
return "", syscall.EOPNOTSUPP
default:
return "", syscall.ENOTSOCK
}
}
func fileConnNet(filetype syscall.Filetype) (string, error) {
switch filetype {
case syscall.FILETYPE_SOCKET_STREAM:
return "tcp", nil
case syscall.FILETYPE_SOCKET_DGRAM:
return "udp", nil
default:
return "", syscall.ENOTSOCK
}
}
func newFileListener(fd *netFD) Listener {
switch fd.net {
case "tcp":
return &TCPListener{fd: fd}
default:
panic("unsupported network for file listener: " + fd.net)
}
}
func newFileConn(fd *netFD) Conn {
switch fd.net {
case "tcp":
return &TCPConn{conn{fd: fd}}
case "udp":
return &UDPConn{conn{fd: fd}}
default:
panic("unsupported network for file connection: " + fd.net)
}
}
// This helper is implemented in the syscall package. It means we don't have
// to redefine the fd_fdstat_get host import or the fdstat struct it
// populates.
//
//go:linkname fd_fdstat_get_type syscall.fd_fdstat_get_type
func fd_fdstat_get_type(fd int) (uint8, error)
|