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

时间:2011-04-07 14:11:12

标签: c++ tcp winsock

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

在尝试连接之前是否有类似的功能(除ping之外)确定IP是否有效?

5 个答案:

答案 0 :(得分:2)

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

您可以通过创建SOCK_DGRAM套接字,绑定到本地端口,并调用sendto()发送到未侦听的已知远程端口来完成此操作。然后,您可以轮询并调用recvfrom(),如果您的主机收到ICMP响应,则应该给出错误。如果主机没有启动,那么您将无法获得响应。您可以使用相同的端口重用相同的套接字,以定期发送所需数量的数据报。

发送ICMP echo请求需要大多数系统的高权限,因此很难直接从您的代码中执行。

以下是一些示例代码,大致与我的描述相同:

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 up\n");

    s = recvfrom(sfd, &buf[0], 512, 0, 0, 0);

    perror("recvfrom");
} else {
    printf("Timeout, host is down\n");
}

答案 1 :(得分:1)

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

显示在此处:ICMP.DLL Method

<强> 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;

答案 2 :(得分:0)

在这里,您可以在C ++中找到short DNS resolver的来源。

答案 3 :(得分:0)

DNS查询不会帮助您确定框是否已启动(这似乎是您尝试做的事情)。

如果您可以在目标框上运行进程,则可以运行某种心跳服务,该服务将接受来自监控应用程序的TCP连接,并每2.5秒发送一次“我活着”消息。无法连接或缺少心跳会告诉您的监控应用程序存在问题。

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

答案 4 :(得分:0)

如果您只允许一定数量的临时端口,请停止使用临时端口。将源套接字绑定到已知端口号,然后再使用它尝试连接到另一台机器。

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