From 536837b44823aedf3dab0b8ef844d57cbae7af74 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sun, 12 Nov 2023 09:07:50 +0000 Subject: Handshake a peer --- src/main.zig | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 9966a55..d50cc90 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,8 +1,15 @@ const std = @import("std"); const MetaInfo = @import("metainfo.zig"); const bencode = @import("bencode.zig"); +const AnyWriter = @import("anywriter.zig"); +const peerproto = @import("peer_protocol.zig"); + +const prr = "00112233445566778899"; pub fn main() !void { + var peer_id: [20]u8 = undefined; + @memcpy(&peer_id, prr); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const a = gpa.allocator(); @@ -27,7 +34,7 @@ pub fn main() !void { var info_hash = try mi.info.hash(a); var buf_left = [_]u8{0} ** 1024; try q.put("info_hash", &info_hash); - try q.put("peer_id", "00112233445566778899"); + try q.put("peer_id", &peer_id); try q.put("port", "6881"); try q.put("uploaded", "0"); try q.put("downloaded", "0"); @@ -44,10 +51,27 @@ pub fn main() !void { defer trb.deinit(a); var tr = try TrackerResp.parse(a, trb); defer tr.deinit(a); - var w = std.io.getStdOut().writer(); + for (tr.peers) |peer| { - try std.fmt.format(w, "peer: {}\n", .{peer}); + std.log.info("peer: {}", .{peer}); } + + // Handle peers... + + + const p = tr.peers[0]; + var ps = try std.net.tcpConnectToAddress(p); + defer ps.close(); + var pw = ps.writer(); + var pr = ps.reader(); + var hs: peerproto.Handshake = .{ + .info_hash = info_hash, + .peer_id = peer_id, + }; + + try hs.write(pw); + var phs = try peerproto.Handshake.read(pr); + std.log.info("peer at {} peer_id {s}", .{ p, std.fmt.fmtSliceHexLower(&phs.peer_id) }); } } @@ -95,6 +119,7 @@ const TrackerResp = struct { } }; + test { _ = bencode; _ = MetaInfo; -- cgit v1.2.3-ZIG