level 6
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip_icmp.h>
/*
*
*/
unsigned short check_sum(unsigned short *buffer, int size)
{
unsigned long cksum = 0;
while(size>1)
{
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if(size)
{
cksum += *(unsigned char*)buffer;
}
cksum = (cksum>>16) + (cksum&0xffff);
cksum += (cksum>>16);
return (unsigned short)(~cksum);
}
int main(int argc, char** argv) {
int fd = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(fd == -1)
{
perror("");
return -1;
}
struct icmp imp;
bzero(&imp,sizeof(imp));
imp.icmp_type = ICMP_ECHO;
imp.icmp_code = 0;
imp.icmp_id = getpid();
imp.icmp_seq = 0;
imp.icmp_cksum = 0;//check_sum(&imp,sizeof(imp));
//
struct sockaddr_in sock;
bzero(&sock,sizeof(sock));
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = inet_addr("192.168.1.102");
int size = sendto(fd,&imp,sizeof(imp),0,&sock,sizeof(sock));
if(size == -1)
{
perror("sendto");
return -1;
}
//
char buffer[1024];
int len = sizeof(sock);
size = recvfrom(fd,buffer,1024,0,&sock,&len);
if(size == -1)
{
perror("recvfrom");
return -1;
}
//
struct ip* p = buffer;
struct icmp* temp = (struct icmp*)(buffer+20);
printf("%d\n",temp->icmp_type);
printf("%d\n",temp->icmp_code);
//
return (EXIT_SUCCESS);
}
/*
* File: main.c
* Author: root
*
* Created on December 2, 2009, 8:14 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <sys/types.h>
2009年12月09日 14点12分
1
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip_icmp.h>
/*
*
*/
unsigned short check_sum(unsigned short *buffer, int size)
{
unsigned long cksum = 0;
while(size>1)
{
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if(size)
{
cksum += *(unsigned char*)buffer;
}
cksum = (cksum>>16) + (cksum&0xffff);
cksum += (cksum>>16);
return (unsigned short)(~cksum);
}
int main(int argc, char** argv) {
int fd = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(fd == -1)
{
perror("");
return -1;
}
struct icmp imp;
bzero(&imp,sizeof(imp));
imp.icmp_type = ICMP_ECHO;
imp.icmp_code = 0;
imp.icmp_id = getpid();
imp.icmp_seq = 0;
imp.icmp_cksum = 0;//check_sum(&imp,sizeof(imp));
//
struct sockaddr_in sock;
bzero(&sock,sizeof(sock));
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = inet_addr("192.168.1.102");
int size = sendto(fd,&imp,sizeof(imp),0,&sock,sizeof(sock));
if(size == -1)
{
perror("sendto");
return -1;
}
//
char buffer[1024];
int len = sizeof(sock);
size = recvfrom(fd,buffer,1024,0,&sock,&len);
if(size == -1)
{
perror("recvfrom");
return -1;
}
//
struct ip* p = buffer;
struct icmp* temp = (struct icmp*)(buffer+20);
printf("%d\n",temp->icmp_type);
printf("%d\n",temp->icmp_code);
//
return (EXIT_SUCCESS);
}
/*
* File: main.c
* Author: root
*
* Created on December 2, 2009, 8:14 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <sys/types.h>