summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-29 03:51:21 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-03 17:26:06 -0500
commit1e03ed3670a8386ede93a09fa0c67785e7da6478 (patch)
tree220efd5f6db89ecb6dd09d3dd6686f7710bd9720
parentb69fd50df8ec24733cef44cb5772fea69cc1e511 (diff)
Support TCP nodes in readnodes utility.
-rw-r--r--dht/examples/readnodes.hs32
1 files changed, 18 insertions, 14 deletions
diff --git a/dht/examples/readnodes.hs b/dht/examples/readnodes.hs
index e4a5b522..3ecffbaf 100644
--- a/dht/examples/readnodes.hs
+++ b/dht/examples/readnodes.hs
@@ -10,11 +10,13 @@ import Data.Word
10import Foreign.Ptr 10import Foreign.Ptr
11import Foreign.Marshal.Utils 11import Foreign.Marshal.Utils
12import Foreign.Storable 12import Foreign.Storable
13import Network.Tox.NodeId
14import System.Environment 13import System.Environment
15import System.IO (stdout) 14import System.IO (stdout)
16import Text.Read 15import Text.Read
17 16
17import qualified Network.Tox.NodeId as UDP
18import Network.Tox.TCP.NodeId as TCP
19
18-- struct bootstrap_node { 20-- struct bootstrap_node {
19-- 8 char *address; 21-- 8 char *address;
20-- 2 bool ipv6; 22-- 2 bool ipv6;
@@ -51,32 +53,34 @@ node_bytes num_nodes a s n = do
51 Just _ | bigendian -> [0,1] 53 Just _ | bigendian -> [0,1]
52 | otherwise -> [1,0] 54 | otherwise -> [1,0]
53 _ -> [0,0] 55 _ -> [0,0]
54 t = [0,0] -- TODO: TCP port 56 nid = UDP.nodeId $ udpNodeInfo n
55 nid = nodeId n
56 adr <- int_bytes (base + a) 57 adr <- int_bytes (base + a)
57 u <- int16_bytes $ fromIntegral $ nodePort n 58 u <- int16_bytes $ fromIntegral $ UDP.nodePort $ udpNodeInfo n
59 t <- int16_bytes $ fromIntegral $ tcpPort n
58 return $ foldr (++) [] 60 return $ foldr (++) []
59 [ adr -- char *address; 61 [ adr -- char *address;
60 , ip6 -- bool ipv6; 62 , ip6 -- bool ipv6;
61 , u -- uint16_t port_udp 63 , u -- uint16_t port_udp
62 , t -- uint16_t port_tcp 64 , t -- uint16_t port_tcp
63 , BA.unpack (id2key nid) -- uint8_t key[32]; 65 , BA.unpack (UDP.id2key nid) -- uint8_t key[32];
64 , [0,0] -- padding 66 , [0,0] -- padding
65 ] 67 ]
66 68
67addressString :: NodeInfo -> String 69addressString :: NodeInfo -> String
68addressString ni = case show (nodeAddr ni) of 70addressString ni = case show (UDP.nodeAddr $ udpNodeInfo ni) of
69 '[':xs -> takeWhile (/=']') xs 71 '[':xs -> takeWhile (/=']') xs
70 xs -> takeWhile (/=':') xs 72 xs -> takeWhile (/=':') xs
71 where 73
72 a = show (nodeAddr ni) 74readNode :: String -> Maybe NodeInfo
75readNode nstr = mplus (readMaybe nstr)
76 (fromUDPNode <$> readMaybe nstr)
73 77
74main = do 78main = do
75 args <- getArgs 79 args <- getArgs
76 let [ifilename] = args 80 let [ifilename] = args
77 ws <- words <$> readFile ifilename 81 ws <- words <$> readFile ifilename
78 let ns :: [ NodeInfo ] 82 let ns :: [ NodeInfo ]
79 ns = mapMaybe readMaybe ws 83 ns = mapMaybe readNode ws
80 num_nodes :: Int32 84 num_nodes :: Int32
81 num_nodes = fromIntegral $ length ns 85 num_nodes = fromIntegral $ length ns
82 ptr_size :: Int32 86 ptr_size :: Int32