summaryrefslogtreecommitdiff
path: root/src/Network/Tox
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-18 00:49:38 -0400
committerjoe <joe@jerkface.net>2018-06-18 00:49:38 -0400
commit07b1494c9d5c692371c9689a8f78f4cf7ee58732 (patch)
treeaac158efc14bdb210717018704c43f2542804bf8 /src/Network/Tox
parent6de7e6d299254010ebe2fd3fc5fb7c7fd6c89fc6 (diff)
Tox: Added timestamps to dhtkey and sockaddr information.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r--src/Network/Tox/ContactInfo.hs46
-rw-r--r--src/Network/Tox/DHT/Handlers.hs10
2 files changed, 28 insertions, 28 deletions
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs
index 7f74caff..47c07237 100644
--- a/src/Network/Tox/ContactInfo.hs
+++ b/src/Network/Tox/ContactInfo.hs
@@ -4,6 +4,7 @@ module Network.Tox.ContactInfo where
4import ConnectionKey 4import ConnectionKey
5import Connection 5import Connection
6 6
7import Data.Time.Clock.POSIX
7import Control.Concurrent.STM 8import Control.Concurrent.STM
8import Control.Monad 9import Control.Monad
9import Crypto.PubKey.Curve25519 10import Crypto.PubKey.Curve25519
@@ -35,14 +36,10 @@ data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionDat
35 | AddrChange { contact :: PublicKey, addrChange :: SockAddr } 36 | AddrChange { contact :: PublicKey, addrChange :: SockAddr }
36 37
37data Contact = Contact 38data Contact = Contact
38 { contactKeyPacket :: TVar (Maybe (DHT.DHTPublicKey)) 39 { contactKeyPacket :: TVar (Maybe (POSIXTime,DHT.DHTPublicKey))
39 , contactLastSeenAddr :: TVar (Maybe SockAddr) 40 , contactLastSeenAddr :: TVar (Maybe (POSIXTime,SockAddr))
40 , contactFriendRequest :: TVar (Maybe (DHT.FriendRequest)) 41 , contactFriendRequest :: TVar (Maybe (POSIXTime,DHT.FriendRequest))
41 , contactPolicy :: TVar (Maybe (Connection.Policy)) 42 , contactPolicy :: TVar (Maybe Connection.Policy)
42 -- Possible semantics
43 -- RefusingToConnect : rejected friend-request or blocked or unknown.
44 -- OpenToConnect : pending friend-request.
45 -- TryingToConnect : roster entry.
46 } 43 }
47 44
48newContactInfo :: IO ContactInfo 45newContactInfo :: IO ContactInfo
@@ -57,10 +54,11 @@ myKeyPairs (ContactInfo accounts) = do
57updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () 54updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO ()
58updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do 55updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do
59 hPutStrLn stderr "updateContactInfo!!!" 56 hPutStrLn stderr "updateContactInfo!!!"
57 now <- getPOSIXTime
60 atomically $ do 58 atomically $ do
61 as <- readTVar (accounts roster) 59 as <- readTVar (accounts roster)
62 maybe (return ()) 60 maybe (return ())
63 (updateAccount remoteUserKey omsg) 61 (updateAccount now remoteUserKey omsg)
64 $ HashMap.lookup (key2id localUserKey) as 62 $ HashMap.lookup (key2id localUserKey) as
65 63
66initContact :: STM Contact 64initContact :: STM Contact
@@ -80,31 +78,31 @@ updateAccount' remoteUserKey acc updater = do
80 return contact 78 return contact
81 updater contact 79 updater contact
82 80
83updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () 81updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM ()
84updateAccount remoteUserKey omsg acc = do 82updateAccount now remoteUserKey omsg acc = do
85 updateAccount' remoteUserKey acc $ onionUpdate omsg 83 updateAccount' remoteUserKey acc $ onionUpdate now omsg
86 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg 84 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg
87 85
88onionUpdate :: OnionData -> Contact -> STM () 86onionUpdate :: POSIXTime -> OnionData -> Contact -> STM ()
89onionUpdate (Onion.OnionDHTPublicKey dhtpk) contact 87onionUpdate now (Onion.OnionDHTPublicKey dhtpk) contact
90 = writeTVar (contactKeyPacket contact) $ Just dhtpk 88 = writeTVar (contactKeyPacket contact) $ Just (now,dhtpk)
91onionUpdate (Onion.OnionFriendRequest fr) contact 89onionUpdate now (Onion.OnionFriendRequest fr) contact
92 = writeTVar (contactFriendRequest contact) $ Just fr 90 = writeTVar (contactFriendRequest contact) $ Just (now,fr)
93 91
94policyUpdate :: Policy -> Contact -> STM () 92policyUpdate :: Policy -> Contact -> STM ()
95policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy 93policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy
96 94
97addrUpdate :: SockAddr -> Contact -> STM () 95addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM ()
98addrUpdate addr contact = writeTVar (contactLastSeenAddr contact) $ Just addr 96addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr)
99 97
100setContactPolicy :: PublicKey -> Policy -> Account -> STM () 98setContactPolicy :: PublicKey -> Policy -> Account -> STM ()
101setContactPolicy remoteUserKey policy acc = do 99setContactPolicy remoteUserKey policy acc = do
102 updateAccount' remoteUserKey acc $ policyUpdate policy 100 updateAccount' remoteUserKey acc $ policyUpdate policy
103 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy 101 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy
104 102
105setContactAddr :: PublicKey -> SockAddr -> Account -> STM () 103setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM ()
106setContactAddr remoteUserKey addr acc = do 104setContactAddr now remoteUserKey addr acc = do
107 updateAccount' remoteUserKey acc $ addrUpdate addr 105 updateAccount' remoteUserKey acc $ addrUpdate now addr
108 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr 106 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr
109 107
110 108
@@ -129,7 +127,7 @@ dnsPresentation (ContactInfo accsvar) = do
129 cs <- readTVar cvar 127 cs <- readTVar cvar
130 rs <- forM (HashMap.toList cs) $ \(nid,c) -> do 128 rs <- forM (HashMap.toList cs) $ \(nid,c) -> do
131 mkpkt <- readTVar (contactKeyPacket c) 129 mkpkt <- readTVar (contactKeyPacket c)
132 return $ fmap ((,) nid) mkpkt 130 return $ fmap (\(_,d) -> (nid,d)) mkpkt
133 return $ 131 return $
134 "; local key = " ++ show (key2id $ toPublic sec) ++ "\n" 132 "; local key = " ++ show (key2id $ toPublic sec) ++ "\n"
135 ++ concatMap dnsPresentation1 (catMaybes rs) 133 ++ concatMap dnsPresentation1 (catMaybes rs)
@@ -150,6 +148,6 @@ friendRequests (ContactInfo roster) = do
150 cs <- readTVar cvar 148 cs <- readTVar cvar
151 rs <- forM (HashMap.toList cs) $ \(nid,c) -> do 149 rs <- forM (HashMap.toList cs) $ \(nid,c) -> do
152 mfr <- readTVar (contactFriendRequest c) 150 mfr <- readTVar (contactFriendRequest c)
153 return $ fmap ((,) nid) mfr 151 return $ fmap (\(_,x) -> (nid,x)) mfr
154 return $ catMaybes rs 152 return $ catMaybes rs
155 153
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs
index 6a80b4a4..7e986fdd 100644
--- a/src/Network/Tox/DHT/Handlers.hs
+++ b/src/Network/Tox/DHT/Handlers.hs
@@ -117,8 +117,8 @@ classify client msg = fromMaybe (IsUnknown "unknown")
117data NodeInfoCallback = NodeInfoCallback 117data NodeInfoCallback = NodeInfoCallback
118 { interestingNodeId :: NodeId 118 { interestingNodeId :: NodeId
119 , listenerId :: Int 119 , listenerId :: Int
120 , observedAddress :: NodeInfo -> STM () 120 , observedAddress :: POSIXTime -> NodeInfo -> STM ()
121 , rumoredAddress :: SockAddr -> NodeInfo -> STM () 121 , rumoredAddress :: POSIXTime -> SockAddr -> NodeInfo -> STM ()
122 } 122 }
123 123
124data Routing = Routing 124data Routing = Routing
@@ -405,22 +405,24 @@ getNodes client cbvar nid addr = do
405 -- hPutStrLn stderr $ show addr ++ " -sendnodes-> " ++ show reply 405 -- hPutStrLn stderr $ show addr ++ " -sendnodes-> " ++ show reply
406 forM_ (join reply) $ \(SendNodes ns) -> 406 forM_ (join reply) $ \(SendNodes ns) ->
407 forM_ ns $ \n -> do 407 forM_ ns $ \n -> do
408 now <- getPOSIXTime
408 atomically $ do 409 atomically $ do
409 mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar 410 mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar
410 forM_ mcbs $ \cbs -> do 411 forM_ mcbs $ \cbs -> do
411 forM_ cbs $ \cb -> do 412 forM_ cbs $ \cb -> do
412 rumoredAddress cb (nodeAddr addr) n 413 rumoredAddress cb now (nodeAddr addr) n
413 return $ fmap unwrapNodes $ join reply 414 return $ fmap unwrapNodes $ join reply
414 415
415updateRouting :: Client -> Routing -> OnionRouter -> NodeInfo -> Message -> IO () 416updateRouting :: Client -> Routing -> OnionRouter -> NodeInfo -> Message -> IO ()
416updateRouting client routing orouter naddr msg 417updateRouting client routing orouter naddr msg
417 | PacketKind 0x21 <- msgType msg = dput XLan "(tox)updateRouting: ignoring lan discovery" -- ignore lan discovery 418 | PacketKind 0x21 <- msgType msg = dput XLan "(tox)updateRouting: ignoring lan discovery" -- ignore lan discovery
418 | otherwise = do 419 | otherwise = do
420 now <- getPOSIXTime
419 atomically $ do 421 atomically $ do
420 m <- HashMap.lookup (nodeId naddr) <$> readTVar (nodesOfInterest routing) 422 m <- HashMap.lookup (nodeId naddr) <$> readTVar (nodesOfInterest routing)
421 forM_ m $ mapM_ $ \NodeInfoCallback{interestingNodeId,observedAddress} -> do 423 forM_ m $ mapM_ $ \NodeInfoCallback{interestingNodeId,observedAddress} -> do
422 when (interestingNodeId == nodeId naddr) 424 when (interestingNodeId == nodeId naddr)
423 $ observedAddress naddr 425 $ observedAddress now naddr
424 case prefer4or6 naddr Nothing of 426 case prefer4or6 naddr Nothing of
425 Want_IP4 -> updateTable client naddr orouter (committee4 routing) (refresher4 routing) 427 Want_IP4 -> updateTable client naddr orouter (committee4 routing) (refresher4 routing)
426 Want_IP6 -> updateTable client naddr orouter (committee6 routing) (refresher6 routing) 428 Want_IP6 -> updateTable client naddr orouter (committee6 routing) (refresher6 routing)