blob: cd70ad22399c65402cdb0b16c7297d0b1231ea7a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
const std = @import("std");
pub const Handshake = struct {
info_hash: [20]u8,
peer_id: [20]u8,
pub fn read(reader: anytype) !Handshake {
var msg = [_]u8{0} ** 68;
try reader.readNoEof(&msg);
if (msg[0] != 19) return error.ProtocolError;
if (!std.mem.eql(u8, msg[1..20], "BitTorrent protocol")) return error.ProtocolError;
//if (!std.mem.allEqual(u8, msg[20..28], 0)) return error.ProtocolError;
var res: Handshake = undefined;
@memcpy(&res.info_hash, msg[28..48]);
@memcpy(&res.peer_id, msg[48..68]);
return res;
}
pub fn write(self: Handshake, writer: anytype) !void {
try writer.writeByte(19);
try writer.writeAll("BitTorrent protocol");
try writer.writeByteNTimes(0, 8);
try writer.writeAll(&self.info_hash);
try writer.writeAll(&self.peer_id);
}
};
|