diff options
-rwxr-xr-x | ci | 4 | ||||
-rw-r--r-- | examples/dhtd.hs | 4 | ||||
-rw-r--r-- | src/Network/Tox/NodeId.hs | 32 |
3 files changed, 28 insertions, 12 deletions
@@ -3,5 +3,5 @@ compile=ghci | |||
3 | defs="-DBENCODE_AESON -DTHREAD_DEBUG" | 3 | defs="-DBENCODE_AESON -DTHREAD_DEBUG" |
4 | hide="-hide-package crypto-random -hide-package crypto-api -hide-package crypto-numbers -hide-package cryptohash -hide-package prettyclass" | 4 | hide="-hide-package crypto-random -hide-package crypto-api -hide-package crypto-numbers -hide-package cryptohash -hide-package prettyclass" |
5 | # cbits="cbits/*.c" | 5 | # cbits="cbits/*.c" |
6 | $compile -fdefer-typed-holes -Wno-typed-holes -freverse-errors $hide -isrc -XOverloadedStrings -XRecordWildCards $defs $cbits "$@" | 6 | includes="-isrc -icryptonite-backport" |
7 | 7 | $compile -Wmissing-signatures -fdefer-typed-holes -freverse-errors $hide $includes -XOverloadedStrings -XRecordWildCards $defs $cbits "$@" | |
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 1ab2778a..37f16f02 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -288,13 +288,13 @@ data Session = Session | |||
288 | 288 | ||
289 | clientSession :: Session -> t1 -> t -> Handle -> IO () | 289 | clientSession :: Session -> t1 -> t -> Handle -> IO () |
290 | clientSession s@Session{..} sock cnum h = do | 290 | clientSession s@Session{..} sock cnum h = do |
291 | line <- map toLower . dropWhile isSpace <$> hGetLine h | 291 | line <- dropWhile isSpace <$> hGetLine h |
292 | let (c,args) = second (dropWhile isSpace) $ break isSpace line | 292 | let (c,args) = second (dropWhile isSpace) $ break isSpace line |
293 | cmd0 :: IO () -> IO () | 293 | cmd0 :: IO () -> IO () |
294 | cmd0 action = action >> clientSession s sock cnum h | 294 | cmd0 action = action >> clientSession s sock cnum h |
295 | switchNetwork dest = do hPutClient h ("Network: "++dest) | 295 | switchNetwork dest = do hPutClient h ("Network: "++dest) |
296 | clientSession s{netname=dest} sock cnum h | 296 | clientSession s{netname=dest} sock cnum h |
297 | case (c,args) of | 297 | case (map toLower c,args) of |
298 | ("stop", _) -> do hPutClient h "Terminating DHT Daemon." | 298 | ("stop", _) -> do hPutClient h "Terminating DHT Daemon." |
299 | hClose h | 299 | hClose h |
300 | putMVar signalQuit () | 300 | putMVar signalQuit () |
diff --git a/src/Network/Tox/NodeId.hs b/src/Network/Tox/NodeId.hs index d0c57416..bba56e27 100644 --- a/src/Network/Tox/NodeId.hs +++ b/src/Network/Tox/NodeId.hs | |||
@@ -40,6 +40,7 @@ import qualified Data.ByteArray as BA | |||
40 | import qualified Data.ByteString as B | 40 | import qualified Data.ByteString as B |
41 | ;import Data.ByteString (ByteString) | 41 | ;import Data.ByteString (ByteString) |
42 | import qualified Data.ByteString.Base16 as Base16 | 42 | import qualified Data.ByteString.Base16 as Base16 |
43 | import qualified Data.ByteString.Base64 as Base64 | ||
43 | import qualified Data.ByteString.Char8 as C8 | 44 | import qualified Data.ByteString.Char8 as C8 |
44 | import Data.Char | 45 | import Data.Char |
45 | import Data.Data | 46 | import Data.Data |
@@ -122,15 +123,23 @@ instance Ord NodeId where | |||
122 | zeroID :: NodeId | 123 | zeroID :: NodeId |
123 | zeroID = NodeId $ replicate 4 0 -- throwCryptoError $ publicKey $ B.replicate 32 0 | 124 | zeroID = NodeId $ replicate 4 0 -- throwCryptoError $ publicKey $ B.replicate 32 0 |
124 | 125 | ||
126 | -- | Convert to and from a Base64 variant that uses .- instead of +/. | ||
127 | nmtoken64 :: Bool -> Char -> Char | ||
128 | nmtoken64 False '.' = '+' | ||
129 | nmtoken64 False '-' = '/' | ||
130 | nmtoken64 True '+' = '.' | ||
131 | nmtoken64 True '/' = '-' | ||
132 | nmtoken64 _ c = c | ||
133 | |||
125 | instance Read NodeId where | 134 | instance Read NodeId where |
126 | readsPrec _ str | 135 | readsPrec _ str |
127 | | (bs, xs) <- Base16.decode $ C8.pack str | 136 | | Right bs <- fmap (BA.drop 1) $ Base64.decode $ C8.pack $ 'A':map (nmtoken64 False) (take 43 str) |
128 | , CryptoPassed pub <- publicKey bs -- B.length bs == 32 | 137 | , CryptoPassed pub <- publicKey bs -- B.length bs == 32 |
129 | = [ (key2id pub, drop 64 str) ] | 138 | = [ (key2id pub, drop 43 str) ] |
130 | | otherwise = [] | 139 | | otherwise = [] |
131 | 140 | ||
132 | instance Show NodeId where | 141 | instance Show NodeId where |
133 | show nid = C8.unpack $ Base16.encode $ BA.convert $ id2key nid | 142 | show nid = map (nmtoken64 True) $ C8.unpack $ BA.drop 1 $ Base64.encode $ BA.cons 0 $ BA.convert $ id2key nid |
134 | 143 | ||
135 | instance S.Serialize NodeId where | 144 | instance S.Serialize NodeId where |
136 | get = key2id <$> getPublicKey | 145 | get = key2id <$> getPublicKey |
@@ -241,18 +250,25 @@ instance S.Serialize NodeInfo where | |||
241 | hexdigit :: Char -> Bool | 250 | hexdigit :: Char -> Bool |
242 | hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F') | 251 | hexdigit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'f') || ( 'A' <= c && c <= 'F') |
243 | 252 | ||
253 | b64digit :: Char -> Bool | ||
254 | b64digit '.' = True | ||
255 | b64digit '+' = True | ||
256 | b64digit '-' = True | ||
257 | b64digit '/' = True | ||
258 | b64digit c = ('0' <= c && c <= '9') || ( 'a' <= c && c <= 'z') || ( 'A' <= c && c <= 'Z') | ||
259 | |||
244 | instance Read NodeInfo where | 260 | instance Read NodeInfo where |
245 | readsPrec i = RP.readP_to_S $ do | 261 | readsPrec i = RP.readP_to_S $ do |
246 | RP.skipSpaces | 262 | RP.skipSpaces |
247 | let n = 64 -- characters in node id. | 263 | let n = 43 -- characters in node id. |
248 | parseAddr = RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')')) | 264 | parseAddr = RP.between (RP.char '(') (RP.char ')') (RP.munch (/=')')) |
249 | RP.+++ RP.munch (not . isSpace) | 265 | RP.+++ RP.munch (not . isSpace) |
250 | nodeidAt = do hexhash <- sequence $ replicate n (RP.satisfy hexdigit) | 266 | nodeidAt = do hexhash <- sequence $ replicate n (RP.satisfy b64digit) |
251 | RP.char '@' RP.+++ RP.satisfy isSpace | 267 | RP.char '@' RP.+++ RP.satisfy isSpace |
252 | addrstr <- parseAddr | 268 | addrstr <- parseAddr |
253 | nid <- case Base16.decode $ C8.pack hexhash of | 269 | nid <- case Base64.decode $ C8.pack $ 'A' : map (nmtoken64 False) hexhash of |
254 | (bs,_) | B.length bs==32 -> return (bs2id bs) | 270 | Right bs | B.length bs - 1==32 -> return (bs2id $ BA.drop 1 bs) |
255 | _ -> fail "Bad node id." | 271 | _ -> fail "Bad node id." |
256 | return (nid,addrstr) | 272 | return (nid,addrstr) |
257 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) | 273 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) |
258 | let raddr = do | 274 | let raddr = do |