Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1682925
  • 博文数量: 311
  • 博客积分: 7778
  • 博客等级: 少将
  • 技术积分: 4186
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-09 19:59
个人简介

蓝点工坊(http://www.bluedrum.cn) 创始人,App和嵌入式产品开发。同时也做相应培训和外包工作。 详细介绍 http://pan.baidu.com/s/1y2g88

文章存档

2012年(3)

2011年(115)

2010年(170)

2009年(23)

分类: 系统运维

2010-10-13 01:31:58

/*
  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);
}


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