From 56325badfaa713a920128d27479e9205615957c1 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Fri, 1 Dec 2023 23:53:25 +0000 Subject: Make peer_id a constant --- src/main.zig | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index f27735d..7425c6d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,17 +5,17 @@ const AnyWriter = @import("anywriter.zig"); const peerproto = @import("peer_protocol.zig"); const trackproto = @import("tracker_protocol.zig"); +// TODO figure this out. It's not that important, I think, unless +// other clients have special handling for different patterns. +// Spec looks like a bit of a free-for-all here. +const peer_id = [20]u8{ 0x30, 0x30, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33, 0x34, 0x34, 0x35, 0x35, 0x36, 0x36, 0x37, 0x37, 0x38, 0x38, 0x39, 0x39 }; +// To make this a practical torrent app... +// Add some UI? or keep it a console app? I might keep it as a console app for now pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const a = gpa.allocator(); - // TODO figure this out. It's not that important, I think, unless - // other clients have special handling for different patterns. - // Spec looks like a bit of a free-for-all here. - var peer_id: [20]u8 = undefined; - @memcpy(&peer_id, "00112233445566778899"); - const f = try std.fs.cwd().openFile("src/sample.torrent", .{}); defer f.close(); var fr = f.reader(); @@ -68,7 +68,6 @@ pub fn main() !void { var phs = try peerproto.Handshake.read(pr); std.log.info("peer at {} peer_id {s}", .{ p, std.fmt.fmtSliceHexLower(&phs.peer_id) }); - var bf = try peerproto.readMessage(a, pr, peerproto.Bitfield); _ = bf; // ignore it for now. try peerproto.Interested.write(pw); @@ -90,10 +89,10 @@ pub fn main() !void { var s1 = std.crypto.hash.Sha1.init(.{}); // Send a request message for each 16KiB block of the first piece - const blklen: u32 = 16*1024; + const blklen: u32 = 16 * 1024; var blkcount = try std.math.divCeil(u32, piece_length, blklen); for (0..blkcount) |i| { - const begin = std.math.cast(u32, i*blklen).?; + const begin = std.math.cast(u32, i * blklen).?; const len = @min(blklen, piece_length - begin); const req = peerproto.Request{ .index = @intCast(pi), @@ -108,14 +107,14 @@ pub fn main() !void { if (piece.begin != req.begin) return error.ProtocolError; if (piece.block.len != req.length) return error.ProtocolError; s1.update(piece.block); - @memcpy(piece_buf[piece.begin..piece.begin+piece.block.len], piece.block); + @memcpy(piece_buf[piece.begin .. piece.begin + piece.block.len], piece.block); } var ah = s1.finalResult(); var ph0 = mi.info.pieceHash(pi).?; if (std.mem.eql(u8, &ah, &ph0)) { try of.writeAll(piece_buf[0..piece_length]); } else { - return error.BadHash; + return error.BadHash; } } std.log.info("fin", .{}); -- cgit v1.2.3-ZIG