From 1006b2f7417939380bde54bcd82c38b996f1e0b7 Mon Sep 17 00:00:00 2001 From: Bin Jin Date: Fri, 17 Mar 2017 00:41:13 +0800 Subject: handle exception in openTun --- src/Network/WireGuard/Foreign/Tun.hs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/Network/WireGuard') 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 -- cgit v1.2.3-59-g8ed1b