Initial commit
This commit is contained in:
commit
00ab7bf0fb
4 changed files with 105 additions and 0 deletions
30
LICENSE
Normal file
30
LICENSE
Normal file
|
@ -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.
|
2
Setup.hs
Normal file
2
Setup.hs
Normal file
|
@ -0,0 +1,2 @@
|
|||
import Distribution.Simple
|
||||
main = defaultMain
|
47
src/Network/TUNTAP.hsc
Normal file
47
src/Network/TUNTAP.hsc
Normal file
|
@ -0,0 +1,47 @@
|
|||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
module Network.TUNTAP where
|
||||
|
||||
import Foreign
|
||||
import Foreign.C.String
|
||||
|
||||
import Data.Bits
|
||||
|
||||
import System.Posix.IOCtl
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
|
||||
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}
|
26
tuntap-simple.cabal
Normal file
26
tuntap-simple.cabal
Normal file
|
@ -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
|
Reference in a new issue