我在C中写了一个小ping程序。我已经成功构建了ip header和icmp header。我发送的数据长度为64个字节。但是,我的ICMP标头的校验和值有问题。 IP正在发送正确的校验和,但ICMP中的校验和不断变化,在wireshark中,我收到一条消息,告诉我它需要相同的值。
这是我的代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
#include <math.h>
#include <sys/termios.h>
#define PCKT_LEN 8192
struct ipheader {
unsigned char iph_ihl:4, iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
//unsigned char iph_flag;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
struct icmpheader{
char type;
char code;
short int checksum;
char content[4];
char data[64];
};
unsigned short checksum(unsigned short *buf, int nwords)
{ //
unsigned long sum;
for(sum=0; nwords>0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum &0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
int main(int argc, char *argv[]){
int sd;
char buffer[92]; // ICMP +IP_HEADER + DATA(64 BYTES)
int i=0;
//Definiendo estructura de header IP
struct ipheader *ip = (struct ipheader *) buffer;
struct icmpheader *icmp = (struct icmpheader *) (buffer + sizeof(struct ipheader));
//Direcciones host y dest
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;
memset(buffer, 0, 92);
if(argc != 3)
{
printf("- Parametros invalidos\n");
printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(-1);
}
sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(sd < 0){
perror("socket() error");
// If something wrong just exit
exit(-1);
}
printf("socket() Creado exitosamente.");
// The source is redundant, may be used later if needed
// The address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// IP addresses
sin.sin_addr.s_addr = inet_addr(argv[2]);
din.sin_addr.s_addr = inet_addr(argv[1]);
// Creando el header IP
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16;
ip->iph_len = sizeof(struct ipheader) + 8+64;
ip->iph_ident = htons(54321);
ip->iph_ttl = 64; // hops
ip->iph_protocol = 1; // ICMP
// Source IP
ip->iph_sourceip = inet_addr(argv[2]);
//DEST IP
ip->iph_destip = inet_addr(argv[1]);
//Creamos header icmp
icmp->type=8;
icmp->code=0;
for(i=0;i<4;i++){
icmp->content[i]=0;
}
for(i=0;i<64;i++){
icmp->data[i]=(char)i;
}
printf("ICMP HEADER: %d\n",sizeof(struct icmpheader));
printf("IP HEADER: %d\n",sizeof(struct ipheader));
// Calcular checksum IP
ip->iph_chksum = checksum((unsigned short *)buffer, sizeof(struct ipheader) +sizeof(struct icmpheader));
//ICMP checksum
icmp->checksum = 0;
icmp->checksum = checksum((unsigned short *)buffer+sizeof(struct ipheader) , sizeof(struct icmpheader));
//icmp->checksum = 0xfc13;
// Inform the kernel do not fill up the packet structure. we will build our own...
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(-1);
}
else{
printf("setsockopt() OK.\n");
}
// Send loop, enviar cada 2 segundos por 100
printf("Trying...\n");
printf("Using raw socket and ICMP protocol\n");
printf("Using Source IP: %s Destination IP: %s .\n", argv[2], argv[1]);
int count;
for(count = 1; count <=20; count++)
{
if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
// Verify
{
perror("sendto() error");
exit(-1);
}
else
{
printf("Count #%u - sendto() is OK.\n", count);
sleep(2);
}
}
close(sd);
return 0;
}
有人能告诉我计算ICMP标头校验和的正确方法吗?
答案 0 :(得分:0)
int ip_headerlen = ip->iph_ihl * 4;
ip->iph_chksum = in_cksum ((unsigned short *) ip,
ip_headerlen);//only need ipheaderlen
icmp->icmp_chksum = in_cksum((unsigned short *)icmp,
//sizeof(struct icmpheader));
ntohs(ip->iph_len)-ip_headerlen );
//need icmp header + icmp data