diff options
Diffstat (limited to 'dht/src/Network/Tox/NodeId.hs')
-rw-r--r-- | dht/src/Network/Tox/NodeId.hs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/dht/src/Network/Tox/NodeId.hs b/dht/src/Network/Tox/NodeId.hs index d05e3697..667e7d71 100644 --- a/dht/src/Network/Tox/NodeId.hs +++ b/dht/src/Network/Tox/NodeId.hs | |||
@@ -47,6 +47,7 @@ module Network.Tox.NodeId | |||
47 | import Control.Applicative | 47 | import Control.Applicative |
48 | import Control.Arrow | 48 | import Control.Arrow |
49 | import Control.Monad | 49 | import Control.Monad |
50 | import Control.Monad.Fail as MF | ||
50 | #ifdef CRYPTONITE_BACKPORT | 51 | #ifdef CRYPTONITE_BACKPORT |
51 | import Crypto.Error.Types (CryptoFailable (..), | 52 | import Crypto.Error.Types (CryptoFailable (..), |
52 | throwCryptoError) | 53 | throwCryptoError) |
@@ -70,7 +71,11 @@ import Data.Char | |||
70 | import Data.Data | 71 | import Data.Data |
71 | import Data.Hashable | 72 | import Data.Hashable |
72 | #if MIN_VERSION_iproute(1,7,4) | 73 | #if MIN_VERSION_iproute(1,7,4) |
73 | import Data.IP hiding (fromSockAddr) | 74 | import Data.IP hiding ( fromSockAddr |
75 | #if MIN_VERSION_iproute(1,7,8) | ||
76 | , toSockAddr | ||
77 | #endif | ||
78 | ) | ||
74 | #else | 79 | #else |
75 | import Data.IP | 80 | import Data.IP |
76 | #endif | 81 | #endif |
@@ -258,7 +263,7 @@ getIP 0x02 = IPv4 <$> S.get | |||
258 | getIP 0x0a = IPv6 <$> S.get | 263 | getIP 0x0a = IPv6 <$> S.get |
259 | getIP 0x82 = IPv4 <$> S.get -- TODO: TCP TOX_TCP_INET | 264 | getIP 0x82 = IPv4 <$> S.get -- TODO: TCP TOX_TCP_INET |
260 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP TOX_TCP_INET6 | 265 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP TOX_TCP_INET6 |
261 | getIP x = fail ("unsupported address family ("++show x++")") | 266 | getIP x = MF.fail ("unsupported address family ("++show x++")") |
262 | 267 | ||
263 | instance Sized NodeInfo where | 268 | instance Sized NodeInfo where |
264 | size = VarSize $ \(NodeInfo nid ip port) -> | 269 | size = VarSize $ \(NodeInfo nid ip port) -> |
@@ -306,7 +311,7 @@ instance Read NodeInfo where | |||
306 | return (nid,addrstr) | 311 | return (nid,addrstr) |
307 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) | 312 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) |
308 | (ip,port) <- case RP.readP_to_S (ip_w_port i) addrstr of | 313 | (ip,port) <- case RP.readP_to_S (ip_w_port i) addrstr of |
309 | [] -> fail "Bad address." | 314 | [] -> MF.fail "Bad address." |
310 | ((ip,port),_):_ -> return (ip,port) | 315 | ((ip,port),_):_ -> return (ip,port) |
311 | return $ NodeInfo nid ip port | 316 | return $ NodeInfo nid ip port |
312 | 317 | ||
@@ -406,7 +411,7 @@ getIP 0x02 = IPv4 <$> S.get | |||
406 | getIP 0x0a = IPv6 <$> S.get | 411 | getIP 0x0a = IPv6 <$> S.get |
407 | getIP 0x82 = IPv4 <$> S.get -- TODO: TCP | 412 | getIP 0x82 = IPv4 <$> S.get -- TODO: TCP |
408 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP | 413 | getIP 0x8a = IPv6 <$> S.get -- TODO: TCP |
409 | getIP x = fail ("unsupported address family ("++show x++")") | 414 | getIP x = MF.fail ("unsupported address family ("++show x++")") |
410 | 415 | ||
411 | instance S.Serialize NodeInfo where | 416 | instance S.Serialize NodeInfo where |
412 | get = do | 417 | get = do |
@@ -445,7 +450,7 @@ instance Read NodeInfo where | |||
445 | addrstr <- parseAddr | 450 | addrstr <- parseAddr |
446 | nid <- case Base16.decode $ C8.pack hexhash of | 451 | nid <- case Base16.decode $ C8.pack hexhash of |
447 | (bs,_) | B.length bs==32 -> return (PubKey bs) | 452 | (bs,_) | B.length bs==32 -> return (PubKey bs) |
448 | _ -> fail "Bad node id." | 453 | _ -> MF.fail "Bad node id." |
449 | return (nid,addrstr) | 454 | return (nid,addrstr) |
450 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) | 455 | (nid,addrstr) <- ( nodeidAt RP.+++ ( (zeroID,) <$> parseAddr) ) |
451 | let raddr = do | 456 | let raddr = do |
@@ -457,7 +462,7 @@ instance Read NodeInfo where | |||
457 | return (ip, port) | 462 | return (ip, port) |
458 | 463 | ||
459 | (ip,port) <- case RP.readP_to_S raddr addrstr of | 464 | (ip,port) <- case RP.readP_to_S raddr addrstr of |
460 | [] -> fail "Bad address." | 465 | [] -> MF.fail "Bad address." |
461 | ((ip,port),_):_ -> return (ip,port) | 466 | ((ip,port),_):_ -> return (ip,port) |
462 | return $ NodeInfo nid ip port | 467 | return $ NodeInfo nid ip port |
463 | 468 | ||
@@ -518,15 +523,15 @@ instance Read NoSpam where | |||
518 | ('0':'x':ws,rs) | (length ws == 12) -> base16decode rs (NoSpam <$> get <*> (Just <$> get)) ws | 523 | ('0':'x':ws,rs) | (length ws == 12) -> base16decode rs (NoSpam <$> get <*> (Just <$> get)) ws |
519 | _ -> [] | 524 | _ -> [] |
520 | 525 | ||
521 | base64decode :: Monad m => t1 -> Get t -> String -> m (t, t1) | 526 | base64decode :: MonadFail m => t1 -> Get t -> String -> m (t, t1) |
522 | base64decode rs getter s = | 527 | base64decode rs getter s = |
523 | either fail (\a -> return (a,rs)) | 528 | either MF.fail (\a -> return (a,rs)) |
524 | $ runGet getter | 529 | $ runGet getter |
525 | =<< Base64.decode (C8.pack $ map (nmtoken64 False) s) | 530 | =<< Base64.decode (C8.pack $ map (nmtoken64 False) s) |
526 | 531 | ||
527 | base16decode :: Monad m => t1 -> Get t -> String -> m (t, t1) | 532 | base16decode :: MonadFail m => t1 -> Get t -> String -> m (t, t1) |
528 | base16decode rs getter s = | 533 | base16decode rs getter s = |
529 | either fail (\a -> return (a,rs)) | 534 | either MF.fail (\a -> return (a,rs)) |
530 | $ runGet getter | 535 | $ runGet getter |
531 | $ fst | 536 | $ fst |
532 | $ Base16.decode (C8.pack s) | 537 | $ Base16.decode (C8.pack s) |
@@ -559,7 +564,7 @@ instance Show NoSpamId where | |||
559 | show (NoSpamId nspam pub) = '$' : nospam64 nspam ++ "@" ++ show (key2id pub) ++ ".tox" | 564 | show (NoSpamId nspam pub) = '$' : nospam64 nspam ++ "@" ++ show (key2id pub) ++ ".tox" |
560 | 565 | ||
561 | instance Read NoSpamId where | 566 | instance Read NoSpamId where |
562 | readsPrec d s = either fail id $ do | 567 | readsPrec d s = either MF.fail id $ do |
563 | (jid,xs) <- Right $ break isSpace s | 568 | (jid,xs) <- Right $ break isSpace s |
564 | nsid <- parseNoSpamId $ Text.pack jid | 569 | nsid <- parseNoSpamId $ Text.pack jid |
565 | return [(nsid,xs)] | 570 | return [(nsid,xs)] |