关于delphi的hdrincl.c程序(100分)

W

wharre

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我转为delphi程序的时候总是不能成功运行?
代码如下,望大虾指点。
unit rawsock;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,sockets,winsock;
const
Icmp_echo =8;
icmp_echoreply =0;
icmp_min =8;
def_packet_size =32;
man_ip_hdr_size =60;
MAX_MESSAGE =4068;
MAX_PACKET =4096;
DEFAULT_PORT =5150;
DEFAULT_IP ='10.0.0.1';

DEFAULT_COUNT =5;
DEFAULT_MESSAGE ='';

type TicmpHeader = record
i_type: byte; // Version and length
i_code: byte; // Type of service
i_sum: word; // Total datagram length
i_id: word; // Identification
i_seq: word; // Flags, fragment offset
i_timestamp: word; // Header checksum
end;

type Ttcpheader=record //定义TCP首部
TCP_Sport :word; //16位源端口
TCP_Dport :word; //16位目的端口
th_seq :longword; //32位序列号
th_ack :longword; //32位确认号
th_lenres :byte; //4位首部长度/6位保留字
th_flag :char; //6位标志位
th_win :word; //16位窗口大小
th_sum :word; //16位校验和
th_urp :word; //16位紧急数据偏移量
end;

type Tudpheader=record //定义UDP首部
src_portno :word; //16位源端口
dst_portno :word; //16位目的端口
udp_length :word; //16位长度
udp_checksum :word; //16位校验和
end;

type Tipoptionheader = record
code :byte;
len :byte;
ptr :byte;
addr :longword;
end;

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
buf:array[0..4095] of char;
strmessage:array[0..4067] of char;
{ Public declarations }
end;

var
Form1: TForm1;
IPHeader:TIPHeader;
udpheader:Tudpheader;
tcpheader:Ttcpheader;
icmpheader:Ticmpheader;
s:Tsocket;
// WSADATA wsd;
// SOCKET s;
bOpt:boolean;
remote:sockaddr_in; // IP addressing structures
ret:integer;
// DWORD i;
iTotalSize:word; // Lots of sizes needed to fill
iUdpSize:word; // the various headers with
iUdpChecksumSize:word;
iIPVersion:word;
iIPSize:word;
cksum:word;
// buf[MAX_PACKET]:byte;
inaddr:IN_ADDR;
dwToIP:longword; // IP to send to
dwFromIP:longword; // IP to send from (spoof)
iToPort:word; // Port to send to
iFromPort:word; // Port to send from (spoof)
dwCount:longword; // Number of times to send

implementation

{$R *.dfm}


function checksum( buffer:pword ; size:integer):word;
var
csum : longword;
begin
csum :=0;

while (size > 1) do
begin
inc(csum, buffer^);
dec(size, sizeof(word));
inc(buffer);
end;
if (size=0) then
begin
inc(csum ,buffer^);
end;
csum := (csum shr 16) + (csum and $ffff);
inc(csum , csum shr 16);

result:=csum;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
ptrbuf:pchar;
i:integer;
bufw:pword;
begin
iToPort := DEFAULT_PORT;
iFromPort := DEFAULT_PORT;
dwToIP := Inet_Addr (default_ip);
dwFromIP := Inet_Addr (DEFAULT_IP);

dwCount := DEFAULT_COUNT;

strcopy(strMessage, DEFAULT_MESSAGE);

s := Socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
if (s = INVALID_SOCKET) then
begin
showmessage('WSASocket() failed:'+ inttostr(WSAGetLastError()));
end;
bOpt := TRUE;
ret := setsockopt(s, IPPROTO_IP, 2,@bOpt, sizeof(bOpt));
if (ret = SOCKET_ERROR) then
begin
showmessage('setsockopt(IP_HDRINCL) failed: '+ inttostr(WSAGetLastError()));
end;
iTotalSize := sizeof(ipHeader) + sizeof(udpHeader) + strlen(strMessage);

iIPVersion := 4;
iIPSize := sizeof(ipHeader) div sizeof(longword);
ipHeader.iph_verlen := 4 ;
ipHeader.iph_tos := 0; // IP type of service
ipHeader.iph_length := htons(iTotalSize); // Total packet len
ipHeader.iph_id := 0; // Unique identifier: set to 0
ipHeader.iph_offset := 0; // Fragment offset field
ipHeader.iph_ttl := 128; // Time to live
ipHeader.iph_protocol := $11; // Protocol(UDP)
ipHeader.iph_xsum := 0 ; // IP checksum
ipHeader.iph_src := dwFromIP; // Source address
ipHeader.iph_dest := dwToIP; // Destination address
iUdpSize := sizeof(udpHeader) + strlen(strMessage);
udpHeader.src_portno := htons(iFromPort) ;
udpHeader.dst_portno := htons(iToPort) ;
udpHeader.udp_length := htons(iUdpSize) ;
udpHeader.udp_checksum := 0 ;
iUdpChecksumSize := 0;
ptrbuf := @buf;
ZeroMemory(@buf, MAX_PACKET);

CopyMemory(ptrbuf, @ipHeader.iph_src, sizeof(ipHeader.iph_src));
inc(ptrbuf ,sizeof(ipHeader.iph_src));
inc(iUdpChecksumSize ,sizeof(ipHeader.iph_src));

CopyMemory(ptrbuf, @ipHeader.iph_dest, sizeof(ipHeader.iph_dest));
inc(ptrbuf, sizeof(ipHeader.iph_dest));
inc(iUdpChecksumSize,sizeof(ipHeader.iph_dest));

inc(ptrbuf);
inc(iUdpChecksumSize);

CopyMemory(ptrbuf, @ipHeader.iph_protocol, sizeof(ipHeader.iph_protocol));
inc(ptrbuf,sizeof(ipHeader.iph_protocol));
inc(iUdpChecksumSize,sizeof(ipHeader.iph_protocol));

CopyMemory(ptrbuf, @udpHeader.udp_length, sizeof(udpHeader.udp_length));
inc(ptrbuf,sizeof(udpHeader.udp_length));
inc(iUdpChecksumSize,sizeof(udpHeader.udp_length));

CopyMemory(ptrbuf, @udpHeader, sizeof(udpHeader));
inc(ptrbuf,sizeof(udpHeader));
inc(iUdpChecksumSize,sizeof(udpHeader));
for i:=0 to strlen(strMessage) do
begin
ptrbuf^ := strMessage;
inc(ptrbuf);
end;
inc(iUdpChecksumSize,strlen(strMessage));
bufw:=@buf;
cksum := checksum(bufw, iUdpChecksumSize);
udpHeader.udp_checksum := cksum;
//
// Now assemble the IP and UDP headers along with the data
// so we can send it
//
ZeroMemory(@buf, MAX_PACKET);
ptrbuf := @buf;

CopyMemory(ptrbuf, @ipHeader, sizeof(ipHeader)); inc(ptrbuf,sizeof(ipHeader));
CopyMemory(ptrbuf, @udpHeader, sizeof(udpHeader)); inc(ptrbuf,sizeof(udpHeader));
CopyMemory(ptrbuf, @strMessage, strlen(strMessage));
remote.sin_family := AF_INET;
remote.sin_port := htons(iToPort);
remote.sin_addr.s_addr := dwToIP;
for i := 0 to dwCount do
begin
ret := sendto(s, buf, iTotalSize, 0,remote,sizeof(remote));
if (ret = SOCKET_ERROR) then
begin
showmessage('sendto() failed: '+ inttostr(WSAGetLastError()));
break;
end
else
showmessage('sent'+ inttostr(ret));
end;
closesocket(s) ;
end;

end.
 
不好意思,写错了,应该是iphdrinc.c
 
顶部