]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Seperated fwmark update code into function
authorMathias Hall-Andersen <mathias@hall-andersen.dk>
Sun, 18 Feb 2018 19:49:03 +0000 (20:49 +0100)
committerMathias Hall-Andersen <mathias@hall-andersen.dk>
Sun, 18 Feb 2018 19:49:03 +0000 (20:49 +0100)
conn.go
uapi.go

diff --git a/conn.go b/conn.go
index fb30ec28df13869f51fe4e8601684e1cac91c86f..6bb262c6f93af229b3e71a154f51bf71cebcaf40 100644 (file)
--- a/conn.go
+++ b/conn.go
@@ -64,6 +64,32 @@ func unsafeCloseBind(device *Device) error {
        return err
 }
 
+func (device *Device) BindSetMark(mark uint32) error {
+
+       device.net.mutex.Lock()
+       defer device.net.mutex.Unlock()
+
+       device.peers.mutex.Lock()
+       defer device.peers.mutex.Unlock()
+
+       // check if modified
+
+       if device.net.fwmark == mark {
+               return nil
+       }
+
+       // update fwmark on existing bind
+
+       device.net.fwmark = mark
+       if device.isUp.Get() && device.net.bind != nil {
+               if err := device.net.bind.SetMark(mark); err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
 func (device *Device) BindUpdate() error {
 
        device.net.mutex.Lock()
@@ -89,14 +115,17 @@ func (device *Device) BindUpdate() error {
                netc.bind, netc.port, err = CreateBind(netc.port)
                if err != nil {
                        netc.bind = nil
+                       netc.port = 0
                        return err
                }
 
-               // set mark
+               // set fwmark
 
-               err = netc.bind.SetMark(netc.fwmark)
-               if err != nil {
-                       return err
+               if netc.fwmark != 0 {
+                       err = netc.bind.SetMark(netc.fwmark)
+                       if err != nil {
+                               return err
+                       }
                }
 
                // clear cached source addresses
diff --git a/uapi.go b/uapi.go
index caaa49837fdd043a2e10bc759cdd47e1a70df7d3..01cdab632687c91841905b384df8d54c2686e792 100644 (file)
--- a/uapi.go
+++ b/uapi.go
@@ -191,11 +191,7 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
 
                                logDebug.Println("UAPI: Updating fwmark")
 
-                               device.net.mutex.Lock()
-                               device.net.fwmark = uint32(fwmark)
-                               device.net.mutex.Unlock()
-
-                               if err := device.BindUpdate(); err != nil {
+                               if err := device.BindSetMark(uint32(fwmark)); err != nil {
                                        logError.Println("Failed to update fwmark:", err)
                                        return &IPCError{Code: ipcErrorPortInUse}
                                }