]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/PeerMsgs.hs
quick and dirty initial extended metadata protocol implementation
[functorrent.git] / src / FuncTorrent / PeerMsgs.hs
index d6bbdcfadefac6d3f7672f090a952492d7299fa2..467ac8d6f72e94a41bc9233643e731ded0a4e802 100644 (file)
@@ -64,7 +64,12 @@ data PeerMsg = KeepAliveMsg
              | CancelMsg Integer Integer Integer
              | PortMsg Port
              | ExtendedMsg Integer ByteString
-             deriving (Show)
+  deriving (Show)
+
+data ExtMetadataMsg = Request Integer
+                    | Data Integer Integer
+                    | Reject Integer
+  deriving (Eq, Show)
 
 instance Binary PeerMsg where
   put msg = case msg of
@@ -101,14 +106,13 @@ instance Binary PeerMsg where
              PortMsg p -> do putWord32be 3
                              putWord8 9
                              putWord16be (fromIntegral p)
-             ExtendedHandshakeMsg t b-> do putWord32be msgLen
-                                           putWord8 20
-                                           putWord8 t -- 0 => handshake msg
-                                           -- actual extension msg follows
-                                           mapM_ putWord8 blockList
-                                             where blockList = unpack b
-                                                   blockLen  = length blockList
-
+             ExtendedMsg t b-> do putWord32be (fromIntegral blockLen)
+                                  putWord8 20
+                                  putWord8 (fromIntegral t) -- 0 => handshake msg
+                                  -- actual extension msg follows
+                                  mapM_ putWord8 blockList
+                                    where blockList = unpack b
+                                          blockLen  = length blockList
 
     where putIndexOffsetLength i o l = do
             putWord32be (fromIntegral i)
@@ -152,9 +156,9 @@ genHandshakeMsg :: ByteString -> String -> ByteString
 genHandshakeMsg infoHash peer_id = concat [pstrlen, pstr, reserved1, reserved2, reserved3, infoHash, peerID]
   where pstrlen = singleton 19
         pstr = BC.pack "BitTorrent protocol"
-        reserved1 = BC.replicate 4 '\0'
+        reserved1 = BC.replicate 5 '\0'
         reserved2 = singleton 0x10 -- support extension protocol
-        reserved3 = BC.replicate 3 '\0'
+        reserved3 = BC.replicate 2 '\0'
         peerID = BC.pack peer_id
 
 bsToInt :: ByteString -> Int