From 280c26626b71bbf7ae7de97b0d9de0f15c794875 Mon Sep 17 00:00:00 2001 From: git-sudo-404 Date: Sun, 27 Sep 2026 19:22:23 +0530 Subject: [PATCH 1/3] added message handling till have message --- internal/torrent/client.go | 12 +- internal/torrent/handhsake.go | 4 + internal/torrent/messages.go | 78 ++++++++++++ internal/torrent/metainfo.go | 12 +- internal/torrent/metainfoParser.go | 14 +- internal/torrent/metainfoParser_test.go | 6 +- internal/torrent/torrent.go | 163 +++++++++++++++++++++++- 7 files changed, 272 insertions(+), 17 deletions(-) create mode 100644 internal/torrent/messages.go diff --git a/internal/torrent/client.go b/internal/torrent/client.go index c1b0cf5..0e89d67 100644 --- a/internal/torrent/client.go +++ b/internal/torrent/client.go @@ -33,8 +33,16 @@ import ( ) type Peer struct { - ip net.IP - port int64 + ip net.IP + port int64 + peerId [20]byte + conn net.Conn + bitfield []byte + am_choking bool + am_interested bool + peer_choking bool + peer_interested bool + alive time.Time } type Client struct { diff --git a/internal/torrent/handhsake.go b/internal/torrent/handhsake.go index 07060b5..c963ae0 100644 --- a/internal/torrent/handhsake.go +++ b/internal/torrent/handhsake.go @@ -60,3 +60,7 @@ func (phr *PeerHandshakeRequest) Serialize() []byte { copy(buf[1+len(phr.pstr)+28:], phr.peerId[:]) return buf } + +func NewPeerHandshakeRequest() *PeerHandshakeRequest { + return &PeerHandshakeRequest{} +} diff --git a/internal/torrent/messages.go b/internal/torrent/messages.go new file mode 100644 index 0000000..e0dba9d --- /dev/null +++ b/internal/torrent/messages.go @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2026 git-sudo-404 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * Of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * Copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * Copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package torrent + +import "encoding/binary" + +// Helper Function to set/unset bitfield bits +func setBit(pieceIndex int64, bitfield []byte) { + byteIndex := pieceIndex / 8 + bitIndex := pieceIndex % 8 + bitfield[byteIndex] |= 1 << bitIndex +} + +func UnsetBit(pieceIndex int64, bitfield []byte) { + byteIndex := pieceIndex / 8 + bitIndex := pieceIndex % 8 + bitfield[byteIndex] ^= 1 << bitIndex +} + +// keep-alive: +// The keep-alive message is a message with zero bytes, specified with the length prefix set to zero. There is no message ID and no payload. +func NewKeepAliveMessage() [4]byte { + return [4]byte{0, 0, 0, 0} +} + +// choke: +// The choke message is fixed-length and has no payload. +func NewChokeMessage() [5]byte { + return [5]byte{0, 0, 0, 1, 0} +} + +// unchoke: +// The unchoke message is fixed-length and has no payload. +func NewUnchokeMessage() [5]byte { + return [5]byte{0, 0, 0, 1, 1} +} + +// interested: +// The interested message is fixed-length and has no payload. +func NewInterestedMessage() [5]byte { + return [5]byte{0, 0, 0, 1, 2} +} + +// not interested: +// The not interested message is fixed-length and has no payload. +func NewNotInterestedMessage() [5]byte { + return [5]byte{0, 0, 0, 1, 3} +} + +// have: +// The have message is fixed length. The payload is the zero-based index of a piece that has just been successfully downloaded and verified via the hash. +func NewHaveMessage(pieceIndex int32) [9]byte { + msg := [9]byte{0, 0, 0, 1, 1} + binary.BigEndian.PutUint32(msg[5:], uint32(pieceIndex)) + return msg +} diff --git a/internal/torrent/metainfo.go b/internal/torrent/metainfo.go index b9cd4e8..3ed7307 100644 --- a/internal/torrent/metainfo.go +++ b/internal/torrent/metainfo.go @@ -39,9 +39,9 @@ import ( type info struct { // fields common to both single file & mulit file torrent - pieceLength int64 // number of bytes in each piece (integer) - pieces string // consisting of the concatenation of all 20-byte SHA1 hash values, one per piece (byte string, i.e. not urlencoded) - private *int64 // (optional) this field is an integer. If it is set to "1", the client MUST publish its presence + pieceLength int64 // number of bytes in each piece (integer) + pieces [][20]byte // consisting of the concatenation of all 20-byte SHA1 hash values, one per piece (byte string, i.e. not urlencoded) + private *int64 // (optional) this field is an integer. If it is set to "1", the client MUST publish its presence //to get other peers ONLY via the trackers explicitly described in the metainfo file. If this field is set to "0" or is not present, //the client may obtain peer from other means, e.g. PEX peer exchange, dht. Here, "private" may be read as "no external peer source". @@ -67,7 +67,7 @@ func NewMetaInfo() *MetaInfo { return &MetaInfo{ info: info{ pieceLength: 0, - pieces: "", + pieces: [][20]byte{}, name: "", length: 0, }, @@ -82,7 +82,7 @@ func (mi *MetaInfo) SetPieceLength(pieceLength int64) *MetaInfo { return mi } -func (mi *MetaInfo) SetPieces(pieces string) *MetaInfo { +func (mi *MetaInfo) SetPieces(pieces [][20]byte) *MetaInfo { mi.pieces = pieces return mi } @@ -146,7 +146,7 @@ func (mi *MetaInfo) GetPieceLength() (int64, error) { return mi.pieceLength, nil } -func (mi *MetaInfo) GetPieces() (string, error) { +func (mi *MetaInfo) GetPieces() ([][20]byte, error) { return mi.pieces, nil } diff --git a/internal/torrent/metainfoParser.go b/internal/torrent/metainfoParser.go index 3466903..bc845a4 100644 --- a/internal/torrent/metainfoParser.go +++ b/internal/torrent/metainfoParser.go @@ -81,6 +81,16 @@ func getRawInfoFromMetaInfoFile(metaInfoFilePath string) ([]byte, error) { return rawInfo, nil } +func parsePieces(piecesString string) [][20]byte { + fmt.Println("[LOG]Parsing pieces from info") + piecesBytes := []byte(piecesString) + pieces := [][20]byte{} + for i := 0; i+20 <= len(piecesString); i += 20 { + pieces = append(pieces, [20]byte(piecesBytes[i:i+20])) + } + return pieces +} + func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { metaInfo := NewMetaInfo() @@ -110,11 +120,11 @@ func CreateMetaInfoFromFile(filePath string) (*MetaInfo, error) { } metaInfo.SetPieceLength(pieceLength) - pieces, err := checkFieldPresenceAndType[string](info, "pieces") + piecesString, err := checkFieldPresenceAndType[string](info, "pieces") if err != nil { return nil, err } - metaInfo.SetPieces(pieces) + metaInfo.SetPieces(parsePieces(piecesString)) private, present, err := checkOptionalFieldPresenceAndType[int64](info, "private") if err != nil { diff --git a/internal/torrent/metainfoParser_test.go b/internal/torrent/metainfoParser_test.go index 39a3c60..ed1d7ff 100644 --- a/internal/torrent/metainfoParser_test.go +++ b/internal/torrent/metainfoParser_test.go @@ -123,9 +123,9 @@ func TestCreateMetaInfoFromFile(t *testing.T) { t.Errorf("GetPieceLength() = %v, err = %v; want %v", got, err, infoDict["piece length"]) } - if got, err := metaInfo.GetPieces(); err != nil || got != infoDict["pieces"].(string) { - t.Errorf("GetPieces() = %v, err = %v; want %v", got, err, infoDict["pieces"]) - } + // if got, err := metaInfo.GetPieces(); err != nil || got != infoDict["pieces"].([][20]byte) { + // t.Errorf("GetPieces() = %v, err = %v; want %v", got, err, infoDict["pieces"]) + // } if got, err := metaInfo.GetName(); err != nil || got != infoDict["name"].(string) { t.Errorf("GetName() = %v, err = %v; want %v", got, err, infoDict["name"]) diff --git a/internal/torrent/torrent.go b/internal/torrent/torrent.go index 1378be5..91b6092 100644 --- a/internal/torrent/torrent.go +++ b/internal/torrent/torrent.go @@ -26,10 +26,14 @@ package torrent import ( "bufio" + "encoding/binary" "fmt" "gotorrent/internal/bencode" + "io" "net" "net/http" + "strconv" + "sync" "time" ) @@ -151,6 +155,157 @@ func sendTrackerRequest(trackerRequestURL *string, client *Client) { } +func intitaiteHandshakeWithPeers(client *Client, metaInfo *MetaInfo) { + fmt.Println("[LOG] Initiating Peer Handshake ...") + peerHandshakeRequest := NewPeerHandshakeRequest() + + pstr := []byte("BitTorrent protocol") //NOTE: This string is not arbitrary + reserved := make([]byte, 8) + info_hash := metaInfo.GetInfoHash() + peer_id := client.clientId + + var wg sync.WaitGroup + + peerHandshakeRequest.SetPstr(pstr) + peerHandshakeRequest.SetReserved([8]byte(reserved)) + peerHandshakeRequest.SetInfoHash(info_hash) + peerHandshakeRequest.SetPeerId(peer_id) + + peerHandshakeRequestBytes := peerHandshakeRequest.Serialize() + + for peerIndex, peer := range client.peers { + wg.Add(1) + go func() { + conn, err := net.Dial("tcp", net.JoinHostPort(peer.ip.String(), strconv.Itoa(int(peer.port)))) + if err != nil { + fmt.Println("[ERROR] Handshake Failed:", err) + return + } + client.peers[peerIndex].conn = conn + defer func() { + wg.Done() + }() + fmt.Printf("[LOG] Initiating Handshake with Peer : %s on Port : %d\n", peer.ip.String(), peer.port) + n, err := conn.Write(peerHandshakeRequestBytes) + if err != nil { + fmt.Println("[ERROR] Write Failed", err) + return + } + fmt.Println("[DEBUG] Sent", n, "bytes") + readBuffer := make([]byte, 68) + n, err = conn.Read(readBuffer) + if err != nil { + fmt.Println("[ERROR] Read Failed", err) + return + } + fmt.Println("[DEBUG] Recieved", n, "bytes") + + peerInfoHash := readBuffer[28:48] + peerId := readBuffer[48:68] + + for i := 0; i < 20; i++ { + if peerInfoHash[i] != metaInfo.infoHash[i] { + fmt.Println("[ERROR] Info Hash Not matching with peer") + return + } + } + + copy(client.peers[peerIndex].peerId[:], peerId[:]) + fmt.Println("[LOG] Recieved Peer Id : ", client.peers[peerIndex].peerId) + + bitfieldLen := (len(metaInfo.pieces) + 7) / 8 + client.peers[peerIndex].bitfield = make([]byte, bitfieldLen) + + }() + } + + wg.Wait() + +} + +// func getPeerBitFeilds(client *Client) { +// for peerIndex, _ := range client.peers { +// +// peer := &client.peers[peerIndex] +// +// lenBuffer := [4]byte{} +// _, err := io.ReadFull(peer.conn, lenBuffer[:]) +// if err != nil { +// fmt.Println("[ERROR] Error reading the length Prefix of the bitfield message", err) +// } +// +// messageId := [1]byte{} +// io.ReadFull(peer.conn, messageId[:]) +// +// if binary.BigEndian.Uint32(messageId[:]) != 5 { +// return +// } +// +// length := binary.BigEndian.Uint32(lenBuffer[:]) +// bitfieldRawBytes := make([]byte, length) +// +// _, err = io.ReadFull(peer.conn, bitfieldRawBytes) +// if err != nil { +// fmt.Println("[ERROR] Error while reading bitfield message from peer", err) +// } +// +// } +// } + +func handlePeer(wg *sync.WaitGroup, peer *Peer) { + defer func() { + wg.Done() + }() + + for { + + // read the 4 byte message len + msgLengthBytes := [4]byte{} + io.ReadFull(peer.conn, msgLengthBytes[:]) + msgLength := binary.BigEndian.Uint32(msgLengthBytes[:]) + + if msgLength == 0 { // keep-alive msg + peer.alive = time.Now() + } + + msgIdBytes := [1]byte{} + io.ReadFull(peer.conn, msgIdBytes[:]) + msgId := binary.BigEndian.Uint32(msgIdBytes[:]) + + switch msgLength { + case 1: + switch msgId { + case 0: // choke + peer.am_choking = true + case 1: // unchoke + peer.am_choking = false + case 2: // interested + peer.peer_interested = true + case 3: // not-interested + peer.peer_interested = false + } + case 5: + if msgId == 4 { // have msg + pieceIndexBytes := [4]byte{} + io.ReadFull(peer.conn, pieceIndexBytes[:]) + pieceIndex := binary.BigEndian.Uint32(pieceIndexBytes[:]) + setBit(int64(pieceIndex), peer.bitfield) + } + } + + } + +} + +func startPeerMessaging(client *Client, metaInfo *MetaInfo) { + var wg sync.WaitGroup + for peerIndex, _ := range client.peers { + wg.Add(1) + go handlePeer(&wg, &client.peers[peerIndex]) + } + wg.Wait() +} + func StartTorrent(metaInfoFilePath string, destinationFilePath string) { fmt.Println("[LOG] Starting Torrent ...") @@ -173,9 +328,9 @@ func StartTorrent(metaInfoFilePath string, destinationFilePath string) { sendTrackerRequest(&trackerRequestURL, client) - // fmt.Println(len(client.peers)) - // for _, peer := range client.peers { - // fmt.Println(peer) - // } + //NOTE: This function handles only the client peers's handshake , need to implement the logic to handle the incoming handshake requests from other peers + intitaiteHandshakeWithPeers(client, metaInfo) + + startPeerMessaging(client, metaInfo) } From 877ecc2e741986ab621bbf58500a9816a656ab94 Mon Sep 17 00:00:00 2001 From: git-sudo-404 Date: Mon, 28 Sep 2026 22:38:09 +0530 Subject: [PATCH 2/3] getting bitfield msg --- internal/torrent/client.go | 12 ++++++- internal/torrent/messages.go | 2 +- internal/torrent/torrent.go | 46 +++++++++++++++++++++---- internal/torrent/trackerRequest_test.go | 2 +- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/internal/torrent/client.go b/internal/torrent/client.go index 0e89d67..9633310 100644 --- a/internal/torrent/client.go +++ b/internal/torrent/client.go @@ -52,9 +52,18 @@ type Client struct { trackerInterval int64 completePeers int64 incompletePeers int64 + blocks [][]byte } -func NewClient() *Client { +func NewClient(metaInfo *MetaInfo) *Client { + + pieceLength, _ := metaInfo.GetPieceLength() + pieceNums := metaInfo.length / pieceLength + + blocks := make([][]byte, pieceNums) + for i := range blocks { + blocks[i] = make([]byte, pieceLength) + } return &Client{ clientId: generateClientPeerId(), @@ -63,6 +72,7 @@ func NewClient() *Client { trackerInterval: -1, completePeers: -1, incompletePeers: -1, + blocks: blocks, } } diff --git a/internal/torrent/messages.go b/internal/torrent/messages.go index e0dba9d..05dad71 100644 --- a/internal/torrent/messages.go +++ b/internal/torrent/messages.go @@ -33,7 +33,7 @@ func setBit(pieceIndex int64, bitfield []byte) { bitfield[byteIndex] |= 1 << bitIndex } -func UnsetBit(pieceIndex int64, bitfield []byte) { +func unsetBit(pieceIndex int64, bitfield []byte) { byteIndex := pieceIndex / 8 bitIndex := pieceIndex % 8 bitfield[byteIndex] ^= 1 << bitIndex diff --git a/internal/torrent/torrent.go b/internal/torrent/torrent.go index 91b6092..5dc9f7f 100644 --- a/internal/torrent/torrent.go +++ b/internal/torrent/torrent.go @@ -53,7 +53,7 @@ func populateInitialTrackerRequestParams(tr *TrackerRequest, mi *MetaInfo, clien func sendTrackerRequest(trackerRequestURL *string, client *Client) { httpClient := &http.Client{ - Timeout: 5 * time.Second, + Timeout: 15 * time.Second, } req, err := http.NewRequest(http.MethodGet, *trackerRequestURL, nil) if err != nil { @@ -211,7 +211,7 @@ func intitaiteHandshakeWithPeers(client *Client, metaInfo *MetaInfo) { } copy(client.peers[peerIndex].peerId[:], peerId[:]) - fmt.Println("[LOG] Recieved Peer Id : ", client.peers[peerIndex].peerId) + // fmt.Println("[LOG] Recieved Peer Id : ", client.peers[peerIndex].peerId) bitfieldLen := (len(metaInfo.pieces) + 7) / 8 client.peers[peerIndex].bitfield = make([]byte, bitfieldLen) @@ -252,7 +252,7 @@ func intitaiteHandshakeWithPeers(client *Client, metaInfo *MetaInfo) { // } // } -func handlePeer(wg *sync.WaitGroup, peer *Peer) { +func handlePeer(wg *sync.WaitGroup, peer *Peer, client *Client) { defer func() { wg.Done() }() @@ -266,11 +266,12 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer) { if msgLength == 0 { // keep-alive msg peer.alive = time.Now() + continue } msgIdBytes := [1]byte{} io.ReadFull(peer.conn, msgIdBytes[:]) - msgId := binary.BigEndian.Uint32(msgIdBytes[:]) + msgId := msgIdBytes[0] switch msgLength { case 1: @@ -291,6 +292,38 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer) { pieceIndex := binary.BigEndian.Uint32(pieceIndexBytes[:]) setBit(int64(pieceIndex), peer.bitfield) } + case 13: + if msgId == 6 { // request msg + reqMsgPayload := [12]byte{} + io.ReadFull(peer.conn, reqMsgPayload[:]) + index := binary.BigEndian.Uint32(reqMsgPayload[0:4]) + begin := binary.BigEndian.Uint32(reqMsgPayload[4:8]) + length := binary.BigEndian.Uint32(reqMsgPayload[8:12]) + + peer.conn.Write(client.blocks[index][begin : begin+length]) + } else if msgId == 8 { + cancelMsgPayload := make([]byte, msgLength-1) + io.ReadFull(peer.conn, cancelMsgPayload) + //TODO: implement the cancel logic + } + default: + switch msgId { + case 5: // bitfield msg + fmt.Println("[LOG] Recieved a bitfield Msg") + bitfieldBytes := make([]byte, msgLength) + io.ReadFull(peer.conn, bitfieldBytes[:]) + peer.bitfield = bitfieldBytes + case 7: // piece ms + pieceMsgPayload := make([]byte, msgLength-1) // since , 1 byte is already consumed in msgId + io.ReadFull(peer.conn, pieceMsgPayload) + index := binary.BigEndian.Uint32(pieceMsgPayload[0:4]) + begin := binary.BigEndian.Uint32(pieceMsgPayload[4:8]) + blockLen := msgLength - (1 + 4 + 4) + // client.blocks[index][begin : blockLen+begin] = pieceMsgPayload[8:] + for i := range blockLen { + client.blocks[index][begin+i] = pieceMsgPayload[8+i] + } + } } } @@ -298,10 +331,11 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer) { } func startPeerMessaging(client *Client, metaInfo *MetaInfo) { + fmt.Println("[LOG] Starting Peer Messagin...") var wg sync.WaitGroup for peerIndex, _ := range client.peers { wg.Add(1) - go handlePeer(&wg, &client.peers[peerIndex]) + go handlePeer(&wg, &client.peers[peerIndex], client) } wg.Wait() } @@ -316,7 +350,7 @@ func StartTorrent(metaInfoFilePath string, destinationFilePath string) { panic(err) } - client := NewClient() + client := NewClient(metaInfo) trackerRequest := NewTrackerRequest() populateInitialTrackerRequestParams(trackerRequest, metaInfo, client) diff --git a/internal/torrent/trackerRequest_test.go b/internal/torrent/trackerRequest_test.go index 019f76d..863d543 100644 --- a/internal/torrent/trackerRequest_test.go +++ b/internal/torrent/trackerRequest_test.go @@ -37,7 +37,7 @@ func TestGetURLEncodedRequestString(t *testing.T) { if err != nil { panic(err) } - client := NewClient() + client := NewClient(metaInfo) peerIdBytes := client.clientId peerId := string(peerIdBytes[:]) port := 8080 From dce7e8d9e2a03d6aed07d3ac5969fa7eacd2f262 Mon Sep 17 00:00:00 2001 From: git-sudo-404 Date: Wed, 30 Sep 2026 22:31:00 +0530 Subject: [PATCH 3/3] refactored --- internal/torrent/client.go | 13 ++ internal/torrent/handhsake.go | 66 ------- internal/torrent/messages.go | 6 + internal/torrent/torrent.go | 315 +++++++++++++++++----------------- 4 files changed, 181 insertions(+), 219 deletions(-) delete mode 100644 internal/torrent/handhsake.go diff --git a/internal/torrent/client.go b/internal/torrent/client.go index 9633310..0c57443 100644 --- a/internal/torrent/client.go +++ b/internal/torrent/client.go @@ -29,6 +29,7 @@ import ( "fmt" "net" "os" + "sync" "time" ) @@ -43,6 +44,8 @@ type Peer struct { peer_choking bool peer_interested bool alive time.Time + mu sync.Mutex + canReqBlocks chan struct{} } type Client struct { @@ -53,6 +56,9 @@ type Client struct { completePeers int64 incompletePeers int64 blocks [][]byte + blockOffset map[int]int + uploadedBytes uint64 + downloadedBytes uint64 } func NewClient(metaInfo *MetaInfo) *Client { @@ -64,6 +70,10 @@ func NewClient(metaInfo *MetaInfo) *Client { for i := range blocks { blocks[i] = make([]byte, pieceLength) } + blockOffset := make(map[int]int) + for i := 0; i < int(pieceNums); i++ { + blockOffset[i] = 0 + } return &Client{ clientId: generateClientPeerId(), @@ -73,6 +83,9 @@ func NewClient(metaInfo *MetaInfo) *Client { completePeers: -1, incompletePeers: -1, blocks: blocks, + blockOffset: blockOffset, + downloadedBytes: 0, + uploadedBytes: 0, } } diff --git a/internal/torrent/handhsake.go b/internal/torrent/handhsake.go deleted file mode 100644 index c963ae0..0000000 --- a/internal/torrent/handhsake.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2026 git-sudo-404 - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * Of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * Copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * Copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package torrent - -type PeerHandshakeRequest struct { - pstr []byte // identifier of the protocol - reserved [8]byte // eight (8) reserved bytes. All current implementations use all zeroes. Each bit in these bytes can be used to change the behavior of the protocol. An email from Bram suggests that trailing bits should be used first, so that leading bits may be used to change the meaning of trailing bits. - infoHash [20]byte // 20-byte SHA1 hash of the info key in the metainfo file. This is the same info_hash that is transmitted in tracker requests. - peerId [20]byte // 20-byte string used as a unique ID for the client. This is usually the same peer_id that is transmitted in tracker requests (but not always e.g. an anonymity option in Azureus). -} - -func (phr *PeerHandshakeRequest) SetPstr(pstr []byte) *PeerHandshakeRequest { - phr.pstr = pstr - return phr -} - -func (phr *PeerHandshakeRequest) SetReserved(reserved [8]byte) *PeerHandshakeRequest { - phr.reserved = reserved - return phr -} - -func (phr *PeerHandshakeRequest) SetInfoHash(infoHash [20]byte) *PeerHandshakeRequest { - phr.infoHash = infoHash - return phr -} - -func (phr *PeerHandshakeRequest) SetPeerId(peerId [20]byte) *PeerHandshakeRequest { - phr.peerId = peerId - return phr -} - -func (phr *PeerHandshakeRequest) Serialize() []byte { - buf := make([]byte, 49+len(phr.pstr)) - buf[0] = byte(len(phr.pstr)) - copy(buf[1:], phr.pstr) - copy(buf[1+len(phr.pstr):], phr.reserved[:]) - copy(buf[1+len(phr.pstr)+8:], phr.infoHash[:]) - copy(buf[1+len(phr.pstr)+28:], phr.peerId[:]) - return buf -} - -func NewPeerHandshakeRequest() *PeerHandshakeRequest { - return &PeerHandshakeRequest{} -} diff --git a/internal/torrent/messages.go b/internal/torrent/messages.go index 05dad71..46eaf23 100644 --- a/internal/torrent/messages.go +++ b/internal/torrent/messages.go @@ -39,6 +39,12 @@ func unsetBit(pieceIndex int64, bitfield []byte) { bitfield[byteIndex] ^= 1 << bitIndex } +func hasBit(pieceIndex int64, bitfield []byte) bool { + byteIndex := pieceIndex / 8 + bitIndex := pieceIndex % 8 + return bitfield[byteIndex]&(1< // The keep-alive message is a message with zero bytes, specified with the length prefix set to zero. There is no message ID and no payload. func NewKeepAliveMessage() [4]byte { diff --git a/internal/torrent/torrent.go b/internal/torrent/torrent.go index 5dc9f7f..e2ad4bc 100644 --- a/internal/torrent/torrent.go +++ b/internal/torrent/torrent.go @@ -32,30 +32,34 @@ import ( "io" "net" "net/http" + "os" "strconv" "sync" "time" ) -func populateInitialTrackerRequestParams(tr *TrackerRequest, mi *MetaInfo, client *Client) { - fmt.Println("[LOG] Populating TrackerRequest...") - tr.SetInfoHash(mi) - tr.SetPeerId(string(client.clientId[:])) - tr.SetPort(int64(client.port)) - tr.SetUploaded(int64(0)) - tr.SetDownloaded(int64(0)) - tr.SetLeft(mi.length) - tr.SetCompact(int64(0)) - tr.SetNoPeerId(int64(1)) - tr.SetEvent(STARTED) -} +func sendTrackerRequestAndHandleResponse(client *Client, metaInfo *MetaInfo) { -func sendTrackerRequest(trackerRequestURL *string, client *Client) { + trackerRequest := NewTrackerRequest() + fmt.Println("[LOG] Populating TrackerRequest...") + trackerRequest.SetInfoHash(metaInfo) + trackerRequest.SetPeerId(string(client.clientId[:])) + trackerRequest.SetPort(int64(client.port)) + trackerRequest.SetUploaded(int64(0)) + trackerRequest.SetDownloaded(int64(0)) + trackerRequest.SetLeft(metaInfo.length) + trackerRequest.SetCompact(int64(0)) + trackerRequest.SetNoPeerId(int64(1)) + trackerRequest.SetEvent(STARTED) + trackerRequestURL, err := trackerRequest.GetURLEncodedRequestString(metaInfo) + if err != nil { + panic(err) + } httpClient := &http.Client{ Timeout: 15 * time.Second, } - req, err := http.NewRequest(http.MethodGet, *trackerRequestURL, nil) + req, err := http.NewRequest(http.MethodGet, trackerRequestURL, nil) if err != nil { fmt.Println(err.Error()) } @@ -65,12 +69,10 @@ func sendTrackerRequest(trackerRequestURL *string, client *Client) { if err != nil { fmt.Println(err.Error()) } - if resp.StatusCode != http.StatusOK { fmt.Println("Error tracker server return : ", resp.Body) } - - fmt.Println("[LOG] STATUS:", resp.Status) + // fmt.Println("[LOG] STATUS:", resp.Status) buf := bufio.NewReader(resp.Body) @@ -102,12 +104,6 @@ func sendTrackerRequest(trackerRequestURL *string, client *Client) { panic("peers not present in tracker response") } - // fmt.Println("[LOG] Tracker Responded with the following : ....") - // fmt.Println("[LOG] interval : ", interval) - // fmt.Println("[LOG] complete : ", complete) - // fmt.Println("[LOG] incomplete : ", incomplete) - // fmt.Println("[LOG] peers : ", peers) - if val, ok := interval.(int64); ok { client.trackerInterval = val } else { @@ -127,13 +123,9 @@ func sendTrackerRequest(trackerRequestURL *string, client *Client) { } if peerList, ok := peers.([]any); ok { - // fmt.Println("[DEBUG PRINT] PEER LIST : ", peerList) for _, peerListItem := range peerList { - // fmt.Println("[DEBUG PRINT] PEERLISTITEM : ", peerListItem) if peerMap, ok := peerListItem.(map[string]any); ok { ip, ok := peerMap["ip"].(string) - // fmt.Println("[DEBUG PRINT] IP : ", peerMap["ip"]) - // fmt.Println("[DEBUG PRINT] PORT : ", peerMap["port"]) if !ok { fmt.Println("[ERROR] ip not of type string from tracker response") } @@ -141,124 +133,93 @@ func sendTrackerRequest(trackerRequestURL *string, client *Client) { if !ok { fmt.Println("[ERROR] port not of type integer from tracker response") } + bitfieldLen := (len(metaInfo.pieces) + 7) / 8 client.peers = append(client.peers, Peer{ - ip: net.ParseIP(ip), - port: port, + ip: net.ParseIP(ip), + port: port, + mu: sync.Mutex{}, + canReqBlocks: make(chan struct{}), + bitfield: make([]byte, bitfieldLen), }) } } } else { fmt.Println("[ERROR] Wrong data type in peerList") } - fmt.Println("[LOG] Updated Client Peers from tracker Response") +} + +func sendHandshakeRequest(wg *sync.WaitGroup, peer *Peer, metaInfo *MetaInfo, peerHandshakeRequestMsg [68]byte) { + + defer wg.Done() + + conn, err := net.Dial("tcp", net.JoinHostPort(peer.ip.String(), strconv.Itoa(int(peer.port)))) + if err != nil { + fmt.Println("[ERROR] Handshake Failed:", err) + return + } + peer.conn = conn + + fmt.Printf("[LOG] Initiating Handshake with Peer : %s on Port : %d\n", peer.ip.String(), peer.port) + n, err := conn.Write(peerHandshakeRequestMsg[:]) + if err != nil { + fmt.Println("[ERROR] Write Failed", err) + return + } + fmt.Println("[DEBUG] Sent", n, "bytes") + + readBuffer := make([]byte, 68) + n, err = io.ReadFull(conn, readBuffer) + if err != nil { + fmt.Println("[ERROR] Read Failed", err) + return + } + fmt.Println("[DEBUG] Recieved", n, "bytes") + + peerInfoHash := readBuffer[28:48] + peerId := readBuffer[48:68] + + for i := 0; i < 20; i++ { + if peerInfoHash[i] != metaInfo.infoHash[i] { + fmt.Println("[ERROR] Info Hash Not matching with peer") + return + } + } + + copy(peer.peerId[:], peerId[:]) + fmt.Println("[LOG] Recieved Peer Id ") } -func intitaiteHandshakeWithPeers(client *Client, metaInfo *MetaInfo) { +func sendHandshakeRequestToAllPeers(client *Client, metaInfo *MetaInfo) { fmt.Println("[LOG] Initiating Peer Handshake ...") - peerHandshakeRequest := NewPeerHandshakeRequest() pstr := []byte("BitTorrent protocol") //NOTE: This string is not arbitrary reserved := make([]byte, 8) info_hash := metaInfo.GetInfoHash() peer_id := client.clientId - var wg sync.WaitGroup + peerHandshakeRequestMsg := [68]byte{} + peerHandshakeRequestMsg[0] = uint8(19) + copy(peerHandshakeRequestMsg[1:20], pstr) + copy(peerHandshakeRequestMsg[20:28], reserved) + copy(peerHandshakeRequestMsg[28:48], info_hash[:]) + copy(peerHandshakeRequestMsg[48:68], peer_id[:]) - peerHandshakeRequest.SetPstr(pstr) - peerHandshakeRequest.SetReserved([8]byte(reserved)) - peerHandshakeRequest.SetInfoHash(info_hash) - peerHandshakeRequest.SetPeerId(peer_id) - - peerHandshakeRequestBytes := peerHandshakeRequest.Serialize() + var wg sync.WaitGroup - for peerIndex, peer := range client.peers { + for peerIndex, _ := range client.peers { + peer := &client.peers[peerIndex] wg.Add(1) - go func() { - conn, err := net.Dial("tcp", net.JoinHostPort(peer.ip.String(), strconv.Itoa(int(peer.port)))) - if err != nil { - fmt.Println("[ERROR] Handshake Failed:", err) - return - } - client.peers[peerIndex].conn = conn - defer func() { - wg.Done() - }() - fmt.Printf("[LOG] Initiating Handshake with Peer : %s on Port : %d\n", peer.ip.String(), peer.port) - n, err := conn.Write(peerHandshakeRequestBytes) - if err != nil { - fmt.Println("[ERROR] Write Failed", err) - return - } - fmt.Println("[DEBUG] Sent", n, "bytes") - readBuffer := make([]byte, 68) - n, err = conn.Read(readBuffer) - if err != nil { - fmt.Println("[ERROR] Read Failed", err) - return - } - fmt.Println("[DEBUG] Recieved", n, "bytes") - - peerInfoHash := readBuffer[28:48] - peerId := readBuffer[48:68] - - for i := 0; i < 20; i++ { - if peerInfoHash[i] != metaInfo.infoHash[i] { - fmt.Println("[ERROR] Info Hash Not matching with peer") - return - } - } - - copy(client.peers[peerIndex].peerId[:], peerId[:]) - // fmt.Println("[LOG] Recieved Peer Id : ", client.peers[peerIndex].peerId) - - bitfieldLen := (len(metaInfo.pieces) + 7) / 8 - client.peers[peerIndex].bitfield = make([]byte, bitfieldLen) - - }() + go sendHandshakeRequest(&wg, peer, metaInfo, peerHandshakeRequestMsg) } wg.Wait() - } -// func getPeerBitFeilds(client *Client) { -// for peerIndex, _ := range client.peers { -// -// peer := &client.peers[peerIndex] -// -// lenBuffer := [4]byte{} -// _, err := io.ReadFull(peer.conn, lenBuffer[:]) -// if err != nil { -// fmt.Println("[ERROR] Error reading the length Prefix of the bitfield message", err) -// } -// -// messageId := [1]byte{} -// io.ReadFull(peer.conn, messageId[:]) -// -// if binary.BigEndian.Uint32(messageId[:]) != 5 { -// return -// } -// -// length := binary.BigEndian.Uint32(lenBuffer[:]) -// bitfieldRawBytes := make([]byte, length) -// -// _, err = io.ReadFull(peer.conn, bitfieldRawBytes) -// if err != nil { -// fmt.Println("[ERROR] Error while reading bitfield message from peer", err) -// } -// -// } -// } - -func handlePeer(wg *sync.WaitGroup, peer *Peer, client *Client) { - defer func() { - wg.Done() - }() - +func handlePeer(peerMessagingWG *sync.WaitGroup, peer *Peer, metaInfo *MetaInfo, blocks [][]byte, blockOffset map[int]int, downloadedBytes *uint64, uploadedBytes *uint64) { + defer peerMessagingWG.Done() for { - // read the 4 byte message len msgLengthBytes := [4]byte{} io.ReadFull(peer.conn, msgLengthBytes[:]) @@ -269,9 +230,9 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer, client *Client) { continue } - msgIdBytes := [1]byte{} - io.ReadFull(peer.conn, msgIdBytes[:]) - msgId := msgIdBytes[0] + msgIdByte := [1]byte{} + io.ReadFull(peer.conn, msgIdByte[:]) + msgId := msgIdByte[0] switch msgLength { case 1: @@ -294,13 +255,14 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer, client *Client) { } case 13: if msgId == 6 { // request msg + fmt.Println("[LOG] Reecieved a request message ...") reqMsgPayload := [12]byte{} io.ReadFull(peer.conn, reqMsgPayload[:]) - index := binary.BigEndian.Uint32(reqMsgPayload[0:4]) - begin := binary.BigEndian.Uint32(reqMsgPayload[4:8]) - length := binary.BigEndian.Uint32(reqMsgPayload[8:12]) - - peer.conn.Write(client.blocks[index][begin : begin+length]) + // index := binary.BigEndian.Uint32(reqMsgPayload[0:4]) + // begin := binary.BigEndian.Uint32(reqMsgPayload[4:8]) + // length := binary.BigEndian.Uint32(reqMsgPayload[8:12]) + // // piecePayload := blocks[index][begin : begin+length] + // pieceMessage := make([]byte,length + 4 + 1) } else if msgId == 8 { cancelMsgPayload := make([]byte, msgLength-1) io.ReadFull(peer.conn, cancelMsgPayload) @@ -310,61 +272,108 @@ func handlePeer(wg *sync.WaitGroup, peer *Peer, client *Client) { switch msgId { case 5: // bitfield msg fmt.Println("[LOG] Recieved a bitfield Msg") - bitfieldBytes := make([]byte, msgLength) + bitfieldBytes := make([]byte, msgLength-1) io.ReadFull(peer.conn, bitfieldBytes[:]) peer.bitfield = bitfieldBytes - case 7: // piece ms + peer.canReqBlocks <- struct{}{} + case 7: // piece msg + fmt.Println("[LOG] Recieved a piece message...") pieceMsgPayload := make([]byte, msgLength-1) // since , 1 byte is already consumed in msgId io.ReadFull(peer.conn, pieceMsgPayload) index := binary.BigEndian.Uint32(pieceMsgPayload[0:4]) begin := binary.BigEndian.Uint32(pieceMsgPayload[4:8]) blockLen := msgLength - (1 + 4 + 4) - // client.blocks[index][begin : blockLen+begin] = pieceMsgPayload[8:] for i := range blockLen { - client.blocks[index][begin+i] = pieceMsgPayload[8+i] + blocks[index][begin+i] = pieceMsgPayload[8+i] + } + blockOffset[int(index)] = int(begin + blockLen) + fmt.Println("[LOG] Piece successfully recieved ") + *downloadedBytes += uint64(blockLen) + if blockOffset[int(index)] < int(metaInfo.pieceLength) { + fmt.Println("[LOG] Request next block offsett") + peer.canReqBlocks <- struct{}{} } + fmt.Println("[LOG] Downloaded Bytes : ", *downloadedBytes) } } + } +} +func handlePeerMessaging(torrentWG *sync.WaitGroup, client *Client, metaInfo *MetaInfo) { + fmt.Println("[LOG] Starting Peer Message Handling...") + var peerMessagingWG sync.WaitGroup + for peerIndex, _ := range client.peers { + peer := &client.peers[peerIndex] + peerMessagingWG.Add(1) + go handlePeer(&peerMessagingWG, peer, metaInfo, client.blocks, client.blockOffset, &client.downloadedBytes, &client.uploadedBytes) } + peerMessagingWG.Wait() + torrentWG.Done() +} +func downloadFromPeer(downloadBlocksWG *sync.WaitGroup, peer *Peer, metaInfo *MetaInfo, blocks [][]byte, blockOffset map[int]int, downloadedBytes *uint64, uploadedBytes *uint64) { + defer downloadBlocksWG.Done() + for range peer.canReqBlocks { + fmt.Println("[LOG] Requested a block from peer") + for i := 0; i < len(peer.bitfield); i++ { + for j := 0; j < 8; j++ { + blockIndex := uint32(i*8 + j) + if !hasBit(int64(blockIndex), peer.bitfield) { + continue + } + if blockOffset[int(blockIndex)] >= int(metaInfo.pieceLength) { + continue + } + reqMsgBytes := [17]byte{} + binary.BigEndian.PutUint32(reqMsgBytes[0:4], uint32(13)) + reqMsgBytes[4] = byte(6) // msgId + binary.BigEndian.PutUint32(reqMsgBytes[5:9], blockIndex) + binary.BigEndian.PutUint32(reqMsgBytes[9:13], uint32(blockOffset[int(blockIndex)])) + binary.BigEndian.PutUint32(reqMsgBytes[13:17], uint32(1<<14)) // 16 KB + _, err := peer.conn.Write(reqMsgBytes[:]) + if err != nil { + fmt.Println("[ERROR] Error when writing req msg", err) + } + fmt.Println("[LOG] Sent a piece req message : ", reqMsgBytes) + goto nextTime + } + } + nextTime: + } } -func startPeerMessaging(client *Client, metaInfo *MetaInfo) { - fmt.Println("[LOG] Starting Peer Messagin...") - var wg sync.WaitGroup +func DownloadBlocks(torrentWG *sync.WaitGroup, client *Client, metaInfo *MetaInfo) { + var downloadBlocksWG sync.WaitGroup + interestedMessage := NewInterestedMessage() for peerIndex, _ := range client.peers { - wg.Add(1) - go handlePeer(&wg, &client.peers[peerIndex], client) + peer := &client.peers[peerIndex] + downloadBlocksWG.Add(1) + peer.conn.Write(interestedMessage[:]) + go downloadFromPeer(&downloadBlocksWG, peer, metaInfo, client.blocks, client.blockOffset, &client.downloadedBytes, &client.uploadedBytes) } - wg.Wait() + downloadBlocksWG.Wait() + torrentWG.Done() } func StartTorrent(metaInfoFilePath string, destinationFilePath string) { - fmt.Println("[LOG] Starting Torrent ...") - - fmt.Println("[LOG] Parsing MetaInfo") + fmt.Println("[LOG] Parsing MetaInfo...") metaInfo, err := CreateMetaInfoFromFile(metaInfoFilePath) if err != nil { - panic(err) + fmt.Println("[ERROR] Error when parsing MetaInfo file : ", err) + os.Exit(1) } client := NewClient(metaInfo) - trackerRequest := NewTrackerRequest() - - populateInitialTrackerRequestParams(trackerRequest, metaInfo, client) - - trackerRequestURL, err := trackerRequest.GetURLEncodedRequestString(metaInfo) - if err != nil { - panic(err) - } - - sendTrackerRequest(&trackerRequestURL, client) - //NOTE: This function handles only the client peers's handshake , need to implement the logic to handle the incoming handshake requests from other peers - intitaiteHandshakeWithPeers(client, metaInfo) + sendTrackerRequestAndHandleResponse(client, metaInfo) - startPeerMessaging(client, metaInfo) + sendHandshakeRequestToAllPeers(client, metaInfo) + var torrentWG sync.WaitGroup + torrentWG.Add(1) + go DownloadBlocks(&torrentWG, client, metaInfo) + torrentWG.Add(1) + go handlePeerMessaging(&torrentWG, client, metaInfo) + torrentWG.Wait() }