home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / ethertop / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  1.8 KB  |  78 lines

  1. /*
  2.  * Copyright (c) 1991 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Irvine.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netdb.h>
  21. #include <string.h>
  22. #include "ethertop.h"
  23.  
  24.  
  25. static host_ip_ent *hash_table[H2IP_HASH_SIZE];
  26.  
  27. void init_gethostnamebyip()
  28. {
  29.   register int i;
  30.   for(i=0; i<H2IP_HASH_SIZE; i++)
  31.     hash_table[i]=NULL;
  32. }
  33.  
  34.  
  35. char *gethostnamebyip(h_addr)
  36.      int h_addr;
  37. {
  38.   register host_ip_ent *ptr;
  39.   struct hostent *host;
  40.   host_ip_ent *temp;
  41.   unsigned int index;
  42.   if (displayip)
  43.     {
  44.       return(inet_ntoa(&h_addr));
  45.     }
  46.   index=((unsigned int)h_addr & H2IP_HASH_MASK);
  47.  
  48.   for (ptr=hash_table[index] ; ptr!=NULL; ptr=ptr->h_nxt )
  49.     {
  50.       if (ptr->h_addr==h_addr)
  51.     {
  52.       return(ptr->h_name);
  53.     }
  54.     }
  55.   temp=hash_table[index];
  56.   ptr=hash_table[index]=(host_ip_ent *)malloc(sizeof(host_ip_ent));
  57.   ptr->h_nxt=temp;
  58.   ptr->h_addr=h_addr;
  59.   if ((host=gethostbyaddr(&h_addr,sizeof(int),AF_INET))!=NULL)
  60.     {
  61.       ptr->h_name=strdup(host->h_name);
  62.     }
  63.   else
  64.     {
  65.       ptr->h_name=strdup(inet_ntoa(&h_addr));
  66.     }
  67.   return(ptr->h_name);
  68. }
  69.  
  70.  
  71.  
  72.   
  73.   
  74.  
  75.  
  76.  
  77.  
  78.