Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1372395
  • 博文数量: 198
  • 博客积分: 1629
  • 博客等级: 上尉
  • 技术积分: 2743
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 15:41
文章分类
文章存档

2023年(6)

2022年(20)

2021年(8)

2020年(3)

2018年(17)

2017年(3)

2016年(3)

2015年(9)

2014年(13)

2013年(17)

2012年(77)

2011年(22)

分类: LINUX

2014-02-12 16:19:32


点击(此处)折叠或打开

  1. /*
  2.  * check net
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/time.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/ip_icmp.h>
  13. #include <sys/signal.h>

  14. #define PACKET_SIZE 512
  15. #define TRYCONT     59

  16. char sendpacket[PACKET_SIZE];
  17. char recvpacket[PACKET_SIZE];

  18. int sockfd, datalen = 56;

  19. struct sockaddr_in dest_addr;
  20. struct sockaddr_in from;
  21. pid_t pid;

  22. int my_sequence = 0;
  23. int g_try = TRYCONT;

  24. void handle_sigalrm(int signo);
  25. void handle_sigint(int signum);
  26. void send_pack(void);
  27. void recv_pack(void);
  28. unsigned short check_sum(unsigned short *addr, int len);

  29. void reboot_sys()
  30. {
  31. if(!(g_try%10))
  32. {
  33. system("ifconfig eth0 down");
  34. printf("Restart net\n");
  35. system("ifconfig eth0 up");
  36. }

  37. if(!g_try)
  38. {
  39. printf("System reboot now ......\n");
  40. system("reboot");
  41. }
  42. printf("\nHas -> %d times last\n", g_try);
  43. g_try--;
  44. }

  45. /*signal producure for sending icmp */
  46. void handle_sigalrm(int signo)
  47. {
  48. send_pack();
  49. alarm(2); //produce a signal for sending icmp
  50. }

  51. /*signal producure for finishing ping */
  52. void handle_sigint(int signum) {
  53. printf("\texit\n");
  54. close(sockfd);
  55. exit(0);
  56. }
  57. /* Check Sum */
  58. unsigned short check_sum(unsigned short *addr, int len)
  59. {
  60. int nleft = len;
  61. unsigned short *w = addr;
  62. int sum = 0;
  63. unsigned short answer = 0;

  64. while (nleft > 1)
  65. {
  66. sum += *w++;
  67. nleft -= 2;
  68. }

  69. if (nleft == 1)
  70. {
  71. *(unsigned char *)(&answer)=*(unsigned char *)w;
  72. sum += answer;
  73. }

  74. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  75. sum += (sum >> 16); /* add carry */
  76. answer = ~sum; /* truncate to 16 bits */

  77. return (answer);
  78. }
  79. /*packet*/
  80. int pack(int pack_no)
  81. {
  82. int i, packsize;
  83. struct icmp *icmp;
  84. struct timeval *tval;

  85. icmp = (struct icmp*)sendpacket;
  86. icmp->icmp_type = ICMP_ECHO;
  87. icmp->icmp_code = 0;
  88. icmp->icmp_cksum = 0;
  89. icmp->icmp_seq = pack_no;
  90. icmp->icmp_id = pid;
  91. packsize = 8+datalen;
  92. tval = (struct timeval *)icmp->icmp_data;
  93. gettimeofday(tval,NULL);
  94. icmp->icmp_cksum = check_sum((unsigned short *)icmp,packsize);
  95. return packsize;
  96. }
  97. /*send icmp packet*/
  98. void send_pack(void)
  99. {
  100. int len = 0;
  101. int packetsize = pack(my_sequence);
  102. len = sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr));
  103. if(len < 0)
  104. {
  105. perror("sendto error");
  106. reboot_sys();
  107. }
  108. //printf("%s:len=%d\n",__func__,len);

  109. ++my_sequence;
  110. printf("Sendto %s\n", inet_ntoa(dest_addr.sin_addr));
  111. }

  112. /*unpacket*/
  113. int unpack(char *buf, int len)
  114. {
  115. int i;
  116. int iphdrlen;
  117. struct ip *ip;
  118. struct icmp *icmp;
  119. struct timeval *tvsend;
  120. double rtt;

  121. ip = (struct ip *)buf;
  122. iphdrlen = ip->ip_hl << 2;
  123. icmp = (struct icmp *)(buf+iphdrlen);
  124. len -= iphdrlen;
  125. if(len < 8)
  126. {
  127. printf("ICMP packet\'s length is less than 8\n");
  128. return -1;
  129. }

  130. if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid))
  131. {
  132. tvsend = (struct timeval *)icmp->icmp_data;
  133. printf("Internet ......OK!!\n");
  134. g_try = TRYCONT;
  135. }
  136. else return -1;

  137. return 0;
  138. }

  139. void recv_pack(void)
  140. {
  141. int len, fromlen, res;
  142. fromlen = sizeof(from);

  143. while(1)
  144. {
  145. len = recvfrom(sockfd, recvpacket, sizeof(recvpacket),0,(struct sockaddr *)&from, &fromlen);
  146. if(len < 0)
  147. {
  148. perror("recvform eorror");
  149. reboot_sys();
  150. }
  151. printf("len = %d\n", len);

  152. if(unpack(recvpacket,len) == -1) continue;
  153. sleep(2);
  154. }
  155. }

  156. int main(int argc, char **argv)
  157. {
  158. struct protoent *protocol;
  159. struct sigaction act;
  160. struct sigaction act2;

  161. if (2 != argc)
  162. {
  163. printf("Parameter is Wrong!\n");
  164. printf("\tusage [param]\n");
  165. printf("\tparam:ip address\n");
  166. exit(-1);
  167. }

  168. if((protocol = getprotobyname("icmp")) == NULL)
  169. {
  170. perror("getprotobyname");
  171. exit(0);
  172. }
  173. if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
  174. {
  175. perror("fail to socket!");
  176. exit(-1);
  177. }

  178. memset(&act, 0, sizeof(act));
  179. act.sa_handler = handle_sigalrm;
  180. sigemptyset(&act.sa_mask);
  181. act.sa_flags = 0;

  182. if (-1 == sigaction(SIGALRM, &act, NULL))
  183. perror("sigaction-1");

  184. pid = getpid();

  185. bzero(&dest_addr, sizeof(struct sockaddr_in));
  186. dest_addr.sin_family = AF_INET;
  187. inet_pton(AF_INET, argv[1], &dest_addr.sin_addr.s_addr);

  188. struct sigaction sa2;
  189. memset(&sa2, 0, sizeof(sa2));
  190. sa2.sa_handler = handle_sigint;//catch_sigint;
  191. sigemptyset(&sa2.sa_mask);
  192. sa2.sa_flags = 0;
  193. if(sigaction(SIGINT, &sa2, NULL) == -1)
  194. perror("sigaction-2");

  195. alarm(2);
  196. sleep(2);
  197. recv_pack();

  198. return 0;

  199. }



阅读(1360) | 评论(0) | 转发(0) |
1

上一篇:utest.lds文件

下一篇:去掉光标和制作Logo

给主人留下些什么吧!~~