commit 00ab7bf0fbdf069a8fa00a70fba3450ae2e49a00 Author: Alex Kotov Date: Sun Dec 27 05:03:17 2020 +0500 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8557949 --- /dev/null +++ b/LICENSE @@ -0,0 +1,30 @@ +Copyright Zesen Qian (c) 2016 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Zesen Qian nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/src/Network/TUNTAP.hsc b/src/Network/TUNTAP.hsc new file mode 100644 index 0000000..21ab025 --- /dev/null +++ b/src/Network/TUNTAP.hsc @@ -0,0 +1,47 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +module Network.TUNTAP where + +import Foreign +import Foreign.C.String + +import Data.Bits + +import System.Posix.IOCtl + +#include +#include +#include +#include + +data Flag = TUN | TAP | NO_PI | MULTI_QUEUE + deriving (Eq) + +data Ifreq = Ifreq { flags :: [Flag] + , name :: String} + +sheet = [ (#{const IFF_TUN}, TUN) + , (#{const IFF_TAP}, TAP) + , (#{const IFF_NO_PI}, NO_PI) + , (#{const IFF_MULTI_QUEUE}, MULTI_QUEUE) + ] + +instance Storable Ifreq where + alignment _ = #{alignment struct ifreq} + sizeOf _ = #{size struct ifreq} + peek ptr = do + flags_ <- #{peek struct ifreq, ifr_flags} ptr :: IO (#{type typeof(((struct ifreq *)0)->ifr_flags)}) + let flags = foldr (\a b -> if ((fst a) .&. flags_) /= 0 then (snd a) : b else b) [] sheet + name <- peekCString $ #{ptr struct ifreq, ifr_name} ptr + return (Ifreq flags name) + poke ptr (Ifreq flags name) = do + #{poke struct ifreq, ifr_flags} ptr + ((foldr (\a b -> if elem (snd a) flags then b .|. (fst a) else b) 0 sheet) :: #{type typeof(((struct ifreq *)0)->ifr_flags)}) + withCStringLen (take maxLen name) $ uncurry (copyArray $ #{ptr struct ifreq, ifr_name} ptr) + where maxLen = #{const IFNAMSIZ} + +--foreign import ccall "tunsetiff" tunsetiff :: CInt + +data TUNSETIFF = TUNSETIFF + +instance IOControl TUNSETIFF Ifreq where + ioctlReq _ = #{const TUNSETIFF} diff --git a/tuntap-simple.cabal b/tuntap-simple.cabal new file mode 100644 index 0000000..a9f655a --- /dev/null +++ b/tuntap-simple.cabal @@ -0,0 +1,26 @@ +name: tuntap-simple +version: 0.1.0.0 +synopsis: A simple tun/tap library +description: Please see README.md +homepage: https://github.com/riaqn/tuntap-simple#readme +license: BSD3 +license-file: LICENSE +author: Zesen Qian +maintainer: haskell@riaqn.org +copyright: GPL3 +category: Network +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +library + hs-source-dirs: src + exposed-modules: Network.TUNTAP + build-depends: base >= 4.7 && < 5 + , ioctl + default-language: Haskell2010 + default-extensions: MultiParamTypeClasses + +source-repository head + type: git + location: https://github.com/riaqn/tuntap-simple