2020-08-04 20:24:28 +03:00
|
|
|
{-# LANGUAGE ForeignFunctionInterface, DeriveDataTypeable #-}
|
|
|
|
|
|
|
|
-- | This module is a thin wrapper above libnatpmp.h and getgateway.h.
|
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
module Network.NatPmp (Error(..),
|
2020-08-05 22:33:37 +03:00
|
|
|
NatPmpResponse(..),
|
|
|
|
ProtocolType(..),
|
|
|
|
NatPmpHandle,
|
2020-08-13 17:08:02 +03:00
|
|
|
Port,
|
|
|
|
LifetimeSeconds,
|
2020-08-05 22:33:37 +03:00
|
|
|
initNatPmp,
|
|
|
|
closeNatPmp,
|
|
|
|
getDefaultGateway,
|
|
|
|
getPublicAddress,
|
|
|
|
setPortMapping) where
|
2020-08-04 20:24:28 +03:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <getgateway.h>
|
|
|
|
#include <natpmp.h>
|
|
|
|
#include <binding.h>
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
import Foreign
|
|
|
|
import Foreign.C
|
|
|
|
import Network.Socket
|
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
import Control.Monad.IO.Unlift (MonadIO(..))
|
2020-08-04 20:24:28 +03:00
|
|
|
|
|
|
|
-- Opaque type for the internals of nat pmp
|
|
|
|
data NatPmpStruct
|
|
|
|
type NatPmpHandle = Ptr NatPmpStruct
|
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
type Port = Word16
|
|
|
|
type LifetimeSeconds = Word32
|
|
|
|
|
2020-08-04 20:24:28 +03:00
|
|
|
-- The response type, in its internal form. This struct is a C tagged union
|
|
|
|
-- with additional data, but we need to read and write from its C form.
|
|
|
|
data NatPmpResponse
|
|
|
|
= NatPmpResponsePublicAddress HostAddress
|
2020-08-13 17:08:02 +03:00
|
|
|
| NatPmpResponseUdpPortMapping Port Port LifetimeSeconds
|
|
|
|
| NatPmpResponseTcpPortMapping Port Port LifetimeSeconds
|
2020-08-04 20:24:28 +03:00
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance Storable NatPmpResponse where
|
|
|
|
sizeOf _ = #{size natpmpresp_t}
|
|
|
|
alignment _ = alignment (undefined :: CString)
|
|
|
|
|
|
|
|
peek p = do
|
|
|
|
t <- uintToEnum <$> (#{peek natpmpresp_t, type} p)
|
|
|
|
case t of
|
|
|
|
RTPublicAddress ->
|
|
|
|
NatPmpResponsePublicAddress <$>
|
|
|
|
(#{peek natpmpresp_t, pnu.publicaddress.addr} p)
|
2020-08-13 17:08:02 +03:00
|
|
|
RTUdpPortMapping ->
|
|
|
|
NatPmpResponseUdpPortMapping
|
2020-08-04 20:24:28 +03:00
|
|
|
<$> (#{peek natpmpresp_t, pnu.newportmapping.privateport} p)
|
|
|
|
<*> (#{peek natpmpresp_t, pnu.newportmapping.mappedpublicport} p)
|
|
|
|
<*> (#{peek natpmpresp_t, pnu.newportmapping.lifetime} p)
|
2020-08-13 17:08:02 +03:00
|
|
|
RTTcpPortMapping ->
|
|
|
|
NatPmpResponseTcpPortMapping
|
2020-08-04 20:24:28 +03:00
|
|
|
<$> (#{peek natpmpresp_t, pnu.newportmapping.privateport} p)
|
|
|
|
<*> (#{peek natpmpresp_t, pnu.newportmapping.mappedpublicport} p)
|
|
|
|
<*> (#{peek natpmpresp_t, pnu.newportmapping.lifetime} p)
|
|
|
|
|
|
|
|
poke _ _ = error "Responses are an output data structure; poke makes no sense"
|
|
|
|
|
|
|
|
type NatPmpResponseHandle = Ptr NatPmpResponse
|
|
|
|
|
|
|
|
foreign import ccall unsafe "getgateway.h getdefaultgateway" _get_default_gateway :: Ptr CUInt -> IO CInt
|
|
|
|
|
|
|
|
foreign import ccall unsafe "natpmp.h initnatpmp" _init_nat_pmp :: NatPmpHandle -> CInt -> CInt -> IO CInt
|
|
|
|
foreign import ccall unsafe "natpmp.h closenatpmp" _close_nat_pmp :: NatPmpHandle -> IO CInt
|
|
|
|
foreign import ccall unsafe "natpmp.h sendpublicaddressrequest" sendPublicAddressRequest :: NatPmpHandle -> IO CInt
|
|
|
|
foreign import ccall unsafe "natpmp.h sendnewportmappingrequest" sendNewPortMappingRequest :: NatPmpHandle -> CInt -> CUShort -> CUShort -> CUInt -> IO CInt
|
|
|
|
|
|
|
|
foreign import ccall unsafe "binding.h readNatResponseSynchronously" readNatResponseSynchronously :: NatPmpHandle -> NatPmpResponseHandle -> IO CInt
|
|
|
|
|
|
|
|
-- Give the type system some help
|
|
|
|
_peekCUInt :: Ptr CUInt -> IO CUInt
|
|
|
|
_peekCUInt = peek
|
|
|
|
|
|
|
|
uintToEnum :: Enum e => CUInt -> e
|
|
|
|
uintToEnum = toEnum . fromIntegral
|
|
|
|
|
|
|
|
intToEnum :: Enum e => CInt -> e
|
|
|
|
intToEnum = toEnum . fromIntegral
|
|
|
|
|
|
|
|
|
|
|
|
-- Fetches the default gateway as an ipv4 address
|
|
|
|
getDefaultGateway :: IO (Maybe HostAddress)
|
|
|
|
getDefaultGateway =
|
|
|
|
alloca $ \(pReturnAddr :: Ptr CUInt) -> do
|
2020-08-13 18:49:38 +03:00
|
|
|
_get_default_gateway pReturnAddr >>= \case
|
2020-08-04 20:24:28 +03:00
|
|
|
0 -> (Just . fromIntegral) <$> _peekCUInt pReturnAddr
|
|
|
|
_ -> pure Nothing
|
|
|
|
|
|
|
|
|
|
|
|
data RespType
|
|
|
|
= RTPublicAddress
|
2020-08-13 17:08:02 +03:00
|
|
|
| RTUdpPortMapping
|
|
|
|
| RTTcpPortMapping
|
2020-08-04 20:24:28 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance Enum RespType where
|
|
|
|
fromEnum RTPublicAddress = 0
|
2020-08-13 17:08:02 +03:00
|
|
|
fromEnum RTUdpPortMapping = 1
|
|
|
|
fromEnum RTTcpPortMapping = 2
|
2020-08-04 20:24:28 +03:00
|
|
|
|
|
|
|
toEnum 0 = RTPublicAddress
|
2020-08-13 17:08:02 +03:00
|
|
|
toEnum 1 = RTUdpPortMapping
|
|
|
|
toEnum 2 = RTTcpPortMapping
|
2020-08-04 20:24:28 +03:00
|
|
|
toEnum unmatched = error ("RespType.toEnum: Cannot match " ++ show unmatched)
|
|
|
|
|
2020-08-10 21:55:08 +03:00
|
|
|
|
2020-08-04 20:24:28 +03:00
|
|
|
data ProtocolType
|
2020-08-13 17:08:02 +03:00
|
|
|
= PTUdp
|
|
|
|
| PTTcp
|
2020-08-04 20:24:28 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance Enum ProtocolType where
|
2020-08-13 17:08:02 +03:00
|
|
|
fromEnum PTUdp = 1
|
|
|
|
fromEnum PTTcp = 2
|
2020-08-04 20:24:28 +03:00
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
toEnum 1 = PTUdp
|
|
|
|
toEnum 2 = PTTcp
|
2020-08-04 20:24:28 +03:00
|
|
|
toEnum x = error ("ProtocolType.toEnum: Cannot match " ++ show x)
|
|
|
|
|
|
|
|
|
|
|
|
data Error
|
|
|
|
= ErrInvalidArgs
|
|
|
|
| ErrSocketError
|
|
|
|
| ErrCannotGetGateway
|
|
|
|
| ErrCloseErr
|
|
|
|
| ErrRecvFrom
|
|
|
|
| ErrNoPendingReq
|
|
|
|
| ErrNoGatewaySupport
|
|
|
|
| ErrConnectErr
|
|
|
|
| ErrWrongPacketSource
|
|
|
|
| ErrSendErr
|
|
|
|
| ErrFcntlError
|
|
|
|
| ErrGetTimeOfDayError
|
|
|
|
--
|
|
|
|
| ErrUnsuportedVersion
|
|
|
|
| ErrUnsupportedOpcode
|
|
|
|
--
|
|
|
|
| ErrUndefinedError
|
|
|
|
| ErrNotAuthorized
|
|
|
|
| ErrNetworkFailure
|
|
|
|
| ErrOutOfResources
|
|
|
|
--
|
|
|
|
| ErrTryAgain
|
2020-08-13 18:49:38 +03:00
|
|
|
| ErrHaskellBindings
|
2020-08-04 20:24:28 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance Enum Error where
|
|
|
|
fromEnum ErrInvalidArgs = -1
|
|
|
|
fromEnum ErrSocketError = -2
|
|
|
|
fromEnum ErrCannotGetGateway = -3
|
|
|
|
fromEnum ErrCloseErr = -4
|
|
|
|
fromEnum ErrRecvFrom = -5
|
|
|
|
fromEnum ErrNoPendingReq = -6
|
|
|
|
fromEnum ErrNoGatewaySupport = -7
|
|
|
|
fromEnum ErrConnectErr = -8
|
|
|
|
fromEnum ErrWrongPacketSource = -9
|
|
|
|
fromEnum ErrSendErr = -10
|
|
|
|
fromEnum ErrFcntlError = -11
|
|
|
|
fromEnum ErrGetTimeOfDayError = -12
|
|
|
|
--
|
|
|
|
fromEnum ErrUnsuportedVersion = -14
|
|
|
|
fromEnum ErrUnsupportedOpcode = -15
|
|
|
|
--
|
|
|
|
fromEnum ErrUndefinedError = -49
|
|
|
|
fromEnum ErrNotAuthorized = -51
|
|
|
|
fromEnum ErrNetworkFailure = -52
|
|
|
|
fromEnum ErrOutOfResources = -53
|
|
|
|
--
|
|
|
|
fromEnum ErrTryAgain = -100
|
2020-08-13 18:49:38 +03:00
|
|
|
fromEnum ErrHaskellBindings = -200
|
2020-08-04 20:24:28 +03:00
|
|
|
|
|
|
|
toEnum (-1) = ErrInvalidArgs
|
|
|
|
toEnum (-2) = ErrSocketError
|
|
|
|
toEnum (-3) = ErrCannotGetGateway
|
|
|
|
toEnum (-4) = ErrCloseErr
|
|
|
|
toEnum (-5) = ErrRecvFrom
|
|
|
|
toEnum (-6) = ErrNoPendingReq
|
|
|
|
toEnum (-7) = ErrNoGatewaySupport
|
|
|
|
toEnum (-8) = ErrConnectErr
|
|
|
|
toEnum (-9) = ErrWrongPacketSource
|
|
|
|
toEnum (-10) = ErrSendErr
|
|
|
|
toEnum (-11) = ErrFcntlError
|
|
|
|
toEnum (-12) = ErrGetTimeOfDayError
|
|
|
|
--
|
|
|
|
toEnum (-14) = ErrUnsuportedVersion
|
|
|
|
toEnum (-15) = ErrUnsupportedOpcode
|
|
|
|
--
|
|
|
|
toEnum (-49) = ErrUndefinedError
|
|
|
|
toEnum (-51) = ErrNotAuthorized
|
|
|
|
toEnum (-52) = ErrNetworkFailure
|
|
|
|
toEnum (-53) = ErrOutOfResources
|
|
|
|
--
|
|
|
|
toEnum (-100) = ErrTryAgain
|
2020-08-13 18:49:38 +03:00
|
|
|
toEnum (-200) = ErrHaskellBindings
|
2020-08-04 20:24:28 +03:00
|
|
|
toEnum unmatched = error ("Error.toEnum: Cannot match " ++ show unmatched)
|
|
|
|
|
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
initNatPmp :: MonadIO m => m (Either Error NatPmpHandle)
|
2020-08-13 18:12:26 +03:00
|
|
|
initNatPmp = liftIO do
|
|
|
|
natpmp <- mallocBytes #{size natpmp_t}
|
|
|
|
ret <- _init_nat_pmp natpmp 0 0
|
2020-08-04 20:24:28 +03:00
|
|
|
case ret of
|
|
|
|
0 -> pure $ Right natpmp
|
|
|
|
_ -> do
|
2020-08-13 18:12:26 +03:00
|
|
|
free natpmp
|
2020-08-04 20:24:28 +03:00
|
|
|
pure $ Left $ intToEnum ret
|
|
|
|
|
2020-08-10 21:55:08 +03:00
|
|
|
|
2020-08-13 17:08:02 +03:00
|
|
|
closeNatPmp :: MonadIO m => NatPmpHandle -> m (Either Error ())
|
2020-08-13 18:12:26 +03:00
|
|
|
closeNatPmp handle = liftIO do
|
|
|
|
ret <- _close_nat_pmp handle
|
|
|
|
free handle
|
2020-08-04 20:24:28 +03:00
|
|
|
case ret of
|
|
|
|
0 -> pure $ Right ()
|
|
|
|
_ -> pure $ Left $ intToEnum ret
|
|
|
|
|
|
|
|
|
2020-08-13 18:49:38 +03:00
|
|
|
-- | Public interface for getting the public IPv4 address
|
2020-08-13 17:08:02 +03:00
|
|
|
getPublicAddress :: MonadIO m => NatPmpHandle -> m (Either Error HostAddress)
|
2020-08-13 18:12:26 +03:00
|
|
|
getPublicAddress natpmp = liftIO do
|
|
|
|
sendRetcode <- sendPublicAddressRequest natpmp
|
2020-08-04 20:24:28 +03:00
|
|
|
case sendRetcode of
|
2020-08-13 18:12:26 +03:00
|
|
|
2 -> alloca $ \(pResponse :: NatPmpResponseHandle) -> do
|
2020-08-04 20:24:28 +03:00
|
|
|
respRetcode <- readNatResponseSynchronously natpmp pResponse
|
|
|
|
case respRetcode of
|
|
|
|
0 -> peek pResponse >>= \case
|
|
|
|
NatPmpResponsePublicAddress addr -> pure $ Right addr
|
2020-08-13 18:49:38 +03:00
|
|
|
_ -> pure $ Left ErrHaskellBindings
|
2020-08-04 20:24:28 +03:00
|
|
|
_ -> pure $ Left $ intToEnum respRetcode
|
|
|
|
_ -> pure $ Left $ intToEnum sendRetcode
|
|
|
|
|
2020-08-13 18:49:38 +03:00
|
|
|
-- | Requests that the router maps the privatePort on our local computer in our
|
|
|
|
-- private network to publicPort on the public internet.
|
2020-08-13 17:08:02 +03:00
|
|
|
setPortMapping :: MonadIO m
|
|
|
|
=> NatPmpHandle
|
|
|
|
-> ProtocolType
|
|
|
|
-> Port
|
|
|
|
-> Port
|
|
|
|
-> LifetimeSeconds
|
2020-08-10 20:15:03 +03:00
|
|
|
-> m (Either Error ())
|
2020-08-13 18:12:26 +03:00
|
|
|
setPortMapping natpmp protocol privatePort publicPort lifetime = liftIO do
|
2020-08-04 20:24:28 +03:00
|
|
|
let protocolNum = fromEnum protocol
|
|
|
|
sendResp <-
|
2020-08-13 18:12:26 +03:00
|
|
|
sendNewPortMappingRequest natpmp
|
2020-08-13 18:49:38 +03:00
|
|
|
(fromIntegral protocolNum) (CUShort privatePort) (CUShort publicPort)
|
|
|
|
(CUInt lifetime)
|
2020-08-04 20:24:28 +03:00
|
|
|
|
|
|
|
case sendResp of
|
2020-08-13 18:12:26 +03:00
|
|
|
12 -> alloca $ \(pResponse :: NatPmpResponseHandle) -> do
|
2020-08-04 20:24:28 +03:00
|
|
|
respRetcode <- readNatResponseSynchronously natpmp pResponse
|
|
|
|
case respRetcode of
|
|
|
|
0 -> peek pResponse >>= \case
|
2020-08-13 17:08:02 +03:00
|
|
|
NatPmpResponseUdpPortMapping _ _ _ -> pure $ Right ()
|
|
|
|
NatPmpResponseTcpPortMapping _ _ _ -> pure $ Right ()
|
2020-08-13 18:49:38 +03:00
|
|
|
_ -> pure $ Left ErrHaskellBindings
|
2020-08-04 20:24:28 +03:00
|
|
|
_ -> pure $ Left $ intToEnum respRetcode
|
|
|
|
x -> pure $ Left $ intToEnum x
|