home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume16 / log_tcp / part01 / fromhost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-10  |  1.3 KB  |  51 lines

  1.  /*
  2.   * fromhost() returns the name or address of the host at the other end of
  3.   * standard input, "stdin" if it is connected to a terminal, or a null
  4.   * pointer if it fails. Diagnostics are logged through syslog(3).
  5.   * 
  6.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  7.   */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#) fromhost.c 1.1 91/01/06 22:30:24";
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/param.h>
  16. #include <sys/socket.h>
  17. #include <syslog.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20.  
  21. /* fromhost - find out what is at the other end of standard input */
  22.  
  23. char   *fromhost()
  24. {
  25.     struct sockaddr sa;
  26.     struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  27.     struct hostent *hp;
  28.     int     sockt = fileno(stdin);
  29.     int     length = sizeof(sa);
  30.     char   *inet_ntoa();
  31.  
  32.     if (getpeername(sockt, &sa, &length) < 0) {
  33.     if (isatty(sockt)) {
  34.         return ("stdin");
  35.     } else {
  36.         syslog(LOG_ERR, "getpeername: %m");
  37.         return (0);
  38.     }
  39.     } else {
  40.     switch (sa.sa_family) {
  41.     case AF_INET:
  42.         hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
  43.                    sizeof(sin->sin_addr.s_addr), AF_INET);
  44.         return (hp ? hp->h_name : inet_ntoa(sin->sin_addr));
  45.     default:
  46.         syslog(LOG_ERR, "unknown address family %ld", sa.sa_family);
  47.         return (0);
  48.     }
  49.     }
  50. }
  51.