diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox.hs | 22 | ||||
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 52 | ||||
-rw-r--r-- | src/Network/Tox/DHT/Transport.hs | 2 |
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 | ||
251 | data Tox = Tox | 251 | data 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 |
269 | netCrypto :: Tox -> SecretKey -> PublicKey{-UserKey -} -> IO [NetCryptoSession] | 269 | netCrypto :: Tox extra -> SecretKey -> PublicKey{-UserKey -} -> IO [NetCryptoSession] |
270 | netCrypto tox myseckey theirpubkey = netCryptoWithBackoff 1000000 tox myseckey theirpubkey | 270 | netCrypto 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 |
273 | netCryptoWithBackoff :: Int -> Tox -> SecretKey -> PublicKey -> IO [NetCryptoSession] | 273 | netCryptoWithBackoff :: Int -> Tox extra -> SecretKey -> PublicKey -> IO [NetCryptoSession] |
274 | netCryptoWithBackoff millisecs tox myseckey theirpubkey = do | 274 | netCryptoWithBackoff 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. |
361 | getContactInfo :: Tox -> IO DHT.DHTPublicKey | 361 | getContactInfo :: Tox extra -> IO DHT.DHTPublicKey |
362 | getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do | 362 | getContactInfo 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) |
420 | newTox keydb addr mbSessionsState suppliedDHTKey = do | 420 | newTox 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 | ||
509 | onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) | 509 | onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) |
510 | onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTimeout or n8 od | 510 | onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTimeout or n8 od |
511 | 511 | ||
512 | routing4nodeInfo :: DHT.Routing -> IO NodeInfo | 512 | routing4nodeInfo :: DHT.Routing -> IO NodeInfo |
513 | routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv | 513 | routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv |
514 | 514 | ||
515 | dnssdAnnounce :: Tox -> IO () | 515 | dnssdAnnounce :: Tox extra -> IO () |
516 | dnssdAnnounce (toxRouting -> r) = do | 516 | dnssdAnnounce (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 | ||
520 | dnssdDiscover :: Tox -> NodeInfo -> IO () | 520 | dnssdDiscover :: Tox extra -> NodeInfo -> IO () |
521 | dnssdDiscover (toxDHT -> client) ni = void $ DHT.ping client ni | 521 | dnssdDiscover (toxDHT -> client) ni = void $ DHT.ping client ni |
522 | 522 | ||
523 | forkTox :: Tox -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) | 523 | forkTox :: Tox extra -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) |
524 | forkTox tox = do | 524 | forkTox 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) | |||
19 | import Network.Tox.Onion.Transport as Onion | 19 | import Network.Tox.Onion.Transport as Onion |
20 | import System.IO | 20 | import System.IO |
21 | 21 | ||
22 | newtype ContactInfo = ContactInfo | 22 | newtype 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 | ||
27 | data Account = Account | 27 | data 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 | ||
34 | data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData } | 34 | data 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 | ||
47 | newContactInfo :: IO ContactInfo | 47 | newContactInfo :: IO (ContactInfo extra) |
48 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty | 48 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty |
49 | 49 | ||
50 | myKeyPairs :: ContactInfo -> STM [(SecretKey,PublicKey)] | 50 | myKeyPairs :: ContactInfo extra -> STM [(SecretKey,PublicKey)] |
51 | myKeyPairs (ContactInfo accounts) = do | 51 | myKeyPairs (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 | ||
56 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () | 56 | updateContactInfo :: ContactInfo extra -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () |
57 | updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do | 57 | updateContactInfo 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 | ||
72 | updateAccount' :: PublicKey -> Account -> (Contact -> STM ()) -> STM () | 72 | updateAccount' :: PublicKey -> Account extra -> (Contact -> STM ()) -> STM () |
73 | updateAccount' remoteUserKey acc updater = do | 73 | updateAccount' 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 | ||
83 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () | 83 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account extra -> STM () |
84 | updateAccount now remoteUserKey omsg acc = do | 84 | updateAccount 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 | |||
97 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () | 97 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () |
98 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) | 98 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) |
99 | 99 | ||
100 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () | 100 | setContactPolicy :: PublicKey -> Policy -> Account extra -> STM () |
101 | setContactPolicy remoteUserKey policy acc = do | 101 | setContactPolicy 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 | ||
105 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () | 105 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account extra -> STM () |
106 | setContactAddr now remoteUserKey addr acc = do | 106 | setContactAddr 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 | ||
110 | setEstablished :: POSIXTime -> PublicKey -> Account -> STM () | 110 | setEstablished :: POSIXTime -> PublicKey -> Account extra -> STM () |
111 | setEstablished now remoteUserKey acc = | 111 | setEstablished now remoteUserKey acc = |
112 | writeTChan (eventChan acc) $ SessionEstablished remoteUserKey | 112 | writeTChan (eventChan acc) $ SessionEstablished remoteUserKey |
113 | 113 | ||
114 | setTerminated :: POSIXTime -> PublicKey -> Account -> STM () | 114 | setTerminated :: POSIXTime -> PublicKey -> Account extra -> STM () |
115 | setTerminated now remoteUserKey acc = | 115 | setTerminated now remoteUserKey acc = |
116 | writeTChan (eventChan acc) $ SessionTerminated remoteUserKey | 116 | writeTChan (eventChan acc) $ SessionTerminated remoteUserKey |
117 | 117 | ||
118 | 118 | ||
119 | addContactInfo :: ContactInfo -> SecretKey -> STM () | 119 | addContactInfo :: ContactInfo extra -> SecretKey -> extra -> STM () |
120 | addContactInfo (ContactInfo as) sk = do | 120 | addContactInfo (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 | ||
124 | delContactInfo :: ContactInfo -> PublicKey -> STM () | 124 | delContactInfo :: ContactInfo extra -> PublicKey -> STM () |
125 | delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) | 125 | delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) |
126 | 126 | ||
127 | newAccount :: SecretKey -> STM Account | 127 | newAccount :: SecretKey -> extra -> STM (Account extra) |
128 | newAccount sk = Account sk <$> newTVar HashMap.empty | 128 | newAccount sk extra = Account sk <$> newTVar HashMap.empty |
129 | <*> newTVar Set.empty | 129 | <*> newTVar extra |
130 | <*> newBroadcastTChan | 130 | <*> newBroadcastTChan |
131 | 131 | ||
132 | dnsPresentation :: ContactInfo -> STM String | 132 | dnsPresentation :: ContactInfo extra -> STM String |
133 | dnsPresentation (ContactInfo accsvar) = do | 133 | dnsPresentation (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 | |||
150 | type LocalKey = NodeId | 150 | type LocalKey = NodeId |
151 | type RemoteKey = NodeId | 151 | type RemoteKey = NodeId |
152 | 152 | ||
153 | friendRequests :: ContactInfo -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) | 153 | friendRequests :: ContactInfo extra -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) |
154 | friendRequests (ContactInfo roster) = do | 154 | friendRequests (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 |