tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/netdev/netdev.go (about) 1 // L3/L4 network/transport layer 2 3 package netdev 4 5 import ( 6 "errors" 7 "net/netip" 8 "time" 9 _ "unsafe" // to use go:linkname 10 ) 11 12 const ( 13 AF_INET = 0x2 14 SOCK_STREAM = 0x1 15 SOCK_DGRAM = 0x2 16 SOL_SOCKET = 0x1 17 SO_KEEPALIVE = 0x9 18 SOL_TCP = 0x6 19 TCP_KEEPINTVL = 0x5 20 IPPROTO_TCP = 0x6 21 IPPROTO_UDP = 0x11 22 // Made up, not a real IP protocol number. This is used to create a 23 // TLS socket on the device, assuming the device supports mbed TLS. 24 IPPROTO_TLS = 0xFE 25 F_SETFL = 0x4 26 ) 27 28 // GethostByName() errors 29 var ( 30 ErrHostUnknown = errors.New("Host unknown") 31 ErrMalAddr = errors.New("Malformed address") 32 ) 33 34 // Socket errors 35 var ( 36 ErrFamilyNotSupported = errors.New("Address family not supported") 37 ErrProtocolNotSupported = errors.New("Socket protocol/type not supported") 38 ErrStartingDHCPClient = errors.New("Error starting DHPC client") 39 ErrStartingDHCPServer = errors.New("Error starting DHPC server") 40 ErrNoMoreSockets = errors.New("No more sockets") 41 ErrClosingSocket = errors.New("Error closing socket") 42 ErrNotSupported = errors.New("Not supported") 43 ErrInvalidSocketFd = errors.New("Invalid socket fd") 44 ) 45 46 // Duplicate of non-exported net.errTimeout 47 var ErrTimeout error = &timeoutError{} 48 49 type timeoutError struct{} 50 51 func (e *timeoutError) Error() string { return "i/o timeout" } 52 func (e *timeoutError) Timeout() bool { return true } 53 func (e *timeoutError) Temporary() bool { return true } 54 55 //go:linkname UseNetdev net.useNetdev 56 func UseNetdev(dev Netdever) 57 58 // Netdever is TinyGo's OSI L3/L4 network/transport layer interface. Network 59 // drivers implement the Netdever interface, providing a common network L3/L4 60 // interface to TinyGo's "net" package. net.Conn implementations (TCPConn, 61 // UDPConn, and TLSConn) use the Netdever interface for device I/O access. 62 // 63 // A Netdever is passed to the "net" package using UseNetdev(). 64 // 65 // Just like a net.Conn, multiple goroutines may invoke methods on a Netdever 66 // simultaneously. 67 // 68 // NOTE: The Netdever interface is mirrored in tinygo/src/net/netdev.go. 69 // NOTE: If making changes to this interface, mirror the changes in 70 // NOTE: tinygo/src/net/netdev.go, and vice-versa. 71 72 type Netdever interface { 73 74 // GetHostByName returns the IP address of either a hostname or IPv4 75 // address in standard dot notation 76 GetHostByName(name string) (netip.Addr, error) 77 78 // Addr returns IP address assigned to the interface, either by 79 // DHCP or statically 80 Addr() (netip.Addr, error) 81 82 // Berkely Sockets-like interface, Go-ified. See man page for socket(2), etc. 83 Socket(domain int, stype int, protocol int) (int, error) 84 Bind(sockfd int, ip netip.AddrPort) error 85 Connect(sockfd int, host string, ip netip.AddrPort) error 86 Listen(sockfd int, backlog int) error 87 Accept(sockfd int) (int, netip.AddrPort, error) 88 Send(sockfd int, buf []byte, flags int, deadline time.Time) (int, error) 89 Recv(sockfd int, buf []byte, flags int, deadline time.Time) (int, error) 90 Close(sockfd int) error 91 SetSockOpt(sockfd int, level int, opt int, value interface{}) error 92 }