diff options
author | James Crayne <jim.crayne@gmail.com> | 2018-05-28 06:13:05 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2018-05-28 06:13:05 +0000 |
commit | 57ff75437ba23b29f3397c0f5ed00944b759f35f (patch) | |
tree | 3d78e3b2d090b865371a78c10a44d40f11f8c36e /src/Network | |
parent | 5a1f5550a9135894993cf323ca4970f7c8470991 (diff) |
netcrypto wip, compiles
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox.hs | 51 | ||||
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 11 | ||||
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 2 |
3 files changed, 51 insertions, 13 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index 5d791a8a..e65110e1 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -108,6 +108,10 @@ import Network.Tox.Transport | |||
108 | import OnionRouter | 108 | import OnionRouter |
109 | import Network.Tox.ContactInfo | 109 | import Network.Tox.ContactInfo |
110 | import Text.XXD | 110 | import Text.XXD |
111 | import qualified Data.HashMap.Strict as HashMap | ||
112 | import Data.HashMap.Strict (HashMap) | ||
113 | import qualified Data.Map.Strict as Map | ||
114 | |||
111 | 115 | ||
112 | newCrypto :: IO TransportCrypto | 116 | newCrypto :: IO TransportCrypto |
113 | newCrypto = do | 117 | newCrypto = do |
@@ -245,16 +249,45 @@ data Tox = Tox | |||
245 | } | 249 | } |
246 | 250 | ||
247 | -- | initiate a netcrypto session, blocking | 251 | -- | initiate a netcrypto session, blocking |
248 | netCrypto :: Tox -> SecretKey -> PublicKey -> IO NetCryptoSession | 252 | netCrypto :: Tox -> SecretKey -> PublicKey{-UserKey -} -> IO [NetCryptoSession] |
249 | netCrypto tox myseckey theirpubkey = do | 253 | netCrypto tox myseckey theirpubkey = do |
250 | -- convert public key to NodeInfo check ContactInfo | 254 | let mykeyAsId = key2id (toPublic myseckey) |
251 | -- if no session: | 255 | mbContactsVar <- fmap contacts . HashMap.lookup mykeyAsId <$> atomically (readTVar (accounts (toxContactInfo tox))) |
252 | -- 1) send dht key, actually maybe send dht-key regardless | 256 | case mbContactsVar of |
253 | -- 2) send handshakes to last seen ip's, if any | 257 | Nothing -> do |
254 | -- | 258 | hPutStrLn stderr ("netCrypto: (" ++ show mykeyAsId ++") accounts lookup failed.") |
255 | -- if sessions found, is it using this private key? | 259 | return [] |
256 | -- if not, send handshake, this is separate session | 260 | |
257 | error "todo" | 261 | Just contactsVar -> do |
262 | let theirkeyAsId = key2id theirpubkey | ||
263 | mbContact <- HashMap.lookup theirkeyAsId <$> atomically (readTVar contactsVar) | ||
264 | case mbContact of | ||
265 | Nothing -> do | ||
266 | hPutStrLn stderr ("netCrypto: (" ++ show mykeyAsId ++") friend not found (" ++ show theirkeyAsId ++ ").") | ||
267 | return [] | ||
268 | Just contact -> do | ||
269 | -- TODO | ||
270 | -- convert public key to NodeInfo check ContactInfo | ||
271 | -- | ||
272 | -- > case nodeInfo (key2id theirpubkey) saddr of | ||
273 | -- > Left -> fail... | ||
274 | -- > Right ni -> | ||
275 | -- | ||
276 | sessionsMap <- atomically $ readTVar (netCryptoSessionsByKey (toxCryptoSessions tox) ) | ||
277 | let sessionUsesIdentity key session = key == ncMyPublicKey session | ||
278 | case Map.lookup theirpubkey sessionsMap of | ||
279 | -- if sessions found, is it using this private key? | ||
280 | Just sessions | matchedSessions <- filter (sessionUsesIdentity (toPublic myseckey)) sessions | ||
281 | , not (null matchedSessions) | ||
282 | -> do | ||
283 | hPutStrLn stderr ("netCrypto: Already have a session for " ++ show mykeyAsId ++ "<-->" ++ show theirkeyAsId) | ||
284 | return matchedSessions | ||
285 | -- if not, send handshake, this is separate session | ||
286 | Nothing -> error "netCrypto: todo" | ||
287 | -- if no session: | ||
288 | -- 1) send dht key, actually maybe send dht-key regardless | ||
289 | -- 2) send handshakes to last seen ip's, if any | ||
290 | -- | ||
258 | 291 | ||
259 | getContactInfo :: Tox -> IO DHT.DHTPublicKey | 292 | getContactInfo :: Tox -> IO DHT.DHTPublicKey |
260 | getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do | 293 | getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do |
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs index d03fb249..153cd130 100644 --- a/src/Network/Tox/ContactInfo.hs +++ b/src/Network/Tox/ContactInfo.hs | |||
@@ -16,20 +16,22 @@ import Network.Tox.DHT.Transport as DHT | |||
16 | import Network.Tox.NodeId | 16 | import Network.Tox.NodeId |
17 | import Network.Tox.Onion.Transport as Onion | 17 | import Network.Tox.Onion.Transport as Onion |
18 | import System.IO | 18 | import System.IO |
19 | import Network.Socket | ||
19 | 20 | ||
20 | newtype ContactInfo = ContactInfo | 21 | newtype ContactInfo = ContactInfo |
21 | -- | Map our toxid public key to an Account record. | 22 | -- | Map our toxid public key to an Account record. |
22 | { accounts :: TVar (HashMap NodeId Account) | 23 | { accounts :: TVar (HashMap NodeId{-my userkey-} Account) |
23 | } | 24 | } |
24 | 25 | ||
25 | data Account = Account | 26 | data Account = Account |
26 | { userSecret :: SecretKey -- local secret key | 27 | { userSecret :: SecretKey -- local secret key |
27 | , contacts :: TVar (HashMap NodeId Contact) -- received contact info | 28 | , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info |
28 | , clientRefs :: TVar (Set ConnectionKey) | 29 | , clientRefs :: TVar (Set ConnectionKey) -- reference count so we know when to send offline etc |
29 | } | 30 | } |
30 | 31 | ||
31 | data Contact = Contact | 32 | data Contact = Contact |
32 | { contactKeyPacket :: Maybe (DHT.DHTPublicKey) | 33 | { contactKeyPacket :: Maybe (DHT.DHTPublicKey) |
34 | , contactLastSeenAddr :: Maybe SockAddr | ||
33 | , contactFriendRequest :: Maybe (DHT.FriendRequest) | 35 | , contactFriendRequest :: Maybe (DHT.FriendRequest) |
34 | , contactPolicy :: Maybe (Connection.Policy) | 36 | , contactPolicy :: Maybe (Connection.Policy) |
35 | -- Possible semantics | 37 | -- Possible semantics |
@@ -46,9 +48,10 @@ nullContact = Contact | |||
46 | } | 48 | } |
47 | 49 | ||
48 | mergeContact :: Contact -> Maybe Contact -> Maybe Contact | 50 | mergeContact :: Contact -> Maybe Contact -> Maybe Contact |
49 | mergeContact (Contact newk newf newp) (Just (Contact oldk oldf oldp)) = | 51 | mergeContact (Contact newk news newf newp) (Just (Contact oldk olds oldf oldp)) = |
50 | Just $ Contact mergek -- Prefer newer public key packet as long as its stamp | 52 | Just $ Contact mergek -- Prefer newer public key packet as long as its stamp |
51 | -- is later than the stored one. | 53 | -- is later than the stored one. |
54 | (mplus news olds) -- Prefer newer last-seen | ||
52 | (mplus newf oldf) -- Prefer newer friend request. | 55 | (mplus newf oldf) -- Prefer newer friend request. |
53 | (mplus newp oldp) -- Prefer newer connection policy. | 56 | (mplus newp oldp) -- Prefer newer connection policy. |
54 | where | 57 | where |
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index cca8b899..bffb4280 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -85,6 +85,7 @@ type SessionID = Word64 | |||
85 | 85 | ||
86 | data NetCryptoSession = NCrypto | 86 | data NetCryptoSession = NCrypto |
87 | { ncState :: TVar NetCryptoSessionStatus | 87 | { ncState :: TVar NetCryptoSessionStatus |
88 | , ncMyPublicKey :: PublicKey | ||
88 | , ncSessionId :: SessionID | 89 | , ncSessionId :: SessionID |
89 | , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam | 90 | , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam |
90 | , ncTheirBaseNonce :: TVar Nonce24 -- base nonce + packet number | 91 | , ncTheirBaseNonce :: TVar Nonce24 -- base nonce + packet number |
@@ -339,6 +340,7 @@ freshCryptoSession sessions | |||
339 | pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 | 340 | pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 |
340 | let netCryptoSession0 = | 341 | let netCryptoSession0 = |
341 | NCrypto { ncState = ncState0 | 342 | NCrypto { ncState = ncState0 |
343 | , ncMyPublicKey = toPublic key | ||
342 | , ncSessionId = sessionId | 344 | , ncSessionId = sessionId |
343 | , ncTheirPublicKey = remotePublicKey | 345 | , ncTheirPublicKey = remotePublicKey |
344 | , ncTheirBaseNonce = ncTheirBaseNonce0 | 346 | , ncTheirBaseNonce = ncTheirBaseNonce0 |