]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: linux: do not spam events every second from hack listener
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 11 Mar 2021 16:23:11 +0000 (09:23 -0700)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 11 Mar 2021 16:23:11 +0000 (09:23 -0700)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tun/tun_linux.go

index e0c9878c160eabcf31e749d97d3a7296bb6819d1..46eb171bcd0d38726b589317f98678bb724cc0d0 100644 (file)
@@ -55,6 +55,11 @@ func (tun *NativeTun) routineHackListener() {
        /* This is needed for the detection to work across network namespaces
         * If you are reading this and know a better method, please get in touch.
         */
+       last := 0
+       const (
+               up   = 1
+               down = 2
+       )
        for {
                sysconn, err := tun.tunFile.SyscallConn()
                if err != nil {
@@ -68,13 +73,19 @@ func (tun *NativeTun) routineHackListener() {
                }
                switch err {
                case unix.EINVAL:
-                       // If the tunnel is up, it reports that write() is
-                       // allowed but we provided invalid data.
-                       tun.events <- EventUp
+                       if last != up {
+                               // If the tunnel is up, it reports that write() is
+                               // allowed but we provided invalid data.
+                               tun.events <- EventUp
+                               last = up
+                       }
                case unix.EIO:
-                       // If the tunnel is down, it reports that no I/O
-                       // is possible, without checking our provided data.
-                       tun.events <- EventDown
+                       if last != down {
+                               // If the tunnel is down, it reports that no I/O
+                               // is possible, without checking our provided data.
+                               tun.events <- EventDown
+                               last = down
+                       }
                default:
                        return
                }