-
Notifications
You must be signed in to change notification settings - Fork 2
/
EthThrd.pas
788 lines (701 loc) · 22.5 KB
/
EthThrd.pas
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/////////////////////////////////////////////////////////////////////////
// //
// Orion/Z (Orion-128 + Z80-CARD-II) emulator, version 1.9 //
// //
// Author: Sergey A. <a-s-m@km.ru> //
// //
// Copyright (C) 2006-2016 Sergey A. //
// //
// This program is free software; you can redistribute it and/or //
// modify it in any ways. //
// This program is distributed "AS IS" in the hope that it will be //
// useful, but WITHOUT ANY WARRANTY; without even the implied //
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
// //
/////////////////////////////////////////////////////////////////////////
{*****************************************************************
EtherNet Layer2 emulation with destination TAP virtual adapter
*****************************************************************}
unit EthThrd;
interface
{$I 'OrionZEm.inc'}
Uses Windows, Messages, SysUtils, Classes, Registry;
const
ETH_P_IP = $0800; // IPv4 protocol
ETH_P_IP6 = $86dd; // IPv6 protocol
ETH_P_ARP = $0806; // ARP protocol
ETH_HLEN = 14;
ETH_MTU = 1500;
FRAME_SIZE = 1518;
MAXFRAMEF_SZ = 1638;
FRAME_SIZE_FIELD = 1634;
ETH_HEADER_LEN = ETH_HLEN;
ETHTYPE_ARP = ETH_P_ARP;
ETHTYPE_IP = ETH_P_IP;
ETHTYPE_IP6 = ETH_P_IP6;
MAC_ADDR_TYPE = $0001;
ARP_REQUEST = $0001;
ARP_REPLY = $0002;
ARP_OPCODE_REQUEST = ARP_REQUEST;
ARP_OPCODE_REPLY = ARP_REPLY;
IP_HEADER_LEN = 20;
IP_PROTO_ICMP = 1;
IP_PROTO_TCP = 6;
IP_PROTO_UDP = 17;
UDP_HEADER_LEN = 8;
ICMP_HEADER_LEN = 8;
ICMP_TYPE_ECHOREPLY= 0;
ICMP_TYPE_ECHOREQUEST = 8;
{
TAP_IOCTL_GET_VERSION = TAP_CONTROL_CODE CTL_CODE (FILE_DEVICE_UNKNOWN, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
TAP_IOCTL_SET_MEDIA_STATUS = CTL_CODE (FILE_DEVICE_UNKNOWN, 6, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define CTL_CODE(DeviceType, Function, Method, Access) (
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
)
}
FILE_DEVICE_UNKNOWN = $00000022;
METHOD_BUFFERED = 0;
FILE_ANY_ACCESS = 0;
TAP_IOCTL_SET_MEDIA_STATUS = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or (6 shl 2) or (METHOD_BUFFERED);
TAP_IOCTL_GET_VERSION = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or (2 shl 2) or (METHOD_BUFFERED);
type
TMacAddr = array [0..5] of char;
const
MacBroadcast: TMacAddr = (#$FF, #$FF, #$FF, #$FF, #$FF, #$FF);
type
TIPAddr = DWORD;
//----------------
// Ethernet header
//----------------
TEthHeader = packed record
dest:TMacAddr; // destination eth addr
src:TMacAddr; // source ether addr
proto:word; // packet type ID field
end;
PEthHeader = ^TEthHeader;
//----------------
// ARP packet
//----------------
TArpHeader = packed record
// dest:TMacAddr; // Reverse these two +0
// src:TMacAddr; // to answer ARP requests +6
// proto:word; // 0x0806 +0C
hwtype:word; // 0x0001 +0E
protocol:word; // 0x0800 +10
hwlen:byte; // 0x06 +12
protolen:byte; // 0x04 +13
opcode:word; // 0x0001 for ARP request, 0x0002 for ARP reply +14
shwaddr:TMacAddr; // +16
sipaddr:TIPAddr; // +1C
dhwaddr:TMacAddr; // +20
dipaddr:TIPAddr; // +26
end;
PArpHeader = ^TArpHeader;
TEthArpHeader = packed record // sizeof(TEthArpHeader)=$2A=42
eth: TEthHeader;
arp: TArpHeader;
end;
PEthArpHeader= ^TEthArpHeader;
TNetIpHeader = packed record
vhl: BYTE;
tos: BYTE;
len: WORD;
ipid: WORD;
ipoffset: WORD;
ttl: BYTE;
proto: BYTE;
ipchksum: WORD;
srcipaddr: DWORD;
destipaddr: DWORD;
end;
PNetIpHeader = ^TNetIpHeader;
TNetIcmpHeader = packed record
itype: BYTE;
icode: BYTE;
icmpchksum:WORD;
id: WORD;
seqno: WORD;
end;
PNetIcmpHeader = ^TNetIcmpHeader;
TNetUdpHeader = packed record
srcport: WORD;
destport: WORD;
udplen: WORD;
udpchksum: WORD;
end;
PNetUdpHeader = ^TNetUdpHeader;
TNetTcpHeader = packed record
srcport: WORD;
destport: WORD;
seqno: DWORD;
ackno: DWORD;
tcpoffset: BYTE;
flags: BYTE;
wnd: WORD;
tcpchksum: WORD;
urgp: WORD;
// optdata[4]: BYTE ;
end;
PNetTcpHeader = ^TNetTcpHeader;
// Ethernet/IP header
TNetEthIpHeader = packed record
eth: TEthHeader;
ip: TNetIpHeader;
end;
PNetEthIpHeader = ^TNetEthIpHeader;
// The IP header
ip_hdr = TNetIpHeader;
// The IP/TCP headers
tcpip_hdr = packed record
ip: TNetIpHeader;
tcp: TNetTcpHeader;
end;
// The IP/ICMP headers
icmpip_hdr = packed record
ip: TNetIpHeader;
icmp: TNetIcmpHeader;
end;
PICMPip_hdr = ^icmpip_hdr;
// The UDP and IP headers
udpip_hdr = packed record
ip: TNetIpHeader;
udp: TNetUdpHeader;
end;
TFrame = array[0..MAXFRAMEF_SZ] of Byte;
PFrame = ^TFrame;
TEthThread = class(TThread)
private
FVersion: packed record
major: DWORD;
minor: DWORD;
debug: DWORD;
end;
FMACAddr: TMacAddr;
FTAPguid: string;
FTAPHandle: THANDLE;
FOverRead: TOverlapped;
FOverWrite: TOverlapped;
FReadEvent: THandle;
FWriteEvent: THandle;
FBufferSize: integer;
FBufferUsed: integer;
FFramesList: TThreadList;
FFreeList: TList;
FFreeMax: integer;
FFreeBuf: array of TFrame;
FInpCh: char;
FOutCh: char;
FDisCh: char;
FInpFrames: integer;
FOutFrames: integer;
FDisFrames: integer;
FPrevInpFrames: integer;
FPrevOutFrames: integer;
FPrevDisFrames: integer;
FPromiscuous: boolean;
FAcceptRunt: boolean;
FBroadcast: boolean;
FMulticast: boolean;
procedure SetMacAddr(Value: PChar);
procedure SetTAPguid(const Value: string);
procedure OpenTAP();
procedure CloseTAP();
function GetFreeBuf:PFrame;
procedure DisposeBuf(ptr:PFrame);
function GetActive: boolean;
procedure SetActive(const Value: boolean);
procedure SetBufSize(const Value: integer);
function GetMacAddr: PChar;
function MacEqual(mac1, mac2: PChar):boolean;
function IsBroadcast(mac: PChar):boolean;
function IsMulticast(mac: PChar):boolean;
procedure SetPromiscuous(const Value: boolean);
procedure SetAcceptRunt(const Value: boolean);
procedure SetBroadcast(const Value: boolean);
procedure SetMulticast(const Value: boolean);
protected
procedure Execute; override;
public
constructor Create();
destructor Destroy; override;
procedure Start;
procedure Stop;
procedure Reset;
function FrameLen(const Buffer: TFrame):integer;
function GetPacket(var Buffer:TFrame):WORD;
function PutPacket(const Buffer; WrSize:integer):boolean;
function GetStat: string;
function IsPhysical(mac: PChar):boolean;
property VersionMajor: DWORD read FVersion.major; // TAP driver version
property VersionMinor: DWORD read FVersion.minor;
property Active: boolean read GetActive write SetActive;
property BufSize: integer read FBufferSize write SetBufSize;
property MACAddr: PChar read GetMacAddr write SetMacAddr;
property TAPguid: string read FTAPguid write SetTAPguid;
property Promiscuous: boolean read FPromiscuous write SetPromiscuous;
property AcceptMulticast: boolean read FMulticast write SetMulticast;
property AcceptBroadcast: boolean read FBroadcast write SetBroadcast;
property AcceptRunt: boolean read FAcceptRunt write SetAcceptRunt;
property InpFramesList: TThreadList read FFramesList;
property InpFrames: integer read FInpFrames;
property OutFrames: integer read FOutFrames;
property DisFrames: integer read FDisFrames; // discarded due free buffer not ready
end;
function htons(val:WORD):WORD;
function htonl(val:DWORD): DWORD;
function netChecksum(data:PBYTE; len:WORD): WORD;
procedure GetConnectionInfo(Names, Guids: TStrings);
procedure CreateEthThread;
procedure DestroyEthThread;
var
EthThread: TEthThread;
implementation
function StrWinError(Err:DWORD): string;
var lpMsgBuf: PChar;
begin
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or
FORMAT_MESSAGE_FROM_SYSTEM,
nil, Err, 0,
@lpMsgBuf, 0, nil);
Result:=string(lpMsgBuf);
LocalFree( HLOCAL(lpMsgBuf) );
end;
function LastWinError: string;
begin
StrWinError(GetLastError);
end;
function htons(val:WORD):WORD;
begin
result:=(val shl 8) or (val shr 8);
end;
function htonl(val:DWORD): DWORD;
begin
result:=(htons(val shr 16) or DWORD(htons(val and $0000FFFF) shl 16));
end;
function netChecksum(data:PBYTE; len:WORD): WORD;
var sum: DWORD;
begin
sum := 0;
while (len>=2) do begin
sum:=sum+(PWORD(data))^;
inc(data); inc(data);
len:=len-2;
end;
if (len>0) then
sum:=sum+(PBYTE(data))^;
while ( (sum shr 16) <> 0 ) do
sum := WORD(sum) + WORD(sum shr 16);
result:= WORD(sum xor $FFFF);
end;
procedure GetConnectionInfo(Names, Guids: TStrings);
const
StartKey = '\SYSTEM\CurrentControlSet\Control\Network';
SubKeyNetName = 'Class';
SubKeyNetValue = 'Net';
NameValueName = 'Name';
ShowIconValueName = 'ShowIcon';
var
Reg: TRegistry;
KeyNames : TStringList;
i : Integer;
KeyFound : Boolean;
Key : string;
begin
Names.Clear;
Guids.Clear;
// Access the registry in read only mode
Reg := TRegistry.Create(HKEY_LOCAL_MACHINE);
KeyNames := TStringList.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKeyReadOnly(StartKey);
// Need to find the child that has a value named
// 'Class' which value is 'Net'
Reg.GetKeyNames(KeyNames);
i := 0;
KeyFound := False;
Key := StartKey;
while (i < KeyNames.Count) and not KeyFound do
begin
Reg.OpenKeyReadOnly(Key+'\'+KeyNames[i]);
if Reg.ReadString(SubKeyNetName) = SubKeyNetValue then
KeyFound := True;
Inc(i);
end;
// Found a key, open it and read its subkeys
// which in turn contain the names we are looking for
// in their Connection subkey
if KeyFound then
begin
Key := '\'+Reg.CurrentPath;
Reg.OpenKeyReadOnly(KeyNames[i-1]);
Reg.GetKeyNames(KeyNames);
for i := 0 to KeyNames.Count - 1 do
begin
Reg.OpenKeyReadOnly(Key+'\'+KeyNames[i]+'\Connection');
if Reg.ValueExists(NameValueName) then
begin
Names.Add(Reg.ReadString(NameValueName));
Guids.Add(KeyNames[i]);
end;
end;
end;
finally
Reg.Free;
KeyNames.Free;
end;
end;
procedure CreateEthThread;
begin
if not Assigned(EthThread) then
begin
EthThread:=TEthThread.Create; { create suspended }
EthThread.Priority := tpLower; { set the priority to normal }
end;
end;
procedure DestroyEthThread;
begin
if Assigned(EthThread) then with EthThread do begin
if Suspended then Resume;
Terminate;
WaitFor;
Active:=False; // CloseTAP
Free;
end;
EthThread:=nil;
end;
{ TEthThread }
procedure TEthThread.Reset;
begin
Stop;
FInpCh:='I';
FOutCh:='O';
FDisCh:='D';
FInpFrames:=0;
FOutFrames:=0;
FDisFrames:=0;
FPrevInpFrames:=0;
FPrevOutFrames:=0;
FPrevDisFrames:=0;
with FFramesList.LockList do
try
while (Count>0) do begin
DisposeBuf(Items[0]);
Delete(0);
end;
finally
FFramesList.UnlockList;
end;
end;
constructor TEthThread.Create();
var RandInt: Integer;
begin
FBroadcast:=True;
FMulticast:=False;
FAcceptRunt:=False; // False => ignore (skip) runt packets (packet length < 64 bytes)
FPromiscuous:=False;
Randomize();
FFramesList:=TThreadList.Create;
FFreeList:=TList.Create;
Reset;
FTAPguid:='';
FTAPHandle:=INVALID_HANDLE_VALUE;
FBufferUsed:=0;
RandInt:=Random(MaxInt);
FMACAddr[0]:=chr(0);
FMACAddr[1]:=chr(255);
FMACAddr[2]:=chr(lo(RandInt shr 24));
FMACAddr[3]:=chr(lo(RandInt shr 16));
FMACAddr[4]:=chr(lo(RandInt shr 8));
FMACAddr[5]:=chr(lo(RandInt));
FReadEvent:=CreateEvent(nil, False {True}, False, nil);
FOverRead.Offset:=0;
FOverRead.OffsetHigh:=0;
FOverRead.hEvent := FReadEvent;
if FOverRead.hEvent = 0 then
raise Exception.Create('Error creating TAP read event');
FWriteEvent:=CreateEvent(nil, False {True}, False, nil);
FOverWrite.Offset:=0;
FOverWrite.OffsetHigh:=0;
FOverWrite.hEvent := FWriteEvent;
if FOverRead.hEvent = 0 then
raise Exception.Create('Error creating TAP write event');
inherited Create(True); // Create Suspended
end;
destructor TEthThread.Destroy;
begin
CloseHandle(FWriteEvent);
CloseHandle(FReadEvent);
FFramesList.Free;
FFreeList.Free;
inherited;
end;
procedure TEthThread.Start;
begin
if not Active then
Active:=True; // OpenTap
Resume; { now run the thread }
end;
procedure TEthThread.Stop;
begin
Suspend; { pause the thread }
end;
function TEthThread.MacEqual(mac1, mac2: PChar):boolean;
begin
Result:=False;
if mac1[0]<>mac2[0] then exit;
if mac1[1]<>mac2[1] then exit;
if mac1[2]<>mac2[2] then exit;
if mac1[3]<>mac2[3] then exit;
if mac1[4]<>mac2[4] then exit;
Result:=mac1[5]=mac2[5];
end;
function TEthThread.FrameLen(const Buffer: TFrame):integer;
begin
Result:=PDWORD(@Buffer[FRAME_SIZE_FIELD])^;
end;
function TEthThread.GetPacket(var Buffer:TFrame):WORD;
var PFrameBuf: PFrame;
function min(a,b:integer):integer; begin if a<b then result:=a else result:=b; end;
begin
Result:=0;
PFrameBuf:=nil;
if (not Active) then exit;
with FFramesList.LockList do
try
if (Count>0) then begin
PFrameBuf:=Items[0];
Delete(0);
Result:=WORD(FrameLen(PFrameBuf^));
end;
finally
FFramesList.UnlockList;
end;
if Result<>0 then
CopyMemory(@Buffer[0], PFrameBuf, min(Result, sizeof(TFrame)));
if Assigned(PFrameBuf) then
DisposeBuf(PFrameBuf);
end;
function TEthThread.PutPacket(const Buffer; WrSize:integer):boolean;
var dwWrited: DWORD;
error: DWORD;
res: LongBool;
begin
Result:=False;
if not Active then exit;
res:=GetOverlappedResult(FTAPHandle, FOverWrite, dwWrited, FALSE);
if (not res) and (GetLastError=ERROR_IO_INCOMPLETE) then
WaitForSingleObject(FWriteEvent, INFINITE);
res:=WriteFile(FTAPHandle, Buffer, WrSize, dwWrited, @FOverWrite);
if (not res) then begin
error:=GetLastError;
if (error=ERROR_IO_PENDING) then
begin
WaitForSingleObject(FWriteEvent, INFINITE);
if not GetOverlappedResult(FTAPHandle, FOverWrite, dwWrited, FALSE) then
raise Exception.CreateFmt('Error reading TAP-adapter, phase 0: %s', [LastWinError()]);
end
else
raise Exception.CreateFmt('Error writing TAP-adapter: write failed: %s', [StrWinError(error)]);
end;
FOutFrames:=FOutFrames+1;
Result:=True;
end;
procedure TEthThread.Execute;
var
dwError: DWORD;
PFrameBuf: PFrame;
TmpFrameBuf: TFrame;
dwRead: DWORD;
begin
PFrameBuf:=nil;
FreeOnTerminate := False;
while not Terminated do
if FTAPHandle=INVALID_HANDLE_VALUE then sleep(1) else
begin
dwRead:=0;
if not Assigned(PFrameBuf) then begin
PFrameBuf:=GetFreeBuf();
if not Assigned(PFrameBuf) then PFrameBuf:=@TmpFrameBuf;
end;
if not ReadFile(FTAPHandle, PFrameBuf^, sizeof(TFrame), dwRead, @FOverRead) then
begin
dwError := GetLastError;
if dwError = ERROR_IO_PENDING then begin
WaitForSingleObject(FReadEvent, INFINITE);
if not GetOverlappedResult(FTAPHandle, FOverRead, dwRead, False) then
raise Exception.CreateFmt('Error reading TAP-adapter, phase 0: %s', [LastWinError()]);
end
else
raise Exception.CreateFmt('Error waiting TAP adapter event: %s', [StrWinError(dwError)]);
end;
if (dwRead>=42) or ((dwRead>0)and FAcceptRunt) then // PRE+42+CRC=64
begin
with FFramesList.LockList do
try
if (PFrameBuf=@TmpFrameBuf) then
FDisFrames:=FDisFrames+1
else
if MacEqual(pointer(PFrameBuf), @FMACAddr[0]) // we are destination
or (IsBroadcast(pointer(PFrameBuf)) and FBroadcast) // or broadcast accepted
or (IsMulticast(pointer(PFrameBuf)) and FMulticast) // or multicast accepted
or (IsPhysical(pointer(PFrameBuf)) and FPromiscuous) then // or Promiscuous node on
begin
PDWORD(@(PFrameBuf^[FRAME_SIZE_FIELD]))^:=dwRead;
Add(PFrameBuf);
FInpFrames:=FInpFrames+1;
end;
PFrameBuf:=nil;
finally
FFramesList.UnlockList;
end;
end
end;
end;
procedure TEthThread.OpenTAP;
var len, val: DWORD;
hndl: THANDLE;
begin
if (FTAPHandle<>INVALID_HANDLE_VALUE) then
CloseTap();
hndl:=CreateFile(PChar('\\.\Global\'+FTAPguid+'.tap'), GENERIC_READ or GENERIC_WRITE, {FILE_SHARE_READ or FILE_SHARE_WRITE}0, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM or FILE_FLAG_OVERLAPPED, 0);
if (hndl=INVALID_HANDLE_VALUE) then
raise Exception.CreateFmt('Error opening TAP adapter: %s', [LastWinError()]);
if not (DeviceIoControl(hndl, TAP_IOCTL_GET_VERSION,
@FVersion, sizeof (FVersion),
@FVersion, sizeof (FVersion), len, nil)) then
begin
CloseHandle(hndl);
raise Exception.CreateFmt('Error TAP adapter - DeviceIoControl (1): %s', [LastWinError()]);
end;
val:=1;
if (not DeviceIoControl(hndl, TAP_IOCTL_SET_MEDIA_STATUS,
@val, sizeof(val), @val, sizeof(val), len, nil)) then
begin
CloseHandle(hndl);
raise Exception.CreateFmt('Error TAP adapter - DeviceIoControl (2): %s', [LastWinError()]);
end;
FTAPHandle:=hndl;
end;
procedure TEthThread.CloseTAP;
var len, val: DWORD;
begin
if (FTAPHandle<>INVALID_HANDLE_VALUE) then
begin
val:=0;
if (not DeviceIoControl(FTAPHandle, TAP_IOCTL_SET_MEDIA_STATUS,
@val, sizeof(val), @val, sizeof(val), len, nil)) then
raise Exception.CreateFmt('Error TAP adapter - DeviceIoControl: %s', [LastWinError()]);
CloseHandle(FTAPHandle);
end;
FTAPHandle:=INVALID_HANDLE_VALUE;
end;
procedure TEthThread.SetMacAddr(Value: PChar);
begin
FMACAddr[0]:=Value[0];
FMACAddr[1]:=Value[1];
FMACAddr[2]:=Value[2];
FMACAddr[3]:=Value[3];
FMACAddr[4]:=Value[4];
FMACAddr[5]:=Value[5];
end;
procedure TEthThread.SetPromiscuous(const Value: boolean);
begin
FPromiscuous := Value;
end;
procedure TEthThread.SetTAPguid(const Value: string);
begin
if (trim(FTAPguid)=trim(Value)) then exit;
if (FTAPguid<>'') then
begin
CloseTAP;
FTAPguid:='';
end;
FTAPguid := trim(Value);
end;
function TEthThread.GetActive: boolean;
begin
Result:=FTAPHandle<>INVALID_HANDLE_VALUE;
end;
procedure TEthThread.SetActive(const Value: boolean);
begin
If (Value) then
begin
if (not Active) then OpenTAP;
end
else CloseTAP;
end;
procedure TEthThread.SetBufSize(const Value: integer);
var i: integer;
begin
FBufferSize:=Value;
FFreeMax:=FBufferSize div MAXFRAMEF_SZ;
SetLength(FFreeBuf, FFreeMax);
FFreeList.Clear;
for i:=0 to FFreeMax-1 do FFreeList.Add(@FFreeBuf[i][0]);
end;
procedure TEthThread.DisposeBuf(ptr:PFrame);
begin
FFreeList.Add(ptr);
end;
function TEthThread.GetFreeBuf: PFrame;
begin
if (FFreeList.Count=0) then
Result:=nil
else begin
Result:=FFreeList.Items[0];
FFreeList.Delete(0);
end;
end;
function TEthThread.GetMacAddr: PChar;
begin
Result:=@FMACAddr[0];
end;
function TEthThread.GetStat: string;
function hsize(Actual: integer; var Prev:integer; var ch:char):string;
begin
if Actual<>Prev then
begin
Prev:=Actual;
ch:=chr(ord(ch) xor $20);
end;
if Actual>=1024*1024 then
result:=ch+format(':%dÌ', [Actual div (1024*1024)])
else if Actual>=1024 then
result:=ch+format(':%dK', [Actual div 1024])
else
result:=ch+format(':%d', [Actual]);
end;
begin
Result:=format('%s, %s, %s', [hsize(FInpFrames, FPrevInpFrames, FInpCh),
hsize(FOutFrames, FPrevOutFrames, FOutCh),
hsize(FDisFrames, FPrevDisFrames, FDisCh)]);
end;
procedure TEthThread.SetAcceptRunt(const Value: boolean);
begin
FAcceptRunt := Value;
end;
procedure TEthThread.SetBroadcast(const Value: boolean);
begin
FBroadcast := Value;
end;
procedure TEthThread.SetMulticast(const Value: boolean);
begin
FMulticast := Value;
end;
function TEthThread.IsBroadcast(mac: PChar): boolean;
begin
Result:=MacEqual(mac, @MacBroadcast[0]);
end;
function TEthThread.IsMulticast(mac: PChar): boolean;
begin
Result:=(ord(mac^) and 1 <> 0) and (mac^ <> #$FF);
end;
function TEthThread.IsPhysical(mac: PChar): boolean;
begin
Result:=(not IsMulticast(mac)) and (not IsBroadcast(mac));
end;
initialization
EthThread:=nil;
end.