aboutsummaryrefslogtreecommitdiffstats
path: root/src/Network/WireGuard/TunListener.hs
diff options
context:
space:
mode:
authorBin Jin <bjin@ctrl-d.org>2017-03-14 21:18:00 +0800
committerBin Jin <bjin@ctrl-d.org>2017-03-14 21:18:00 +0800
commita60988db82aed029b71531e8a4bc5698fa247c02 (patch)
treebcf9432ac5b0ee14c8e89fb0a4c7e0eece013616 /src/Network/WireGuard/TunListener.hs
parentRemove unused language extension pragma (diff)
downloadwireguard-hs-a60988db82aed029b71531e8a4bc5698fa247c02.tar.xz
wireguard-hs-a60988db82aed029b71531e8a4bc5698fa247c02.zip
Tun: add timestamp for incoming packets
Diffstat (limited to 'src/Network/WireGuard/TunListener.hs')
-rw-r--r--src/Network/WireGuard/TunListener.hs8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/Network/WireGuard/TunListener.hs b/src/Network/WireGuard/TunListener.hs
index 0cc23ec..8e058df 100644
--- a/src/Network/WireGuard/TunListener.hs
+++ b/src/Network/WireGuard/TunListener.hs
@@ -9,6 +9,7 @@ import qualified Data.ByteArray as BA
import Data.Word (Word8)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Ptr (Ptr)
+import System.Posix.Time (epochTime)
import System.Posix.Types (Fd)
import Network.WireGuard.Foreign.Tun
@@ -17,7 +18,7 @@ import Network.WireGuard.Internal.PacketQueue
import Network.WireGuard.Internal.Types
import Network.WireGuard.Internal.Util
-runTunListener :: [Fd] -> PacketQueue TunPacket -> PacketQueue TunPacket -> IO ()
+runTunListener :: [Fd] -> PacketQueue (Time, TunPacket) -> PacketQueue TunPacket -> IO ()
runTunListener fds readTunChan writeTunChan = loop fds []
where
loop [] asyncs = mapM_ wait asyncs
@@ -26,9 +27,10 @@ runTunListener fds readTunChan writeTunChan = loop fds []
withAsync (retryWithBackoff $ handleWrite writeTunChan fd) $ \wt ->
loop rest (rt:wt:asyncs)
-handleRead :: PacketQueue TunPacket -> Fd -> IO ()
+handleRead :: PacketQueue (Time, TunPacket) -> Fd -> IO ()
handleRead readTunChan fd = allocaBytes tunReadBufferLength $ \buf ->
- forever (readTun buf fd >>= atomically . pushPacketQueue readTunChan)
+ forever (((,) <$> epochTime <*> readTun buf fd)
+ >>= atomically . pushPacketQueue readTunChan)
handleWrite :: PacketQueue TunPacket -> Fd -> IO ()
handleWrite writeTunChan fd =