Jump to content
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
HP.com home
Transition solutions documentation home > Tutorial home

Example: gethost-example.c

» 

Transition solutions documentation

» HP technical documentation
» Application transition
» HP-UX to HP-UX 11i
» Linux to HP-UX 11i
» Tru64 UNIX to HP-UX 11i
» Solaris to HP-UX 11i
» Solaris to Linux
Content starts here

The original gethost-example.c file:

#include 
#include 
#include 
#include 


int main()
{
  char hostname[MAXHOSTNAMELEN];
  char addrString[512];
  struct hostent *hostPtr;
  int errorNum;

  gethostname(hostname, MAXHOSTNAMELEN);

  // If necessary, get the domain to form the canonical name.
  if (NULL==strchr(hostname, (int)'.'))
  {
    int x;

    hostPtr=gethostbyname(hostname);

    if (!strchr(hostPtr->h_name, '.'))
    {
      if (hostPtr->h_aliases)
      {
	for (x = 0; hostPtr->h_aliases[x]; ++x)
	{
	  if (strchr(hostPtr->h_aliases[x], '.') &&
	      (!strncasecmp(hostPtr->h_aliases[x], hostPtr->h_name,
			    strlen(hostPtr->h_name))))
	    strcpy(hostname, hostPtr->h_aliases[x]);
	}
      }
    }
    else
      strcpy(hostname, hostPtr->h_name);
  }

  printf("hostname is %s\n", hostname);

  hostPtr = getipnodebyname(hostname, AF_INET, AI_DEFAULT, &errorNum);
  
  if (inet_ntop(AF_INET, hostPtr->h_addr, addrString, 512))
    printf("address is %s\n", addrString);

}

Here is the gethost-example.c file after it has been edited to remove references to the getipnodebyname routine and use the getaddinfo and getnameinfo routines:

#include 
#include 
#include 
#include 


int main()
{
  char hostname[MAXHOSTNAMELEN];
  char addrString[512];
  struct hostent *hostPtr;
  int errorNum;

  gethostname(hostname, MAXHOSTNAMELEN);

  // If necessary, get the domain to form the canonical name.
  if (NULL==strchr(hostname, (int)'.'))
  {
    int x;

    hostPtr=gethostbyname(hostname);

    if (!strchr(hostPtr->h_name, '.'))
    {
      if (hostPtr->h_aliases)
      {
	for (x = 0; hostPtr->h_aliases[x]; ++x)
	{
	  if (strchr(hostPtr->h_aliases[x], '.') &&
	      (!strncasecmp(hostPtr->h_aliases[x], hostPtr->h_name,
			    strlen(hostPtr->h_name))))
	    strcpy(hostname, hostPtr->h_aliases[x]);
	}
      }
    }
    else
      strcpy(hostname, hostPtr->h_name);
  }

  printf("hostname is %s\n", hostname);

  // hostPtr = getipnodebyname(hostname, AF_INET, AI_DEFAULT, &errorNum);
  {
    struct addrinfo *ai;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET; /* IPv4 address family */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_DEFAULT;

    if (getaddrinfo(hostname, NULL, &hints, &ai))
    {
      printf("getaddrinfo() failed\n");
    }
    else
    {
      //if (inet_ntop(ai->ai_family, ai->ai_addr, addrString, 512))

      if (0==getnameinfo(ai->ai_addr, sizeof(struct sockaddr),
	addrString, 512, NULL, 0, NI_NUMERICHOST))
	printf("address is %s\n", addrString);
    }
    freeaddrinfo(ai);
  }

}

» Return to Porting example

Privacy statement Using this site means you accept its terms Feedback to Transition Solutions Documentation
© 2006 Hewlett-Packard Development Company, L.P.