diff options
author | James Crayne <jim.crayne@gmail.com> | 2018-05-29 21:16:37 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2018-05-29 21:16:37 +0000 |
commit | ee0f50943efd65eb1c906338b00c204591826a16 (patch) | |
tree | fce79735472afbb483d6946cfcea53629ed59d12 /src/Network/Tox | |
parent | a4d4c31015014cba7f9305710b57b589eb532f5a (diff) |
sendCrypto & friends
* sendOnline
* sendOffline
* sendKill
* setNick
* setStatus
* setStatusMsg
* setTyping
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 80 |
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 | ||
290 | type XMessage = CryptoMessage -- todo | 290 | type 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 | |||
292 | ncToWire :: STM (State,Nonce24,RangeMap TArray Word8 TVar) | 301 | ncToWire :: 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 | |||
540 | cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do | 548 | cryptoNetHandler 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 | ||
682 | sendCrypto :: TransportCrypto -> NetCryptoSession -> (STM ()) -> CryptoMessage -> IO (Either String ()) | ||
683 | sendCrypto 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 | |||
694 | sendOnline :: TransportCrypto -> NetCryptoSession -> IO (Either String ()) | ||
695 | sendOnline crypto session = sendCrypto crypto session (return ()) (OneByte ONLINE) | ||
696 | |||
697 | sendOffline :: TransportCrypto -> NetCryptoSession -> IO (Either String ()) | ||
698 | sendOffline crypto session = sendCrypto crypto session (return ()) (OneByte OFFLINE) | ||
699 | |||
700 | |||
701 | sendKill :: TransportCrypto -> NetCryptoSession -> IO (Either String ()) | ||
702 | sendKill crypto session = sendCrypto crypto session (return ()) (OneByte KillPacket) | ||
703 | |||
674 | setNick :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) | 704 | setNick :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) |
675 | setNick crypto session nick = do | 705 | setNick 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 | -- | ||
688 | setTyping :: TransportCrypto -> NetCryptoSession -> TypingStatus -> IO (Either String ()) | 717 | setTyping :: TransportCrypto -> NetCryptoSession -> TypingStatus -> IO (Either String ()) |
689 | setTyping crypto session status = do | 718 | setTyping 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 | ||
697 | setNoSpam :: TransportCrypto -> NetCryptoSession -> Maybe NoSpam -> IO (Either String ()) | 725 | setNoSpam :: TransportCrypto -> NetCryptoSession -> Maybe NoSpam -> IO (Either String ()) |
698 | setNoSpam crypto session mbnospam = do | 726 | setNoSpam crypto session mbnospam = do |
@@ -704,12 +732,11 @@ setNoSpam crypto session mbnospam = do | |||
704 | 732 | ||
705 | setStatus :: TransportCrypto -> NetCryptoSession -> UserStatus -> IO (Either String ()) | 733 | setStatus :: TransportCrypto -> NetCryptoSession -> UserStatus -> IO (Either String ()) |
706 | setStatus crypto session status = do | 734 | setStatus 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 | ||
714 | setStatusMsg :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) | 741 | setStatusMsg :: TransportCrypto -> NetCryptoSession -> ByteString -> IO (Either String ()) |
715 | setStatusMsg crypto session msg = do | 742 | setStatusMsg 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 |
728 | defaultCryptoDataHooks :: Map.Map MessageType [NetCryptoHook] | 754 | defaultCryptoDataHooks :: Map.Map MessageType [NetCryptoHook] |