1.Windows下ping实现
Windows平台实现ping功能,需要涉及到两个动态库Icmp.dll和Iphlpapi.dll。
根据msdn的说明:
大概意思是说 IcmpParseReplies函数在Windows2000上面由Icmp.dll导出,而在Windows XP及以后的版本是由Iphlpapi.dll导出的。应用程序不应该用判断系统版本静态链接Icmp.lib和Iphlpapi.lib,而应该通过调用LoadLibrary和GetProcAddress来检测 IcmpParseReplies由哪个动态库导出。优先判断Iphlpapi.dll是否存在且有导出此函数,如果存在就使用此动态库,否则再判断Icmp.dll。“The IcmpParseReplies function is exported from the Icmp.dll on Windows 2000. The IcmpParseReplies function is exported from theIphlpapi.dll on Windows XP and later. OS version checking is not recommended to use this function. Applications requiring portability with this function across Windows 2000, Windows XP, Windows Server 2003 and later Windows versions should not statically link to either the Icmp.lib or the Iphlpapi.lib file.Instead, the application should check for the presence ofIcmpParseReplies in the Iphlpapi.dll with calls toLoadLibrary and GetProcAddress. Failing that, the application should check for the presence ofIcmpParseReplies in the Icmp.dll with calls to LoadLibrary and GetProcAddress. ”
实现源码如下:
enum SOCKET_TYPE { SOCKET_TYPE_IPV4, SOCKET_TYPE_IPV6, }; typedef BOOL (WINAPI *ICMP_CLOSEHANDLE)(HANDLE IcmpHandle); typedef DWORD (WINAPI *ICMP_PARSEREPLIES)(LPVOID ReplyBuffer,DWORD ReplySize); typedef HANDLE (WINAPI *ICMP_CREATEFILE)(VOID); typedef DWORD (WINAPI *ICMP_SENDECHO)(HANDLE IcmpHandle, IPAddr DestinationAddress, LPVOID RequestData, WORD RequestSize, PIP_OPTION_INFORMATION RequestOptions, LPVOID ReplyBuffer, DWORD ReplySize, DWORD Timeout); typedef HANDLE (WINAPI *ICMP6_CREATEFILE)(VOID); typedef DWORD (WINAPI *ICMP6_SENDECHO2)(HANDLE IcmpHandle, HANDLE Event, #ifdef PIO_APC_ROUTINE_DEFINED PIO_APC_ROUTINE ApcRoutine, #else FARPROC ApcRoutine, #endif PVOID ApcContext, struct sockaddr_in6* SourceAddress, struct sockaddr_in6* DestinationAddress, LPVOID RequestData, WORD RequestSize, PIP_OPTION_INFORMATION RequestOptions, LPVOID ReplyBuffer, DWORD ReplySize, DWORD Timeout); int Ping(void *ipAddr, SOCKET_TYPE type) { int ret = 0; DWORD dw; HANDLE hIcmpFile; HMODULE hModule; static char sendData[] = "Hello world."; ICMP_PARSEREPLIES MyIcmpParseReplies; ICMP_CLOSEHANDLE MyIcmpCloseHandle; hModule = LoadLibraryA("Iphlpapi.dll"); if (NULL != hModule) { MyIcmpParseReplies = (ICMP_PARSEREPLIES)GetProcAddress(hModule, "IcmpParseReplies"); if (NULL == MyIcmpParseReplies) { FreeLibrary(hModule); hModule = LoadLibraryA("Icmp.dll"); if (NULL == hModule) { return GetLastError(); } MyIcmpParseReplies = (ICMP_PARSEREPLIES)GetProcAddress(hModule, "IcmpParseReplies"); if (NULL == MyIcmpParseReplies) { ret = GetLastError(); FreeLibrary(hModule); return ret; } } } MyIcmpCloseHandle = (ICMP_CLOSEHANDLE)GetProcAddress(hModule, "IcmpCloseHandle"); if (NULL == MyIcmpCloseHandle) { ret = GetLastError(); FreeLibrary(hModule); return ret; } if (SOCKET_TYPE_IPV4 == type) { ICMP_CREATEFILE MyIcmpCreateFile; ICMP_SENDECHO MyIcmpSendEcho; ICMP_ECHO_REPLY *reply; char ipv4Buff[sizeof(ICMP_ECHO_REPLY) + 8 + 4]; MyIcmpCreateFile = (ICMP_CREATEFILE)GetProcAddress(hModule, "IcmpCreateFile"); if (NULL == MyIcmpCreateFile) { ret = GetLastError(); FreeLibrary(hModule); return ret; } MyIcmpSendEcho = (ICMP_SENDECHO)GetProcAddress(hModule, "IcmpSendEcho"); if (NULL == MyIcmpSendEcho) { ret = GetLastError(); FreeLibrary(hModule); return ret; } hIcmpFile = MyIcmpCreateFile(); if (INVALID_HANDLE_VALUE == hIcmpFile) { ret = GetLastError(); FreeLibrary(hModule); return ret; } dw = MyIcmpSendEcho(hIcmpFile, (int)ipAddr, sendData, sizeof(sendData), NULL, ipv4Buff, sizeof(ipv4Buff), 1000); reply = (ICMP_ECHO_REPLY *)ipv4Buff; if (0 == dw || IP_SUCCESS != reply->Status) { ret = GetLastError(); } } else { ICMP6_CREATEFILE MyIcmp6CreateFile; ICMP6_SENDECHO2 MyIcmp6SendEcho2; struct sockaddr_in6 sa6Src = { 0 }; struct sockaddr_in6 sa6Dest = { 0 }; char ipv6Buff[sizeof(ICMPV6_ECHO_REPLY) + 8 + 4]; ICMPV6_ECHO_REPLY *reply; MyIcmp6CreateFile = (ICMP6_CREATEFILE)GetProcAddress(hModule, "Icmp6CreateFile"); if (NULL == MyIcmp6CreateFile) { ret = GetLastError(); FreeLibrary(hModule); return ret; } MyIcmp6SendEcho2 = (ICMP6_SENDECHO2)GetProcAddress(hModule, "Icmp6SendEcho2"); if (NULL == MyIcmp6SendEcho2) { ret = GetLastError(); FreeLibrary(hModule); return ret; } hIcmpFile = MyIcmp6CreateFile(); if (INVALID_HANDLE_VALUE == hIcmpFile) { ret = GetLastError(); FreeLibrary(hModule); return ret; } sa6Src.sin6_addr = in6addr_any; sa6Src.sin6_family = AF_INET6; sa6Src.sin6_flowinfo = 0; sa6Src.sin6_port = 0; memcpy(sa6Dest.sin6_addr.u.Byte , ipAddr, sizeof(sa6Dest.sin6_addr.u.Byte)); sa6Dest.sin6_family = AF_INET6; sa6Dest.sin6_flowinfo = 0; sa6Dest.sin6_port = 0; dw = MyIcmp6SendEcho2(hIcmpFile, NULL, NULL, NULL, &sa6Src, &sa6Dest, sendData, sizeof(sendData), NULL, ipv6Buff, sizeof(ipv6Buff), 1000); reply = (ICMPV6_ECHO_REPLY *)ipv6Buff; if (0 == dw || IP_SUCCESS != reply->Status) { ret = GetLastError(); } } MyIcmpCloseHandle(hIcmpFile); FreeLibrary(hModule); return ret; }
2.Linux下ping实现
linux下可直接调用工具ping和ping6来完成。代码如下
void ip2str(unsigned char *ip4, char *buff, int bufSize) { inet_ntop(AF_INET, ip4, buff, bufSize); } void ip62str(unsigned char *ip6, char *buff, int bufSize) { inet_ntop(AF_INET6, ip6, buff, bufSize); } int Ping2(unsigned char *ipAddr, SOCKET_TYPE type) { char cmd[100]; char ip[INET6_ADDRSTRLEN]; if (SOCKET_TYPE_IPV4 == type) { ip2str(ipAddr, ip, sizeof(ip)); sprintf(cmd,"/bin/ping %s -c 1 > /dev/null 2>&1", ip); } else { ip62str(ipAddr, ip, sizeof(ip)); sprintf(cmd,"/bin/ping6 %s -c 1 > /dev/null 2>&1", ip); } return system(cmd);