Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157509
  • 博文数量: 85
  • 博客积分: 366
  • 博客等级: 一等列兵
  • 技术积分: 455
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-16 18:32
个人简介

闲下来的时候看看书

文章分类

全部博文(85)

文章存档

2016年(1)

2013年(2)

2012年(81)

2011年(1)

我的朋友

分类:

2012-09-20 17:04:28

/*
  RAW Socket /ICMP ping
   Author: Andrew Huang
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/ip_icmp.h> /* for struct icmp */

#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>



static unsigned char icmp_test_pkg[] = {0x08,0x00,0x39,0xec,0x49,0x52,0x00,0x01,0xe6,0xb4,0xf7,0x4a,0xa5,0xbd,0x06,0x00
,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17
,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27
,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37};

char buffer[1024];
char sendbuf[1024];
int icmp_sock =-1;
int sendnum = 0;


unsigned short my_cksum(unsigned short *data, int len) {
        int result = 0;
          int i;
        for( i=0; i<len/2; i++) {
                result += *data;
                data++;
        }
        while(result >> 16)result = (result&0xffff) + (result>>16);
        return ~result;
}

void send_icmp(int datalen) {

        struct icmp* icmp = (struct icmp*)sendbuf;
      struct sockaddr_in addr;
  

    memset((void *)&addr,0,sizeof(addr));
     addr.sin_family = AF_INET ; /* ipv4 */
     addr.sin_addr.s_addr = inet_addr("127.0.0.1");

        icmp->icmp_type = ICMP_ECHO;
        icmp->icmp_code = 0;
        icmp->icmp_cksum = 0;
        icmp->icmp_id = getpid(); //needn't use htons() call, because peer networking kernel didn't handle this data and won't make different meanings(bigdian litteldian)

        icmp->icmp_seq = ++sendnum; //needn't use hotns() call too.

        gettimeofday((struct timeval*)icmp->icmp_data, NULL);
        int len = 8+datalen;
        icmp->icmp_cksum = my_cksum((unsigned short*)icmp, len);
        int retval = sendto(icmp_sock, sendbuf, len, 0, (struct sockaddr*)&addr, sizeof(addr));
        if(retval == -1){
                perror("sendto()");
                exit(-1);
        } else {
// printf("send icmp request to %s(%d) bytes\n", straddr, len);

        }
}

void send_pkt(int ago)
{

  struct sockaddr_in sock_addr;
socklen_t sockaddr_len;
  int len;

#if 0

  memset((void *)&sock_addr,0,sizeof(sock_addr));
     sock_addr.sin_family = AF_INET ; /* ipv4 */
     sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");


   fprintf(stdout,"send pkt\n");
   
 len = sendto(icmp_sock,icmp_test_pkg,sizeof(icmp_test_pkg),0,(struct sockaddr *)&sock_addr,sockaddr_len) ;

     if(len <= 0)
         perror("sendto");
#endif

  send_icmp(30);
}


int main()
{
  socklen_t sockaddr_len;
   int len;

   //signal(SIGALRM,send_pkt);


   icmp_sock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    if(icmp_sock < 0)
      {
             perror("socket_raw");
          return (-1) ;
      }
#if 0
// len = recvfrom(icmp_sock,buffer,sizeof(buffer),0,(struct sockaddr *)&sock_addr,&sockaddr_len);

 


//构造对端地址

  
   alarm(2);

  len = recvfrom(icmp_sock,buffer,sizeof(buffer),0,(struct sockaddr *)0,0);
   if(len <= 0)
         perror("recvfrom");
#endif

   send_icmp(30);
   getchar();

   close(icmp_sock);
}


阅读(403) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~