diff options
author | joe <joe@jerkface.net> | 2018-06-18 00:49:38 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-18 00:49:38 -0400 |
commit | 07b1494c9d5c692371c9689a8f78f4cf7ee58732 (patch) | |
tree | aac158efc14bdb210717018704c43f2542804bf8 /src/Network/Tox | |
parent | 6de7e6d299254010ebe2fd3fc5fb7c7fd6c89fc6 (diff) |
Tox: Added timestamps to dhtkey and sockaddr information.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 46 | ||||
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 10 |
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 | |||
4 | import ConnectionKey | 4 | import ConnectionKey |
5 | import Connection | 5 | import Connection |
6 | 6 | ||
7 | import Data.Time.Clock.POSIX | ||
7 | import Control.Concurrent.STM | 8 | import Control.Concurrent.STM |
8 | import Control.Monad | 9 | import Control.Monad |
9 | import Crypto.PubKey.Curve25519 | 10 | import 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 | ||
37 | data Contact = Contact | 38 | data 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 | ||
48 | newContactInfo :: IO ContactInfo | 45 | newContactInfo :: IO ContactInfo |
@@ -57,10 +54,11 @@ myKeyPairs (ContactInfo accounts) = do | |||
57 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () | 54 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () |
58 | updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do | 55 | updateContactInfo 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 | ||
66 | initContact :: STM Contact | 64 | initContact :: 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 | ||
83 | updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () | 81 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () |
84 | updateAccount remoteUserKey omsg acc = do | 82 | updateAccount 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 | ||
88 | onionUpdate :: OnionData -> Contact -> STM () | 86 | onionUpdate :: POSIXTime -> OnionData -> Contact -> STM () |
89 | onionUpdate (Onion.OnionDHTPublicKey dhtpk) contact | 87 | onionUpdate now (Onion.OnionDHTPublicKey dhtpk) contact |
90 | = writeTVar (contactKeyPacket contact) $ Just dhtpk | 88 | = writeTVar (contactKeyPacket contact) $ Just (now,dhtpk) |
91 | onionUpdate (Onion.OnionFriendRequest fr) contact | 89 | onionUpdate now (Onion.OnionFriendRequest fr) contact |
92 | = writeTVar (contactFriendRequest contact) $ Just fr | 90 | = writeTVar (contactFriendRequest contact) $ Just (now,fr) |
93 | 91 | ||
94 | policyUpdate :: Policy -> Contact -> STM () | 92 | policyUpdate :: Policy -> Contact -> STM () |
95 | policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy | 93 | policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy |
96 | 94 | ||
97 | addrUpdate :: SockAddr -> Contact -> STM () | 95 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () |
98 | addrUpdate addr contact = writeTVar (contactLastSeenAddr contact) $ Just addr | 96 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) |
99 | 97 | ||
100 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () | 98 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () |
101 | setContactPolicy remoteUserKey policy acc = do | 99 | setContactPolicy 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 | ||
105 | setContactAddr :: PublicKey -> SockAddr -> Account -> STM () | 103 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () |
106 | setContactAddr remoteUserKey addr acc = do | 104 | setContactAddr 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") | |||
117 | data NodeInfoCallback = NodeInfoCallback | 117 | data 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 | ||
124 | data Routing = Routing | 124 | data 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 | ||
415 | updateRouting :: Client -> Routing -> OnionRouter -> NodeInfo -> Message -> IO () | 416 | updateRouting :: Client -> Routing -> OnionRouter -> NodeInfo -> Message -> IO () |
416 | updateRouting client routing orouter naddr msg | 417 | updateRouting 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) |