Browse Source

add tlsConnect function

master
Philipp Balzarek 12 years ago
parent
commit
bd90cbe903
  1. 6
      source/Network/Xmpp.hs
  2. 50
      source/Network/Xmpp/Stream.hs
  3. 21
      source/Network/Xmpp/Tls.hs
  4. 2
      source/Network/Xmpp/Types.hs

6
source/Network/Xmpp.hs

@ -32,6 +32,8 @@ module Network.Xmpp
, StreamConfiguration(..) , StreamConfiguration(..)
, SessionConfiguration(..) , SessionConfiguration(..)
, ConnectionDetails(..) , ConnectionDetails(..)
, closeConnection
, endSession
-- TODO: Close session, etc. -- TODO: Close session, etc.
-- ** Authentication handlers -- ** Authentication handlers
-- | The use of 'scramSha1' is /recommended/, but 'digestMd5' might be -- | The use of 'scramSha1' is /recommended/, but 'digestMd5' might be
@ -39,8 +41,6 @@ module Network.Xmpp
, scramSha1 , scramSha1
, plain , plain
, digestMd5 , digestMd5
, closeConnection
, endSession
-- * Addressing -- * Addressing
-- | A JID (historically: Jabber ID) is XMPPs native format -- | A JID (historically: Jabber ID) is XMPPs native format
-- for addressing entities in the network. It is somewhat similar to an e-mail -- for addressing entities in the network. It is somewhat similar to an e-mail
@ -184,6 +184,7 @@ module Network.Xmpp
, AuthOtherFailure ) , AuthOtherFailure )
, SaslHandler , SaslHandler
, ConnectionState(..) , ConnectionState(..)
, connectTls
) where ) where
import Network.Xmpp.Concurrent import Network.Xmpp.Concurrent
@ -191,3 +192,4 @@ import Network.Xmpp.Sasl
import Network.Xmpp.Sasl.Types import Network.Xmpp.Sasl.Types
import Network.Xmpp.Stanza import Network.Xmpp.Stanza
import Network.Xmpp.Types import Network.Xmpp.Types
import Network.Xmpp.Tls

50
source/Network/Xmpp/Stream.hs

@ -570,42 +570,42 @@ connect realm config = do
liftIO $ hSetBuffering h' NoBuffering liftIO $ hSetBuffering h' NoBuffering
return . Just $ handleToStreamHandle h' return . Just $ handleToStreamHandle h'
UseSrv host -> do UseSrv host -> do
h <- connectSrv host h <- connectSrv (resolvConf config) host
case h of case h of
Nothing -> return Nothing Nothing -> return Nothing
Just h' -> do Just h' -> do
liftIO $ hSetBuffering h' NoBuffering liftIO $ hSetBuffering h' NoBuffering
return . Just $ handleToStreamHandle h' return . Just $ handleToStreamHandle h'
UseRealm -> do UseRealm -> do
h <- connectSrv realm h <- connectSrv (resolvConf config) realm
case h of case h of
Nothing -> return Nothing Nothing -> return Nothing
Just h' -> do Just h' -> do
liftIO $ hSetBuffering h' NoBuffering liftIO $ hSetBuffering h' NoBuffering
return . Just $ handleToStreamHandle h' return . Just $ handleToStreamHandle h'
UseConnection mkC -> Just <$> liftIO mkC UseConnection mkC -> Just <$> mkC
where connectSrv :: ResolvConf -> String -> ErrorT XmppFailure IO (Maybe Handle)
connectSrv host = do connectSrv config host = do
case checkHostName (Text.pack host) of case checkHostName (Text.pack host) of
Just host' -> do Just host' -> do
resolvSeed <- lift $ makeResolvSeed (resolvConf config) resolvSeed <- lift $ makeResolvSeed config
lift $ debugM "Pontarius.Xmpp" "Performing SRV lookup..." lift $ debugM "Pontarius.Xmpp" "Performing SRV lookup..."
srvRecords <- srvLookup host' resolvSeed srvRecords <- srvLookup host' resolvSeed
case srvRecords of case srvRecords of
Nothing -> do Nothing -> do
lift $ debugM "Pontarius.Xmpp" lift $ debugM "Pontarius.Xmpp"
"No SRV records, using fallback process." "No SRV records, using fallback process."
lift $ resolvAndConnectTcp resolvSeed (BSC8.pack $ host) lift $ resolvAndConnectTcp resolvSeed (BSC8.pack $ host)
5222 5222
Just srvRecords' -> do Just srvRecords' -> do
lift $ debugM "Pontarius.Xmpp" lift $ debugM "Pontarius.Xmpp"
"SRV records found, performing A/AAAA lookups." "SRV records found, performing A/AAAA lookups."
lift $ resolvSrvsAndConnectTcp resolvSeed srvRecords' lift $ resolvSrvsAndConnectTcp resolvSeed srvRecords'
Nothing -> do Nothing -> do
lift $ errorM "Pontarius.Xmpp" lift $ errorM "Pontarius.Xmpp"
"The hostname could not be validated." "The hostname could not be validated."
throwError XmppIllegalTcpDetails throwError XmppIllegalTcpDetails
-- Connects to a list of addresses and ports. Surpresses any exceptions from -- Connects to a list of addresses and ports. Surpresses any exceptions from
-- connectTcp. -- connectTcp.

21
source/Network/Xmpp/Tls.hs

@ -15,6 +15,7 @@ import qualified Data.ByteString.Lazy as BL
import Data.Conduit import Data.Conduit
import Data.IORef import Data.IORef
import Data.XML.Types import Data.XML.Types
import Network.DNS.Resolver (ResolvConf)
import Network.TLS import Network.TLS
import Network.Xmpp.Stream import Network.Xmpp.Stream
import Network.Xmpp.Types import Network.Xmpp.Types
@ -154,3 +155,23 @@ mkReadBuffer recv = do
writeIORef buffer rest writeIORef buffer rest
return result return result
return read' return read'
-- | Connect to an XMPP server and secure the connection with TLS before
-- starting the XMPP streams
connectTls :: ResolvConf -- ^ Resolv conf to use (try defaultResolvConf as a
-- default)
-> TLSParams -- ^ TLS parameters to use when securing the connection
-> String -- ^ Host to use when connecting (will be resolved
-- using SRV records)
-> ErrorT XmppFailure IO StreamHandle
connectTls config params host = do
h <- connectSrv config host >>= \h' -> case h' of
Nothing -> throwError TcpConnectionFailure
Just h'' -> return h''
let hand = handleToStreamHandle h
(_raw, _snk, psh, recv, ctx) <- tlsinit params $ mkBackend hand
return $ StreamHandle { streamSend = catchPush . psh
, streamReceive = recv
, streamFlush = contextFlush ctx
, streamClose = bye ctx >> streamClose hand
}

2
source/Network/Xmpp/Types.hs

@ -1007,7 +1007,7 @@ instance Exception InvalidXmppXml
data ConnectionDetails = UseRealm -- ^ Use realm to resolv host data ConnectionDetails = UseRealm -- ^ Use realm to resolv host
| UseSrv HostName -- ^ Use this hostname for a SRV lookup | UseSrv HostName -- ^ Use this hostname for a SRV lookup
| UseHost HostName PortID -- ^ Use specified host | UseHost HostName PortID -- ^ Use specified host
| UseConnection (IO StreamHandle) | UseConnection (ErrorT XmppFailure IO StreamHandle)
-- | Configuration settings related to the stream. -- | Configuration settings related to the stream.
data StreamConfiguration = data StreamConfiguration =

Loading…
Cancel
Save