Blame Network/Socks5/Conf.hs

Packit fc2124
-- |
Packit fc2124
-- Module      : Network.Socks5.Conf
Packit fc2124
-- License     : BSD-style
Packit fc2124
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
Packit fc2124
-- Stability   : experimental
Packit fc2124
-- Portability : unknown
Packit fc2124
--
Packit fc2124
-- typical SOCKS configuration
Packit fc2124
module Network.Socks5.Conf
Packit fc2124
    ( SocksConf(..)
Packit fc2124
    , socksHost
Packit fc2124
    , socksPort
Packit fc2124
    , defaultSocksConf 
Packit fc2124
    , defaultSocksConfFromSockAddr
Packit fc2124
    ) where
Packit fc2124
Packit fc2124
import Network.Socket
Packit fc2124
import Network.Socks5.Types (SocksAddress(..), SocksHostAddress(..), SocksVersion(..))
Packit fc2124
import qualified Data.ByteString.Char8 as BC
Packit fc2124
Packit fc2124
-- | SOCKS configuration structure.
Packit fc2124
-- this structure will be extended in future to support authentification.
Packit fc2124
-- use defaultSocksConf to create new record.
Packit fc2124
data SocksConf = SocksConf
Packit fc2124
    { socksServer  :: SocksAddress -- ^ SOCKS Address
Packit fc2124
    , socksVersion :: SocksVersion -- ^ SOCKS version to use
Packit fc2124
    }
Packit fc2124
Packit fc2124
-- | SOCKS Host
Packit fc2124
socksHost :: SocksConf -> SocksHostAddress
Packit fc2124
socksHost conf = ha where (SocksAddress ha _) = socksServer conf
Packit fc2124
Packit fc2124
-- | SOCKS Port
Packit fc2124
socksPort :: SocksConf -> PortNumber
Packit fc2124
socksPort conf = port where (SocksAddress _ port) = socksServer conf
Packit fc2124
Packit fc2124
-- | defaultSocksConf create a new record, making sure
Packit fc2124
-- API remains compatible when the record is extended.
Packit fc2124
defaultSocksConf host port = SocksConf server SocksVer5
Packit fc2124
    where server = SocksAddress haddr port
Packit fc2124
          haddr  = SocksAddrDomainName $ BC.pack host
Packit fc2124
Packit fc2124
-- | same as defaultSocksConf except the server address is determined from a 'SockAddr'
Packit fc2124
--
Packit fc2124
-- A unix SockAddr will raises an error. Only Inet and Inet6 types supported
Packit fc2124
defaultSocksConfFromSockAddr sockaddr = SocksConf server SocksVer5
Packit fc2124
    where server       = SocksAddress haddr port
Packit fc2124
          (haddr,port) = case sockaddr of
Packit fc2124
                             SockAddrInet p h      -> (SocksAddrIPV4 h, p)
Packit fc2124
                             SockAddrInet6 p _ h _ -> (SocksAddrIPV6 h, p)
Packit fc2124
                             _                     -> error "unsupported unix sockaddr type"