mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
5 years ago
|
|
||
2 years ago
|
package memnet
|
||
5 years ago
|
|
||
|
import (
|
||
4 years ago
|
"net"
|
||
2 years ago
|
"net/netip"
|
||
5 years ago
|
"time"
|
||
|
)
|
||
|
|
||
4 years ago
|
// Conn is a net.Conn that can additionally have its reads and writes blocked and unblocked.
|
||
5 years ago
|
type Conn interface {
|
||
4 years ago
|
net.Conn
|
||
5 years ago
|
|
||
|
// SetReadBlock blocks or unblocks the Read method of this Conn.
|
||
|
// It reports an error if the existing value matches the new value,
|
||
|
// or if the Conn has been Closed.
|
||
|
SetReadBlock(bool) error
|
||
|
|
||
|
// SetWriteBlock blocks or unblocks the Write method of this Conn.
|
||
|
// It reports an error if the existing value matches the new value,
|
||
|
// or if the Conn has been Closed.
|
||
|
SetWriteBlock(bool) error
|
||
|
}
|
||
|
|
||
|
// NewConn creates a pair of Conns that are wired together by pipes.
|
||
|
func NewConn(name string, maxBuf int) (Conn, Conn) {
|
||
|
r := NewPipe(name+"|0", maxBuf)
|
||
|
w := NewPipe(name+"|1", maxBuf)
|
||
|
|
||
|
return &connHalf{r: r, w: w}, &connHalf{r: w, w: r}
|
||
|
}
|
||
|
|
||
2 years ago
|
// NewTCPConn creates a pair of Conns that are wired together by pipes.
|
||
|
func NewTCPConn(src, dst netip.AddrPort, maxBuf int) (local Conn, remote Conn) {
|
||
|
r := NewPipe(src.String(), maxBuf)
|
||
|
w := NewPipe(dst.String(), maxBuf)
|
||
|
|
||
|
lAddr := net.TCPAddrFromAddrPort(src)
|
||
|
rAddr := net.TCPAddrFromAddrPort(dst)
|
||
|
|
||
|
return &connHalf{r: r, w: w, remote: rAddr, local: lAddr}, &connHalf{r: w, w: r, remote: lAddr, local: rAddr}
|
||
|
}
|
||
|
|
||
4 years ago
|
type connAddr string
|
||
|
|
||
|
func (a connAddr) Network() string { return "mem" }
|
||
|
func (a connAddr) String() string { return string(a) }
|
||
|
|
||
5 years ago
|
type connHalf struct {
|
||
2 years ago
|
local, remote net.Addr
|
||
|
r, w *Pipe
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
func (c *connHalf) LocalAddr() net.Addr {
|
||
2 years ago
|
if c.local != nil {
|
||
|
return c.local
|
||
|
}
|
||
4 years ago
|
return connAddr(c.r.name)
|
||
|
}
|
||
|
|
||
|
func (c *connHalf) RemoteAddr() net.Addr {
|
||
2 years ago
|
if c.remote != nil {
|
||
|
return c.remote
|
||
|
}
|
||
4 years ago
|
return connAddr(c.w.name)
|
||
|
}
|
||
|
|
||
5 years ago
|
func (c *connHalf) Read(b []byte) (n int, err error) {
|
||
|
return c.r.Read(b)
|
||
|
}
|
||
|
func (c *connHalf) Write(b []byte) (n int, err error) {
|
||
|
return c.w.Write(b)
|
||
|
}
|
||
4 years ago
|
|
||
5 years ago
|
func (c *connHalf) Close() error {
|
||
4 years ago
|
if err := c.w.Close(); err != nil {
|
||
|
return err
|
||
5 years ago
|
}
|
||
4 years ago
|
return c.r.Close()
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
5 years ago
|
func (c *connHalf) SetDeadline(t time.Time) error {
|
||
|
err1 := c.SetReadDeadline(t)
|
||
|
err2 := c.SetWriteDeadline(t)
|
||
|
if err1 != nil {
|
||
|
return err1
|
||
|
}
|
||
|
return err2
|
||
|
}
|
||
|
func (c *connHalf) SetReadDeadline(t time.Time) error {
|
||
|
return c.r.SetReadDeadline(t)
|
||
|
}
|
||
|
func (c *connHalf) SetWriteDeadline(t time.Time) error {
|
||
|
return c.w.SetWriteDeadline(t)
|
||
|
}
|
||
4 years ago
|
|
||
5 years ago
|
func (c *connHalf) SetReadBlock(b bool) error {
|
||
|
if b {
|
||
|
return c.r.Block()
|
||
|
}
|
||
|
return c.r.Unblock()
|
||
|
}
|
||
|
func (c *connHalf) SetWriteBlock(b bool) error {
|
||
|
if b {
|
||
|
return c.w.Block()
|
||
|
}
|
||
|
return c.w.Unblock()
|
||
|
}
|