News: Windows Sockets gethostbyaddr() doesn't work

From Web (11.10.1996)

Question

Re: Windows Sockets' gethostbyaddr() doesn't work

----------------------------------------------------------
From           kahn@lgk.com (Larry Kahn)
Organization   Dynamics Research Corp.
Date           Thu, 10 Oct 1996 01:55:40 GMT
Newsgroups     microsoft.public.win95.networking,
               comp.os.ms-windows.networking.tcp-ip
Message-ID     <a97cc$143728.6d@p6dnf.drcoffsite.com>
References     1
----------------------------------------------------------

In article <325B5AD9.1093@vtools.es>, jlanyo@vtools.es says...
>
>Hi all.
>I have developed a DLL to cover IP net communications using Windows
>Sockets. Up to date, I have not any problem, until I have try to use
>gethostbyaddr() function. This always returns 0 and I'm not able to find
>out what's going on. The error code returned by WSAGetLastError is 11004
>(Valid name, no data record of requested type...). I have tried to use
>the async version of this function, and the result is the same.
>I'm sure the parameters are well, a pointer to an address in network
>byte order, its length (4 bytes), and PF_INET.
>
>Could somebody help me?
>Thanks in advance.
>
>

include <stdio.h>
#include <stdlib.h>
#include <winsock.h>

void disp_help()
{
  printf("\nGet host name program V 1.1 by L. Kahn (C) 1995\n");
  printf("_______________________________________________\n\n");
  printf("Syntax: ghostname hostname \n");
  printf("WHERE: hostname is the name or ip address of a host.\n\n");
}

void main(argc,argv)
int argc;
char **argv;
{
  WORD wVersionrequired;
  WSADATA WSData;
  char remotename[80];
  static struct sockaddr_in destination;
  static struct hostent *ptrhostentry;
  LPSTR plpstr;
  struct in_addr *pin_addr;
  int rval = 0;

  wVersionrequired = MAKEWORD(1,1);
  if (argc != 2)
    disp_help();
  else
  {
    /* ok assume second arg is host name */
    rval = WSAStartup(wVersionrequired, &WSData);
    if (rval != 0)
    {
      printf("Startup ERROR: could not find a usable a winsock DLL.\n");
      fflush(stdout);
      exit(1);
    }

    if (LOBYTE(WSData.wVersion) != 1 || HIBYTE(WSData.wVersion) != 1)
    {
      printf("Startup ERROR: could not find a winsock ver 1.1 or newer.\n");
      fflush(stdout);
      exit(1);
    }
    else
    {
      strcpy(remotename,argv[1]);
      memset(&destination,0,sizeof(struct sockaddr));
      destination.sin_family=AF_INET;

      destination.sin_addr.s_addr = inet_addr(remotename);
      if(destination.sin_addr.s_addr ==INADDR_NONE)
      {
        ptrhostentry = gethostbyname(remotename);
        if (ptrhostentry == NULL)
        {
          printf("can't get entry for host name: %s\n",remotename);
          fflush(stdout);
          WSACleanup();
          exit(1);
        }
      }
      else
      {
        ptrhostentry=gethostbyaddr(
          (LPSTR)&destination.sin_addr.s_addr,4,PF_INET
        );
        if (ptrhostentry == NULL)
        {
          printf("can't get ip entry for host name: %s\n",remotename);
          fflush(stdout);
          WSACleanup();
          exit(1);
        }
      }

      /* if we get here we have valid entry so do it */
      memcpy(
        &destination.sin_addr,ptrhostentry->h_addr,ptrhostentry->h_length
      );
      printf("Translated Name is: %s\n",ptrhostentry->h_name);

      while((pin_addr=(struct in_addr *)*(ptrhostentry->h_addr_list))!=NULL)
      {
        printf("IP Address is     : %s\n",inet_ntoa(*pin_addr));
        ptrhostentry->h_addr_list++;
      }

      while((plpstr=(LPSTR)*(ptrhostentry->h_aliases))!=NULL)
      {
        printf("Alias: %s\n",plpstr);
        ptrhostentry->h_aliases++;
      }
    } /* end of ok startup */
  } /* end of ok parameters */

  printf("\n");
  fflush(stdout);
  WSACleanup();

} /* end of main */

>--
>Jose Luis Anyonuevo |    Visual Tools S.A.    |  Tel: +34 1 5352642
>Software Engineer   |   Video Surveillance    |  Fax: +34 1 5353695
>                    |      Almansa 62         |
>jlanyo@vtools.es    |  28039 Madrid (SPAIN)   |  sales: nacho@vtools.es
>

--
_________________________________________________________________________
Larry Kahn            __    __    __    __       Senior Software Engineer
                     /  \  /  \  /  \  /  \       Dynamics Research Corp.
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
kahn@lgk.com      /  / \   / \   / \   / \  \____     kahn@drcoffsite.com
                  \_/   \_/   \_/   \_/   \    o \
                                           \_____/--<
                    (TALK: larry@ambra.drcoffsite.com)
         (FINGER: .site@ambra.drcoffsite.com for PGP public key)
_________________________________________________________________________