home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / access_inet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  3.0 KB  |  130 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)access_inet.c    1.4    (Berkeley) 1/9/88";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. #include <stdio.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #ifndef EXCELAN
  11. #include <netdb.h>
  12. #ifndef hpux
  13. #include <arpa/inet.h>
  14. #endif
  15. #endif
  16.  
  17. /*
  18.  * inet_netnames -- return the network, subnet, and host names of
  19.  * our peer process for the Internet domain.
  20.  *
  21.  *    Parameters:    "sock" is our socket, which we don't need.
  22.  *            "sin" is a pointer to the result of
  23.  *            a getpeername() call.
  24.  *            "net_name", "subnet_name", and "host_name"
  25.  *            are filled in by this routine with the
  26.  *            corresponding ASCII names of our peer.
  27.  *    Returns:    Nothing.
  28.  *    Side effects:    None.
  29.  */
  30.  
  31. inet_netnames(sock, sin, net_name, subnet_name, host_name)
  32.     int            sock;
  33.     struct sockaddr_in    *sin;
  34.     char            *net_name;
  35.     char            *subnet_name;
  36.     char            *host_name;
  37. {
  38. #ifdef EXCELAN
  39.     excelan_netnames(sock, sin, net_name, subnet_name, host_name);
  40. #else
  41.     static int        gotsubnetconf;
  42.     u_long            net_addr;
  43.     u_long            subnet_addr;
  44.     struct hostent        *hp;
  45.     struct netent        *np;
  46.  
  47. #ifdef SUBNET
  48.     if (!gotsubnetconf) {
  49.         if (getifconf() < 0) {
  50. #ifdef SYSLOG
  51.             syslog(LOG_ERR, "host_access: getifconf: %m");
  52. #endif
  53.             return;
  54.         }
  55.         gotsubnetconf = 1;
  56.     }
  57. #endif
  58.  
  59.     net_addr = inet_netof(sin->sin_addr);    /* net_addr in host order */
  60.     np = getnetbyaddr(net_addr, AF_INET);
  61.     if (np != NULL)
  62.         (void) strcpy(net_name, np->n_name);
  63.     else
  64.         (void) strcpy(net_name,inet_ntoa(*(struct in_addr *)&net_addr));
  65.  
  66. #ifdef SUBNET
  67.     subnet_addr = inet_snetof(sin->sin_addr.s_addr);
  68.     if (subnet_addr == 0)
  69.         subnet_name[0] = '\0';
  70.     else {
  71.         np = getnetbyaddr(subnet_addr, AF_INET);
  72.         if (np != NULL)
  73.             (void) strcpy(subnet_name, np->n_name);
  74.         else
  75.             (void) strcpy(subnet_name,
  76.                 inet_ntoa(*(struct in_addr *)&subnet_addr));
  77.     }
  78. #else
  79.     subnet_name[0] = '\0';
  80. #endif SUBNET
  81.  
  82.     hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
  83.         sizeof (sin->sin_addr.s_addr), AF_INET);
  84.     if (hp != NULL)
  85.         (void) strcpy(host_name, hp->h_name);
  86.     else
  87.         (void) strcpy(host_name, inet_ntoa(sin->sin_addr));
  88. #endif
  89. }
  90.  
  91.  
  92. #ifdef EXCELAN
  93. excelan_netnames(sock, sin, net_name, subnet_name, host_name)
  94.     int            sock;
  95.     struct sockaddr_in    *sin;
  96.     char            *net_name;
  97.     char            *subnet_name;
  98.     char            *host_name;
  99. {
  100.     char *hp, *raddr();
  101.     int octet[4];
  102.  
  103.     /* assumes sizeof addr is long */
  104.     octet[0] = (sin->sin_addr.s_addr>>24)&0xff;
  105.     octet[1] = (sin->sin_addr.s_addr>>16)&0xff;
  106.     octet[2] = (sin->sin_addr.s_addr>>8 )&0xff;
  107.     octet[3] = (sin->sin_addr.s_addr    )&0xff;
  108.  
  109.     hp = raddr(sin->sin_addr.s_addr);
  110.     if (hp != NULL)
  111.         (void) strcpy(host_name,hp);
  112.     else
  113.  
  114.         (void) sprintf(host_name,"%d.%d.%d.%d",
  115.             octet[0],octet[1],octet[2],octet[3]);    
  116.     /* No inet* routines here, so we fake it. */
  117.     if (octet[0] < 128)        /* CLASS A address */
  118.         (void) sprintf(net_name,"%d",octet[0]);
  119.     else if(octet[0] < 192)        /* CLASS B address */
  120.             (void) sprintf(net_name,"%d.%d",
  121.                         octet[0],octet[1]);
  122.         else             /* CLASS C address */
  123.             (void) sprintf(net_name,"%d.%d.%d",
  124.                     octet[0],octet[1],octet[2]);
  125.  
  126. /* hack to cover the subnet stuff */
  127.     (void) sprintf(subnet_name,"%d.%d.%d",octet[0],octet[1],octet[2]);
  128. }
  129. #endif
  130.