diff options
-rw-r--r-- | dht/examples/readnodes.hs | 32 |
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 | |||
10 | import Foreign.Ptr | 10 | import Foreign.Ptr |
11 | import Foreign.Marshal.Utils | 11 | import Foreign.Marshal.Utils |
12 | import Foreign.Storable | 12 | import Foreign.Storable |
13 | import Network.Tox.NodeId | ||
14 | import System.Environment | 13 | import System.Environment |
15 | import System.IO (stdout) | 14 | import System.IO (stdout) |
16 | import Text.Read | 15 | import Text.Read |
17 | 16 | ||
17 | import qualified Network.Tox.NodeId as UDP | ||
18 | import 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 | ||
67 | addressString :: NodeInfo -> String | 69 | addressString :: NodeInfo -> String |
68 | addressString ni = case show (nodeAddr ni) of | 70 | addressString 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) | 74 | readNode :: String -> Maybe NodeInfo |
75 | readNode nstr = mplus (readMaybe nstr) | ||
76 | (fromUDPNode <$> readMaybe nstr) | ||
73 | 77 | ||
74 | main = do | 78 | main = 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 |