hi,everybody!
I want to use raw socket to send my ipv6 packets.I want to fill the
ipv6 header myself. But I cannot succeed. My programme goes as follows:
#include<signal.h>
#include<sys/socket.h>
#include<netinet/ip6.h>
#include<netinet/icmp6.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
void sigalrm(int );
void SendEchoRequest(void);
#define MAX_BUFF_LEN 1000
void *buff;
struct sockaddr *sendaddr;
int sendlen,sockfd;
int seq=0;
struct addrinfo hints,*res;
main()
{int on=1;
buff=malloc(MAX_BUFF_LEN);
hints.ai_flags=AI_CANONNAME;
hints.ai_family=0;
hints.ai_socktype=0;
getaddrinfo("3ffe:3216:2101:601:101::6",NULL,&hints,&res);
sendaddr=res->ai_addr;
sendlen=res->ai_addrlen;
signal(SIGALRM,sigalrm);
if((sockfd=socket(sendaddr->sa_family,SOCK_RAW,1))<0)
perror("socket");
if(setsockopt(sockfd,IPPROTO_IPV6,IP_HDRINCL,&on,sizeof(on))<0)
perror("setsockopt");
sigalrm(SIGALRM);
while(1);
}
void sigalrm(int signo)
{SendEchoRequest();
seq++;
alarm(3);
return;
void SendEchoRequest(void)
{struct ip6_hdr *ip6;
struct icmp6_hdr *icmp6;
int len=0;
ip6=(struct ip6_hdr*)buff;
ip6->ip6_vfc=6<<4;
ip6->ip6_vfc|=1;
ip6->ip6_flow=ip6->ip6_vfc;
ip6->ip6_flow<<=24;
ip6->ip6_flow|=1;
ip6->ip6_nxt=58;
ip6->ip6_hlim=2;
len+=sizeof(struct ip6_hdr);
icmp6=(struct icmp6_hdr *)(buff+sizeof(struct ip6_hdr));
icmp6->icmp6_type=ICMP6_ECHO_REQUEST;
icmp6->icmp6_code=0;
icmp6->icmp6_id=0x8888;
icmp6->icmp6_seq=seq%1000;
len+=sizeof(struct icmp6_hdr);
icmp6->icmp6_seq=seq%1000;
len+=sizeof(struct icmp6_hdr);
len+=8;
if(sendto(sockfd,buff,len,0,sendaddr,sendlen)!=len)
perror("sendto");
}
This is just a programme to test the setsockopt() using IP_HDRINCL.
The packets I receive is not what I want to send. Why?
Thanks for any help.
_____________________________________________
Ê×¶¼ÔÚÏß--ÏȽøÖйúÈ˵ÄÍøÉϼÒÔ° http://www.263.net
Ãâ·ÑÓÊÏä ÓʼþÔÓÖ¾ Ç©ÃûÓʼþ Óʼþ¼ÓÃÜ Óʼþ×·Éíºô
ËÑË÷ÒýÇæ ¸öÈËÕ¾µã ÔÚÏßÓÎÏ· ÍøÉÏÁÄÌì ÍøÉϹҺÅ
½ðÈÚÍõ¹ú ÔÚÏßɱ¶¾ ÌøÔéÊг¡ Èí¼þÏÂÔØ ÐÝÏÐÓéÀÖ
|