summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2018-05-30 22:56:03 +0000
committerJames Crayne <jim.crayne@gmail.com>2018-05-30 22:56:03 +0000
commit39761dea4e24eb942e4cefbc70b8c8c3d90cf571 (patch)
tree819c0cc89a450e7023c43d7f4931e88ff09e5a1f /src/Network
parentf43dab6b76a5c6022457831caac79c861a91f9ae (diff)
This patch:
* integrates Connection.Tox and Network.Tox.Crypto.Handlers * Network.Tox.netCrypto function uses freshCryptoSession
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Tox.hs54
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs30
2 files changed, 48 insertions, 36 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 018361aa..9d785f67 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -258,6 +258,7 @@ netCrypto tox myseckey theirpubkey = netCryptoWithBackoff 1000000 tox myseckey t
258netCryptoWithBackoff :: Int -> Tox -> SecretKey -> PublicKey -> IO [NetCryptoSession] 258netCryptoWithBackoff :: Int -> Tox -> SecretKey -> PublicKey -> IO [NetCryptoSession]
259netCryptoWithBackoff millisecs tox myseckey theirpubkey = do 259netCryptoWithBackoff millisecs tox myseckey theirpubkey = do
260 let mykeyAsId = key2id (toPublic myseckey) 260 let mykeyAsId = key2id (toPublic myseckey)
261 -- TODO: check status of connection here:
261 mbContactsVar <- fmap contacts . HashMap.lookup mykeyAsId <$> atomically (readTVar (accounts (toxContactInfo tox))) 262 mbContactsVar <- fmap contacts . HashMap.lookup mykeyAsId <$> atomically (readTVar (accounts (toxContactInfo tox)))
262 case mbContactsVar of 263 case mbContactsVar of
263 Nothing -> do 264 Nothing -> do
@@ -321,32 +322,33 @@ netCryptoWithBackoff millisecs tox myseckey theirpubkey = do
321 , hpTheirBaseNonce = error "netCrypto: Unreachable! hpTheirBaseNonce" 322 , hpTheirBaseNonce = error "netCrypto: Unreachable! hpTheirBaseNonce"
322 , hpTheirSessionKeyPublic = error "netCrypto: Unreachable! hpTheirSessionKeyPublic" 323 , hpTheirSessionKeyPublic = error "netCrypto: Unreachable! hpTheirSessionKeyPublic"
323 } 324 }
324 myhandshake <- do 325 freshCryptoSession (toxCryptoSessions tox) saddr hp
325 n24' <- atomically $ transportNewNonce crypto 326-- myhandshake <- do
326 dput XNetCrypto ("Handshake Nonce24: " <> show n24') 327-- n24' <- atomically $ transportNewNonce crypto
327 newBaseNonce <- atomically $ transportNewNonce crypto 328-- dput XNetCrypto ("Handshake Nonce24: " <> show n24')
328 mbMyhandshakeData <- newHandShakeData crypto newBaseNonce hp saddr 329-- newBaseNonce <- atomically $ transportNewNonce crypto
329 forM mbMyhandshakeData $ \hsdata -> do 330-- mbMyhandshakeData <- newHandShakeData crypto newBaseNonce hp saddr
330 state <- lookupSharedSecret crypto myseckey theirpubkey n24' 331-- forM mbMyhandshakeData $ \hsdata -> do
331 return Handshake { handshakeCookie = cookie 332-- state <- lookupSharedSecret crypto myseckey theirpubkey n24'
332 , handshakeNonce = n24' 333-- return Handshake { handshakeCookie = cookie
333 , handshakeData = encrypt state $ encodePlain hsdata 334-- , handshakeNonce = n24'
334 } 335-- , handshakeData = encrypt state $ encodePlain hsdata
335 case myhandshake of 336-- }
336 Nothing -> hPutStrLn stderr "netCrypto: failed to create HandshakeData." >> return [] 337-- case myhandshake of
337 Just handshake -> do 338-- Nothing -> hPutStrLn stderr "netCrypto: failed to create HandshakeData." >> return []
338 sendMessage (toxCrypto tox) saddr (NetHandshake handshake) 339-- Just handshake -> do
339 let secnum :: Double 340-- sendMessage (toxCrypto tox) saddr (NetHandshake handshake)
340 secnum = fromIntegral millisecs / 1000000 341 let secnum :: Double
341 delay = (millisecs * 5 `div` 4) 342 secnum = fromIntegral millisecs / 1000000
342 if secnum < 20000000 343 delay = (millisecs * 5 `div` 4)
343 then do 344 if secnum < 20000000
344 hPutStrLn stderr $ "sent handshake, now delaying " ++ show (secnum * 1.25) ++ " second(s).." 345 then do
345 threadDelay delay 346 hPutStrLn stderr $ "sent handshake, now delaying " ++ show (secnum * 1.25) ++ " second(s).."
346 netCryptoWithBackoff delay tox myseckey theirpubkey -- hopefully it will find an active session this time. 347 threadDelay delay
347 else do 348 netCryptoWithBackoff delay tox myseckey theirpubkey -- hopefully it will find an active session this time.
348 hPutStrLn stderr "Unable to establish session..." 349 else do
349 return [] 350 hPutStrLn stderr "Unable to establish session..."
351 return []
350 352
351getContactInfo :: Tox -> IO DHT.DHTPublicKey 353getContactInfo :: Tox -> IO DHT.DHTPublicKey
352getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do 354getContactInfo Tox{toxCryptoKeys,toxRouting} = join $ atomically $ do
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index bcca65e6..dcae9f2f 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -50,6 +50,8 @@ import DPut
50import Debug.Trace 50import Debug.Trace
51import Text.Printf 51import Text.Printf
52import Data.Bool 52import Data.Bool
53import Connection (Status(..))
54import Connection.Tox (ToxProgress(..))
53 55
54 56
55-- * These types are isomorphic to Maybe, but have the advantage of documenting 57-- * These types are isomorphic to Maybe, but have the advantage of documenting
@@ -99,8 +101,8 @@ instance AsMaybe UponCryptoPacket where
99 frmMaybe (Just x) = HaveCryptoPacket x 101 frmMaybe (Just x) = HaveCryptoPacket x
100 102
101 103
102data NetCryptoSessionStatus = Unaccepted | Accepted {- InProgress AwaitingSessionPacket -} | Confirmed {- Established -} 104--data NetCryptoSessionStatus = Unaccepted | Accepted {- InProgress AwaitingSessionPacket -} | Confirmed {- Established -}
103 deriving (Eq,Ord,Show,Enum) 105-- deriving (Eq,Ord,Show,Enum)
104 106
105 107
106-- | The idea of IOHook is to replicate the familiar pattern 108-- | The idea of IOHook is to replicate the familiar pattern
@@ -214,7 +216,7 @@ type SessionID = Word64
214type ListenerType = Word64 216type ListenerType = Word64
215 217
216data NetCryptoSession = NCrypto 218data NetCryptoSession = NCrypto
217 { ncState :: TVar NetCryptoSessionStatus 219 { ncState :: TVar (Status ToxProgress)
218 , ncMyPublicKey :: PublicKey 220 , ncMyPublicKey :: PublicKey
219 , ncSessionId :: SessionID 221 , ncSessionId :: SessionID
220 , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam 222 , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam
@@ -503,7 +505,10 @@ freshCryptoSession sessions
503 x <- readTVar (nextSessionId sessions) 505 x <- readTVar (nextSessionId sessions)
504 modifyTVar (nextSessionId sessions) (+1) 506 modifyTVar (nextSessionId sessions) (+1)
505 return x 507 return x
506 ncState0 <- atomically $ newTVar Accepted -- (InProgress AwaitingSessionPacket) 508 -- ncState0 <- atomically $ newTVar Accepted -- (InProgress AwaitingSessionPacket)
509 ncState0 <- atomically $ newTVar (if isJust mbtheirBaseNonce
510 then InProgress AwaitingSessionPacket
511 else InProgress AwaitingHandshake)
507 ncTheirBaseNonce0 <- atomically $ newTVar (frmMaybe mbtheirBaseNonce) 512 ncTheirBaseNonce0 <- atomically $ newTVar (frmMaybe mbtheirBaseNonce)
508 n24 <- atomically $ transportNewNonce crypto 513 n24 <- atomically $ transportNewNonce crypto
509 state <- lookupSharedSecret crypto key remotePublicKey n24 514 state <- lookupSharedSecret crypto key remotePublicKey n24
@@ -551,7 +556,7 @@ freshCryptoSession sessions
551 atomically $ do 556 atomically $ do
552 n24 <- readTVar ncMyPacketNonce0 557 n24 <- readTVar ncMyPacketNonce0
553 let n24plus1 = incrementNonce24 n24 558 let n24plus1 = incrementNonce24 n24
554 writeTVar ncMyPacketNonce0 n24plus1 559 trace ("ncMyPacketNonce+1=" ++ show n24plus1) $ writeTVar ncMyPacketNonce0 n24plus1
555 return (return (f n24, n24, ncOutgoingIdMap0)) 560 return (return (f n24, n24, ncOutgoingIdMap0))
556 pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 561 pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0
557 return (HaveHandshake pktoq) 562 return (HaveHandshake pktoq)
@@ -559,6 +564,8 @@ freshCryptoSession sessions
559 listeners <- atomically $ newTVar IntMap.empty 564 listeners <- atomically $ newTVar IntMap.empty
560 msgNum <- atomically $ newTVar 0 565 msgNum <- atomically $ newTVar 0
561 dropNum <- atomically $ newTVar 0 566 dropNum <- atomically $ newTVar 0
567 theirbasenonce <- atomically $ readTVar ncTheirBaseNonce0
568 dput XNetCrypto $ "freshCryptoSession: Session ncTheirBaseNonce=" ++ show theirbasenonce
562 let netCryptoSession0 = 569 let netCryptoSession0 =
563 NCrypto { ncState = ncState0 570 NCrypto { ncState = ncState0
564 , ncMyPublicKey = toPublic key 571 , ncMyPublicKey = toPublic key
@@ -635,8 +642,7 @@ updateCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> NetCr
635updateCryptoSession sessions addr hp session = do 642updateCryptoSession sessions addr hp session = do
636 ncState0 <- atomically $ readTVar (ncState session) 643 ncState0 <- atomically $ readTVar (ncState session)
637 ncTheirBaseNonce0 <- atomically $ readTVar (ncTheirBaseNonce session) 644 ncTheirBaseNonce0 <- atomically $ readTVar (ncTheirBaseNonce session)
638 -- if (ncState0 >= InProgress AwaitingSessionPacket) 645 if (ncState0 >= {-Accepted-}InProgress AwaitingSessionPacket)
639 if (ncState0 >= Accepted)
640 -- If the nonce in the handshake and the dht key are both the same as 646 -- If the nonce in the handshake and the dht key are both the same as
641 -- the ones we have saved, assume we already handled this and this is a 647 -- the ones we have saved, assume we already handled this and this is a
642 -- duplicate handshake packet, otherwise disregard everything, and 648 -- duplicate handshake packet, otherwise disregard everything, and
@@ -661,7 +667,7 @@ updateCryptoSession sessions addr hp session = do
661 ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp)) 667 ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp))
662 if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp)) 668 if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp))
663 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh 669 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh
664 else atomically $ writeTVar (ncState session) Accepted -- (InProgress AwaitingSessionPacket) 670 else atomically $ writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket)
665 671
666 672
667cryptoNetHandler :: NetCryptoSessions -> SockAddr -> NetCrypto -> IO (Maybe (NetCrypto -> NetCrypto)) 673cryptoNetHandler :: NetCryptoSessions -> SockAddr -> NetCrypto -> IO (Maybe (NetCrypto -> NetCrypto))
@@ -728,6 +734,7 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non
728 , hpCookieRemotePubkey = remotePublicKey 734 , hpCookieRemotePubkey = remotePublicKey
729 , hpCookieRemoteDhtkey = remoteDhtPublicKey 735 , hpCookieRemoteDhtkey = remoteDhtPublicKey
730 }) -> do 736 }) -> do
737 dput XNetCrypto ("cryptoNetHandler: hpTheirBaseNonce = " ++ show theirBaseNonce)
731 sessionsmap <- atomically $ readTVar allsessions 738 sessionsmap <- atomically $ readTVar allsessions
732 -- Do a lookup, so we can handle the update case differently 739 -- Do a lookup, so we can handle the update case differently
733 case Map.lookup addr sessionsmap of 740 case Map.lookup addr sessionsmap of
@@ -752,7 +759,10 @@ cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do
752 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks, 759 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks,
753 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce, 760 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce,
754 ncPingMachine}) -> do 761 ncPingMachine}) -> do
755 HaveHandshake theirBaseNonce <- atomically $ readTVar ncTheirBaseNonce 762 mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce
763 case mbTheirBaseNonce of
764 NeedHandshake -> dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?" >> return Nothing
765 HaveHandshake theirBaseNonce -> do
756 -- Try to decrypt message 766 -- Try to decrypt message
757 let diff :: Word16 767 let diff :: Word16
758 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16 768 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16
@@ -796,7 +806,7 @@ cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do
796 ++ " = " ++ show x) (return ()) 806 ++ " = " ++ show x) (return ())
797 writeTVar ncTheirBaseNonce (HaveHandshake y) 807 writeTVar ncTheirBaseNonce (HaveHandshake y)
798 -- then set session confirmed, 808 -- then set session confirmed,
799 atomically $ writeTVar ncState Confirmed {-Established-} 809 atomically $ writeTVar ncState {-Confirmed-}Established
800 -- bump ping machine 810 -- bump ping machine
801 case ncPingMachine of 811 case ncPingMachine of
802 Just pingMachine -> pingBump pingMachine 812 Just pingMachine -> pingBump pingMachine