1-- |
2-- Module      : Network.Connection.Types
3-- License     : BSD-style
4-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
5-- Stability   : experimental
6-- Portability : portable
7--
8-- connection types
9--
10module Network.Connection.Types
11    where
12
13import Control.Concurrent.MVar (MVar)
14
15import Data.Default.Class
16import Data.X509.CertificateStore
17import Data.ByteString (ByteString)
18
19import Network.Socket (PortNumber, Socket)
20import qualified Network.TLS as TLS
21
22import System.IO (Handle)
23
24-- | Simple backend enumeration, either using a raw connection or a tls connection.
25data ConnectionBackend = ConnectionStream Handle
26                       | ConnectionSocket Socket
27                       | ConnectionTLS TLS.Context
28
29
30-- | Hostname This could either be a name string (punycode encoded) or an ipv4/ipv6
31type HostName = String
32
33-- | Connection Parameters to establish a Connection.
34--
35-- The strict minimum is an hostname and the port.
36--
37-- If you need to establish a TLS connection, you should make sure
38-- connectionUseSecure is correctly set.
39--
40-- If you need to connect through a SOCKS, you should make sure
41-- connectionUseSocks is correctly set.
42data ConnectionParams = ConnectionParams
43    { connectionHostname   :: HostName           -- ^ host name to connect to.
44    , connectionPort       :: PortNumber         -- ^ port number to connect to.
45    , connectionUseSecure  :: Maybe TLSSettings  -- ^ optional TLS parameters.
46    , connectionUseSocks   :: Maybe ProxySettings -- ^ optional Proxy/Socks configuration.
47    }
48
49-- | Proxy settings for the connection.
50--
51-- OtherProxy handles specific application-level proxies like HTTP proxies.
52--
53-- The simple SOCKS settings is just the hostname and portnumber of the SOCKS proxy server.
54--
55-- That's for now the only settings in the SOCKS package,
56-- socks password, or any sort of other authentications is not yet implemented.
57data ProxySettings =
58      SockSettingsSimple HostName PortNumber
59    | SockSettingsEnvironment (Maybe String)
60    | OtherProxy HostName PortNumber
61
62type SockSettings = ProxySettings
63
64-- | TLS Settings that can be either expressed as simple settings,
65-- or as full blown TLS.Params settings.
66--
67-- Unless you need access to parameters that are not accessible through the
68-- simple settings, you should use TLSSettingsSimple.
69data TLSSettings
70    = TLSSettingsSimple
71             { settingDisableCertificateValidation :: Bool -- ^ Disable certificate verification completely,
72                                                           --   this make TLS/SSL vulnerable to a MITM attack.
73                                                           --   not recommended to use, but for testing.
74             , settingDisableSession               :: Bool -- ^ Disable session management. TLS/SSL connections
75                                                           --   will always re-established their context.
76                                                           --   Not Implemented Yet.
77             , settingUseServerName                :: Bool -- ^ Use server name extension. Not Implemented Yet.
78             } -- ^ Simple TLS settings. recommended to use.
79    | TLSSettings TLS.ClientParams -- ^ full blown TLS Settings directly using TLS.Params. for power users.
80    deriving (Show)
81
82instance Default TLSSettings where
83    def = TLSSettingsSimple False False False
84
85type ConnectionID = (HostName, PortNumber)
86
87-- | This opaque type represent a connection to a destination.
88data Connection = Connection
89    { connectionBackend :: MVar ConnectionBackend
90    , connectionBuffer  :: MVar (Maybe ByteString) -- ^ this is set to 'Nothing' on EOF
91    , connectionID      :: ConnectionID  -- ^ return a simple tuple of the port and hostname that we're connected to.
92    }
93
94-- | Shared values (certificate store, sessions, ..) between connections
95--
96-- At the moment, this is only strictly needed to shared sessions and certificates
97-- when using a TLS enabled connection.
98data ConnectionContext = ConnectionContext
99    { globalCertificateStore :: !CertificateStore
100    }
101