Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4842750
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-05-09 16:50:19

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>

#define DESTPORT 80
#define LOCALPORT 1716

#define SIP "192.168.198.129"

#if 0
unsigned short check_sum(unsigned short *addr,int len)
{
 register int nleft=len;
 register int sum=0;
 register short *w=addr;
 short answer=0;

 while(nleft>1)
 {
  sum+=*w++;
  nleft-=2;
 }
  
 if(nleft==1)
 {
  *(unsigned char *)(&answer)=*(unsigned char *)w;
  sum+=answer;
 }

 sum=(sum>>16)+(sum&0xffff);
 sum+=(sum>>16);
 answer=~sum;
 return(answer);
}
#endif

/******* send tcp *********/
void send_tcp(int sockfd,struct sockaddr_in *addr)
{
 char options[] = {0x02,0x04,0x05,0xb4,0x01,0x01,0x04,0x02};
     
 char buffer[100]; /**** save the data to send ****/
 struct ip *ip;
 struct tcphdr *tcp;
 int head_len;

 head_len=sizeof(struct ip)+sizeof(struct tcphdr)+8;

 bzero(buffer,100);

 /******** iphdr ******/
 ip=(struct ip *)buffer;
 ip->ip_v=IPVERSION;
 ip->ip_hl=sizeof(struct ip)>>2;
 ip->ip_tos=0;
 ip->ip_len=htons(head_len); /** IP data len**/
 ip->ip_id=0;
 ip->ip_off=0;
 ip->ip_ttl=MAXTTL;
 ip->ip_p=IPPROTO_TCP;
 ip->ip_sum=0;
 ip->ip_dst=addr->sin_addr;

 /******* tcp packet *****/
 tcp=(struct tcphdr *)(buffer +sizeof(struct ip));
 tcp->source=htons(LOCALPORT);
 tcp->dest=addr->sin_port; /** desctination port **/
 tcp->seq=0;
 tcp->ack_seq=0;
 tcp->doff=7;
 tcp->syn=1;
 tcp->window=htons(16384);
 tcp->check=0;

 /*options*/
 memcpy(buffer+sizeof(struct ip)+sizeof(struct tcphdr),options,8);
 
  ip->ip_src.s_addr=inet_addr(SIP);

  //tcp->check=check_sum((unsigned short *)tcp,sizeof(struct tcphdr));

  tcp->check=0;
  sendto(sockfd,buffer,head_len,0,(const struct sockaddr*)addr,sizeof(struct sockaddr_in));
}

int main(int argc,char **argv)
{
 int sockfd;
 struct sockaddr_in addr;
 struct hostent *host;
 int on=1;

 if(argc!=2)
 {
  fprintf(stderr,"Usage:%s hostname\n\a",argv[0]);
  exit(1);
 }

 bzero(&addr,sizeof(struct sockaddr_in));
 addr.sin_family=AF_INET;
 addr.sin_port=htons(DESTPORT);


 if(inet_aton(argv[1],&addr.sin_addr)==0)
 {
  host=gethostbyname(argv[1]);
  if(host==NULL)
  {
   fprintf(stderr,"HostName Error:%s\n\a",hstrerror(h_errno));
    exit(1);
  }
  addr.sin_addr=*(struct in_addr *)(host->h_addr_list[0]);
 }

 sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
 if(sockfd<0)
 {
  fprintf(stderr,"Socket Error:%s\n\a",strerror(errno));
  exit(1);
 }

 setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));
 setuid(getpid());

 send_tcp(sockfd,&addr);
 
 return 0;
}



 

使用doff就可以搞定options了!

阅读(1617) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-05-31 22:23:43

MBT is based on Africa's indigenous people to create innovative walking barefoot, and wear this shoe can stimulate the body to relax the muscles so that the muscles of heat to be released, consumed calories, thus to achieve weight loss results. MBT's pursuit of beauty in order to satisfy customers, design a variety of styles, such as Mbt Chapa shoe, MBT Lami Shoe