1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
int main (int argc, char *argv[])
{
struct ip iphdr;
iphdr.ip_hl = sizeof(struct ip)/4;
iphdr.ip_v = IPVERSION;
iphdr.ip_tos = 0;
iphdr.ip_len = ntohs(20);
iphdr.ip_id = ntohs(0);
iphdr.ip_off = ntohs(0);
iphdr.ip_ttl = 200;
iphdr.ip_p = IPPROTO_IP;
inet_aton("192.168.1.2",&iphdr.ip_src);
inet_aton("127.0.0.1",&iphdr.ip_dst);
iphdr.ip_sum = ntohs(checksum((char * ) &iphdr, 20));
printf("%02x\n", htons(iphdr.ip_sum));
int s;
if((s = socket(PF_INET,SOCK_RAW,IPPROTO_ICMP)) == -1)
{
perror("Socket creation error");
printf("Errno = %d.\n", errno);
}
else
{
struct sockaddr_in to;
int hincl = 0;
if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)) == -1)
{
perror("Setsockopt error");
printf("Errno = %d\n",errno);
}
to.sin_family = AF_INET;
to.sin_port = 0;
to.sin_addr.s_addr = inet_addr("127.0.0.1");
size_t i;
for(i = 0; i < sizeof(to.sin_zero); ++i) {
to.sin_zero[i] = 0;
}
if(sendto(s, &iphdr, sizeof(struct ip), 0, (struct sockaddr *) &to,
sizeof(struct sockaddr_in)) == -1)
{
perror("Sendto error");
printf("Errno = %d.\n", errno);
}
else {
printf("Datagram sent...\n");
}
}
return 0;
} |
Partager