aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-12-01 23:53:25 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-12-01 23:53:25 +0000
commit56325badfaa713a920128d27479e9205615957c1 (patch)
tree2525408dd2f7b2606b0b196bd57a86bf73df93a3 /src/main.zig
parent6c24cd4862cd06f3364810b73e3de1bab411f31a (diff)
downloadzbt-56325badfaa713a920128d27479e9205615957c1.tar.gz
zbt-56325badfaa713a920128d27479e9205615957c1.tar.bz2
zbt-56325badfaa713a920128d27479e9205615957c1.tar.xz
zbt-56325badfaa713a920128d27479e9205615957c1.zip
Make peer_id a constant
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig21
1 files changed, 10 insertions, 11 deletions
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", .{});