diff options
Diffstat (limited to 'ToxAddress.hs')
-rw-r--r-- | ToxAddress.hs | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/ToxAddress.hs b/ToxAddress.hs index ea69f6e3..04ee7d6f 100644 --- a/ToxAddress.hs +++ b/ToxAddress.hs | |||
@@ -13,7 +13,7 @@ | |||
13 | {-# LANGUAGE ScopedTypeVariables #-} | 13 | {-# LANGUAGE ScopedTypeVariables #-} |
14 | {-# LANGUAGE TupleSections #-} | 14 | {-# LANGUAGE TupleSections #-} |
15 | {-# LANGUAGE TypeApplications #-} | 15 | {-# LANGUAGE TypeApplications #-} |
16 | module ToxAddress (NodeInfo(..),NodeId(..),nodeInfo,zeroID) where | 16 | module ToxAddress (NodeInfo(..),NodeId(..),nodeInfo,nodeAddr,zeroID,key2id,id2key) where |
17 | 17 | ||
18 | import Control.Applicative | 18 | import Control.Applicative |
19 | import Control.Monad | 19 | import Control.Monad |
@@ -42,6 +42,7 @@ import System.IO.Unsafe (unsafeDupablePerformIO) | |||
42 | import qualified Text.ParserCombinators.ReadP as RP | 42 | import qualified Text.ParserCombinators.ReadP as RP |
43 | import Text.Read | 43 | import Text.Read |
44 | import Data.Bits | 44 | import Data.Bits |
45 | import ToxCrypto | ||
45 | 46 | ||
46 | -- | perform io for hashes that do allocation and ffi. | 47 | -- | perform io for hashes that do allocation and ffi. |
47 | -- unsafeDupablePerformIO is used when possible as the | 48 | -- unsafeDupablePerformIO is used when possible as the |
@@ -66,6 +67,26 @@ unpackPublicKey bs = loop 0 | |||
66 | newtype NodeId = NodeId PublicKey | 67 | newtype NodeId = NodeId PublicKey |
67 | deriving (Eq,ByteArrayAccess) -- (Eq,Ord,ByteArrayAccess, Bits, Hashable) | 68 | deriving (Eq,ByteArrayAccess) -- (Eq,Ord,ByteArrayAccess, Bits, Hashable) |
68 | 69 | ||
70 | key2id :: PublicKey -> NodeId | ||
71 | key2id = NodeId | ||
72 | |||
73 | id2key :: NodeId -> PublicKey | ||
74 | id2key (NodeId key) = key | ||
75 | |||
76 | {- | ||
77 | id2key :: NodeId -> PublicKey | ||
78 | id2key recipient = case publicKey recipient of | ||
79 | CryptoPassed key -> key | ||
80 | -- This should never happen because a NodeId is 32 bytes. | ||
81 | CryptoFailed e -> error ("Unexpected pattern fail: "++show e) | ||
82 | |||
83 | key2id :: PublicKey -> NodeId | ||
84 | key2id pk = case S.decode (BA.convert pk) of | ||
85 | Left _ -> error "key2id" | ||
86 | Right nid -> nid | ||
87 | |||
88 | -} | ||
89 | |||
69 | instance Ord NodeId where | 90 | instance Ord NodeId where |
70 | compare (NodeId a) (NodeId b) = compare (unpackPublicKey a) (unpackPublicKey b) | 91 | compare (NodeId a) (NodeId b) = compare (unpackPublicKey a) (unpackPublicKey b) |
71 | 92 | ||
@@ -83,8 +104,8 @@ instance Show NodeId where | |||
83 | show (NodeId bs) = C8.unpack $ Base16.encode $ BA.convert bs | 104 | show (NodeId bs) = C8.unpack $ Base16.encode $ BA.convert bs |
84 | 105 | ||
85 | instance S.Serialize NodeId where | 106 | instance S.Serialize NodeId where |
86 | get = NodeId . throwCryptoError . publicKey <$> S.getBytes 32 | 107 | get = NodeId <$> getPublicKey |
87 | put (NodeId bs) = S.putByteString $ BA.convert bs | 108 | put (NodeId bs) = putPublicKey bs |
88 | 109 | ||
89 | instance Bits NodeId where -- TODO | 110 | instance Bits NodeId where -- TODO |
90 | 111 | ||
@@ -144,6 +165,12 @@ getIP 0x82 = IPv4 <$> S.get -- TODO: TCP | |||
144 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP | 165 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP |
145 | getIP x = fail ("unsupported address family ("++show x++")") | 166 | getIP x = fail ("unsupported address family ("++show x++")") |
146 | 167 | ||
168 | instance Sized NodeInfo where | ||
169 | size = VarSize $ \(NodeInfo nid ip port) -> | ||
170 | case ip of | ||
171 | IPv4 _ -> 39 -- 35 + 4 = 1 + 4 + 2 + 32 | ||
172 | IPv6 _ -> 51 -- 35 + 16 = 1 + 16 + 2 + 32 | ||
173 | |||
147 | instance S.Serialize NodeInfo where | 174 | instance S.Serialize NodeInfo where |
148 | get = do | 175 | get = do |
149 | addrfam <- S.get :: S.Get Word8 | 176 | addrfam <- S.get :: S.Get Word8 |
@@ -354,9 +381,6 @@ instance Show NodeInfo where | |||
354 | | IPv6 ip6 <- ip , Just ip4 <- un4map ip6 = shows ip4 | 381 | | IPv6 ip6 <- ip , Just ip4 <- un4map ip6 = shows ip4 |
355 | | otherwise = ('[' :) . shows ip . (']' :) | 382 | | otherwise = ('[' :) . shows ip . (']' :) |
356 | 383 | ||
357 | nodeAddr :: NodeInfo -> SockAddr | ||
358 | nodeAddr (NodeInfo _ ip port) = setPort port $ toSockAddr ip | ||
359 | |||
360 | nodeInfo :: NodeId -> SockAddr -> Either String NodeInfo | 384 | nodeInfo :: NodeId -> SockAddr -> Either String NodeInfo |
361 | nodeInfo nid saddr | 385 | nodeInfo nid saddr |
362 | | Just ip <- fromSockAddr saddr | 386 | | Just ip <- fromSockAddr saddr |
@@ -368,6 +392,10 @@ zeroID = PubKey $ B.replicate 32 0 | |||
368 | 392 | ||
369 | -} | 393 | -} |
370 | 394 | ||
395 | nodeAddr :: NodeInfo -> SockAddr | ||
396 | nodeAddr (NodeInfo _ ip port) = setPort port $ toSockAddr ip | ||
397 | |||
398 | |||
371 | newtype ForwardPath (n::Nat) = ForwardPath ByteString | 399 | newtype ForwardPath (n::Nat) = ForwardPath ByteString |
372 | deriving (Eq, Ord,Data) | 400 | deriving (Eq, Ord,Data) |
373 | 401 | ||