summaryrefslogtreecommitdiff
path: root/ToxAddress.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ToxAddress.hs')
-rw-r--r--ToxAddress.hs40
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 #-}
16module ToxAddress (NodeInfo(..),NodeId(..),nodeInfo,zeroID) where 16module ToxAddress (NodeInfo(..),NodeId(..),nodeInfo,nodeAddr,zeroID,key2id,id2key) where
17 17
18import Control.Applicative 18import Control.Applicative
19import Control.Monad 19import Control.Monad
@@ -42,6 +42,7 @@ import System.IO.Unsafe (unsafeDupablePerformIO)
42import qualified Text.ParserCombinators.ReadP as RP 42import qualified Text.ParserCombinators.ReadP as RP
43import Text.Read 43import Text.Read
44import Data.Bits 44import Data.Bits
45import 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
66newtype NodeId = NodeId PublicKey 67newtype NodeId = NodeId PublicKey
67 deriving (Eq,ByteArrayAccess) -- (Eq,Ord,ByteArrayAccess, Bits, Hashable) 68 deriving (Eq,ByteArrayAccess) -- (Eq,Ord,ByteArrayAccess, Bits, Hashable)
68 69
70key2id :: PublicKey -> NodeId
71key2id = NodeId
72
73id2key :: NodeId -> PublicKey
74id2key (NodeId key) = key
75
76{-
77id2key :: NodeId -> PublicKey
78id2key 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
83key2id :: PublicKey -> NodeId
84key2id pk = case S.decode (BA.convert pk) of
85 Left _ -> error "key2id"
86 Right nid -> nid
87
88-}
89
69instance Ord NodeId where 90instance 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
85instance S.Serialize NodeId where 106instance 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
89instance Bits NodeId where -- TODO 110instance Bits NodeId where -- TODO
90 111
@@ -144,6 +165,12 @@ getIP 0x82 = IPv4 <$> S.get -- TODO: TCP
144getIP 0x8a = IPv6 <$> S.get -- TODO: TCP 165getIP 0x8a = IPv6 <$> S.get -- TODO: TCP
145getIP x = fail ("unsupported address family ("++show x++")") 166getIP x = fail ("unsupported address family ("++show x++")")
146 167
168instance 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
147instance S.Serialize NodeInfo where 174instance 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
357nodeAddr :: NodeInfo -> SockAddr
358nodeAddr (NodeInfo _ ip port) = setPort port $ toSockAddr ip
359
360nodeInfo :: NodeId -> SockAddr -> Either String NodeInfo 384nodeInfo :: NodeId -> SockAddr -> Either String NodeInfo
361nodeInfo nid saddr 385nodeInfo 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
395nodeAddr :: NodeInfo -> SockAddr
396nodeAddr (NodeInfo _ ip port) = setPort port $ toSockAddr ip
397
398
371newtype ForwardPath (n::Nat) = ForwardPath ByteString 399newtype ForwardPath (n::Nat) = ForwardPath ByteString
372 deriving (Eq, Ord,Data) 400 deriving (Eq, Ord,Data)
373 401