From 90e333a6f46f9aa172598220478eed7c76943177 Mon Sep 17 00:00:00 2001 From: Ain Ghazal Date: Sun, 11 Feb 2024 20:55:09 +0100 Subject: [PATCH] document public methods --- internal/tun/tun.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/tun/tun.go b/internal/tun/tun.go index 8b7878db..5e6c9dc6 100644 --- a/internal/tun/tun.go +++ b/internal/tun/tun.go @@ -127,6 +127,8 @@ func (t *TUN) whenDone(fn func()) { t.whenDoneFn = fn } +// Close is an idempotent method that closes the underlying connection (owned by us) and +// potentially executes any registed callback. func (t *TUN) Close() error { t.closeOnce.Do(func() { close(t.hangup) @@ -138,6 +140,7 @@ func (t *TUN) Close() error { return nil } +// Read implements net.Conn func (t *TUN) Read(data []byte) (int, error) { for { count, _ := t.readBuffer.Read(data) @@ -159,6 +162,7 @@ func (t *TUN) Read(data []byte) (int, error) { } } +// Write implements net.Conn func (t *TUN) Write(data []byte) (int, error) { if isClosedChan(t.writeDeadline.wait()) { return 0, os.ErrDeadlineExceeded @@ -173,27 +177,32 @@ func (t *TUN) Write(data []byte) (int, error) { } } +// LocalAddr implements net.Conn func (t *TUN) LocalAddr() net.Addr { ip := t.session.TunnelInfo().IP return &tunBioAddr{ip, t.network} } +// RemoteAddr implements net.Conn func (t *TUN) RemoteAddr() net.Addr { gw := t.session.TunnelInfo().GW return &tunBioAddr{gw, t.network} } +// SetDeadline implements net.Conn func (t *TUN) SetDeadline(tm time.Time) error { t.readDeadline.set(tm) t.writeDeadline.set(tm) return nil } +// SetReadDeadline implements net.Conn func (t *TUN) SetReadDeadline(tm time.Time) error { t.readDeadline.set(tm) return nil } +// SetWriteDeadline implements net.Conn func (t *TUN) SetWriteDeadline(tm time.Time) error { t.writeDeadline.set(tm) return nil @@ -218,6 +227,7 @@ func (t *tunBioAddr) String() string { return t.addr } +// NetMask returns the configured net mask for the TUN interface. func (t *TUN) NetMask() net.IPMask { return net.IPMask(net.ParseIP(t.session.TunnelInfo().NetMask)) }