IpHdr.pas
上传用户:hbtcygglw
上传日期:2007-01-07
资源大小:281k
文件大小:2k
- unit IpHdr;
- interface
- uses Windows,WinSock;
- type
- TIPHdr=record
- ip_verlen:BYTE;
- ip_tos:Byte;
- ip_totallength:Word;
- ip_id:Word;
- ip_offset:Word;
- ip_ttl:Byte;
- ip_protocol:Byte;
- ip_checksum:Word;
- ip_srcaddr:DWORD;
- ip_destaddr:DWORD;
- end;
- PIPHdr=^TIPHdr;
- TUDPHdr=record
- udp_srcport:Word;
- udp_destport:Word;
- udp_length:Word;
- udp_checksum:Word;
- end;
- PUDPHdr=^TUDPHdr;
- function inet_checksum(var buf;buflen:Integer):Word;
- function LookupName(const name:string;var addr:TInAddr):Boolean;
- implementation
- function LookupName(const name: string;var addr:TInAddr):Boolean;
- var
- HostEnt: PHostEnt;
- begin
- Result:=False;
- addr.S_addr:=inet_addr(PChar(name));
- if(addr.S_addr=-1)then
- begin
- HostEnt := gethostbyname(PChar(Name));
- FillChar(Addr, SizeOf(Addr), 0);
- if HostEnt <> nil then
- begin
- with addr, HostEnt^ do
- begin
- S_un_b.s_b1 := h_addr^[0];
- S_un_b.s_b2 := h_addr^[1];
- S_un_b.s_b3 := h_addr^[2];
- S_un_b.s_b4 := h_addr^[3];
- end;
- Result:=True;
- end;
- end
- else Result:=True;
- end;
- function inet_checksum(var buf;buflen:Integer):Word;
- var
- pBuf:PWord;
- cksum:DWORD;
- begin
- cksum:=0;
- pBuf:=PWord(buf);
- while(buflen>1)do
- begin
- cksum:=cksum+pBuf^;
- Inc(pBuf);
- Dec(buflen,SizeOf(Word));
- end;
- if(buflen<>0)then
- begin
- cksum:=cksum+pBuf^;
- end;
- cksum:=(cksum shr 16)+(cksum and $ffff);
- cksum:=cksum shr 16;
- result:=not cksum;
- end;
- //---------------------------------------------
- //USHORT checksum(USHORT *buffer, int size)
- //{
- // unsigned long cksum=0;
- //
- // while (size > 1)
- // {
- // cksum += *buffer++;
- // size -= sizeof(USHORT);
- // }
- // if (size)
- // {
- // cksum += *(UCHAR*)buffer;
- // }
- // cksum = (cksum >> 16) + (cksum & 0xffff);
- // cksum += (cksum >>16);
- //
- // return (USHORT)(~cksum);
- //}
- //--------------------------------------------------
- end.