-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.pm
279 lines (218 loc) · 5.55 KB
/
net.pm
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package net;
use warnings;
use strict;
use IO::Socket::INET;
use event;
use serialize;
use main;
use base58;
use util;
sub D() { 1 }
our $WIRE_MAGIC = "\xf9\xbe\xb4\xd9";
our $VERSION = 300;
our $pszSubVer = ".0";
our $DEFAULT_PORT = 8333;
our $PUBLISH_HOPS = 5;
our $NODE_NETWORK = 1 << 0;
our $pchIPv4 = "\0" x 10 . "\xff" x 2;
our $MSG_TX = 1;
our $MSG_BLOCK = 2;
sub PushMessage {
my ($file, $cmd, $data) = @_;
D && warn "debug fileno=$file->{fileno} cmd=$cmd data="
. serialize::Dump ($cmd, $data);
my $msg = serialize::Serialize ($cmd, $data);
event::timer_reset ($file->{timer_ping})
if $cmd ne 'ping';
event::file_write ($file, pack
'a4 Z12 V ' . ($file->{has_crc} ? 'a4' : 'a0') . ' a*',
$WIRE_MAGIC, $cmd, length $msg, base58::Hash ($msg), $msg);
}
sub state_hdr {
my ($file) = @_;
D && warn "debug";
my $hdr = event::file_read ($file, 4 + 12 + 4);
return \&state_hdr if !defined $hdr;
@$file{qw( net_magic net_func net_len )} = unpack 'a4 Z12 V', $hdr;
$file->{net_magic} eq $WIRE_MAGIC
or die "got bad magic " . unpack 'H*', $hdr;
D && warn "debug func=$file->{net_func} len=$file->{net_len}";
$file->{net_len} < 1_000_000
or die "message is too big";
$file->{has_crc} ? goto &state_crc : goto &state_msg;
}
sub state_crc {
my ($file) = @_;
D && warn "debug";
my $hdr = event::file_read ($file, 4);
return \&state_crc if !defined $hdr;
$file->{net_crc} = unpack 'V', $hdr;
D && warn "debug crc=$file->{net_crc}";
goto &state_msg;
}
sub state_msg {
my ($file) = @_;
D && warn "debug";
my $msg = event::file_read ($file, $file->{net_len});
return \&state_msg if !defined $msg;
if (exists $file->{net_crc}) {
my $crc = unpack 'V', base58::Hash ($msg);
$file->{net_crc} == $crc
or die "bad crc $crc != $file->{net_crc}";
}
event::timer_reset ($file->{timer_inact});
D && warn "debug msg " . unpack ('H*', $msg);
my $str = serialize::Unserialize ($file->{net_func}, $msg);
D && warn "debug $file->{net_func} " .
serialize::Dump ($file->{net_func}, $str);
no strict 'refs';
exists &{"got_$file->{net_func}"}
or die "bad func $file->{net_func}";
&{"got_$file->{net_func}"} ($file, $str);
goto &state_hdr;
}
sub read_cb {
my ($file) = @_;
$file->{net_state} = $file->{net_state} ($file);
}
sub CAddress {
return {
nServices => $NODE_NETWORK,
pchReserved => $pchIPv4,
ip => $_[0],
port => $_[1],
};
}
sub send_version {
my ($file) = @_;
D && warn "debug";
$file->{nLocalHostNonce} = join '', map chr rand 256, 1..8;
PushMessage ($file, 'version', {
nVersion => $VERSION,
nLocalServices => 1,
nTime => time,
addrYou => CAddress ('1.2.3.4', 8333),
addrMe => CAddress ('1.2.3.4', 8333),
nLocalHostNonce => $file->{nLocalHostNonce},
strSubVer => '',
nStartingHeight => -1,
});
}
sub got_version {
my ($file, $ver) = @_;
D && warn "debug";
die "version $ver->{nVersion} is too low"
if $ver->{nVersion} < 200;
die "equal nonce"
if $ver->{nLocalHostNonce} eq $file->{nLocalHostNonce};
# PushMessage ($file, 'getaddr');
}
sub got_ping {
my ($file) = @_;
D && warn "debug";
}
sub PushGetData {
my ($file) = @_;
my @h = data::blk_missed ();
D && warn "debug @util::b2h{@h}";
PushMessage ($file, 'getdata', [ map +{
type => $MSG_BLOCK,
hash => $_,
}, @h ]) if @h;
}
sub PushGetBlocks {
my ($file) = @_;
PushMessage ($file, 'getblocks', {
nVersion => $VERSION,
locator => [ $main::blk_best->{h} ],
hashStop => $main::NULL256,
});
}
sub got_verack {
my ($file) = @_;
D && warn "debug";
PushMessage ($file, 'verack');
$file->{has_crc} = 1;
D && warn "start downloading";
PushGetBlocks ($file);
}
sub got_addr {
my ($file, $addr) = @_;
D && warn "debug";
my $v4 = grep $_->{pchReserved} eq $pchIPv4, @$addr;
warn "$v4 ipv4 addresses of " . @$addr . " total\n";
}
sub got_inv {
my ($file, $inv) = @_;
D && warn "debug";
my $outv = [];
my $flag = 0;
for (@$inv) {
my $h = $_->{hash};
if ($_->{type} == $MSG_TX) {
push @$outv, $_ if !data::tx_exists ($h);
} elsif ($_->{type} == $MSG_BLOCK) {
my $b = data::blk_exists ($h);
push @$outv, $_ if !$b;
$flag = 1 if $b && $b->{nHeight} == -1;
} else {
die "inv unknown type $_->{type}";
}
}
PushMessage ($file, 'getdata', $outv) if @$outv;
PushGetBlocks ($file) if $flag;
}
sub got_block {
my ($file, $blk) = @_;
D && warn "debug";
$blk->{nVersion} == 1
or die "bad version $blk->{nVersion}";
if (!main::ProcessBlock ($blk)) {
# orphaned, continue download on first, or get missing blocks
$file->{bc_dl}++ ? PushGetData ($file) : PushGetBlocks ($file);
}
}
sub got_tx {
my ($file, $tx) = @_;
D && warn "debug";
$tx->{nVersion} == 1
or die "bad version $tx->{nVersion}";
main::ProcessTransaction ($tx);
}
sub got_getblocks {
my ($file, $gb) = @_;
D && warn "debug";
}
sub connect {
my ($addr, $port) = @_;
D && warn "connecting to $addr:$port";
my $sock = IO::Socket::INET->new (
PeerAddr => $addr,
PeerPort => $port,
Blocking => 0,
) or die "connect to $addr:$port failed";
binmode $sock;
warn "connected to $addr:$port";
print "connected to node $addr:$port\n";
my $file;
$file = event::file_new (
fh => $sock,
std_read_cb => \&read_cb,
net_state => \&state_hdr,
close_cb => sub {
event::timer_del ($file->{timer_ping});
event::timer_del ($file->{timer_inact});
die "closed by $file->{closereason}";
},
);
$file->{timer_ping} = event::timer_new (
period => 30 * 60,
cb => sub { PushMessage ($file, 'ping') },
);
$file->{timer_inact} = event::timer_new (
period => 90 * 60,
cb => sub { event::file_close ($file, 'inactivity') },
);
send_version ($file);
}
1;