|
楼主 |
发表于 2004-3-22 22:56:01
|
显示全部楼层
[code:1]#include<unistd.h>
#include<stdlib.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define __FAVOR_BSD
#include<netinet/tcp.h>
#include<netinet/ip.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
unsigned short ip_cksum(unsigned short *pcheck,int len)
{
int nleft=len;
int sum=0;
unsigned short *p=pcheck;
unsigned short result=0;
while(nleft>1){
sum+=*p++;
nleft-=2;}
if(nleft==1){
*(unsigned char*)(&result)=*(unsigned char*)p;
sum+=result;}
sum=(sum>>16)+(sum&0xFFFF);
sum+=(sum>>16);
result=~sum;
return result;}
int main(int argc,char* argv[])
{
struct hostent *host;
int fd;
int foo=1;
int s;
char buffer[40];
struct ip *ip=(struct ip*)buffer;
struct tcphdr *tcp=(struct tcphdr*)(buffer+sizeof(struct ip));
struct sockaddr_in sin,din;
if((host=gethostbyname(argv[1]))==0){
perror("gethostbyname");
exit(0);}
seteuid(geteuid());
if((fd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))<0)
{perror("socket failed!");
exit(0);}
sin.sin_addr=*((struct in_addr*)host->h_addr);
sin.sin_port=htons(9999);
inet_aton(argv[2],&din.sin_addr);
if((s=setsockopt(fd,IPPROTO_IP,IP_HDRINCL,(void*)&foo,sizeof(int)))!=0){
perror("setsockopt failed!");
exit(0);}
bzero(buffer,40*sizeof(char));
ip->ip_v=4;
ip->ip_hl=sizeof(struct ip)/4;
ip->ip_tos=0;
ip->ip_off=0;
ip->ip_id=0;
ip->ip_ttl=50;
ip->ip_p=IPPROTO_TCP;
ip->ip_src=din.sin_addr;
ip->ip_dst=sin.sin_addr;
ip->ip_len=sizeof(struct ip)+sizeof(struct tcphdr);
ip->ip_sum=0;
tcp->th_sport=htons(9999);
tcp->th_dport=htons(9999);
tcp->th_seq=htonl(0x12345678);
tcp->th_flags=TH_SYN;
tcp->th_off=sizeof(struct tcphdr)/4;
tcp->th_win=htons(2048);
tcp->th_sum=0;
tcp->th_sum=ip_cksum((u_short*)tcp,sizeof(struct tcphdr));
ip->ip_sum=ip_cksum((u_short*)ip,sizeof(struct ip));
sendto(fd,buffer,(sizeof(struct ip)+sizeof(struct tcphdr)),0,(struct sockaddr*)&sin,sizeof(struct sockaddr_in));
close(fd);
return(0);}[/code:1]
编译后即可在root下运行,功能:发送一个syn到tcp 9999.我用server 监听于9999端口 ,并且select ,但没有检测到.请帮我测试一下,能否实现?或者有什么错误. |
|