summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Tox.hs22
-rw-r--r--src/Network/Tox/ContactInfo.hs52
-rw-r--r--src/Network/Tox/DHT/Transport.hs2
3 files changed, 38 insertions, 38 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 20302343..a13a4f10 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -248,7 +248,7 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do
248 in client 248 in client
249 return $ either mkclient mkclient tblvar handlers 249 return $ either mkclient mkclient tblvar handlers
250 250
251data Tox = Tox 251data Tox extra = Tox
252 { toxDHT :: DHT.Client 252 { toxDHT :: DHT.Client
253 , toxOnion :: Onion.Client RouteId 253 , toxOnion :: Onion.Client RouteId
254 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData) 254 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData)
@@ -260,17 +260,17 @@ data Tox = Tox
260 , toxTokens :: TVar SessionTokens 260 , toxTokens :: TVar SessionTokens
261 , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys 261 , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys
262 , toxOnionRoutes :: OnionRouter 262 , toxOnionRoutes :: OnionRouter
263 , toxContactInfo :: ContactInfo 263 , toxContactInfo :: ContactInfo extra
264 , toxAnnounceToLan :: IO () 264 , toxAnnounceToLan :: IO ()
265 , toxMgr :: Manager ToxProgress Key 265 , toxMgr :: Manager ToxProgress Key
266 } 266 }
267 267
268-- | initiate a netcrypto session, blocking 268-- | initiate a netcrypto session, blocking
269netCrypto :: Tox -> SecretKey -> PublicKey{-UserKey -} -> IO [NetCryptoSession] 269netCrypto :: Tox extra -> SecretKey -> PublicKey{-UserKey -} -> IO [NetCryptoSession]
270netCrypto tox myseckey theirpubkey = netCryptoWithBackoff 1000000 tox myseckey theirpubkey 270netCrypto tox myseckey theirpubkey = netCryptoWithBackoff 1000000 tox myseckey theirpubkey
271 271
272-- | helper for 'netCrypto', initiate a netcrypto session, retry after specified millisecs 272-- | helper for 'netCrypto', initiate a netcrypto session, retry after specified millisecs
273netCryptoWithBackoff :: Int -> Tox -> SecretKey -> PublicKey -> IO [NetCryptoSession] 273netCryptoWithBackoff :: Int -> Tox extra -> SecretKey -> PublicKey -> IO [NetCryptoSession]
274netCryptoWithBackoff millisecs tox myseckey theirpubkey = do 274netCryptoWithBackoff millisecs tox myseckey theirpubkey = do
275 let mykeyAsId = key2id (toPublic myseckey) 275 let mykeyAsId = key2id (toPublic myseckey)
276 -- TODO: check status of connection here: 276 -- TODO: check status of connection here:
@@ -358,7 +358,7 @@ netCryptoWithBackoff millisecs tox myseckey theirpubkey = do
358 return [] 358 return []
359 359
360-- | Create a DHTPublicKey packet to send to a remote contact. 360-- | Create a DHTPublicKey packet to send to a remote contact.
361getContactInfo :: Tox -> IO DHT.DHTPublicKey 361getContactInfo :: Tox extra -> IO DHT.DHTPublicKey
362getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do 362getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do
363 r4 <- readTVar $ DHT.routing4 toxRouting 363 r4 <- readTVar $ DHT.routing4 toxRouting
364 r6 <- readTVar $ DHT.routing6 toxRouting 364 r6 <- readTVar $ DHT.routing6 toxRouting
@@ -416,7 +416,7 @@ newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rende
416 -> SockAddr -- ^ Bind-address to listen on. 416 -> SockAddr -- ^ Bind-address to listen on.
417 -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. 417 -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links.
418 -> Maybe SecretKey -- ^ Optional DHT secret key to use. 418 -> Maybe SecretKey -- ^ Optional DHT secret key to use.
419 -> IO Tox 419 -> IO (Tox extra)
420newTox keydb addr mbSessionsState suppliedDHTKey = do 420newTox keydb addr mbSessionsState suppliedDHTKey = do
421 (udp,sock) <- {- addVerbosity <$> -} udpTransport' addr 421 (udp,sock) <- {- addVerbosity <$> -} udpTransport' addr
422 (crypto0,sessionsState0) <- case mbSessionsState of 422 (crypto0,sessionsState0) <- case mbSessionsState of
@@ -506,21 +506,21 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
506 , toxMgr = mgr 506 , toxMgr = mgr
507 } 507 }
508 508
509onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) 509onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int)
510onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTimeout or n8 od 510onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTimeout or n8 od
511 511
512routing4nodeInfo :: DHT.Routing -> IO NodeInfo 512routing4nodeInfo :: DHT.Routing -> IO NodeInfo
513routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv 513routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv
514 514
515dnssdAnnounce :: Tox -> IO () 515dnssdAnnounce :: Tox extra -> IO ()
516dnssdAnnounce (toxRouting -> r) = do 516dnssdAnnounce (toxRouting -> r) = do
517 ni <- routing4nodeInfo r 517 ni <- routing4nodeInfo r
518 announceToxService (nodePort ni) (nodeId ni) 518 announceToxService (nodePort ni) (nodeId ni)
519 519
520dnssdDiscover :: Tox -> NodeInfo -> IO () 520dnssdDiscover :: Tox extra -> NodeInfo -> IO ()
521dnssdDiscover (toxDHT -> client) ni = void $ DHT.ping client ni 521dnssdDiscover (toxDHT -> client) ni = void $ DHT.ping client ni
522 522
523forkTox :: Tox -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) 523forkTox :: Tox extra -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ())
524forkTox tox = do 524forkTox tox = do
525 _ <- forkListener "toxHandshakes" (toxHandshakes tox) 525 _ <- forkListener "toxHandshakes" (toxHandshakes tox)
526 _ <- forkListener "toxToRoute" (toxToRoute tox) 526 _ <- forkListener "toxToRoute" (toxToRoute tox)
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs
index 9f29d587..64ea861b 100644
--- a/src/Network/Tox/ContactInfo.hs
+++ b/src/Network/Tox/ContactInfo.hs
@@ -19,16 +19,16 @@ import Network.Tox.NodeId (id2key)
19import Network.Tox.Onion.Transport as Onion 19import Network.Tox.Onion.Transport as Onion
20import System.IO 20import System.IO
21 21
22newtype ContactInfo = ContactInfo 22newtype ContactInfo extra = ContactInfo
23 -- | Map our toxid public key to an Account record. 23 -- | Map our toxid public key to an Account record.
24 { accounts :: TVar (HashMap NodeId{-my userkey-} Account) 24 { accounts :: TVar (HashMap NodeId{-my userkey-} (Account extra))
25 } 25 }
26 26
27data Account = Account 27data Account extra = Account
28 { userSecret :: SecretKey -- local secret key 28 { userSecret :: SecretKey -- local secret key
29 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info 29 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info
30 , clientRefs :: TVar (Set ConnectionKey) -- reference count so we know when to send offline etc 30 , accountExtra :: TVar extra
31 , eventChan :: TChan ContactEvent 31 , eventChan :: TChan ContactEvent
32 } 32 }
33 33
34data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData } 34data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData }
@@ -44,16 +44,16 @@ data Contact = Contact
44 , contactPolicy :: TVar (Maybe Connection.Policy) 44 , contactPolicy :: TVar (Maybe Connection.Policy)
45 } 45 }
46 46
47newContactInfo :: IO ContactInfo 47newContactInfo :: IO (ContactInfo extra)
48newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty 48newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty
49 49
50myKeyPairs :: ContactInfo -> STM [(SecretKey,PublicKey)] 50myKeyPairs :: ContactInfo extra -> STM [(SecretKey,PublicKey)]
51myKeyPairs (ContactInfo accounts) = do 51myKeyPairs (ContactInfo accounts) = do
52 acnts <- readTVar accounts 52 acnts <- readTVar accounts
53 forM (HashMap.toList acnts) $ \(nid, Account{userSecret}) -> do 53 forM (HashMap.toList acnts) $ \(nid, Account{userSecret}) -> do
54 return (userSecret,id2key nid) 54 return (userSecret,id2key nid)
55 55
56updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () 56updateContactInfo :: ContactInfo extra -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO ()
57updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do 57updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do
58 hPutStrLn stderr "updateContactInfo!!!" 58 hPutStrLn stderr "updateContactInfo!!!"
59 now <- getPOSIXTime 59 now <- getPOSIXTime
@@ -69,7 +69,7 @@ initContact = Contact <$> newTVar Nothing
69 <*> newTVar Nothing 69 <*> newTVar Nothing
70 <*> newTVar Nothing 70 <*> newTVar Nothing
71 71
72updateAccount' :: PublicKey -> Account -> (Contact -> STM ()) -> STM () 72updateAccount' :: PublicKey -> Account extra -> (Contact -> STM ()) -> STM ()
73updateAccount' remoteUserKey acc updater = do 73updateAccount' remoteUserKey acc updater = do
74 let rkey = key2id remoteUserKey 74 let rkey = key2id remoteUserKey
75 cmap <- readTVar (contacts acc) 75 cmap <- readTVar (contacts acc)
@@ -80,7 +80,7 @@ updateAccount' remoteUserKey acc updater = do
80 return contact 80 return contact
81 updater contact 81 updater contact
82 82
83updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () 83updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account extra -> STM ()
84updateAccount now remoteUserKey omsg acc = do 84updateAccount now remoteUserKey omsg acc = do
85 updateAccount' remoteUserKey acc $ onionUpdate now omsg 85 updateAccount' remoteUserKey acc $ onionUpdate now omsg
86 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg 86 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg
@@ -97,39 +97,39 @@ policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy
97addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () 97addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM ()
98addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) 98addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr)
99 99
100setContactPolicy :: PublicKey -> Policy -> Account -> STM () 100setContactPolicy :: PublicKey -> Policy -> Account extra -> STM ()
101setContactPolicy remoteUserKey policy acc = do 101setContactPolicy remoteUserKey policy acc = do
102 updateAccount' remoteUserKey acc $ policyUpdate policy 102 updateAccount' remoteUserKey acc $ policyUpdate policy
103 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy 103 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy
104 104
105setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () 105setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account extra -> STM ()
106setContactAddr now remoteUserKey addr acc = do 106setContactAddr now remoteUserKey addr acc = do
107 updateAccount' remoteUserKey acc $ addrUpdate now addr 107 updateAccount' remoteUserKey acc $ addrUpdate now addr
108 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr 108 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr
109 109
110setEstablished :: POSIXTime -> PublicKey -> Account -> STM () 110setEstablished :: POSIXTime -> PublicKey -> Account extra -> STM ()
111setEstablished now remoteUserKey acc = 111setEstablished now remoteUserKey acc =
112 writeTChan (eventChan acc) $ SessionEstablished remoteUserKey 112 writeTChan (eventChan acc) $ SessionEstablished remoteUserKey
113 113
114setTerminated :: POSIXTime -> PublicKey -> Account -> STM () 114setTerminated :: POSIXTime -> PublicKey -> Account extra -> STM ()
115setTerminated now remoteUserKey acc = 115setTerminated now remoteUserKey acc =
116 writeTChan (eventChan acc) $ SessionTerminated remoteUserKey 116 writeTChan (eventChan acc) $ SessionTerminated remoteUserKey
117 117
118 118
119addContactInfo :: ContactInfo -> SecretKey -> STM () 119addContactInfo :: ContactInfo extra -> SecretKey -> extra -> STM ()
120addContactInfo (ContactInfo as) sk = do 120addContactInfo (ContactInfo as) sk extra = do
121 a <- newAccount sk 121 a <- newAccount sk extra
122 modifyTVar' as $ HashMap.insert (key2id $ toPublic sk) a 122 modifyTVar' as $ HashMap.insert (key2id $ toPublic sk) a
123 123
124delContactInfo :: ContactInfo -> PublicKey -> STM () 124delContactInfo :: ContactInfo extra -> PublicKey -> STM ()
125delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) 125delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk)
126 126
127newAccount :: SecretKey -> STM Account 127newAccount :: SecretKey -> extra -> STM (Account extra)
128newAccount sk = Account sk <$> newTVar HashMap.empty 128newAccount sk extra = Account sk <$> newTVar HashMap.empty
129 <*> newTVar Set.empty 129 <*> newTVar extra
130 <*> newBroadcastTChan 130 <*> newBroadcastTChan
131 131
132dnsPresentation :: ContactInfo -> STM String 132dnsPresentation :: ContactInfo extra -> STM String
133dnsPresentation (ContactInfo accsvar) = do 133dnsPresentation (ContactInfo accsvar) = do
134 accs <- readTVar accsvar 134 accs <- readTVar accsvar
135 ms <- forM accs $ \Account { userSecret = sec, contacts = cvar } -> do 135 ms <- forM accs $ \Account { userSecret = sec, contacts = cvar } -> do
@@ -150,7 +150,7 @@ dnsPresentation1 (nid,dk) = unlines
150type LocalKey = NodeId 150type LocalKey = NodeId
151type RemoteKey = NodeId 151type RemoteKey = NodeId
152 152
153friendRequests :: ContactInfo -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) 153friendRequests :: ContactInfo extra -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)])
154friendRequests (ContactInfo roster) = do 154friendRequests (ContactInfo roster) = do
155 accs <- readTVar roster 155 accs <- readTVar roster
156 forM accs $ \Account { userSecret = sec, contacts = cvar } -> do 156 forM accs $ \Account { userSecret = sec, contacts = cvar } -> do
diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs
index 5fdcd252..9ff1839c 100644
--- a/src/Network/Tox/DHT/Transport.hs
+++ b/src/Network/Tox/DHT/Transport.hs
@@ -262,7 +262,7 @@ data FriendRequest = FriendRequest
262 { friendNoSpam :: Word32 262 { friendNoSpam :: Word32
263 , friendRequestText :: ByteString -- UTF8 263 , friendRequestText :: ByteString -- UTF8
264 } 264 }
265 deriving (Eq, Show) 265 deriving (Eq, Ord, Show)
266 266
267 267
268-- When sent as a DHT request packet (this is the data sent in the DHT request 268-- When sent as a DHT request packet (this is the data sent in the DHT request