diff options
author | Daniel Gröber <dxld@darkboxed.org> | 2013-12-22 19:33:22 +0000 |
---|---|---|
committer | Daniel Gröber <dxld@darkboxed.org> | 2013-12-22 19:33:22 +0000 |
commit | fd795d45ea816aaa24adc7ac7185514d8407cae0 (patch) | |
tree | df2410aece7339dea9eab593a7e8a692d1094fd3 /src/Network/BitTorrent/Exchange | |
parent | 4693ed53ec5cdbf0b2fa993d169c96e45f2dce91 (diff) |
Add yourip field to ExtendedHandshake
Diffstat (limited to 'src/Network/BitTorrent/Exchange')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Message.hs | 25 |
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 | |||
108 | import Data.Text as T | 108 | import Data.Text as T |
109 | import Data.Typeable | 109 | import Data.Typeable |
110 | import Data.Word | 110 | import Data.Word |
111 | import Data.IP | ||
111 | import Network | 112 | import Network |
112 | import Network.Socket hiding (KeepAlive) | 113 | import Network.Socket hiding (KeepAlive) |
113 | import Text.PrettyPrint as PP hiding ((<>)) | 114 | import 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 | ||
668 | extHandshakeId :: ExtendedMessageId | 669 | extHandshakeId :: ExtendedMessageId |
@@ -674,7 +675,7 @@ defaultQueueLength = 1 | |||
674 | 675 | ||
675 | -- | All fields are empty. | 676 | -- | All fields are empty. |
676 | instance Default ExtendedHandshake where | 677 | instance 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 | ||
679 | instance BEncode ExtendedHandshake where | 680 | instance 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 | |||
705 | getYourIp :: Maybe BValue -> BE.Get (Maybe IP) | ||
706 | getYourIp 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 | ||
701 | instance Pretty ExtendedHandshake where | 715 | instance 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 | {----------------------------------------------------------------------- |