aboutsummaryrefslogtreecommitdiffstats
path: root/src/Network/WireGuard
diff options
context:
space:
mode:
authorBin Jin <bjin@ctrl-d.org>2017-03-17 00:41:13 +0800
committerBin Jin <bjin@ctrl-d.org>2017-03-17 00:41:13 +0800
commit1006b2f7417939380bde54bcd82c38b996f1e0b7 (patch)
tree133f914c83e604e2e0a180042c8b91a07dd93837 /src/Network/WireGuard
parentadd flag for static build (diff)
downloadwireguard-hs-1006b2f7417939380bde54bcd82c38b996f1e0b7.tar.xz
wireguard-hs-1006b2f7417939380bde54bcd82c38b996f1e0b7.zip
handle exception in openTun
Diffstat (limited to 'src/Network/WireGuard')
-rw-r--r--src/Network/WireGuard/Foreign/Tun.hs14
1 files changed, 6 insertions, 8 deletions
diff --git a/src/Network/WireGuard/Foreign/Tun.hs b/src/Network/WireGuard/Foreign/Tun.hs
index 2d0f929..3fdbbd4 100644
--- a/src/Network/WireGuard/Foreign/Tun.hs
+++ b/src/Network/WireGuard/Foreign/Tun.hs
@@ -14,17 +14,15 @@ import System.Posix.Types (Fd (..))
import Foreign
import Foreign.C
-openTun :: String -> Int -> IO (Maybe [Fd])
+openTun :: String -> Int -> IO [Fd]
openTun intfName threads =
withCString intfName $ \intf_name_c ->
allocaArray threads $ \fds_c -> do
- res <- tun_alloc_c intf_name_c (fromIntegral threads) fds_c -- TODO: handle exception
- if res > 0
- then do
- fds <- peekArray (fromIntegral res) fds_c
- forM_ fds $ \fd -> setNonBlockingFD fd True
- return (Just (map Fd fds))
- else return Nothing
+ res <- throwErrnoIfMinus1Retry "openTun" $
+ tun_alloc_c intf_name_c (fromIntegral threads) fds_c
+ fds <- peekArray (fromIntegral res) fds_c
+ forM_ fds $ \fd -> setNonBlockingFD fd True
+ return (map Fd fds)
tunReadBuf :: Fd -> Ptr Word8 -> CSize -> IO CSize
tunReadBuf _fd _buf 0 = return 0