Chinaunix首页 | 论坛 | 博客
  • 博客访问: 822919
  • 博文数量: 132
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2276
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-03 10:50
个人简介

while(!dead) learning++;

文章分类

全部博文(132)

文章存档

2019年(3)

2018年(11)

2017年(12)

2016年(8)

2015年(8)

2014年(4)

2013年(86)

分类: LINUX

2013-06-02 14:51:39


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <sys/epoll.h>
  12. #include <time.h>

  13. #define BUF_SIZE 1024
  14. #define SRV_PORT 53
  15. #define DNS_FILE "/etc/resolv.conf"

  16. typedef unsigned short U16;
  17. //const char srv_ip[] = "208.67.222.222";

  18. typedef struct _DNS_HDR
  19. {
  20.     U16 id;
  21.     U16 tag;
  22.     U16 numq;
  23.     U16 numa;
  24.     U16 numa1;
  25.     U16 numa2;
  26. }DNS_HDR;

  27. typedef struct _DNS_QER
  28. {
  29.     U16 type;
  30.     U16 classes;
  31. }DNS_QER;

  32. char* get_dns_ip(void)
  33. {
  34.     FILE* fp = NULL;
  35.     static char addr[32] = {''};

  36.     fp = fopen(DNS_FILE, "r");
  37.     if(fp == NULL)
  38.         return NULL;
  39.     
  40.     fscanf(fp, "nameserver %s", addr);

  41.     fclose(fp);
  42.     return addr;
  43. }

  44. char* strtrim(char* str)
  45. {
  46.     char*p = NULL;
  47.     char*q = NULL;

  48.     p = str;
  49.     q = str + strlen(str) - 1;
  50.     while(isspace(*p))
  51.         p++;
  52.     while(isspace(*q))
  53.         q--;
  54.     *(q + 1) = '';
  55.     return p;
  56. }

  57. char* get_reverct_ip(char* ipaddr)
  58. {
  59.     if (ipaddr == NULL)
  60.         return NULL;
  61.     char ipstr[INET_ADDRSTRLEN] = {'',};
  62.     static char retstr[32] = {0};
  63.     char a[4][4];
  64.     struct in_addr addr1, addr2;
  65.     memset(&addr1, 0, sizeof(addr1));
  66.     memset(&addr2, 0, sizeof(addr2));
  67.     int i, j;
  68.     char *p = NULL;
  69.     for (i = 0; i < 4; i++)
  70.     {
  71.         for (j = 0; j < 4; j++)
  72.             a[i][j] = '';
  73.     }
  74.     i = 0;
  75.     p = strtok(ipaddr, ".");
  76.     while (p)
  77.     {
  78.         strcpy(a[i++], p);
  79.         p = strtok(NULL, ".");
  80.     }
  81.     sprintf(retstr, "%s.%s.%s.%s.in-addr.arpa", a[3],a[2],a[1],a[0]);

  82. #if 0
  83.     inet_pton(AF_INET, ipaddr, &addr1);
  84.     addr2.s_addr = ntohl(addr1.s_addr);
  85.     inet_ntop(AF_INET, &addr2, ipstr, sizeof(ipstr));
  86.     sprintf(retstr, "%s.in-addr.arpa", ipstr);
  87. #endif

  88.     return retstr;
  89. }

  90. int main(int argc, char** argv)
  91. {
  92.     int clifd, len = 0, i,count=0;
  93.     int ipv4 = 0, ipv6 = 0;
  94.     socklen_t socklen = 0;
  95.     char buf[BUF_SIZE] = {''};
  96.     char tmpip[32] = {''};
  97.     char *p = NULL;
  98.     char *ipaddr = NULL;
  99.     char *ipaddr2 = NULL;
  100.     time_t sendtime = 0, recvtime = 0, caltime = 0;
  101.     struct sockaddr_in servaddr;
  102.     struct sockaddr_in6 servaddr6;
  103.     struct timeval pretime, aftertime, tv;
  104.     memset(&tv, '', sizeof(tv));

  105.     if (strchr(argv[1], '.'))
  106.         ipv4 = 1;    
  107.     else if (strchr(argv[1], ':'))
  108.         ipv6 = 1;
  109.     else
  110.         return -1;

  111.     ipaddr = get_dns_ip();
  112.     if (ipv4)
  113.     {
  114.         clifd = socket(AF_INET, SOCK_DGRAM, 0 );
  115.         if (clifd < 0)
  116.         {
  117.             printf( " create socket error!\n " );
  118.             return -1;
  119.         }
  120.         bzero(&servaddr, sizeof(servaddr));
  121.         servaddr.sin_family = AF_INET;
  122.         servaddr.sin_port = htons(SRV_PORT);
  123.         inet_pton(AF_INET, ipaddr, &servaddr.sin_addr);
  124.     }
  125.     else if (ipv6)
  126.     {
  127.         clifd = socket(AF_INET6, SOCK_DGRAM, 0 );
  128.         if (clifd < 0)
  129.         {
  130.             printf( " create socket error!\n " );
  131.             return -1;
  132.         }
  133.         bzero(&servaddr6, sizeof(servaddr6));
  134.         servaddr6.sin6_family = AF_INET6;
  135.         servaddr6.sin6_port = htons(SRV_PORT);
  136.         inet_pton(AF_INET6, ipaddr, &servaddr6.sin6_addr);
  137.     }
  138.     
  139.     DNS_HDR *dnshdr = (DNS_HDR *)buf;
  140.     DNS_QER *dnsqer = (DNS_QER *)(buf + sizeof(DNS_HDR));
  141.     memset(buf, 0, BUF_SIZE);
  142.     dnshdr->id = (U16)1;
  143.     dnshdr->tag = htons(0x0100);
  144.     dnshdr->numq = htons(1);
  145.     dnshdr->numa = 0;

  146.     strcpy(tmpip, argv[1]);
  147.     ipaddr2 = get_reverct_ip(argv[1]);
  148.     strncpy(buf + sizeof(DNS_HDR) + 1, ipaddr2, strlen(ipaddr2));
  149.     printf(", len: %d\n", ipaddr2, strlen(ipaddr2));
  150.     p = buf + sizeof(DNS_HDR) + 1;
  151.     i = 0;
  152.     while (p < (buf + sizeof(DNS_HDR) + 1 + strlen(ipaddr2)))
  153.     {
  154.         if ( *p == '.')
  155.         {
  156.             *(p - i - 1) = i;
  157.             i = 0;
  158.         }
  159.         else
  160.         {
  161.             i++;
  162.         }
  163.         p++;
  164.       }
  165.     *(p - i - 1) = i;
  166.     
  167.     dnsqer = (DNS_QER *)(buf + sizeof(DNS_HDR) + 2 + strlen(ipaddr2));
  168.     dnsqer->classes = htons(1);
  169.     dnsqer->type = htons(0x0c);

  170.     tv.tv_sec = 2;
  171.     tv.tv_usec = 0;
  172.     gettimeofday(&pretime, NULL);
  173.     if (ipv4)
  174.     {
  175.         setsockopt(clifd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  176.         socklen = sizeof(struct sockaddr_in);
  177.         len = sendto(clifd, buf, sizeof(DNS_HDR) + sizeof(DNS_QER) + strlen(ipaddr2) + 2, 0, (struct sockaddr *)&servaddr, socklen);
  178.         if (len < 0)
  179.         {
  180.             printf("send error\n");
  181.             return -1;
  182.         }
  183.         len = recvfrom(clifd, buf, BUF_SIZE, 0, (struct sockaddr *)&servaddr, &socklen);
  184.         if (len < 0)
  185.         {
  186.             if (errno == EAGAIN)
  187.             {
  188.                 printf("recvfrom timeout!\n");
  189.             }
  190.         }
  191.     }
  192.     else
  193.     {
  194.         setsockopt(clifd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  195.         socklen = sizeof(struct sockaddr_in6);
  196.         len = sendto(clifd, buf, sizeof(DNS_HDR) + sizeof(DNS_QER) + strlen(ipaddr2) + 2, 0, (struct sockaddr *)&servaddr6, socklen);
  197.         if (len < 0)
  198.         {
  199.             printf("send error\n");
  200.             return -1;
  201.         }
  202.         len = recvfrom(clifd, buf, BUF_SIZE, 0, (struct sockaddr *)&servaddr6, &socklen);
  203.         if (len < 0)
  204.         {
  205.             if (errno == EAGAIN)
  206.             {
  207.                 fprintf(stdout, "recvfrom timeout!\n");
  208.             }
  209.         }
  210.     }
  211.     gettimeofday(&aftertime, NULL);
  212.     fprintf(stdout, "time ==> %d \n", (aftertime.tv_sec - pretime.tv_sec)*1000 + (aftertime.tv_usec - pretime.tv_usec)/1000);
  213.     
  214.     printf("answer: %d\n", dnshdr->numa);
  215.     if (dnshdr->numa == 0)
  216.     {
  217.         printf("ack error\n");
  218.         return -1;
  219.     }
  220.     p = buf + len -21;
  221.     printf("%s ==> %s\n", tmpip, p);
  222.     close(clifd);
  223.     return 0;
  224. }

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