// GetProtoInfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib, "wsock32.lib")

int Printhostent(LPCSTR lpServerNameOrAddress);
void main(int argc, char **argv)
{
    WORD wVersionRequested = MAKEWORD(1,1);
    WSADATA wsaData;
    int nRC;
    if (argc != 2)
    {
        fprintf(stderr,
            "\nSyntax: GetProtoInfo ServerName_Or_IP-Address\n");
        return;
    }
    nRC = WSAStartup(wVersionRequested, &wsaData);
    if (nRC)
    {
        fprintf(stderr,"\nWSAStartup() error: %d\n", nRC); 
        WSACleanup();
        return;
    }

    if (wVersionRequested != wsaData.wVersion)
    {
        fprintf(stderr,"\nWinSock version 1.1 not supported\n");
        WSACleanup();
        return;
    }
    nRC = Printhostent(argv[1]);
    if (nRC)
        fprintf(stderr,"\nPrinthostent return code: %d\n", nRC);
    WSACleanup();
	return;
}

int Printhostent(LPCSTR lpServerNameOrAddress)
{
    LPHOSTENT lpHostEntry;     // Pointer to host entry structure
    struct in_addr iaHost;     // Internet address structure
    struct in_addr *pinAddr;   // Pointer to an internet address
    LPSTR lpAlias;             // Character pointer for alias names
    int iNdx;
    iaHost.s_addr = inet_addr(lpServerNameOrAddress);
    if (iaHost.s_addr == INADDR_NONE)
    {
        lpHostEntry = gethostbyname(lpServerNameOrAddress);
    }
    else
    {
        lpHostEntry = gethostbyaddr((const char *)&iaHost, 
                        sizeof(struct in_addr), AF_INET);
    }

    if (lpHostEntry == NULL)
    {
        fprintf(stderr,"\nError getting host: %d",
                 WSAGetLastError());
        return WSAGetLastError();
    }
    printf("\n\nHOSTENT");
    printf("\n-----------------");
    printf("\nHost Name........: %s", lpHostEntry->h_name);
    printf("\nHost Aliases.....");
    for (iNdx = 0; ; iNdx++)
    {
        lpAlias = lpHostEntry->h_aliases[iNdx];
        if (lpAlias == NULL)
            break;
        printf(": %s", lpAlias);
        printf("\n                 ");
    }
    printf("\nAddress type.....: %d", lpHostEntry->h_addrtype);
    if (lpHostEntry->h_addrtype == AF_INET)
        printf(" (AF_INET)");
    else
        printf(" (UnknownType)");
    printf("\nAddress length...: %d", lpHostEntry->h_length);
    printf("\nIP Addresses.....");
    for (iNdx = 0; ; iNdx++)
    {
        pinAddr = ((LPIN_ADDR)lpHostEntry->h_addr_list[iNdx]);
        if (pinAddr == NULL)
            break;
        printf(": %s", inet_ntoa(*pinAddr));
        printf("\n                 ");
    }
    printf("\n");
    return 0;
}
