diff options
-rw-r--r-- | Presence/Server.hs | 7 | ||||
-rw-r--r-- | Presence/SockAddr.hs | 13 | ||||
-rw-r--r-- | Presence/XMPPServer.hs | 22 | ||||
-rw-r--r-- | xmppServer.hs | 53 |
4 files changed, 18 insertions, 77 deletions
diff --git a/Presence/Server.hs b/Presence/Server.hs index abf9f5e3..fd5f1e43 100644 --- a/Presence/Server.hs +++ b/Presence/Server.hs | |||
@@ -63,16 +63,11 @@ import Network.BSD | |||
63 | import Debug.Trace | 63 | import Debug.Trace |
64 | import Data.Time.Clock (UTCTime,getCurrentTime,diffUTCTime) | 64 | import Data.Time.Clock (UTCTime,getCurrentTime,diffUTCTime) |
65 | import Data.Time.Format (formatTime) | 65 | import Data.Time.Format (formatTime) |
66 | import SockAddr () | ||
66 | -- import System.Locale (defaultTimeLocale) | 67 | -- import System.Locale (defaultTimeLocale) |
67 | 68 | ||
68 | todo = error "unimplemented" | 69 | todo = error "unimplemented" |
69 | 70 | ||
70 | #if MIN_VERSION_network(2,4,0) | ||
71 | #else | ||
72 | deriving instance Ord SockAddr | ||
73 | #endif | ||
74 | |||
75 | |||
76 | type Microseconds = Int | 71 | type Microseconds = Int |
77 | type Miliseconds = Int | 72 | type Miliseconds = Int |
78 | type TimeOut = Miliseconds | 73 | type TimeOut = Miliseconds |
diff --git a/Presence/SockAddr.hs b/Presence/SockAddr.hs new file mode 100644 index 00000000..91a03870 --- /dev/null +++ b/Presence/SockAddr.hs | |||
@@ -0,0 +1,13 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
2 | {-# LANGUAGE StandaloneDeriving #-} | ||
3 | module SockAddr () where | ||
4 | |||
5 | import Network.Socket ( SockAddr(..) ) | ||
6 | |||
7 | #if MIN_VERSION_network(2,4,0) | ||
8 | #else | ||
9 | deriving instance Ord SockAddr | ||
10 | #endif | ||
11 | |||
12 | |||
13 | |||
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index f029810d..9059a4c0 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -66,17 +66,17 @@ import qualified Data.Map as Map | |||
66 | import Data.Set (Set, (\\) ) | 66 | import Data.Set (Set, (\\) ) |
67 | import qualified Data.Set as Set | 67 | import qualified Data.Set as Set |
68 | import qualified System.Random | 68 | import qualified System.Random |
69 | import qualified Network.BSD as BSD | ||
70 | import Data.Void (Void) | 69 | import Data.Void (Void) |
71 | import System.Endian (toBE32) | 70 | import System.Endian (toBE32) |
72 | 71 | ||
73 | import GetHostByAddr (getHostByAddr) | 72 | import ConnectionKey |
74 | import qualified Control.Concurrent.STM.UpdateStream as Slotted | 73 | import qualified Control.Concurrent.STM.UpdateStream as Slotted |
75 | import Nesting | 74 | import Nesting |
76 | import Server | 75 | import Server |
77 | import EventUtil | 76 | import EventUtil |
78 | import ControlMaybe | 77 | import ControlMaybe |
79 | import LockedChan | 78 | import LockedChan |
79 | import PeerResolve | ||
80 | 80 | ||
81 | withPort :: SockAddr -> Int -> SockAddr | 81 | withPort :: SockAddr -> Int -> SockAddr |
82 | withPort (SockAddrInet _ a) port = SockAddrInet (toEnum port) a | 82 | withPort (SockAddrInet _ a) port = SockAddrInet (toEnum port) a |
@@ -91,11 +91,6 @@ clientport = 5222 | |||
91 | my_uuid :: Text | 91 | my_uuid :: Text |
92 | my_uuid = "154ae29f-98f2-4af4-826d-a40c8a188574" | 92 | my_uuid = "154ae29f-98f2-4af4-826d-a40c8a188574" |
93 | 93 | ||
94 | data ConnectionKey | ||
95 | = PeerKey { callBackAddress :: SockAddr } | ||
96 | | ClientKey { localAddress :: SockAddr } | ||
97 | deriving (Show, Ord, Eq) | ||
98 | |||
99 | data JabberShow = Offline | 94 | data JabberShow = Offline |
100 | | ExtendedAway | 95 | | ExtendedAway |
101 | | Away | 96 | | Away |
@@ -251,19 +246,6 @@ peerKeyToResolvedName pk = do | |||
251 | ns <- peerKeyToResolvedNames pk | 246 | ns <- peerKeyToResolvedNames pk |
252 | return $ maybe (peerKeyToText pk) id (listToMaybe ns) | 247 | return $ maybe (peerKeyToText pk) id (listToMaybe ns) |
253 | 248 | ||
254 | peerKeyToResolvedNames :: ConnectionKey -> IO [Text] | ||
255 | peerKeyToResolvedNames k@(ClientKey { localAddress=addr }) = return [] | ||
256 | peerKeyToResolvedNames k@(PeerKey { callBackAddress=addr }) = do | ||
257 | handleIO_ (return []) $ do | ||
258 | ent <- getHostByAddr (unmap6mapped4 addr) -- AF_UNSPEC addr | ||
259 | let names = BSD.hostName ent : BSD.hostAliases ent | ||
260 | return $ map Text.pack $ nub names | ||
261 | |||
262 | unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) = | ||
263 | SockAddrInet port (toBE32 a) | ||
264 | unmap6mapped4 addr = addr | ||
265 | |||
266 | |||
267 | 249 | ||
268 | wlog :: String -> IO () | 250 | wlog :: String -> IO () |
269 | wlog s = putStrLn s >> hFlush stdout | 251 | wlog s = putStrLn s >> hFlush stdout |
diff --git a/xmppServer.hs b/xmppServer.hs index 3752218a..f7f7fb36 100644 --- a/xmppServer.hs +++ b/xmppServer.hs | |||
@@ -7,14 +7,7 @@ import Control.Concurrent.STM.TMVar | |||
7 | import Control.Monad.Trans.Resource (runResourceT) | 7 | import Control.Monad.Trans.Resource (runResourceT) |
8 | import Control.Monad.Trans | 8 | import Control.Monad.Trans |
9 | import Control.Monad.IO.Class (MonadIO, liftIO) | 9 | import Control.Monad.IO.Class (MonadIO, liftIO) |
10 | import Network.Socket | 10 | import Network.Socket ( SockAddr(..) ) |
11 | ( addrAddress | ||
12 | , getAddrInfo | ||
13 | , defaultHints | ||
14 | , addrFlags | ||
15 | , AddrInfoFlag(AI_CANONNAME,AI_V4MAPPED,AI_NUMERICHOST) | ||
16 | , SockAddr(..) | ||
17 | ) | ||
18 | import System.Endian (fromBE32) | 11 | import System.Endian (fromBE32) |
19 | import Data.List (nub, (\\), intersect, groupBy, sort ) | 12 | import Data.List (nub, (\\), intersect, groupBy, sort ) |
20 | import Data.Monoid ( (<>) ) | 13 | import Data.Monoid ( (<>) ) |
@@ -40,6 +33,7 @@ import TraversableT | |||
40 | import UTmp (ProcessID,users) | 33 | import UTmp (ProcessID,users) |
41 | import LocalPeerCred | 34 | import LocalPeerCred |
42 | import XMPPServer | 35 | import XMPPServer |
36 | import PeerResolve | ||
43 | 37 | ||
44 | type UserName = Text | 38 | type UserName = Text |
45 | type ResourceName = Text | 39 | type ResourceName = Text |
@@ -142,42 +136,6 @@ data PresenceState = PresenceState | |||
142 | } | 136 | } |
143 | 137 | ||
144 | 138 | ||
145 | make6mapped4 addr@(SockAddrInet6 {}) = addr | ||
146 | make6mapped4 addr@(SockAddrInet port a) = SockAddrInet6 port 0 (0,0,0xFFFF,fromBE32 a) 0 | ||
147 | |||
148 | resolvePeer :: Text -> IO [SockAddr] | ||
149 | resolvePeer addrtext = do | ||
150 | r <- atomically newEmptyTMVar | ||
151 | mvar <- atomically newEmptyTMVar | ||
152 | rt <- forkOS $ resolver r mvar | ||
153 | tt <- forkIO $ timer r rt | ||
154 | atomically $ putTMVar mvar tt | ||
155 | atomically $ readTMVar r | ||
156 | where | ||
157 | resolver r mvar = do | ||
158 | xs <- handle (\e -> let _ = isDoesNotExistError e in return []) | ||
159 | $ do fmap (map $ make6mapped4 . addrAddress) $ | ||
160 | getAddrInfo (Just $ defaultHints { addrFlags = [ AI_CANONNAME, AI_V4MAPPED ]}) | ||
161 | (Just $ Text.unpack $ strip_brackets addrtext) | ||
162 | (Just "5269") | ||
163 | did <- atomically $ tryPutTMVar r xs | ||
164 | when did $ do | ||
165 | tt <- atomically $ readTMVar mvar | ||
166 | throwTo tt (ErrorCall "Interrupted delay") | ||
167 | return () | ||
168 | timer r rt = do | ||
169 | handle (\(ErrorCall _)-> return ()) $ do | ||
170 | threadDelay 1000000 | ||
171 | did <- atomically $ tryPutTMVar r [] | ||
172 | when did $ do | ||
173 | putStrLn $ "timeout resolving: "++show addrtext | ||
174 | killThread rt | ||
175 | |||
176 | strip_brackets s = | ||
177 | case Text.uncons s of | ||
178 | Just ('[',t) -> Text.takeWhile (/=']') t | ||
179 | _ -> s | ||
180 | |||
181 | 139 | ||
182 | getConsolePids :: PresenceState -> IO [(Text,ProcessID)] | 140 | getConsolePids :: PresenceState -> IO [(Text,ProcessID)] |
183 | getConsolePids state = do | 141 | getConsolePids state = do |
@@ -376,13 +334,6 @@ rewriteJIDForClient1 jid = do | |||
376 | return $ Just ((n,h',r), addr) | 334 | return $ Just ((n,h',r), addr) |
377 | -} | 335 | -} |
378 | 336 | ||
379 | parseAddress :: Text -> IO (Maybe SockAddr) | ||
380 | parseAddress addr_str = do | ||
381 | info <- getAddrInfo (Just $ defaultHints { addrFlags = [ AI_NUMERICHOST ] }) | ||
382 | (Just . Text.unpack $ addr_str) | ||
383 | (Just "0") | ||
384 | return . listToMaybe $ map addrAddress info | ||
385 | |||
386 | todo = error "Unimplemented" | 337 | todo = error "Unimplemented" |
387 | 338 | ||
388 | 339 | ||