diff options
author | joe <joe@jerkface.net> | 2017-08-11 05:50:23 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-08-11 05:50:23 -0400 |
commit | 1d4651428baf4a0c45e35fc909407e2d64d7da0b (patch) | |
tree | d8d38c1e1fead3f48683a8911364405986b1fdca /Tox.hs | |
parent | d5e16955b8264642aae093b345ba356974a60c5f (diff) |
Unverified: data to route response packet sending.
Diffstat (limited to 'Tox.hs')
-rw-r--r-- | Tox.hs | 62 |
1 files changed, 51 insertions, 11 deletions
@@ -459,6 +459,17 @@ decryptAssymetric sk typ assym | |||
459 | . second (either (const (Tox.Nonce8 0)) id . S.decode) | 459 | . second (either (const (Tox.Nonce8 0)) id . S.decode) |
460 | $ B.splitAt (B.length bs - 8) bs | 460 | $ B.splitAt (B.length bs - 8) bs |
461 | 461 | ||
462 | -- TODO: We should not be having to re-serialize this data... :/ | ||
463 | -- There should be a way to pass the Tox.Assymetric value up the layers. | ||
464 | passThroughAssymetric :: Tox.PacketKind -> Tox.PubKey -> Tox.Assymetric -> Either String Msg | ||
465 | passThroughAssymetric typ k assym | ||
466 | = Right $ Msg | ||
467 | { msgNonce = Tox.assymetricNonce . Tox.sent $ assym -- Not used. | ||
468 | , msgType = typ | ||
469 | , msgData = S.encode (k,assym) | ||
470 | , msgSendBack = Nonce8 0 -- Not used. | ||
471 | } | ||
472 | |||
462 | {- | 473 | {- |
463 | decryptUnclm :: SecretKey -> Tox.PacketKind -> NodeId -> Tox.Nonce8 -> Tox.UnclaimedAssymetric -> Either String Msg | 474 | decryptUnclm :: SecretKey -> Tox.PacketKind -> NodeId -> Tox.Nonce8 -> Tox.UnclaimedAssymetric -> Either String Msg |
464 | decryptUnclm sk typ sender n8 unclm | 475 | decryptUnclm sk typ sender n8 unclm |
@@ -559,8 +570,9 @@ msgLayer sk pk = layerTransport parse serialize | |||
559 | parse :: (Tox.PacketKind,InterediateRep) -> ToxPath -> Either String (Msg,ToxPath) | 570 | parse :: (Tox.PacketKind,InterediateRep) -> ToxPath -> Either String (Msg,ToxPath) |
560 | parse (typ,Assym x) addr = fmap (,addr) $ decryptAssymetric sk typ x | 571 | parse (typ,Assym x) addr = fmap (,addr) $ decryptAssymetric sk typ x |
561 | parse (typ,Assym' x) addr = fmap (,addr) $ decryptAssymetric sk typ x | 572 | parse (typ,Assym' x) addr = fmap (,addr) $ decryptAssymetric sk typ x |
573 | parse (typ,ToRoute k x) addr = fmap (,addr) $ passThroughAssymetric typ k x | ||
562 | parse (typ,Unclm n x) addr = Right ( Msg typ (Tox.assymetricNonce x) (S.encode (Tox.assymetricData x)) n | 574 | parse (typ,Unclm n x) addr = Right ( Msg typ (Tox.assymetricNonce x) (S.encode (Tox.assymetricData x)) n |
563 | , addr) | 575 | , addr) |
564 | serialize :: Msg -> ToxPath -> ((Tox.PacketKind,InterediateRep),ToxPath) | 576 | serialize :: Msg -> ToxPath -> ((Tox.PacketKind,InterediateRep),ToxPath) |
565 | serialize x addr@(ToxPath ni _) = case Tox.pktClass (msgType x) of | 577 | serialize x addr@(ToxPath ni _) = case Tox.pktClass (msgType x) of |
566 | Tox.AssymetricClass {} -> ((msgType x, Assym $ encryptAssymetric sk pk (nodeId ni) x), addr) | 578 | Tox.AssymetricClass {} -> ((msgType x, Assym $ encryptAssymetric sk pk (nodeId ni) x), addr) |
@@ -569,7 +581,9 @@ msgLayer sk pk = layerTransport parse serialize | |||
569 | 581 | ||
570 | data InterediateRep = Assym Tox.Assymetric | 582 | data InterediateRep = Assym Tox.Assymetric |
571 | | Assym' Tox.Assymetric | 583 | | Assym' Tox.Assymetric |
584 | | ToRoute Tox.PubKey Tox.Assymetric | ||
572 | | Unclm Tox.Nonce8 Tox.UnclaimedAssymetric | 585 | | Unclm Tox.Nonce8 Tox.UnclaimedAssymetric |
586 | | RouteResponse Tox.Packet | ||
573 | 587 | ||
574 | asymLayer :: Transport String SockAddr Tox.Packet -> Transport String ToxPath (Tox.PacketKind,InterediateRep) | 588 | asymLayer :: Transport String SockAddr Tox.Packet -> Transport String ToxPath (Tox.PacketKind,InterediateRep) |
575 | asymLayer = layerTransport parse (\p@(typ,_) -> trace ("SERIALIZE "++show typ) $ serialize p) | 589 | asymLayer = layerTransport parse (\p@(typ,_) -> trace ("SERIALIZE "++show typ) $ serialize p) |
@@ -577,10 +591,10 @@ asymLayer = layerTransport parse (\p@(typ,_) -> trace ("SERIALIZE "++show typ) $ | |||
577 | parse :: Tox.Packet -> SockAddr -> Either String ((Tox.PacketKind,InterediateRep),ToxPath) | 591 | parse :: Tox.Packet -> SockAddr -> Either String ((Tox.PacketKind,InterediateRep),ToxPath) |
578 | parse x addr = case Tox.pktClass (Tox.pktKind x) of | 592 | parse x addr = case Tox.pktClass (Tox.pktKind x) of |
579 | Tox.AssymetricClass top fromp -> go Tox.senderKey fromp Assym | 593 | Tox.AssymetricClass top fromp -> go Tox.senderKey fromp Assym |
580 | Tox.AliasedClass top fromp -> let (Tox.Aliased a,rpath) = fromp x | 594 | Tox.AliasedClass top fromp -> goalias $ fromp x |
581 | in fmap (\ni -> ( (Tox.pktKind x, Assym' a) | 595 | Tox.ToRouteClass top fromp -> do let (key,y) = fromp x |
582 | , ToxPath ni rpath )) | 596 | ((typ,Assym' a),addr') <- goalias y |
583 | $ nodeInfo (Tox.senderKey a) addr | 597 | return ((typ,ToRoute key a),addr') |
584 | Tox.NoncedUnclaimedClass top fromp -> go (const zeroID) fromp (uncurry Unclm) | 598 | Tox.NoncedUnclaimedClass top fromp -> go (const zeroID) fromp (uncurry Unclm) |
585 | -- OnionClass | 599 | -- OnionClass |
586 | where go mkaddr fromp c = let y = fromp x | 600 | where go mkaddr fromp c = let y = fromp x |
@@ -588,14 +602,19 @@ asymLayer = layerTransport parse (\p@(typ,_) -> trace ("SERIALIZE "++show typ) $ | |||
588 | . (\ni -> ToxPath ni Tox.emptyReturnPath) | 602 | . (\ni -> ToxPath ni Tox.emptyReturnPath) |
589 | ) | 603 | ) |
590 | $ nodeInfo (mkaddr y) addr | 604 | $ nodeInfo (mkaddr y) addr |
605 | goalias (Tox.Aliased a,rpath) = fmap (\ni -> ( (Tox.pktKind x, Assym' a) | ||
606 | , ToxPath ni rpath )) | ||
607 | $ nodeInfo (Tox.senderKey a) addr | ||
591 | 608 | ||
592 | serialize :: (Tox.PacketKind,InterediateRep) -> ToxPath -> (Tox.Packet,SockAddr) | 609 | serialize :: (Tox.PacketKind,InterediateRep) -> ToxPath -> (Tox.Packet,SockAddr) |
593 | serialize (typ,Assym assym) (ToxPath addr rpath) = (x,nodeAddr addr) | 610 | serialize (typ,Assym assym) (ToxPath addr rpath) = (x,nodeAddr addr) |
594 | where x = case Tox.pktClass typ of Tox.AssymetricClass top _ -> top assym | 611 | where x = case Tox.pktClass typ of Tox.AssymetricClass top _ -> top assym |
595 | serialize (typ,Assym' assym) (ToxPath addr rpath) = (x,nodeAddr addr) -- TODO rpath | 612 | serialize (typ,Assym' assym) (ToxPath addr rpath) = (x,nodeAddr addr) -- TODO rpath |
596 | where x = case Tox.pktClass typ of Tox.AliasedClass top _ -> top (Tox.Aliased assym, error "todo: ReturnPath") | 613 | where x = case Tox.pktClass typ of Tox.AliasedClass top _ -> top (Tox.Aliased assym, error "todo: ReturnPath") |
614 | -- An unclm sent to a ToxPath is turned into an OnionResponse before being sent out. | ||
597 | serialize (typ,Unclm nonce unclm) (ToxPath addr rpath) = (Tox.mkOnion rpath x,nodeAddr addr) | 615 | serialize (typ,Unclm nonce unclm) (ToxPath addr rpath) = (Tox.mkOnion rpath x,nodeAddr addr) |
598 | where x = case Tox.pktClass typ of Tox.NoncedUnclaimedClass top _ -> top nonce unclm | 616 | where x = case Tox.pktClass typ of Tox.NoncedUnclaimedClass top _ -> top nonce unclm |
617 | serialize (_,RouteResponse x) (ToxPath addr rpath) = (Tox.mkOnion rpath x, nodeAddr addr) | ||
599 | -- OnionClass | 618 | -- OnionClass |
600 | 619 | ||
601 | toxLayer :: Transport String SockAddr ByteString -> Transport String SockAddr Tox.Packet | 620 | toxLayer :: Transport String SockAddr ByteString -> Transport String SockAddr Tox.Packet |
@@ -627,10 +646,12 @@ trimPackets addr bs = do | |||
627 | GetNodesType -> Just id | 646 | GetNodesType -> Just id |
628 | AnnounceType -> Just id | 647 | AnnounceType -> Just id |
629 | AnnounceResponseType -> Just id | 648 | AnnounceResponseType -> Just id |
649 | DataRequestType -> Just id | ||
650 | -- DataResponseType -> Just id | ||
630 | OnionResponse3Type -> Just id | 651 | OnionResponse3Type -> Just id |
631 | _ -> Nothing | 652 | _ -> Nothing |
632 | 653 | ||
633 | newClient :: SockAddr -> IO (ToxClient, Routing) | 654 | newClient :: SockAddr -> IO (ToxClient, Routing, TVar AnnouncedKeys) |
634 | newClient addr = do | 655 | newClient addr = do |
635 | udp <- udpTransport addr | 656 | udp <- udpTransport addr |
636 | secret <- generateSecretKey | 657 | secret <- generateSecretKey |
@@ -691,7 +712,8 @@ newClient addr = do | |||
691 | $ addVerbosity | 712 | $ addVerbosity |
692 | $ msgLayer secret pubkey | 713 | $ msgLayer secret pubkey |
693 | $ onInbound (updateRouting client routing) | 714 | $ onInbound (updateRouting client routing) |
694 | $ asymLayer | 715 | $ asymnet |
716 | asymnet = asymLayer | ||
695 | -- $ addHandler (handleMessage aclient) | 717 | -- $ addHandler (handleMessage aclient) |
696 | $ toxLayer | 718 | $ toxLayer |
697 | $ addVerbosity2 | 719 | $ addVerbosity2 |
@@ -716,6 +738,7 @@ newClient addr = do | |||
716 | handlers PingType = handler PongType pingH | 738 | handlers PingType = handler PongType pingH |
717 | handlers GetNodesType = handler SendNodesType $ getNodesH routing | 739 | handlers GetNodesType = handler SendNodesType $ getNodesH routing |
718 | handlers AnnounceType = handler' AnnounceResponseType $ announceH routing toks keydb | 740 | handlers AnnounceType = handler' AnnounceResponseType $ announceH routing toks keydb |
741 | handlers DataRequestType = Just $ NoReply (S.decode . msgData) $ dataToRouteH keydb asymnet | ||
719 | {- | 742 | {- |
720 | handlers var OnionRequest0 = noreply OnionRequest0 | 743 | handlers var OnionRequest0 = noreply OnionRequest0 |
721 | $ onionSend0H (symmetricCipher (return symkey) | 744 | $ onionSend0H (symmetricCipher (return symkey) |
@@ -764,7 +787,7 @@ newClient addr = do | |||
764 | , clientResponseId = genNonce24 var | 787 | , clientResponseId = genNonce24 var |
765 | } | 788 | } |
766 | 789 | ||
767 | return (client, routing) | 790 | return (client, routing, keydb) |
768 | 791 | ||
769 | toxKademlia :: ToxClient -> TriadCommittee NodeId SockAddr -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo | 792 | toxKademlia :: ToxClient -> TriadCommittee NodeId SockAddr -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo |
770 | toxKademlia client committee var sched | 793 | toxKademlia client committee var sched |
@@ -862,6 +885,8 @@ classify (Msg { msgType = typ | |||
862 | OnionRequest1Type -> IsQuery typ | 885 | OnionRequest1Type -> IsQuery typ |
863 | OnionRequest2Type -> IsQuery typ | 886 | OnionRequest2Type -> IsQuery typ |
864 | AnnounceType -> IsQuery typ | 887 | AnnounceType -> IsQuery typ |
888 | DataRequestType -> IsQuery typ | ||
889 | DataResponseType -> IsResponse | ||
865 | _ -> const $ IsUnknown ("Unknown message type: "++show typ) | 890 | _ -> const $ IsUnknown ("Unknown message type: "++show typ) |
866 | 891 | ||
867 | {- | 892 | {- |
@@ -1151,6 +1176,19 @@ getNodesH routing addr (GetNodes nid) = do | |||
1151 | 1176 | ||
1152 | k = 4 | 1177 | k = 4 |
1153 | 1178 | ||
1179 | |||
1180 | dataToRouteH :: TVar AnnouncedKeys -> Transport err ToxPath (Tox.PacketKind,InterediateRep) -> addr -> (Tox.PubKey,Assymetric) -> IO () | ||
1181 | dataToRouteH keydb udp _ (k,assym) = do | ||
1182 | mb <- atomically $ do | ||
1183 | ks <- readTVar keydb | ||
1184 | forM (MinMaxPSQ.lookup' k (keyAssoc ks)) $ \(p,(cnt,rpath)) -> do | ||
1185 | writeTVar keydb $ ks { keyAssoc = MinMaxPSQ.insert' k (cnt + 1, rpath) p (keyAssoc ks) } | ||
1186 | return rpath | ||
1187 | forM_ mb $ \rpath -> do | ||
1188 | -- forward | ||
1189 | sendMessage udp rpath (DataResponseType, RouteResponse $ DataToRouteResponse $ Aliased assym) | ||
1190 | hPutStrLn stderr $ "Forwarding data-to-route -->"++show k | ||
1191 | |||
1154 | -- Toxcore generates `ping_id`s by taking a 32 byte sha hash of the current time, | 1192 | -- Toxcore generates `ping_id`s by taking a 32 byte sha hash of the current time, |
1155 | -- some secret bytes generated when the instance is created, the current time | 1193 | -- some secret bytes generated when the instance is created, the current time |
1156 | -- divided by a 20 second timeout, the public key of the requester and the source | 1194 | -- divided by a 20 second timeout, the public key of the requester and the source |
@@ -1187,7 +1225,7 @@ announceH routing toks keydb (ToxPath naddr retpath) req = do | |||
1187 | (announceSeeking req) | 1225 | (announceSeeking req) |
1188 | modifyTVar' keydb (insertKey tm (announceSeeking req) toxpath d) | 1226 | modifyTVar' keydb (insertKey tm (announceSeeking req) toxpath d) |
1189 | ks <- readTVar keydb | 1227 | ks <- readTVar keydb |
1190 | return $ snd <$> MinMaxPSQ.lookup' (announceSeeking req) (keyAssoc ks) | 1228 | return $ snd . snd <$> MinMaxPSQ.lookup' (announceSeeking req) (keyAssoc ks) |
1191 | newtok <- if storing | 1229 | newtok <- if storing |
1192 | then Nonce32 . toPaddedByteString 32 <$> grantToken toks naddr | 1230 | then Nonce32 . toPaddedByteString 32 <$> grantToken toks naddr |
1193 | else return $ zeros32 | 1231 | else return $ zeros32 |
@@ -1332,11 +1370,13 @@ type NodeDistance = Tox.PubKey | |||
1332 | 1370 | ||
1333 | data AnnouncedKeys = AnnouncedKeys | 1371 | data AnnouncedKeys = AnnouncedKeys |
1334 | { keyByAge :: PSQ NodeId (Down POSIXTime) -- timeout of 300 seconds | 1372 | { keyByAge :: PSQ NodeId (Down POSIXTime) -- timeout of 300 seconds |
1335 | , keyAssoc :: MinMaxPSQ' Tox.PubKey NodeDistance ToxPath | 1373 | , keyAssoc :: MinMaxPSQ' Tox.PubKey NodeDistance (Int,ToxPath) |
1336 | } | 1374 | } |
1337 | 1375 | ||
1338 | insertKey :: POSIXTime -> Tox.PubKey -> ToxPath -> NodeDistance -> AnnouncedKeys -> AnnouncedKeys | 1376 | insertKey :: POSIXTime -> Tox.PubKey -> ToxPath -> NodeDistance -> AnnouncedKeys -> AnnouncedKeys |
1339 | insertKey tm pub toxpath d keydb = AnnouncedKeys | 1377 | insertKey tm pub toxpath d keydb = AnnouncedKeys |
1340 | { keyByAge = PSQ.insert pub (Down tm) (keyByAge keydb) | 1378 | { keyByAge = PSQ.insert pub (Down tm) (keyByAge keydb) |
1341 | , keyAssoc = MinMaxPSQ.insert' pub toxpath d (keyAssoc keydb) | 1379 | , keyAssoc = case MinMaxPSQ.lookup' pub (keyAssoc keydb) of |
1380 | Just (_,(cnt,_)) -> MinMaxPSQ.insert' pub (cnt,toxpath) d (keyAssoc keydb) | ||
1381 | Nothing -> MinMaxPSQ.insert' pub (0 ,toxpath) d (keyAssoc keydb) | ||
1342 | } | 1382 | } |