tcp: github.com/mikioh/tcp Index | Examples | Files

package tcp

import "github.com/mikioh/tcp"

Package tcp implements TCP-level socket options.

The package provides TCP-level socket options that allow manipulation of TCP connection facilities.

Index

Examples

Package Files

conn.go conn_linux.go doc.go ipv6zone.go rawconn.go sys.go sys_linux.go sys_linux_amd64.go sys_unix.go zsys_linux.go

type Conn Uses

type Conn struct {
    net.Conn
    // contains filtered or unexported fields
}

A Conn represents an end point that uses TCP connection. It allows to set non-portable, platform-dependent TCP-level socket options.

Monitoring a TCP connection

For now only Darwin, FreeBSD, Linux and NetBSD kernels support the TCP information option. A custom net.Dial function that hooks up an underlying transport connection must be prepared before monitoring.

Code:

// When the underlying transport connection is established,
// your monitor goroutine can start monitoring the connection
// by using the Option method of Conn and tcpinfo package.
monitor := func(tc *tcp.Conn) {
    tc.SetOption(tcpopt.KeepAlive(true))
    tc.SetOption(tcpopt.KeepAliveProbeCount(3))
    var o tcpinfo.Info
    var b [256]byte
    for {
        i, err := tc.Option(o.Level(), o.Name(), b[:])
        if err != nil {
            log.Println(err)
            return
        }
        txt, err := json.Marshal(i)
        if err != nil {
            log.Println(err)
            return
        }
        fmt.Println(string(txt))
    }
}

tr := &http.Transport{
    Dial: func(network, address string) (net.Conn, error) {
        d := net.Dialer{DualStack: true}
        c, err := d.Dial(network, address)
        if err != nil {
            return nil, err
        }
        tc, err := tcp.NewConn(c)
        if err != nil {
            c.Close()
            return nil, err
        }
        go monitor(tc)
        return tc.Conn, nil
    },
    TLSClientConfig: &tls.Config{ServerName: "golang.org"},
}
client := http.Client{Transport: tr}
resp, err := client.Get("https://golang.org")
if err != nil {
    log.Fatal(err)
}
resp.Body.Close()

func NewConn Uses

func NewConn(c net.Conn) (*Conn, error)

NewConn returns a new end point.

func (*Conn) Available Uses

func (c *Conn) Available() int

Available returns how many bytes are unused in the underlying socket write buffer. It returns -1 when the platform doesn't support this feature.

func (*Conn) Buffered Uses

func (c *Conn) Buffered() int

Buffered returns the number of bytes that can be read from the underlying socket read buffer. It returns -1 when the platform doesn't support this feature.

func (*Conn) Option Uses

func (c *Conn) Option(level, name int, b []byte) (tcpopt.Option, error)

Option returns a socket option.

func (*Conn) OriginalDst Uses

func (c *Conn) OriginalDst() (net.Addr, error)

OriginalDst returns an original destination address, which is an address not modified by intermediate entities such as network address and port translators inside the kernel, on the connection.

Only Linux and BSD variants using PF support this feature.

func (*Conn) SetOption Uses

func (c *Conn) SetOption(o tcpopt.Option) error

SetOption sets a socket option.

Package tcp imports 12 packages (graph) and is imported by 1 packages. Updated 2019-03-15. Refresh now. Tools for package owners.