大概是协议解析出错了吧,大家帮我看看哪里错了。 ( 积分: 0 )

  • 主题发起人 主题发起人 linuxping
  • 开始时间 开始时间
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
THdrEthernet = packed record // Ethernet frame header - Network Interface Layer
dmac: TMacAddr;
smac: TMacAddr;
protocol: WORD;
end;
PHdrEthernet = ^THdrEthernet ;

THdrIP = packed record // IP header (RFC 791) - Internet Layer
ihl_ver : BYTE; // Combined field:
// ihl:4 - IP header length divided by 4
// version:4 - IP version
tos : BYTE; // IP type-of-service field
tot_len : WORD; // total length
id : WORD; // unique ID
frag_off: WORD; // Fragment Offset + fragmentation flags (3 bits)
ttl : BYTE; // time to live
protocol: BYTE; // protocol type
check : WORD; // IP header checksum
saddr : TInAddr; // source IP
daddr : TInAddr; // destination IP
{The options start here...}
end;
PHdrIP = ^THdrIP;


THdrTCP = packed record // TCP header (RFC 793) - Transport Layer
source : WORD; // source port
dest : WORD; // destination port
seq : DWORD; // sequence number
ack_seq: DWORD; // next sequence number
flags : WORD; // Combined field:
// res1:4 - reserved, must be 0
// doff:4 - TCP header length divided by 4
// fin:1 - FIN
// syn:1 - SYN
// rst:1 - Reset
// psh:1 - Push
// ack:1 - ACK
// urg:1 - Urgent
// res2:2 - reserved, must be 0
window : WORD; // window size
check : WORD; // checksum, computed later
urg_ptr: WORD; // used for async messaging?
end;
PHdrTCP = ^THdrTCP;

THdrUDP = packed record // UDP header (RFC 768) - Transport Layer
src_port: WORD; // source port
dst_port: WORD; // destination port
length : WORD; // length, including this header
checksum: WORD; // UDP checksum
end;
PHdrUDP = ^THdrUDP;


type TBaseProtoHdrs=packed record
Ether_Hdr:THdrEthernet;
IP_Hdr :THdrIP;
Data:Pointer;
end;
PBaseProtoHdrs= TBaseProtoHdrs;

type TUDPPack=packed record
Base_Hdr:TBaseProtoHdrs;
UDP_Hdr :THdrUDP;
Data:Pointer;
end;

type TTCPPack=packed record
Base_Hdr:TBaseProtoHdrs;
TCP_Hdr :THdrTCP;
Data:Pointer;
end;

//ICMP
{
类型代码 类型描述
0 响应应答(ECHO-REPLY)
3 不可到达
4 源抑制
5 重定向
8 响应请求(ECHO-REQUEST)
11 超时
12 参数失灵
13 时间戳请求
14 时间戳应答
15 信息请求(*已作废)
16 信息应答(*已作废)
17 地址掩码请求
18 地址掩码应答
其中代码为15、16的信息报文已经作废。
}

type TsICMP= packed record
case Integer of
0: (uc1,uc2,uc3,uc4 : CHAR );
1: (us1,us2 : Word );
2: (sUL : LongWord );
end;

type ICMP_hdr=packed record
ICMPType,
ICMPCode : Byte;
ICMPChecksum : Word;
sICMP : TsICMP;
ICMP_Originate_Timestamp,
ICMP_Receive_Timestamp,
ICMP_Transmit_Timestamp : LongWord;
end;

//IGMP
type IGMP_Hdr = record
IGMP_Type : Byte; //协议的信息类型
IGMP_Code : Byte; //routing code
IGMP_CheckSum : Word; //校验和
Group : ULONG;
end;

var
{L,P : integer;
IP : PIP_RHDR;
TCP : PTCP_RHDR;
IPlen : integer; }
Base:TBaseProtoHdrs;
PData1,PData2:Pointer;
DataLen:Word;
List:TStringList;
str:string;
ListItem:TListItem;

TCPPack:TTCPPack;
UDPPack:TUDPPack;


function ChangeToHexStr(s:string):string;
type TC=packed record
case SmallInt of
0: (c1:Char;c2:Char);
1: (SI:SmallInt);
end;

var
I:Integer;
B:Byte;
sInt:TC;
len:Integer;
begin
Result:='';
len :=Length(S);
if len mod 2 =1 then len:=len-1;

for I:=1 to len do
begin
sInt.c1:=str[1];
sInt.c2:=str[2];
Result:=Result+' '+IntToHex(sInt.SI,2);
end;

if Length(S) mod 2 =1 then
begin
sInt.c1:=#0;
sInt.c2:=str[Length(S)];
Result:=Result+' '+IntToHex(sInt.SI,2);
end;

end;

begin
//try
//GetMem(@Base,SizeOf(TBaseProtoHdrs));
CopyMemory(@Base,
Data,
SizeOf(TBaseProtoHdrs)
);

List:=TStringList.Create;
SetLength(str,SizeOf(THdrEthernet));
System.Move(Base,str[1],SizeOf(THdrEthernet));
List.Append('以太网头部: '+ChangeToHexStr(str));

str:='';
SetLength(str,SizeOf(THdrIP));
System.Move(Base.IP_Hdr,str[1],SizeOf(THdrIP));
List.Append('IP头部: '+ChangeToHexStr(str));

PData1:=Base.Data; //指向了高层协议的头部

ListItem:=Lv1.items.add;
with ListItem do
begin
Caption:=ProtocolStr(Base.IP_Hdr.protocol);
SubItems.Add(IPtoStr(Base.IP_Hdr.saddr));
Subitems.add('');
SubItems.Add(IPtoStr(Base.IP_Hdr.daddr));
Subitems.add('');
end;

case Base.IP_Hdr.protocol of
1: //ICMP
begin
str:='';
SetLength(str,SizeOf(ICMP_Hdr));
System.Move(PData1^,str[1],SizeOf(ICMP_Hdr));
List.Append('ICMP头部: '+ChangeToHexStr(str));

PData2:=ptr(Integer(PData1)+SizeOf(ICMP_Hdr));
DataLen:=recvbytes-sizeof(THdrEthernet)-sizeof(THdrIP)-sizeof(ICMP_Hdr);
SetLength(str,DataLen);
System.Move(PData2^,str[1],DataLen);
List.Append('ICMP正文内容: '+ChangeToHexStr(str));

ListItem.Data:=Pointer(List);
end;
2 : //IGMP
begin
str:='';
SetLength(str,SizeOf(IGMP_Hdr));
System.Move(PData1^,str[1],SizeOf(IGMP_Hdr));
List.Append('IGMP头部: '+ChangeToHexStr(str));

PData2:=ptr(Integer(PData1)+SizeOf(IGMP_Hdr));
DataLen:=recvbytes-sizeof(THdrEthernet)-sizeof(THdrIP)-sizeof(IGMP_Hdr);
SetLength(str,DataLen);
System.Move(PData2^,str[1],DataLen);
List.Append('ICMP正文内容: '+ChangeToHexStr(str));

ListItem.Data:=Pointer(List);
end;
6: //TCP
begin
//GetMem(@TCPPack,SizeOf(TBaseProtoHdrs));
CopyMemory(@TCPPack,
Data,
SizeOf(TBaseProtoHdrs)
);

str:='';
SetLength(str,SizeOf(THdrTCP));
System.Move(TCPPack.TCP_Hdr,str[1],SizeOf(THdrTCP));
List.Append('TCP头部: '+ChangeToHexStr(str));

//PData2:=ptr(Integer(PData1)+SizeOf(THdrTCP));
DataLen:=recvbytes-sizeof(THdrEthernet)-sizeof(THdrIP)-sizeof(THdrTCP);
SetLength(str,DataLen);
if TCPPack.Data<>nil then
begin
System.Move(TCPPack.Data^,str[1],DataLen);
List.Append('TCP正文内容: '+ChangeToHexStr(str));
end else
List.Append('TCP正文内容: 空 ');

ListItem.Data:=Pointer(List);

ListItem.SubItems.Strings[1]:=inttostr( ntohs(TCPPack.TCP_Hdr.source));
ListItem.SubItems.Strings[3]:=inttostr( ntohs(TCPPack.TCP_Hdr.dest));
end;
14: ; //telnet
17: //UDP
begin
//GetMem(@UDPPack,SizeOf(TBaseProtoHdrs);
CopyMemory(@UDPPack,
Data,
SizeOf(TBaseProtoHdrs)
);

str:='';
SetLength(str,SizeOf(THdrUDP));
System.Move(UDPPack.UDP_Hdr,str[1],SizeOf(THdrUDP));
List.Append('UDP头部: '+ChangeToHexStr(str));

//PData2:=ptr(Integer(PData1)+SizeOf(THdrUDP));
DataLen:=recvbytes-sizeof(TUDPPack)+4;
SetLength(str,DataLen);
if UDPPack.Data<>nil then
begin
System.Move(UDPPack.Data^,str[1],DataLen);
List.Append('UDP正文内容: '+ChangeToHexStr(str));
end else
List.Append('UDP正文内容: 空');

ListItem.Data:=Pointer(List);

ListItem.SubItems.Strings[1]:=inttostr( ntohs(UDPPack.UDP_Hdr.src_port));
ListItem.SubItems.Strings[3]:=inttostr( ntohs(UDPPack.UDP_Hdr.dst_port));
end;
else
begin
//
end;
//finally
//if @Base<>nil then FreeMem(@base);
//if @TCPPack<

以上是主要代码,其中,Data为接收到的数据,包含以太网头部。recvbytes为接收到的数据的字节数。。。
 
后退
顶部