-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBTCThreadMonitorUnit.pas
More file actions
255 lines (196 loc) · 5.61 KB
/
BTCThreadMonitorUnit.pas
File metadata and controls
255 lines (196 loc) · 5.61 KB
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
unit BTCThreadMonitorUnit;
interface
uses
System.SysUtils, Winapi.Windows, Winapi.Messages,
classes, dialogs, ipwcore, ipwtypes, ipwipport, BTCTypes;
type
TBTCThreadMonitor = class(TThread)
strict private
fparent: tcomponent;
ipwIPPort1: TipwIPPort;
fPeer: string;
strict protected
procedure Execute; override;
procedure ipwIPPort1Connected(Sender: TObject; StatusCode: Integer;
const Description: string);
procedure DataIn(Sender: TObject; Text: String; TextB: TBytes;
EOL: Boolean);
procedure ConnectionStatus(Sender: TObject; const ConnectionEvent: string;
StatusCode: Integer; const Description: string);
procedure ReadyToSend(Sender: TObject);
procedure Error(Sender: TObject; ErrorCode: Integer;
const Description: string);
procedure DoMessageDetected(const aHeader: THeader);
public
constructor Create(const Peer: string; parent: tcomponent);
destructor Destroy; override;
end;
Net_addr = record
services: int64;
ipv64: array [1 .. 16] of char;
port: byte;
end;
BTCPayload = record
version: Int32;
services: int64;
timestamp: int64;
addr_recv: Net_addr;
end;
implementation
uses
BTCMonitorUnit;
function StringBytesToTBytes(const s: string): TBytes;
var
k: Integer;
begin
SetLength(result, length(s) div 2);
for k := 0 to (s.length div 2) - 1 do
result[k] := StrToInt('$' + s[(k * 2) + 1] + s[(k + 1) * 2]);
end;
procedure TBTCThreadMonitor.ConnectionStatus(Sender: TObject;
const ConnectionEvent: string; StatusCode: Integer;
const Description: string);
begin
// showmessage(ConnectionEvent);
end;
constructor TBTCThreadMonitor.Create(const Peer: string; parent: tcomponent);
begin
inherited Create;
fparent := parent;
fPeer := Peer;
ipwIPPort1 := TipwIPPort.Create(nil);
ipwIPPort1.OnConnected := ipwIPPort1Connected;
ipwIPPort1.OnDataIn := DataIn;
ipwIPPort1.OnConnectionStatus := ConnectionStatus;
ipwIPPort1.OnReadyToSend := ReadyToSend;
ipwIPPort1.OnError := Error;
end;
procedure TBTCThreadMonitor.DataIn(Sender: TObject; Text: String; TextB: TBytes;
EOL: Boolean);
var
k, j: Integer;
status: Integer;
amessage: string;
alength, checksum: cardinal;
payload : TBytes;
ppayload : ^byte;
aHeader : THeader;
command : string;
begin
k := 0;
status := 0;
while k < length(TextB) do
begin
case status of
// magic code detection
0:
if TextB[k] = $F9 then
inc(status);
1:
if TextB[k] = $BE then
inc(status);
2:
if TextB[k] = $B4 then
inc(status);
3:
if TextB[k] = $D9 then
inc(status);
// command detection
4:
begin
amessage := '';
j := 0;
while (j < 12) and (TextB[j + k] <> 0) do
begin
amessage := amessage + char(TextB[k + j]);
inc(j);
end;
// showmessage(amessage + ' ' + inttostr(amessage.length));
k := k + 11;
inc(status);
end;
// length detection
5:
begin
alength := TextB[k] + TextB[k + 1] + TextB[k + 2] + TextB[k + 3];
/// ojo, solo el primer byte... añadir los siguientes
// showmessage(inttostr(alength));
k := k + 3;
inc(status);
end;
// checksum detection
6:
begin
/// Please get checksum
checksum := TextB[k] + TextB[k + 1] + TextB[k + 2] + TextB[k + 3];
k := k + 3;
setlength(payload,alength);
CopyMemory(payload, @TextB[k], alength);
DoMessageDetected(BuildHeader(amessage));
status := 0;
end;
end;
inc(k);
end;
end;
destructor TBTCThreadMonitor.Destroy;
begin
ipwIPPort1.Disconnect;
inherited;
end;
procedure TBTCThreadMonitor.DoMessageDetected(const aHeader : THeader);
begin
Synchronize(
procedure
begin
// TBTCMonitorComponent(fparent).PrintMessage(fPeer+' '+aHeader.command_name);
end)
end;
procedure TBTCThreadMonitor.Error(Sender: TObject; ErrorCode: Integer;
const Description: string);
begin
end;
procedure TBTCThreadMonitor.Execute;
begin
inherited;
ipwIPPort1.Connect(fPeer, 8333);
end;
procedure TBTCThreadMonitor.ipwIPPort1Connected(Sender: TObject;
StatusCode: Integer; const Description: string);
begin
end;
procedure TBTCThreadMonitor.ReadyToSend(Sender: TObject);
var
tb: TBytes;
pHeader: ^THeader;
aHeader : THeader;
begin
aHeader := BuildHeader('version');
(*new(pHeader);
pHeader^.start_string := $D9B4BEF9;
pHeader^.command_name[1] := 'v';
pHeader^.command_name[2] := 'e';
pHeader^.command_name[3] := 'r';
pHeader^.command_name[4] := 's';
pHeader^.command_name[5] := 'i';
pHeader^.command_name[6] := 'o';
pHeader^.command_name[7] := 'n';
pHeader^.command_name[8] := char(0);
pHeader^.command_name[9] := char(0);
pHeader^.command_name[10] := char(0);
pHeader^.command_name[11] := char(0);
pHeader^.command_name[12] := char(0);
pHeader^.payload_size := $66;
pHeader^.checksum := $9B3DBA94;
*)
SetLength(tb, sizeof(THeader));
CopyMemory(tb, @aHeader, sizeof(THeader));
// showmessage(Description);
// tb := StringBytesToTBytes('f9beb4d976657273696f6e00000000006600000094ba3d9b');
// tb := StringBytesToTBytes('9c');
ipwIPPort1.send(tb);
tb := StringBytesToTBytes
('8011010008040000000000000defbc6100000000090400000000000000000000000000000000ffff23c121bf208d0804000000000000000000000000000000000000000000000000fdba00e1964f2006102f5361746f7368693a32322e302e302f25c2030001');
ipwIPPort1.send(tb);
end;
end.