須要測試外網的聯通性,想到了用ping。網上下載了ping的源代碼,調試下整理以下:ide
1 /******************************************************************************\ 2 * ping.c - Simple ping utility using SOCK_RAW 3 * 4 * This is a part of the Microsoft Source Code Samples. 5 * Copyright 1996-1997 Microsoft Corporation. 6 * All rights reserved. 7 * This source code is only intended as a supplement to 8 * Microsoft Development Tools and/or WinHelp documentation. 9 * See these sources for detailed information regarding the 10 * Microsoft samples programs. 11 \******************************************************************************/ 12 13 #pragma pack(4) 14 15 #define WIN32_LEAN_AND_MEAN 16 #include <winsock2.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #pragma comment(lib,"ws2_32.lib") 20 21 #define ICMP_ECHO 8 22 #define ICMP_ECHOREPLY 0 23 24 #define ICMP_MIN 8 // minimum 8 byte icmp packet (just header) 25 26 /* The IP header */ 27 typedef struct iphdr 28 { 29 unsigned int h_len:4; // length of the header 30 unsigned int version:4; // Version of IP 31 unsigned char tos; // Type of service 32 unsigned short total_len; // total length of the packet 33 unsigned short ident; // unique identifier 34 unsigned short frag_and_flags; // flags 35 unsigned char ttl; 36 unsigned char proto; // protocol (TCP, UDP etc) 37 unsigned short checksum; // IP checksum 38 unsigned int sourceIP; 39 unsigned int destIP; 40 }IpHeader; 41 42 // 43 // ICMP header 44 // 45 typedef struct _ihdr { 46 BYTE i_type; //消息類型 47 BYTE i_code; //代碼 /* type sub code */ 48 USHORT i_cksum; //校驗和 49 USHORT i_id; //ID號 50 USHORT i_seq; //序列號 51 ULONG timestamp; //時間戳 /* This is not the std header, but we reserve space for time */ 52 }IcmpHeader; //ICMP報文 包括報頭和數據 53 54 #define STATUS_FAILED 0xFFFF 55 #define DEF_PACKET_SIZE 32 56 #define MAX_PACKET 1024 57 58 #define xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s)) 59 #define xfree(p) HeapFree (GetProcessHeap(),0,(p)) 60 61 void fill_icmp_data(char *, int); 62 USHORT checksum(USHORT *, int); 63 void decode_resp(char *,int ,struct sockaddr_in *); 64 65 int main(int argc, char **argv) 66 { 67 WSADATA wsaData; 68 SOCKET sockRaw; 69 struct sockaddr_in dest; 70 struct hostent * hp; 71 int bread,datasize; 72 int timeout = 1000; 73 char *dest_ip; 74 char *icmp_data; 75 char *recvbuf; 76 unsigned int addr=0; 77 USHORT seq_no = 0; 78 struct sockaddr_in from; 79 int fromlen = sizeof(from); 80 81 if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0) 82 { 83 fprintf(stderr,"WSAStartup failed: %d\n",GetLastError()); 84 ExitProcess(STATUS_FAILED); 85 } 86 87 /* 88 爲了使用發送接收超時設置(即設置SO_RCVTIMEO, SO_SNDTIMEO), 89 // 必須將標誌位設爲WSA_FLAG_OVERLAPPED ! 90 */ 91 sockRaw = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,WSA_FLAG_OVERLAPPED); //創建一個原始套接字 92 //sockRaw = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,0); 93 94 if (sockRaw == INVALID_SOCKET) 95 { 96 fprintf(stderr,"WSASocket() failed: %d\n",WSAGetLastError()); 97 ExitProcess(STATUS_FAILED); 98 } 99 100 timeout = 1000; //設置接收超時時間 101 bread = setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout, sizeof(timeout)); //RECVTIMEO是接收超時時間 102 if(bread == SOCKET_ERROR) 103 { 104 fprintf(stderr,"failed to set recv timeout: %d\n",WSAGetLastError()); 105 ExitProcess(STATUS_FAILED); 106 } 107 108 timeout = 1000; //設置發送超時時間 109 bread = setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout, sizeof(timeout)); //SNDTIMEO是發送超時時間 110 if(bread == SOCKET_ERROR) 111 { 112 fprintf(stderr,"failed to set send timeout: %d\n",WSAGetLastError()); 113 ExitProcess(STATUS_FAILED); 114 } 115 memset(&dest,0,sizeof(dest)); //目標地址清零 116 117 hp = gethostbyname("www.baidu.com"); //經過域名或者主機名獲取IP地址 118 if (!hp) //失敗返回NULL 119 { 120 ExitProcess(STATUS_FAILED); 121 } 122 else 123 { 124 addr = inet_addr("14.215.177.37"); //www.baidu.com的ip地址 125 } 126 127 if ((!hp) && (addr == INADDR_NONE)) //既不是域名也不是點分十進制的IP地址 128 { 129 ExitProcess(STATUS_FAILED); 130 } 131 132 if (hp != NULL) //獲取的是域名 133 memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length); //從hostent獲得的對方ip地址 134 else 135 dest.sin_addr.s_addr = addr; 136 137 if (hp) 138 dest.sin_family = hp->h_addrtype; //sin_family不是必定只能填AF_INET嗎? 139 else 140 dest.sin_family = AF_INET; 141 142 dest_ip = inet_ntoa(dest.sin_addr); //目標IP地址 143 144 datasize = DEF_PACKET_SIZE; //ICMP包數據大小設定爲32 145 146 datasize += sizeof(IcmpHeader); //另外加上ICMP包的包頭 其實包頭佔12個字節 147 148 icmp_data = (char *)xmalloc(MAX_PACKET);//發送icmp_data數據包內存 149 recvbuf = (char *)xmalloc(MAX_PACKET); //存放接收到的數據 150 151 if (!icmp_data) //分配內存 152 { 153 ExitProcess(STATUS_FAILED); 154 } 155 156 memset(icmp_data,0,MAX_PACKET); 157 fill_icmp_data(icmp_data,datasize); //只填充了ICMP包 158 159 fprintf(stdout,"\nPinging %s ....\n\n",dest_ip); 160 161 while(1) 162 { 163 int bwrote; 164 165 ((IcmpHeader*)icmp_data)->i_cksum = 0; 166 ((IcmpHeader*)icmp_data)->timestamp = GetTickCount(); //時間戳 167 168 ((IcmpHeader*)icmp_data)->i_seq = seq_no++; //ICMP的序列號 169 ((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data, datasize); //icmp校驗位 170 171 //下面這個函數的問題是 發送數據只是ICMP數據包,而接收到的數據時包含ip頭的 也就是發送和接收不對等 172 //問題是sockRaw 設定了協議爲 IPPROTO_ICMP 173 bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest, sizeof(dest)); 174 if (bwrote == SOCKET_ERROR) 175 { 176 if (WSAGetLastError() == WSAETIMEDOUT) //發送時間超時 177 { 178 printf("timed out\n"); 179 continue; 180 } 181 182 fprintf(stderr,"sendto failed: %d\n",WSAGetLastError()); 183 ExitProcess(STATUS_FAILED); 184 } 185 186 if (bwrote < datasize ) 187 { 188 fprintf(stdout,"Wrote %d bytes\n",bwrote); 189 } 190 191 bread = recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from, &fromlen); 192 if (bread == SOCKET_ERROR) 193 { 194 if (WSAGetLastError() == WSAETIMEDOUT) 195 { 196 printf("timed out\n"); 197 continue; 198 } 199 fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError()); 200 ExitProcess(STATUS_FAILED); 201 } 202 decode_resp(recvbuf,bread,&from); 203 204 Sleep(1000); 205 } 206 207 WSACleanup(); 208 system("pause"); 209 210 return 0; 211 } 212 213 /* 214 The response is an IP packet. We must decode the IP header to locate 215 the ICMP data 216 */ 217 void decode_resp(char *buf, int bytes,struct sockaddr_in *from) 218 { 219 IpHeader *iphdr; 220 IcmpHeader *icmphdr; 221 unsigned short iphdrlen; 222 223 iphdr = (IpHeader *)buf; //接收到的數據就是原始的IP數據報 224 225 iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes 226 227 if (bytes < iphdrlen + ICMP_MIN) 228 { 229 printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr)); 230 } 231 232 icmphdr = (IcmpHeader*)(buf + iphdrlen); 233 234 if(icmphdr->i_type == 3) 235 { 236 printf("network unreachable -- Response from %s.\n",inet_ntoa(from->sin_addr)); 237 return ; 238 } 239 240 if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) 241 { 242 fprintf(stderr,"someone else's packet!\n"); 243 return ; 244 } 245 printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr)); 246 printf(" icmp_seq = %d ",icmphdr->i_seq); 247 printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp); 248 printf(" ttl: %d",iphdr->ttl); 249 printf("\n"); 250 } 251 252 //完成ICMP的校驗 253 USHORT checksum(USHORT *buffer, int size) 254 { 255 unsigned long cksum=0; 256 257 while(size >1) 258 { 259 cksum+=*buffer++; 260 size -=sizeof(USHORT); 261 } 262 263 if(size ) 264 { 265 cksum += *(UCHAR*)buffer; 266 } 267 268 cksum = (cksum >> 16) + (cksum & 0xffff); 269 cksum += (cksum >>16); 270 return (USHORT)(~cksum); 271 } 272 273 /* 274 Helper function to fill in various stuff in our ICMP request. 275 */ 276 void fill_icmp_data(char * icmp_data, int datasize){ 277 278 IcmpHeader *icmp_hdr; 279 char *datapart; 280 281 icmp_hdr = (IcmpHeader*)icmp_data; 282 283 icmp_hdr->i_type = ICMP_ECHO; //ICMP_ECHO要求收到包的主機回覆此ICMP包 284 icmp_hdr->i_code = 0; 285 icmp_hdr->i_id = (USHORT)GetCurrentProcessId(); //id填當前進程的id 286 icmp_hdr->i_cksum = 0; 287 icmp_hdr->i_seq = 0; 288 289 datapart = icmp_data + sizeof(IcmpHeader); 290 // 291 // Place some junk in the buffer. 292 // 293 memset(datapart,'E', datasize - sizeof(IcmpHeader)); //填充了一些廢物 294 }
我下到代碼的時候,第91行建立原始套接字的地方本來是被屏蔽的第92行,區別在與建立套接字時賦予的標誌位不同。函數
WSASocket函數的定義以下:測試
SOCKET WSASocket ( int af, int type, int protocol, LPWSAPROTOCOL_INFO lpProtocolInfo, GROUP g, DWORD dwFlags );
af:[in]一個地址族規範。目前僅支持AF_INET格式,亦即ARPA Internet地址格式。spa
type:新套接口的類型描述。指針
protocol:套接口使用的特定協議,若是調用者不肯指定協議則定爲0。調試
lpProtocolInfo:一個指向PROTOCOL_INFO結構的指針,該結構定義所建立套接口的特性。若是本參數非零,則前三個參數(af, type, protocol)被忽略。code
g:保留給將來使用的套接口組。套接口組的標識符。orm
iFlags:套接口屬性描述。blog
具體詳細介紹看微軟官方介紹文檔:https://msdn.microsoft.com/en-us/library/ms742212(VS.85).aspx接口