{"modulePath":"github.com/celzero/firestack","version":"v0.0.0-20261126203528-ea10d50bc2a7","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package netstack\n\nPackage netstack provides the implemention of data-link layer endpoints backed by boundary-preserving file descriptors (e.g., TUN devices, seqpacket/datagram sockets).\n\nAdopted from: github.com/google/gvisor/blob/f33d034/pkg/tcpip/link/fdbased/endpoint.go since fdbased isn't built when building for android (it is only built for linux).\n\n## Constants\n\n```go\nconst (\n\t// DirectionSend indicates a sent packet.\n\tDirectionSend = iota\n\t// DirectionRecv indicates a received packet.\n\tDirectionRecv\n)\n```\n\n```go\nconst SnapLen uint32 = 2048 // in bytes; some sufficient value\n\n```\nSnapLen is the maximum bytes of a packet to be saved. Packets with a length less than or equal to snapLen will be saved in their entirety. Longer packets will be truncated to snapLen. TODO: MTU instead of SnapLen? Must match pcapsink.begin()\n\n## Functions\n\n```go\nfunc DebugLog(y bool) (l string)\n```\n\n```go\nfunc HandlerAddrs(hdl GConnHandler) (ifaddr4 netip.Prefix, ifaddr6 netip.Prefix)\n```\n\n```go\nfunc InboundTCP(who string, s *stack.Stack, in net.Conn, to, from netip.AddrPort, h GTCPConnHandler) error\n```\ns is the netstack to use for dialing (reads/writes). in is the incoming connection to netstack, s. to (src) is remote. from (dst) is local (to netstack, s). h is the handler that handles connection in into netstack, s, by dialing to from (dst) from to (src).\n\n```go\nfunc InboundUDP(who string, s *stack.Stack, in net.Conn, to, from netip.AddrPort, h GUDPConnHandler) error\n```\n\n```go\nfunc LogPacket(prefix string, dir Direction, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer)\n```\nLogPacket logs a packet to stdout.\n\n```go\nfunc NewNetstack() (s *stack.Stack)\n```\nalso: github.com/google/gvisor/blob/adbdac747/runsc/boot/loader.go#L1132 github.com/FlowerWrong/tun2socks/blob/1045a49618/cmd/netstack/main.go github.com/zen-of-proxy/go-tun2io/blob/c08b329b8/tun2io/util.go github.com/WireGuard/wireguard-go/blob/42c9af4/tun/netstack/tun.go github.com/telepresenceio/telepresence/pull/2709\n\n```go\nfunc NewReverseGConnHandler(pctx context.Context, to *stack.Stack, of tcpip.NICID, ep SeamlessEndpoint, via GConnHandler) *gconnhandler\n```\n\n```go\nfunc OutboundICMP(id string, s *stack.Stack, hdl GICMPHandler)\n```\ngithub.com/google/gvisor/blob/738e1d995f/pkg/tcpip/network/ipv4/icmp.go github.com/google/gvisor/blob/738e1d995f/pkg/tcpip/network/ipv6/icmp.go\n\n```go\nfunc OutboundTCP(who string, s *stack.Stack, h GTCPConnHandler)\n```\nOutboundTCP sets up a TCP forwarder h to handle TCP packets. If h is nil, s uses the (built-in) default TCP forwarding logic.\n\n```go\nfunc OutboundUDP(who string, s *stack.Stack, h GUDPConnHandler)\n```\nOutboundUDP sets up a UDP forwarder h for outbound UDP packets. If h is nil, s uses the (built-in) default UDP forwarding logic.\n\n```go\nfunc Pcap2File(y bool) (ok bool)\n```\n\n```go\nfunc Pcap2Stdout(y bool) (ok bool)\n```\n\n```go\nfunc PcapModes() string\n```\nPCAP logging modes: - stdout: packets are logged to stdout - file: packets are logged to a file - none: no packets are logged\n\n```go\nfunc Route(s *stack.Stack, l3 string)\n```\n\n```go\nfunc SetNetstackOpts(s *stack.Stack)\n```\n\n```go\nfunc StackAddrs(s *stack.Stack, nic tcpip.NICID) (netip.Addr, netip.Addr)\n```\n\n```go\nfunc Stat(s *stack.Stack) (out *x.NetStat, err error)\n```\n\n```go\nfunc Up(s *stack.Stack, ep SeamlessEndpoint, h GConnHandler) (tcpip.NICID, error)\n```\nref: github.com/brewlin/net-protocol/blob/ec64e5f899/internal/endpoint/endpoint.go#L20\n\n```go\nfunc WritePCAPHeader(w io.Writer) error\n```\n\n## Types\n\n```go\ntype DemuxerFn func(in net.Conn, to netip.AddrPort) error\n```\n\n```go\ntype Direction int\n```\nA Direction indicates whether the packing is being sent or received.\n\n```go\nfunc (dr Direction) String() string\n```\n\n```go\ntype EpStat struct {\n\t// Fd is the file descriptor of the endpoint.\n\tFd int\n\t// Alive indicates whether the endpoint is alive.\n\tAlive bool\n\t// Age is the age of the endpoint.\n\tAge string\n\t// Read is the number of bytes read from the endpoint.\n\tRead string\n\t// Written is the number of bytes written to the endpoint.\n\tWritten string\n\t// LastRead is the last time the endpoint was read from.\n\tLastRead string\n\t// LastWrite is the last time the endpoint was written to.\n\tLastWrite string\n\t// IOStart is the time the endpoint's dispatcher was started.\n\tIOStart string\n\t// IOEnd is the time the endpoint's dispatcher was stopped.\n\tIOEnd string\n}\n```\n\n```go\nfunc (s EpStat) String() string\n```\n\n```go\ntype FdSwapper interface {\n\t// Swap closes existing FDs; uses new fd.\n\tSwap(fd, mtu int) error\n\t// Dispose closes all existing FDs.\n\tDispose() error\n\t// Stat returns EpStat (fd, age, read, written, lastRead, lastWrite).\n\tStat() EpStat\n}\n```\n\n```go\ntype GBaseConnHandler interface {\n\t// OpenConns returns the number of active connections.\n\tOpenConns() string\n\t// CloseConns closes conns by ids, or all if ids is empty.\n\tCloseConns([]string) []string\n\t// end closes the handler and all its connections.\n\tEnd()\n\t// Reset informs the handler of a new stack (post restart, for example).\n\tReset()\n}\n```\n\n```go\ntype GConnHandler interface {\n\tSrc() []netip.Prefix\n\tTCP() GTCPConnHandler         // TCP returns the TCP handler.\n\tUDP() GUDPConnHandler         // UDP returns the UDP handler.\n\tICMP() GICMPHandler           // ICMP returns the ICMP handler.\n\tCloseConns(csv string) string // CloseConns closes the connections with the given IDs, or all if empty.\n\tReset()                       // Reset re-arms all handlers for a new stack (restart).\n}\n```\n\n```go\nfunc NewGConnHandler(addrs []netip.Prefix, tcp GTCPConnHandler, udp GUDPConnHandler, icmp GICMPHandler) GConnHandler\n```\n\n```go\ntype GEchoConnHandler interface {\n\t// Ping informs if ICMP Echo from src to dst is replied to\n\tPing(msg []byte, src, dst netip.AddrPort) bool\n}\n```\n\n```go\ntype GICMPConn struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc DialPingAddr(s *stack.Stack, nic tcpip.NICID, laddr, raddr netip.Addr) (*GICMPConn, error)\n```\n\n```go\nfunc (pc *GICMPConn) Close() error\n```\n\n```go\nfunc (pc *GICMPConn) LocalAddr() net.Addr\n```\n\n```go\nfunc (pc *GICMPConn) Read(p []byte) (n int, err error)\n```\n\n```go\nfunc (pc *GICMPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)\n```\n\n```go\nfunc (pc *GICMPConn) RemoteAddr() net.Addr\n```\n\n```go\nfunc (pc *GICMPConn) SetDeadline(t time.Time) error\n```\n\n```go\nfunc (pc *GICMPConn) SetReadDeadline(t time.Time) error\n```\n\n```go\nfunc (pc *GICMPConn) SetWriteDeadline(t time.Time) error\n```\n\n```go\nfunc (pc *GICMPConn) SyscallConn() (syscall.RawConn, error)\n```\nSyscallConn implements core.ICMPConn.\n\n```go\nfunc (pc *GICMPConn) Write(p []byte) (n int, err error)\n```\n\n```go\nfunc (pc *GICMPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)\n```\n\n```go\ntype GICMPHandler interface {\n\tGBaseConnHandler\n\tGEchoConnHandler\n}\n```\n\n```go\ntype GMuxConnHandler[T gconns] interface {\n\t// ProxyMux proxies data between conn and multiple destinations\n\t// (endpoint-independent mapping).\n\tProxyMux(in T, src, dst netip.AddrPort, dmx DemuxerFn) bool\n}\n```\n\n```go\ntype GSpecConnHandler[T gconns] interface {\n\tGBaseConnHandler\n\t// Proxy copies data between conn and dst (egress).\n\t// must not block forever as it may block netstack\n\t// see: netstack/dispatcher.go:newReadvDispatcher\n\tProxy(in T, src, dst netip.AddrPort) bool\n\t// ReverseProxy copies data between conn and dst (ingress).\n\tReverseProxy(out T, in net.Conn, src, dst netip.AddrPort) bool\n\t// Error notes the error in connecting src to dst; retrying if necessary.\n\tError(in T, src, dst netip.AddrPort, err error)\n}\n```\n\n```go\ntype GTCPConn struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (g *GTCPConn) Abort()\n```\nAbort aborts the connection by sending a RST segment.\n\n```go\nfunc (g *GTCPConn) Close() error\n```\n\n```go\nfunc (g *GTCPConn) CloseRead() error\n```\n\n```go\nfunc (g *GTCPConn) CloseWrite() error\n```\n\n```go\nfunc (g *GTCPConn) Establish() (open bool, err error)\n```\n\n```go\nfunc (g *GTCPConn) LocalAddr() net.Addr\n```\ngonet conn local and remote addresses may be nil ref: github.com/tailscale/tailscale/blob/8c5c87be2/wgengine/netstack/netstack.go#L768-L775 and: github.com/google/gvisor/blob/ffabadf0/pkg/tcpip/transport/tcp/endpoint.go#L2759\n\n```go\nfunc (g *GTCPConn) Read(data []byte) (int, error)\n```\n\n```go\nfunc (g *GTCPConn) RemoteAddr() net.Addr\n```\n\n```go\nfunc (g *GTCPConn) SetDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GTCPConn) SetReadDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GTCPConn) SetWriteDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GTCPConn) Write(data []byte) (int, error)\n```\n\n```go\ntype GTCPConnHandler interface {\n\tGSpecConnHandler[*GTCPConn]\n}\n```\n\n```go\ntype GUDPConn struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (g *GUDPConn) Close() error\n```\nClose closes the connection.\n\n```go\nfunc (g *GUDPConn) Establish() error\n```\n\n```go\nfunc (g *GUDPConn) LocalAddr() (addr net.Addr)\n```\n\n```go\nfunc (g *GUDPConn) Read(data []byte) (int, error)\n```\n\n```go\nfunc (g *GUDPConn) ReadFrom(data []byte) (int, net.Addr, error)\n```\n\n```go\nfunc (g *GUDPConn) RemoteAddr() (addr net.Addr)\n```\n\n```go\nfunc (g *GUDPConn) SetDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GUDPConn) SetReadDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GUDPConn) SetWriteDeadline(t time.Time) error\n```\n\n```go\nfunc (g *GUDPConn) StatefulTeardown() (fin bool)\n```\n\n```go\nfunc (g *GUDPConn) Write(data []byte) (int, error)\n```\n\n```go\nfunc (g *GUDPConn) WriteTo(data []byte, addr net.Addr) (int, error)\n```\n\n```go\ntype GUDPConnHandler interface {\n\tGSpecConnHandler[*GUDPConn]\n\tGMuxConnHandler[*GUDPConn]\n}\n```\n\n```go\ntype Options struct {\n\t// FDs is a set of FDs used to read/write packets.\n\tFDs []int\n\n\t// MTU is the mtu to use for this endpoint.\n\tMTU uint32\n\n\t// EthernetHeader if true, indicates that the endpoint should read/write\n\t// ethernet frames instead of IP packets.\n\tEthernetHeader bool\n\n\t// Address is the link address for this endpoint. Only used if\n\t// EthernetHeader is true.\n\tAddress tcpip.LinkAddress\n\n\t// SaveRestore if true, indicates that this NIC capability set should\n\t// include CapabilitySaveRestore\n\tSaveRestore bool\n\n\t// DisconnectOk if true, indicates that this NIC capability set should\n\t// include CapabilityDisconnectOk.\n\tDisconnectOk bool\n\n\t// TXChecksumOffload if true, indicates that this endpoints capability\n\t// set should include CapabilityTXChecksumOffload.\n\tTXChecksumOffload bool\n\n\t// RXChecksumOffload if true, indicates that this endpoints capability\n\t// set should include CapabilityRXChecksumOffload.\n\tRXChecksumOffload bool\n}\n```\nOptions specify the details about the fd-based endpoint to be created.\n\n```go\ntype PingAddr struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc PingAddrFromAddr(addr netip.Addr) *PingAddr\n```\n\n```go\nfunc (ipp PingAddr) Addr() netip.Addr\n```\n\n```go\nfunc (ipp PingAddr) Network() string\n```\n\n```go\nfunc (ipp PingAddr) String() string\n```\n\n```go\ntype SeamlessEndpoint interface {\n\tstack.LinkEndpoint\n\tFdSwapper\n}\n```\n\n```go\nfunc NewEndpoint(dev, mtu int, sink io.WriteCloser) (ep SeamlessEndpoint, err error)\n```\nref: github.com/google/gvisor/blob/91f58d2cc/pkg/tcpip/sample/tun\\_tcp\\_echo/main.go#L102\n\n","path":"github.com/celzero/firestack/intra/netstack","name":"netstack","synopsis":"Package netstack provides the implemention of data-link layer endpoints backed by boundary-preserving file descriptors (e.g., TUN devices, seqpacket/datagram sockets).","isRedistributable":true}
