Back to index

glibc  2.9
tst-gethnm.c
Go to the documentation of this file.
00001 /* Based on a test case by grd@algonet.se.  */
00002 
00003 #include <netdb.h>
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 #include <sys/param.h>
00009 #include <sys/socket.h>
00010 #include <netinet/in.h>
00011 #include <arpa/inet.h>
00012 
00013 int
00014 main (void)
00015 {
00016   struct hostent *ent;
00017   struct in_addr hostaddr;
00018   int result = 0;
00019 
00020   inet_aton ("127.0.0.1", (struct in_addr *) &hostaddr.s_addr);
00021   ent = gethostbyaddr (&hostaddr, sizeof (hostaddr), AF_INET);
00022   if (ent == NULL)
00023     puts ("gethostbyaddr (...) == NULL");
00024   else
00025     {
00026       puts ("Using gethostbyaddr(..):");
00027       printf ("h_name: %s\n", ent->h_name);
00028 
00029       if (ent->h_aliases == NULL)
00030        puts ("ent->h_aliases == NULL");
00031       else
00032        printf ("h_aliases[0]: %s\n", ent->h_aliases[0]);
00033     }
00034 
00035   ent = gethostbyname ("127.0.0.1");
00036   if (ent == NULL)
00037     {
00038       puts ("gethostbyname (\"127.0.0.1\") == NULL");
00039       result = 1;
00040     }
00041   else
00042     {
00043       printf ("\nNow using gethostbyname(..):\n");
00044       printf ("h_name: %s\n", ent->h_name);
00045       if (strcmp (ent->h_name, "127.0.0.1") != 0)
00046        {
00047          puts ("ent->h_name != \"127.0.0.1\"");
00048          result = 1;
00049        }
00050 
00051       if (ent->h_aliases == NULL)
00052        {
00053          puts ("ent->h_aliases == NULL");
00054          result = 1;
00055        }
00056       else
00057        {
00058          printf ("h_aliases[0]: %s\n", ent->h_aliases[0]);
00059          result |= ent->h_aliases[0] != NULL;
00060        }
00061     }
00062 
00063   return result;
00064 }