PingImpl.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // THIS CODE IS BASED ON THE CODE FROM
  3. // THE BOOK WINSOCK 2.0 BY LEWIS NAPPER...
  4. //
  5. //
  6. #include "RestfulFunc.h"
  7. #if defined(_MSC_VER)
  8. #include <winsock.h>
  9. //路由选项类型,占用3字节
  10. #define IP_RECORD_ROUTE 0x7
  11. //IP时间戳选项
  12. #define IP_TIMESTAMP 0x44
  13. #define DEFAULT_PACKET_SIZE 32
  14. #define MAX_PACKET 1024 + 12 + 20 + 1
  15. #define MAX_IP_HDR_SIZE 60
  16. //回送请求或回答
  17. #define ICMP_ECHO 8
  18. #define ICMP_ECHO_REPLY 0
  19. #define MAX_UNSIGNED_SHORT 65535
  20. #define DEFAULT_TIMEOUT 3000
  21. #define DEFAULT_INTERVAL 1000
  22. typedef struct _iphdr
  23. {
  24. //注意顺序,网络字节序,大端
  25. unsigned int h_len : 4;
  26. unsigned int version : 4;
  27. unsigned char tos; // type of service
  28. unsigned short total_len; // Total length of the packet
  29. unsigned short ident; // Unique identifier
  30. unsigned short frag_and_flags; // 标志和片偏移
  31. unsigned char ttl; // time to live
  32. unsigned char proto;
  33. unsigned short checksum;
  34. unsigned int sourceIP;
  35. unsigned int destIP;
  36. }IpHeader;
  37. #define DEFAULT_IP_HEAD_SIZE 20
  38. typedef struct _icmphdr
  39. {
  40. BYTE btType; //type of icmp
  41. BYTE btCode; // code of icmp 进一步定义查询或消息的类型 默认0
  42. USHORT usCksum; //16bits 对ICMP头内容的一个补余求和
  43. USHORT usId; //identification
  44. USHORT usSeq;
  45. //选项值
  46. ULONG ulTimestamp;
  47. }IcmpHeader;
  48. #define DEFAULT_ICMP_HEAD_SIZE sizeof(IcmpHeader)
  49. const double MAX_GSM_SIGNAL_STRENGTH = 31.0;
  50. ///*TODO: to implement!!!! (80374374@11/15/2023)*/
  51. int PingTest(const std::string& dst_ip)
  52. {
  53. return -1;
  54. }
  55. #else
  56. #include<sys/socket.h>
  57. #include<sys/types.h>
  58. #include<netinet/in.h>
  59. #include<netinet/ip_icmp.h>
  60. #include<arpa/inet.h>
  61. #include<unistd.h>
  62. #include<sys/time.h>
  63. typedef uint32_t DWORD;
  64. typedef int SOCKET;
  65. typedef struct sockaddr_in* LPSOCKADDR_IN;
  66. typedef struct hostent* LPHOSTENT;
  67. #define SOCKET_ERROR (-1)
  68. #define FAILURE (-1)
  69. #define SIZE_LINE_NORMAL (1024)
  70. #define SUCCESS (0)
  71. #pragma pack(1)
  72. #define ICMP_ECHOREPLY 0
  73. #define ICMP_ECHOREQ 8
  74. // IP Header -- RFC 791
  75. typedef struct tagIPHDR
  76. {
  77. u_char VIHL; // Version and IHL
  78. u_char TOS; // Type Of Service
  79. short TotLen; // Total Length
  80. short ID; // Identification
  81. short FlagOff; // Flags and Fragment Offset
  82. u_char TTL; // Time To Live
  83. u_char Protocol; // Protocol
  84. u_short Checksum; // Checksum
  85. struct in_addr iaSrc; // Internet Address - Source
  86. struct in_addr iaDst; // Internet Address - Destination
  87. } IPHDR, * PIPHDR;
  88. // ICMP Header - RFC 792
  89. typedef struct tagICMPHDR
  90. {
  91. u_char Type; // Type
  92. u_char Code; // Code
  93. u_short Checksum; // Checksum
  94. u_short ID; // Identification
  95. u_short Seq; // Sequence
  96. char Data; // Data
  97. } ICMPHDR, * PICMPHDR;
  98. #define REQ_DATASIZE 32 // Echo Request Data size
  99. // ICMP Echo Request
  100. typedef struct tagECHOREQUEST
  101. {
  102. ICMPHDR icmpHdr;
  103. DWORD dwTime;
  104. char cData[REQ_DATASIZE];
  105. } ECHOREQUEST, * PECHOREQUEST;
  106. // ICMP Echo Reply
  107. typedef struct tagECHOREPLY
  108. {
  109. IPHDR ipHdr;
  110. ECHOREQUEST echoRequest;
  111. char cFiller[256];
  112. } ECHOREPLY, * PECHOREPLY;
  113. #pragma pack()
  114. static u_short api_checksum16(u_short* buffer, int size);
  115. static int gen_icmp_packet(struct icmp* icmp_packet, int type, int seq);
  116. int gen_icmp_packet(struct icmp* icmp_packet, int type, int seq)
  117. {
  118. if (!icmp_packet) {
  119. return FAILURE;
  120. }
  121. icmp_packet->icmp_type = type;
  122. icmp_packet->icmp_code = 0;
  123. icmp_packet->icmp_cksum = 0;
  124. icmp_packet->icmp_id = htons(getpid());
  125. icmp_packet->icmp_seq = htons(seq);
  126. gettimeofday((struct timeval*)icmp_packet->icmp_data, NULL);
  127. icmp_packet->icmp_cksum = api_checksum16((unsigned short*)icmp_packet, sizeof(struct icmp));
  128. return SUCCESS;
  129. }
  130. u_short api_checksum16(u_short* buffer, int size)
  131. {
  132. u_int cksum = 0;
  133. if (!buffer) {
  134. printf("NULL\n");
  135. return 0;
  136. }
  137. while (size > 1) {
  138. printf("1. Cksum: 0x%08x + 0x%04x\n", cksum, *buffer);
  139. cksum += *buffer++;
  140. size -= sizeof(u_short);
  141. }
  142. if (size) {
  143. cksum += *(u_char*)buffer;
  144. }
  145. printf("2. Cksum: 0x%08x\n", cksum);
  146. /* 32 bit change to 16 bit */
  147. while (cksum >> 16) {
  148. cksum = (cksum >> 16) + (cksum & 0xFFFF);
  149. printf("3. Cksum: 0x%08x\n", cksum);
  150. }
  151. return (u_short)(~cksum);
  152. }
  153. int PingTest(const std::string& dst_ip)
  154. {
  155. int ret = FAILURE;
  156. int sd = 0;
  157. char buf[SIZE_LINE_NORMAL] = { 0 };
  158. struct ip* ip = NULL;
  159. struct sockaddr_in dst_addr = { 0 };
  160. struct icmp icmp_packet = { 0 };
  161. struct timeval tm = { .tv_sec = 1, .tv_usec = 0 };
  162. fd_set rdfds;
  163. if (dst_ip.empty()) {
  164. goto _E1;
  165. }
  166. sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  167. if (sd < 0) {
  168. printf("socket fail: %d\n", errno);
  169. goto _E1;
  170. }
  171. dst_addr.sin_family = AF_INET;
  172. dst_addr.sin_addr.s_addr = inet_addr(dst_ip.c_str());
  173. ret = gen_icmp_packet(&icmp_packet, 8, 1);
  174. if (SUCCESS != ret) {
  175. goto _E2;
  176. }
  177. ret = sendto(sd, &icmp_packet, sizeof(struct icmp), 0,
  178. (struct sockaddr*)&dst_addr, sizeof(struct sockaddr_in));
  179. if (ret < 0) {
  180. goto _E2;
  181. }
  182. /* Timeout 1s to recv icmp */
  183. FD_ZERO(&rdfds);
  184. FD_SET(sd, &rdfds);
  185. ret = select(sd + 1, &rdfds, NULL, NULL, &tm);
  186. if (-1 == ret && EINTR != errno) {
  187. /* if serial error */
  188. printf("select fail\n");
  189. goto _E2;
  190. } else if (0 == ret) {
  191. /* timeout */
  192. printf("recv timeout\n");
  193. ret = FAILURE;
  194. goto _E2;
  195. }
  196. if (FD_ISSET(sd, &rdfds)) {
  197. ret = recv(sd, buf, sizeof(buf), 0);
  198. if (ret <= 0) {
  199. perror("recv\n");
  200. goto _E2;
  201. }
  202. ip = (struct ip*)buf;
  203. printf("from: %s\n", inet_ntoa(ip->ip_src));
  204. printf(" to: %s\n", inet_ntoa(ip->ip_dst));
  205. }
  206. ret = SUCCESS;
  207. _E2:
  208. close(sd);
  209. _E1:
  210. return ret;
  211. }
  212. #endif //_MSC_VER