红联Linux门户
Linux帮助

求助:构造IP头问题

发布时间:2007-05-11 17:45:49来源:红联作者:Roading
试着构造一个最简单的IP包,发送出去.却老是被报以Segmentation fault 错误的位置我通
过printf跟踪,在程序里已指出,还望各位高手指点:


#include
#include
#include
#include
#include

#include
#include

int main(void)
{
int s, i, on = 1;
char buf[200] = {0} ; // for store the data
struct ip *uheader_ip;
struct sockaddr_in dip;
struct icmphdr *uheader_icmp = (struct icmphdr *)(uheader_ip + 1);
char * sipaddr = "\xc0\xa8\x23\xa9"; // 192.168.35.169
char *dipaddr = "\xc0\xa8\x24\x59"; //192.168.36.89

// create a raw_socket
if((s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0 ) {
perror("socket");
exit(1);
}

dip.sin_addr.s_addr = inet_addr("192.168.36.89");
dip.sin_family = AF_INET;
// set the IP_HDRINCL option ,so the user have
//the right to create his own IP header
if( (i = setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))) < 0 )
{
perror ("setsocket");
exit(1);
}


//notice : the big/little order

//fill the ip header
uheader_ip =(struct ip *) buf;
uheader_ip->ip_hl = 20;
uheader_ip->ip_v = 4;
uheader_ip->ip_tos = 0;
uheader_ip->ip_len = 200 ;
uheader_ip->ip_off = 0;
uheader_ip->ip_ttl = 255;
uheader_ip->ip_p = 1;
//struct in_addr

//错误位置就在这两行!!
uheader_ip->ip_src.s_addr = inet_addr("192.168.35.169");
uheader_ip->ip_dst.s_addr = inet_addr("192.168.36.89");


//fill the icmp header
uheader_icmp->type = ICMP_ECHO;
uheader_icmp->code = 0;
uheader_icmp->checksum = htons (~(ICMP_ECHO) << 8);

if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&dip, 4) ) {
perror("sendto");
}
return 1;
文章评论

共有 0 条评论