struct icmp *icmp;
7 struct iphdr *ip;
8 struct sockaddr_in *me, *he;
9 he = (void *)pr->sasend;
10
11 ip = (struct iphdr *)sendbuf;
12 memset(ip, 0, 20);
13 static int ttl = 10;
14 ip->ttl = ttl;
15 ip->saddr = saddr;
16 ip->daddr = he->sin_addr.s_addr;//inet_addr("192.168.1.12");
17 ip->protocol = IPPROTO_ICMP ;
18 ip->tot_len = htons(sizeof(*ip) + datalen + 8 ); //ip头部+icmp payload+icmp header
19 ip->ihl = sizeof(*ip)/4 ;
20 ip->id = 0;
21 ip->frag_off = htons(IP_DF);
22 ip->version = 4 ;
23 ip->check = 0;
24 ip->check = in_cksum((uint16_t *)ip, 20);
25
26 printf("%d\n", sizeof(struct icmp));
27
28 icmp = (struct icmp *)(ip + 1);
29 icmp->icmp_type = ICMP_ECHO;
30 icmp->icmp_code = 0;
31 icmp->icmp_id = pid;
32 icmp->icmp_seq = nsent++;
33 memset(icmp->icmp_data, 0xa5, datalen);
34 gettimeofday((struct timeval *)icmp->icmp_data, NULL);
35
36 len = 8 + datalen; //payload + header
37 icmp->icmp_cksum = 0;
38 icmp->icmp_cksum = in_cksum((uint16_t *)icmp, len);
39
40 struct sockaddr_ll sll;
41 static uint8_t dmac[]={0xff,0xff,0xff,0xff,0xff,0xff}; //链路上广播
42 init_addr_ll(&sll, dmac);
43 int i = sendto(sockfd, sendbuf, len + 20, 0, (void *)&sll, sizeof(sll) );
44 if(i < 0)perror("sendto");
|