summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorDaniel Gröber <dxld@darkboxed.org>2013-12-22 19:33:22 +0000
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-25 07:32:37 +0400
commit96098e860d20e18637764156b3a067371bd49110 (patch)
tree199e589ab037edb12fc19bc191297ff1a54e0fe7 /src/Network/BitTorrent
parent390a08c9ec0c7d650567227f6f1d5ab6803b201b (diff)
Add yourip field to ExtendedHandshake
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Exchange/Message.hs25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs
index d0b6c19d..b7567e60 100644
--- a/src/Network/BitTorrent/Exchange/Message.hs
+++ b/src/Network/BitTorrent/Exchange/Message.hs
@@ -108,6 +108,7 @@ import Data.String
108import Data.Text as T 108import Data.Text as T
109import Data.Typeable 109import Data.Typeable
110import Data.Word 110import Data.Word
111import Data.IP
111import Network 112import Network
112import Network.Socket hiding (KeepAlive) 113import Network.Socket hiding (KeepAlive)
113import Text.PrettyPrint as PP hiding ((<>)) 114import Text.PrettyPrint as PP hiding ((<>))
@@ -661,8 +662,8 @@ data ExtendedHandshake = ExtendedHandshake
661 -- | Client name and version. 662 -- | Client name and version.
662 , ehsVersion :: Maybe Text 663 , ehsVersion :: Maybe Text
663 664
664-- -- | 665 -- | IP of the remote end
665-- , yourip :: Maybe (Either HostAddress HostAddress6) 666 , ehsYourIp :: Maybe IP
666 } deriving (Show, Eq, Typeable) 667 } deriving (Show, Eq, Typeable)
667 668
668extHandshakeId :: ExtendedMessageId 669extHandshakeId :: ExtendedMessageId
@@ -674,7 +675,7 @@ defaultQueueLength = 1
674 675
675-- | All fields are empty. 676-- | All fields are empty.
676instance Default ExtendedHandshake where 677instance Default ExtendedHandshake where
677 def = ExtendedHandshake def def def def def def def 678 def = ExtendedHandshake def def def def def def def def
678 679
679instance BEncode ExtendedHandshake where 680instance BEncode ExtendedHandshake where
680 toBEncode ExtendedHandshake {..} = toDict $ 681 toBEncode ExtendedHandshake {..} = toDict $
@@ -685,8 +686,11 @@ instance BEncode ExtendedHandshake where
685 .: "p" .=? ehsPort 686 .: "p" .=? ehsPort
686 .: "reqq" .=? ehsQueueLength 687 .: "reqq" .=? ehsQueueLength
687 .: "v" .=? ehsVersion 688 .: "v" .=? ehsVersion
688-- .: "yourip" .=? yourip 689 .: "yourip" .=? (runPut <$> either put put <$> toEither <$> ehsYourIp)
689 .: endDict 690 .: endDict
691 where
692 toEither (IPv4 v4) = Left v4
693 toEither (IPv6 v6) = Right v6
690 694
691 fromBEncode = fromDict $ ExtendedHandshake 695 fromBEncode = fromDict $ ExtendedHandshake
692 <$>? "ipv4" 696 <$>? "ipv4"
@@ -696,7 +700,17 @@ instance BEncode ExtendedHandshake where
696 <*>? "p" 700 <*>? "p"
697 <*>? "reqq" 701 <*>? "reqq"
698 <*>? "v" 702 <*>? "v"
699-- <*>? "yourip" 703 <*> (opt "yourip" >>= getYourIp)
704
705getYourIp :: Maybe BValue -> BE.Get (Maybe IP)
706getYourIp f =
707 return $ do
708 BString ip <- f
709 either (const Nothing) Just $
710 case BS.length ip of
711 4 -> IPv4 <$> S.decode ip
712 16 -> IPv6 <$> S.decode ip
713 _ -> fail ""
700 714
701instance Pretty ExtendedHandshake where 715instance Pretty ExtendedHandshake where
702 pretty = PP.text . show 716 pretty = PP.text . show
@@ -722,6 +736,7 @@ nullExtendedHandshake caps = ExtendedHandshake
722 , ehsPort = Nothing 736 , ehsPort = Nothing
723 , ehsQueueLength = Just defaultQueueLength 737 , ehsQueueLength = Just defaultQueueLength
724 , ehsVersion = Just $ T.pack $ render $ pretty libFingerprint 738 , ehsVersion = Just $ T.pack $ render $ pretty libFingerprint
739 , ehsYourIp = Nothing
725 } 740 }
726 741
727{----------------------------------------------------------------------- 742{-----------------------------------------------------------------------