diff options
author | joe <joe@jerkface.net> | 2017-07-29 00:58:52 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-29 01:03:35 -0400 |
commit | f876da224f503542394b3d7614fcc161106ebbb4 (patch) | |
tree | 411d1c94ac482a94dcaeb6ff17175673bce0939b /Mainline.hs | |
parent | d0ff6c3ac977035f3493b679978da73517550028 (diff) |
Detect a global ipv6 address via "ip" command.
Diffstat (limited to 'Mainline.hs')
-rw-r--r-- | Mainline.hs | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/Mainline.hs b/Mainline.hs index deb75079..6aa1c517 100644 --- a/Mainline.hs +++ b/Mainline.hs | |||
@@ -77,6 +77,7 @@ import Control.Exception (SomeException (..), handle) | |||
77 | import qualified Data.Aeson as JSON | 77 | import qualified Data.Aeson as JSON |
78 | ;import Data.Aeson (FromJSON, ToJSON, (.=)) | 78 | ;import Data.Aeson (FromJSON, ToJSON, (.=)) |
79 | import Text.Read | 79 | import Text.Read |
80 | import Global6 | ||
80 | 81 | ||
81 | newtype NodeId = NodeId ByteString | 82 | newtype NodeId = NodeId ByteString |
82 | deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable) | 83 | deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable) |
@@ -524,17 +525,24 @@ newClient :: SockAddr -> IO (MainlineClient, Routing, SwarmsDatabase) | |||
524 | newClient addr = do | 525 | newClient addr = do |
525 | udp <- udpTransport addr | 526 | udp <- udpTransport addr |
526 | nid <- NodeId <$> getRandomBytes 20 | 527 | nid <- NodeId <$> getRandomBytes 20 |
527 | let tenative_info = NodeInfo | 528 | let tentative_info = NodeInfo |
528 | { nodeId = nid | 529 | { nodeId = nid |
529 | , nodeIP = fromMaybe (toEnum 0) $ fromSockAddr addr | 530 | , nodeIP = fromMaybe (toEnum 0) $ fromSockAddr addr |
530 | , nodePort = fromMaybe 0 $ sockAddrPort addr | 531 | , nodePort = fromMaybe 0 $ sockAddrPort addr |
531 | } | 532 | } |
533 | tentative_info6 <- | ||
534 | maybe tentative_info | ||
535 | (\ip6 -> tentative_info { nodeId = fromMaybe (nodeId tentative_info) | ||
536 | $ bep42 (toSockAddr ip6) (nodeId tentative_info) | ||
537 | , nodeIP = IPv6 ip6 | ||
538 | }) | ||
539 | <$> global6 | ||
532 | addr4 <- atomically $ newTChan | 540 | addr4 <- atomically $ newTChan |
533 | addr6 <- atomically $ newTChan | 541 | addr6 <- atomically $ newTChan |
534 | routing <- atomically $ do | 542 | routing <- atomically $ do |
535 | let nobkts = R.defaultBucketCount :: Int | 543 | let nobkts = R.defaultBucketCount :: Int |
536 | tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tenative_info nobkts | 544 | tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info nobkts |
537 | tbl6 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tenative_info nobkts | 545 | tbl6 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info6 nobkts |
538 | let updateIPVote tblvar addrvar a = do | 546 | let updateIPVote tblvar addrvar a = do |
539 | bkts <- readTVar tblvar | 547 | bkts <- readTVar tblvar |
540 | case bep42 a (nodeId $ R.thisNode bkts) of | 548 | case bep42 a (nodeId $ R.thisNode bkts) of |
@@ -552,7 +560,7 @@ newClient addr = do | |||
552 | committee6 <- newTriadCommittee $ updateIPVote tbl6 addr6 | 560 | committee6 <- newTriadCommittee $ updateIPVote tbl6 addr6 |
553 | sched4 <- newTVar Int.empty | 561 | sched4 <- newTVar Int.empty |
554 | sched6 <- newTVar Int.empty | 562 | sched6 <- newTVar Int.empty |
555 | return $ Routing tenative_info sched4 tbl4 committee4 sched6 tbl6 committee6 | 563 | return $ Routing tentative_info sched4 tbl4 committee4 sched6 tbl6 committee6 |
556 | swarms <- newSwarmsDatabase | 564 | swarms <- newSwarmsDatabase |
557 | map_var <- atomically $ newTVar (0, mempty) | 565 | map_var <- atomically $ newTVar (0, mempty) |
558 | let net = onInbound (updateRouting outgoingClient routing) | 566 | let net = onInbound (updateRouting outgoingClient routing) |
@@ -619,11 +627,11 @@ newClient addr = do | |||
619 | refresh_thread4 <- forkPollForRefresh | 627 | refresh_thread4 <- forkPollForRefresh |
620 | (15*60) | 628 | (15*60) |
621 | (sched4 routing) | 629 | (sched4 routing) |
622 | (refreshBucket (nodeSearch client) (routing4 routing) (nodeId tenative_info)) | 630 | (refreshBucket (nodeSearch client) (routing4 routing)) |
623 | refresh_thread6 <- forkPollForRefresh | 631 | refresh_thread6 <- forkPollForRefresh |
624 | (15*60) | 632 | (15*60) |
625 | (sched6 routing) | 633 | (sched6 routing) |
626 | (refreshBucket (nodeSearch client) (routing6 routing) (nodeId tenative_info)) | 634 | (refreshBucket (nodeSearch client) (routing6 routing)) |
627 | 635 | ||
628 | return (client, routing, swarms) | 636 | return (client, routing, swarms) |
629 | 637 | ||
@@ -705,13 +713,15 @@ updateRouting client routing naddr msg = do | |||
705 | Want_IP6 -> go (routing6 routing) (committee6 routing) (sched6 routing) | 713 | Want_IP6 -> go (routing6 routing) (committee6 routing) (sched6 routing) |
706 | where | 714 | where |
707 | go tbl committee sched = do | 715 | go tbl committee sched = do |
708 | case msg of | 716 | self <- atomically $ R.thisNode <$> readTVar tbl |
709 | R { rspReflectedIP = Just sockaddr } | 717 | when (nodeIP self /= nodeIP naddr) $ do |
710 | -> do | 718 | case msg of |
711 | -- hPutStrLn stderr $ "External: "++show (nodeId naddr,sockaddr) | 719 | R { rspReflectedIP = Just sockaddr } |
712 | atomically $ addVote committee (nodeId naddr) sockaddr | 720 | -> do |
713 | _ -> return () | 721 | -- hPutStrLn stderr $ "External: "++show (nodeId naddr,sockaddr) |
714 | insertNode (mainlineKademlia client committee tbl sched) naddr | 722 | atomically $ addVote committee (nodeId naddr) sockaddr |
723 | _ -> return () | ||
724 | insertNode (mainlineKademlia client committee tbl sched) naddr | ||
715 | 725 | ||
716 | data Ping = Ping deriving Show | 726 | data Ping = Ping deriving Show |
717 | 727 | ||
@@ -809,11 +819,19 @@ ipFamily ip = case ip of | |||
809 | findNodeH :: Routing -> NodeInfo -> FindNode -> IO NodeFound | 819 | findNodeH :: Routing -> NodeInfo -> FindNode -> IO NodeFound |
810 | findNodeH routing addr (FindNode node iptyp) = do | 820 | findNodeH routing addr (FindNode node iptyp) = do |
811 | let preferred = prefer4or6 addr iptyp | 821 | let preferred = prefer4or6 addr iptyp |
812 | ks <- bool (return []) (go $ routing4 routing) (preferred /= Want_IP6) | 822 | |
813 | ks6 <- bool (return []) (go $ routing6 routing) (preferred /= Want_IP4) | 823 | (append4,append6) <- atomically $ do |
824 | ni4 <- R.thisNode <$> readTVar (routing4 routing) | ||
825 | ni6 <- R.thisNode <$> readTVar (routing6 routing) | ||
826 | return $ case ipFamily (nodeIP addr) of | ||
827 | Want_IP4 -> (id, (++ [ni6])) | ||
828 | Want_IP6 -> ((++ [ni4]), id) | ||
829 | ks <- bool (return []) (go append4 $ routing4 routing) (preferred /= Want_IP6) | ||
830 | ks6 <- bool (return []) (go append6 $ routing6 routing) (preferred /= Want_IP4) | ||
814 | return $ NodeFound ks ks6 | 831 | return $ NodeFound ks ks6 |
815 | where | 832 | where |
816 | go var = R.kclosest mainlineSpace k node <$> atomically (readTVar var) | 833 | go f var = f . R.kclosest mainlineSpace k node <$> atomically (readTVar var) |
834 | |||
817 | k = R.defaultK | 835 | k = R.defaultK |
818 | 836 | ||
819 | 837 | ||