diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-10-08 22:03:32 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-10-08 22:03:32 +0200 |
commit | a72b0f7ae5dda27d839bb317b7c01d11b215e77a (patch) | |
tree | bd70e6fc71574b300e6e928b7887d69c7cf4ddef /src/conn.go | |
parent | 2d856045a0dbfc15d38d738e2a9d159ba2a49a47 (diff) |
Added new UDPBind interface
Diffstat (limited to 'src/conn.go')
-rw-r--r-- | src/conn.go | 87 |
1 files changed, 22 insertions, 65 deletions
diff --git a/src/conn.go b/src/conn.go index 61be3bf..db4020d 100644 --- a/src/conn.go +++ b/src/conn.go @@ -5,6 +5,14 @@ import ( "net" ) +type UDPBind interface { + SetMark(value uint32) error + ReceiveIPv6(buff []byte, end *Endpoint) (int, error) + ReceiveIPv4(buff []byte, end *Endpoint) (int, error) + Send(buff []byte, end *Endpoint) error + Close() error +} + func parseEndpoint(s string) (*net.UDPAddr, error) { // ensure that the host is an IP address @@ -26,19 +34,6 @@ func parseEndpoint(s string) (*net.UDPAddr, error) { return addr, err } -func ListenerClose(l *Listener) (err error) { - if l.active { - err = CloseIPv4Socket(l.sock) - l.active = false - } - return -} - -func (l *Listener) Init() { - l.update = make(chan struct{}, 1) - ListenerClose(l) -} - func ListeningUpdate(device *Device) error { netc := &device.net netc.mutex.Lock() @@ -46,11 +41,7 @@ func ListeningUpdate(device *Device) error { // close existing sockets - if err := ListenerClose(&netc.ipv4); err != nil { - return err - } - - if err := ListenerClose(&netc.ipv6); err != nil { + if err := device.net.bind.Close(); err != nil { return err } @@ -58,45 +49,22 @@ func ListeningUpdate(device *Device) error { if device.tun.isUp.Get() { - // listen on IPv4 - - { - list := &netc.ipv6 - sock, port, err := CreateIPv4Socket(netc.port) - if err != nil { - return err - } - netc.port = port - list.sock = sock - list.active = true - - if err := SetMark(list.sock, netc.fwmark); err != nil { - ListenerClose(list) - return err - } - signalSend(list.update) + // bind to new port + + var err error + netc.bind, netc.port, err = CreateUDPBind(netc.port) + if err != nil { + return err } - // listen on IPv6 - - { - list := &netc.ipv6 - sock, port, err := CreateIPv6Socket(netc.port) - if err != nil { - return err - } - netc.port = port - list.sock = sock - list.active = true - - if err := SetMark(list.sock, netc.fwmark); err != nil { - ListenerClose(list) - return err - } - signalSend(list.update) + // set mark + + err = netc.bind.SetMark(netc.fwmark) + if err != nil { + return err } - // TODO: clear endpoint caches + // TODO: clear endpoint (src) caches } return nil @@ -106,16 +74,5 @@ func ListeningClose(device *Device) error { netc := &device.net netc.mutex.Lock() defer netc.mutex.Unlock() - - if err := ListenerClose(&netc.ipv4); err != nil { - return err - } - signalSend(netc.ipv4.update) - - if err := ListenerClose(&netc.ipv6); err != nil { - return err - } - signalSend(netc.ipv6.update) - - return nil + return netc.bind.Close() } |