summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2018-05-29 21:16:37 +0000
committerJames Crayne <jim.crayne@gmail.com>2018-05-29 21:16:37 +0000
commitee0f50943efd65eb1c906338b00c204591826a16 (patch)
treefce79735472afbb483d6946cfcea53629ed59d12
parenta4d4c31015014cba7f9305710b57b589eb532f5a (diff)
sendCrypto & friends
* sendOnline * sendOffline * sendKill * setNick * setStatus * setStatusMsg * setTyping
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs80
1 files changed, 53 insertions, 27 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index 2fb7f2c1..486ee076 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -289,6 +289,15 @@ newHandShakeData crypto basenonce (HParam {hpOtherCookie,hpMySecretKey,hpCookieR
289 289
290type XMessage = CryptoMessage -- todo 290type XMessage = CryptoMessage -- todo
291 291
292-- THIS Would work if not for the IO shared secret cache...
293-- increments packet nonce, only call when actually queuing an outgoing packet
294-- getOutGoingParam crypto session = do
295-- n24 <- (ncMyPacketNonce session)
296-- let state = computeSharedSecret (transportSecret crypto) (ncTheirPublicKey session) n24
297-- modifyTVar (ncMyPacketNonce session) (+1)
298-- rangemap <- readTVar (ncOutgoingIdMap session)
299-- return (state,n24,rangemap)
300
292ncToWire :: STM (State,Nonce24,RangeMap TArray Word8 TVar) 301ncToWire :: STM (State,Nonce24,RangeMap TArray Word8 TVar)
293 -> Word32{- packet number we expect to recieve -} 302 -> Word32{- packet number we expect to recieve -}
294 -> Word32{- buffer_end -} 303 -> Word32{- buffer_end -}
@@ -536,7 +545,6 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non
536 Just session -> updateCryptoSession sessions addr hp session -- update existing session 545 Just session -> updateCryptoSession sessions addr hp session -- update existing session
537 return Nothing 546 return Nothing
538 547
539
540cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do 548cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do
541 let crypto = transportCrypto sessions 549 let crypto = transportCrypto sessions
542 allsessions = netCryptoSessions sessions 550 allsessions = netCryptoSessions sessions
@@ -671,28 +679,48 @@ allMsgTypes fDefault = A.listArray (minBound,maxBound) (0:knownMsgs)
671 , map (const 0) [ 200 .. 255 ] -- All lossy, exept the last 679 , map (const 0) [ 200 .. 255 ] -- All lossy, exept the last
672 ] 680 ]
673 681
682sendCrypto :: TransportCrypto -> NetCryptoSession -> (STM ()) -> CryptoMessage -> IO (Either String ())
683sendCrypto crypto session updateLocal cm = do
684 let outq = ncOutgoingQueue session
685 -- XXX: potential race? if shared secret comes out of sync with cache?
686 getOutGoingParam <- PQ.readyOutGoing outq
687 atomically $ do
688 result <- PQ.tryAppendQueueOutgoing getOutGoingParam outq cm
689 case result of
690 PQ.OGSuccess -> updateLocal >> return (Right())
691 PQ.OGFull -> return (Left "Outgoing packet buffer is full")
692 PQ.OGEncodeFail -> return (Left "Failed to encode outgoing packet")
693
694sendOnline :: TransportCrypto -> NetCryptoSession -> IO (Either String ())
695sendOnline crypto session = sendCrypto crypto session (return ()) (OneByte ONLINE)
696
697sendOffline :: TransportCrypto -> NetCryptoSession -> IO (Either String ())
698sendOffline crypto session = sendCrypto crypto session (return ()) (OneByte OFFLINE)
699
700
701sendKill :: TransportCrypto -> NetCryptoSession -> IO (Either String ())
702sendKill crypto session = sendCrypto crypto session (return ()) (OneByte KillPacket)
703
674setNick :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) 704setNick :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ())
675setNick crypto session nick = do 705setNick crypto session nick = do
676 let Just (_,maxlen) = msgSizeParam NICKNAME 706 let Just (_,maxlen) = msgSizeParam NICKNAME
677 if B.length nick > maxlen 707 if B.length nick > maxlen
678 then return (Left $ "nickname must not exceed " ++ show maxlen ++ " bytes.") 708 then return (Left $ "nickname must not exceed " ++ show maxlen ++ " bytes.")
679 else do 709 else do
680 let viewVar = ncView session 710 let updateLocal = do
681 atomically $ do 711 let viewVar = ncView session
682 view <- readTVar viewVar 712 view <- readTVar viewVar
683 writeTVar (svNick view) nick 713 writeTVar (svNick view) nick
684 let nickPacket = error "todo" 714 let cm = UpToN NICKNAME nick
685 return (Left "TODO: sendMessage crypto (NetCrypto nickPacket)") 715 sendCrypto crypto session updateLocal cm
686 -- return (Right ()) 716
687 --
688setTyping :: TransportCrypto -> NetCryptoSession -> TypingStatus -> IO (Either String ()) 717setTyping :: TransportCrypto -> NetCryptoSession -> TypingStatus -> IO (Either String ())
689setTyping crypto session status = do 718setTyping crypto session status = do
690 let viewVar = ncView session 719 let updateLocal = do
691 atomically $ do 720 view <- readTVar (ncView session)
692 view <- readTVar viewVar 721 writeTVar (svTyping view) status
693 writeTVar (svTyping view) status 722 let cm = TwoByte TYPING (fromEnum8 status)
694 let typingPacket = error "todo" 723 sendCrypto crypto session updateLocal cm
695 return (Left "TODO: sendMessage crypto (NetCrypto typingPacket)")
696 724
697setNoSpam :: TransportCrypto -> NetCryptoSession -> Maybe NoSpam -> IO (Either String ()) 725setNoSpam :: TransportCrypto -> NetCryptoSession -> Maybe NoSpam -> IO (Either String ())
698setNoSpam crypto session mbnospam = do 726setNoSpam crypto session mbnospam = do
@@ -704,12 +732,11 @@ setNoSpam crypto session mbnospam = do
704 732
705setStatus :: TransportCrypto -> NetCryptoSession -> UserStatus -> IO (Either String ()) 733setStatus :: TransportCrypto -> NetCryptoSession -> UserStatus -> IO (Either String ())
706setStatus crypto session status = do 734setStatus crypto session status = do
707 let viewVar = ncView session 735 let updateLocal = do
708 atomically $ do 736 view <- readTVar (ncView session)
709 view <- readTVar viewVar 737 writeTVar (svStatus view) status
710 writeTVar (svStatus view) status 738 let cm = TwoByte USERSTATUS (fromEnum8 status)
711 let statusPacket = error "todo" 739 sendCrypto crypto session updateLocal cm
712 return (Left "TODO: sendMessage crypto (NetCrypto statusPacket)")
713 740
714setStatusMsg :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) 741setStatusMsg :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ())
715setStatusMsg crypto session msg = do 742setStatusMsg crypto session msg = do
@@ -717,12 +744,11 @@ setStatusMsg crypto session msg = do
717 if B.length msg > maxlen 744 if B.length msg > maxlen
718 then return (Left $ "status message must not exceed " ++ show maxlen ++ " bytes.") 745 then return (Left $ "status message must not exceed " ++ show maxlen ++ " bytes.")
719 else do 746 else do
720 let viewVar = ncView session 747 let updateLocal = do
721 atomically $ do 748 view <- readTVar (ncView session)
722 view <- readTVar viewVar 749 writeTVar (svStatusMsg view) msg
723 writeTVar (svStatusMsg view) msg 750 let cm = UpToN STATUSMESSAGE msg
724 let statusMsgPacket = error "todo" 751 sendCrypto crypto session updateLocal cm
725 return (Left "TODO: sendMessage crypto (NetCrypto statusMsgPacket)")
726 752
727-- | handles nothings 753-- | handles nothings
728defaultCryptoDataHooks :: Map.Map MessageType [NetCryptoHook] 754defaultCryptoDataHooks :: Map.Map MessageType [NetCryptoHook]