在连接(ping)之前验证IP地址(主机)是否存在



我有一个问题,我需要确定主机在连接到它之前是否存在。该主机不能与gethostbyaddr()函数一起工作,因为它不是基于pc的,并且不返回主机信息。它仅基于ip。每当我尝试在IP地址上调用gethostbyaddr()时,WinSock返回11004 (WSANODATA)。

是否有一个类似的功能(除了ping),以确定是否一个IP是有效的,然后再尝试连接?

如果您对目标主机有某种控制,您可以定期检查主机是否存在而不使用临时端口的一种方法是发送UDP数据报,并等待ICMP响应告诉您数据报被主机拒绝。

您可以通过创建SOCK_DGRAM套接字,绑定到本地端口,并调用sendto()将其发送到未侦听的已知远程端口来实现此目的。然后,您可以轮询并调用recvfrom(),如果您的主机收到ICMP响应,该方法将给出一个错误。如果主机没有启动,那么您将得不到响应。您可以使用相同的套接字和相同的端口来周期性地发送尽可能多的数据报。

发送ICMP回显请求在大多数系统上需要很高的权限,因此很难直接从您的代码中完成。

下面是一些示例代码,大致完成了我所描述的:

struct sockaddr_in local_address;
struct sockaddr_in remote_address;
int sfd;
char * remote_host;
int s;
fd_set fds;
struct timeval timeout;
remote_host = argv[1];
sfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sfd < 0) {
    perror("socket");
}
memset(&local_address, 0, sizeof(struct sockaddr_in));
local_address.sin_family = AF_INET;
local_address.sin_addr.s_addr = INADDR_ANY;
local_address.sin_port = htons(6799);
s = bind(sfd,
         (struct sockaddr*)&local_address,
         sizeof(local_address));
if (s != 0) {
    perror("bind");
    exit(1);
}
memset(&remote_address, 0, sizeof(struct sockaddr_in));
remote_address.sin_family = AF_INET;
remote_address.sin_addr.s_addr = inet_addr(remote_host);
remote_address.sin_port = htons(6799);
s = sendto(sfd,
           "MSG",
           3,
           0,
           (struct sockaddr*)&remote_address,
           sizeof(remote_address));
if (s != 3) {
    perror("sento");
    exit(1);
}
FD_ZERO(&fds);
FD_SET(sfd, &fds);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
s = select(sfd + 1, &fds, 0, 0, &timeout);
if (s == 1) {
    char buf[512];
    printf("Got data, host is upn");
    s = recvfrom(sfd, &buf[0], 512, 0, 0, 0);
    perror("recvfrom");
} else {
    printf("Timeout, host is downn");
}

我通过使用内置的Windows PING API解决了这个问题。我把gethostbyname()改成了inet_addr

如下所示:

dllping.cpp

// Borland C++ 5.0: bcc32.cpp ping.cpp
// Visual C++ 5.0:  cl ping.cpp wsock32.lib
//
// This sample program is hereby placed in the public domain.
#include <iostream.h>
#include <winsock.h>
#include <windowsx.h>
#include "icmpdefs.h"
int doit(int argc, char* argv[])
{
    // Check for correct command-line args
    if (argc < 2) {
        cerr << "usage: ping <host>" << endl;
        return 1;
    }
    // Load the ICMP.DLL
    HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
    if (hIcmp == 0) {
        cerr << "Unable to locate ICMP.DLL!" << endl;
        return 2;
    }
    // Look up an IP address for the given host name
    struct hostent* phe;
    if ((phe = gethostbyname(argv[1])) == 0) {
        cerr << "Could not find IP address for " << argv[1] << endl;
        return 3;
    }
    // Get handles to the functions inside ICMP.DLL that we'll need
    typedef HANDLE (WINAPI* pfnHV)(VOID);
    typedef BOOL (WINAPI* pfnBH)(HANDLE);
    typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
            PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
    pfnHV pIcmpCreateFile;
    pfnBH pIcmpCloseHandle;
    pfnDHDPWPipPDD pIcmpSendEcho;
    pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp,
            "IcmpCreateFile");
    pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp,
            "IcmpCloseHandle");
    pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp,
            "IcmpSendEcho");
    if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || 
            (pIcmpSendEcho == 0)) {
        cerr << "Failed to get proc addr for function." << endl;
        return 4;
    }
    // Open the ping service
    HANDLE hIP = pIcmpCreateFile();
    if (hIP == INVALID_HANDLE_VALUE) {
        cerr << "Unable to open ping service." << endl;
        return 5;
    }
    // Build ping packet
    char acPingBuffer[64];
    memset(acPingBuffer, 'xAA', sizeof(acPingBuffer));
    PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(
            GMEM_FIXED | GMEM_ZEROINIT,
            sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
    if (pIpe == 0) {
        cerr << "Failed to allocate global ping packet buffer." << endl;
        return 6;
    }
    pIpe->Data = acPingBuffer;
    pIpe->DataSize = sizeof(acPingBuffer);      
    // Send the ping packet
    DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), 
            acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, 
            sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
    if (dwStatus != 0) {
        cout << "Addr: " <<
                int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
                int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
                int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
                int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
                "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
                "TTL: " << int(pIpe->Options.Ttl) << endl;
    }
    else {
        cerr << "Error obtaining info from ping packet." << endl;
    }
    // Shut down...
    GlobalFree(pIpe);
    FreeLibrary(hIcmp);
    return 0;
}
int main(int argc, char* argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }
    int retval = doit(argc, argv);
    WSACleanup();
    return retval;
}

icmpdefs.h

// Structures required to use functions in ICMP.DLL
typedef struct {
    unsigned char Ttl;                         // Time To Live
    unsigned char Tos;                         // Type Of Service
    unsigned char Flags;                       // IP header flags
    unsigned char OptionsSize;                 // Size in bytes of options data
    unsigned char *OptionsData;                // Pointer to options data
} IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
typedef struct {
    DWORD Address;                             // Replying address
    unsigned long  Status;                     // Reply status
    unsigned long  RoundTripTime;              // RTT in milliseconds
    unsigned short DataSize;                   // Echo data size
    unsigned short Reserved;                   // Reserved for system use
    void *Data;                                // Pointer to the echo data
    IP_OPTION_INFORMATION Options;             // Reply options
} IP_ECHO_REPLY, * PIP_ECHO_REPLY;

在这里你可以找到一个简短的c++ DNS解析器的源代码。

DNS查询不会帮助您确定盒子是否打开(这是您似乎正在尝试做的事情)。

如果你可以在目标机器上运行进程,你可以运行某种类型的心跳服务,它将接受来自监控应用程序的TCP连接,并每2.5秒发送一条"我还活着"的消息。无法连接或没有心跳会告诉你的监控应用有问题。

或者(也许更直接),为什么不使用ICMP ping呢?

如果只允许使用一定数量的临时端口,请停止使用临时端口。在使用源套接字尝试连接到其他机器之前,将其绑定到一个已知的端口号。

或者,您不说明为什么要避免ping。如果只是在代码中完成,您可以自己生成一个ICMP数据包并使用它。

最新更新