]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Peer.hs
Merge pull request #26 from jaseemabid/master
[functorrent.git] / src / FuncTorrent / Peer.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module FuncTorrent.Peer
3     (Peer(..),
4      handShakeMsg
5     ) where
6
7 import Prelude hiding (lookup, concat, replicate, splitAt)
8
9 import Data.ByteString.Char8 (ByteString, pack, concat, replicate)
10 import Data.ByteString.Lazy (toChunks)
11 import Data.Int (Int8)
12 import qualified Data.Binary as Bin (encode)
13
14 import FuncTorrent.Metainfo (Metainfo(..))
15
16 -- | Peer is a IP address, port tuple
17 data Peer = Peer String Integer
18             deriving (Show, Eq)
19
20 handShakeMsg :: Metainfo -> String -> ByteString
21 handShakeMsg m peer_id = concat [pstrlen, pstr, reserved, infoH, peerID]
22     where pstrlen = concat $ toChunks $ Bin.encode (19 :: Int8)
23           pstr = pack "BitTorrent protocol"
24           reserved = replicate 8 '\0'
25           infoH = infoHash m
26           peerID = pack peer_id