home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xauth / gethost.c next >
Encoding:
C/C++ Source or Header  |  1991-07-26  |  6.5 KB  |  264 lines

  1. /*
  2.  * $XConsortium: gethost.c,v 1.14 91/07/26 19:54:39 keith Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Jim Fulton, MIT X Consortium
  24.  */
  25.  
  26. /* sorry, streams support does not really work yet */
  27. #if defined(STREAMSCONN) && defined(SVR4)
  28. #undef STREAMSCONN
  29. #define TCPCONN
  30. #endif
  31.  
  32. #include "xauth.h"
  33. #include <X11/X.h>
  34. #include <signal.h>
  35. #include <setjmp.h>
  36. #include <ctype.h>
  37. #ifndef __TYPES__
  38. #include <sys/types.h>
  39. #define __TYPES__
  40. #endif
  41. #ifndef STREAMSCONN
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <netinet/in.h>
  45. #ifdef SYSV386
  46. #ifndef SVR4
  47. #ifndef SCO
  48. #include <net/errno.h>
  49. #endif /* !SCO */
  50. #endif /* !SVR4 */
  51. #endif /* SYSV386 */
  52. #endif /* !STREAMSCONN */
  53. #include <errno.h>
  54. extern int errno;            /* for stupid errno.h files */
  55. #ifdef DNETCONN
  56. #include <netdnet/dn.h>
  57. #include <netdnet/dnetdb.h>
  58. #endif
  59.  
  60. Bool nameserver_timedout = False;
  61.  
  62.  
  63. /*
  64.  * get_hostname - Given an internet address, return a name (CHARON.MIT.EDU)
  65.  * or a string representing the address (18.58.0.13) if the name cannot
  66.  * be found.  Stolen from xhost.
  67.  */
  68.  
  69. static jmp_buf env;
  70. static 
  71. #ifdef SIGNALRETURNSINT
  72. int
  73. #else
  74. void
  75. #endif
  76. nameserver_lost(sig)
  77. {
  78.   nameserver_timedout = True;
  79.   longjmp (env, -1);
  80.   /* NOTREACHED */
  81. #ifdef SIGNALRETURNSINT
  82.   return -1;                /* for picky compilers */
  83. #endif
  84. }
  85.  
  86.  
  87. char *get_hostname (auth)
  88.     Xauth *auth;
  89. {
  90.     struct hostent *hp = NULL;
  91.     char *inet_ntoa();
  92. #ifdef DNETCONN
  93.     struct nodeent *np;
  94.     static char nodeaddr[16];
  95. #endif /* DNETCONN */
  96.  
  97.     if (auth->address_length == 0)
  98.     return "Illegal Address";
  99. #ifdef TCPCONN
  100.     if (auth->family == FamilyInternet) {
  101.     /* gethostbyaddr can take a LONG time if the host does not exist.
  102.        Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
  103.        that something is wrong and do not make the user wait.
  104.        gethostbyaddr will continue after a signal, so we have to
  105.        jump out of it. 
  106.        */
  107.     nameserver_timedout = False;
  108.     signal (SIGALRM, nameserver_lost);
  109.     alarm (4);
  110.     if (setjmp(env) == 0) {
  111.         hp = gethostbyaddr (auth->address, auth->address_length, AF_INET);
  112.     }
  113.     alarm (0);
  114.     if (hp)
  115.       return (hp->h_name);
  116.     else
  117.       return (inet_ntoa(*((struct in_addr *)(auth->address))));
  118.     }
  119. #endif
  120. #ifdef DNETCONN
  121.     if (auth->family == FamilyDECnet) {
  122.     if (np = getnodebyaddr(auth->address, auth->address_length,
  123.                    AF_DECnet)) {
  124.         sprintf(nodeaddr, "%s:", np->n_name);
  125.     } else {
  126.         sprintf(nodeaddr, "%s:", dnet_htoa(auth->address));
  127.     }
  128.     return(nodeaddr);
  129.     }
  130. #endif
  131.  
  132.     return (NULL);
  133. }
  134.  
  135. #ifdef TCPCONN
  136. /*
  137.  * cribbed from lib/X/XConnDis.c
  138.  */
  139. static Bool get_inet_address (name, resultp)
  140.     char *name;
  141.     unsigned long *resultp;        /* return */
  142. {
  143.     unsigned long hostinetaddr = inet_addr (name);
  144.     struct hostent *host_ptr;
  145.     struct sockaddr_in inaddr;        /* dummy variable for size calcs */
  146.  
  147.     if (hostinetaddr == -1) {        /* oh, gross.... */
  148.     if ((host_ptr = gethostbyname (name)) == NULL) {
  149.         /* No such host! */
  150.         errno = EINVAL;
  151.         return False;
  152.     }
  153.     /* Check the address type for an internet host. */
  154.     if (host_ptr->h_addrtype != AF_INET) {
  155.         /* Not an Internet host! */
  156.         errno = EPROTOTYPE;
  157.         return False;
  158.     }
  159.  
  160.     bcopy((char *)host_ptr->h_addr, (char *)&hostinetaddr,
  161.           sizeof(inaddr.sin_addr));
  162.     }
  163.     *resultp = hostinetaddr;
  164.     return True;
  165. }
  166. #endif
  167.  
  168. #ifdef DNETCONN
  169. static Bool get_dnet_address (name, resultp)
  170.     char *name;
  171.     struct dn_naddr *resultp;
  172. {
  173.     struct dn_naddr *dnaddrp, dnaddr;
  174.     struct nodeent *np;
  175.  
  176.     if (dnaddrp = dnet_addr (name)) {    /* stolen from xhost */
  177.     dnaddr = *dnaddrp;
  178.     } else {
  179.     if ((np = getnodebyname (name)) == NULL) return False;
  180.     dnaddr.a_len = np->n_length;
  181.     bcopy (np->n_addr, dnaddr.a_addr, np->n_length);
  182.     }
  183.     *resultp = dnaddr;
  184.     return True;
  185. }
  186. #endif
  187.  
  188. char *get_address_info (family, fulldpyname, prefix, host, lenp)
  189.     int family;
  190.     char *fulldpyname;
  191.     int prefix;
  192.     char *host;
  193.     int *lenp;
  194. {
  195.     char *retval = NULL;
  196.     int len = 0;
  197.     char *src = NULL;
  198. #ifdef TCPCONN
  199.     unsigned long hostinetaddr;
  200.     struct sockaddr_in inaddr;        /* dummy variable for size calcs */
  201. #endif
  202. #ifdef DNETCONN
  203.     struct dn_naddr dnaddr;
  204. #endif
  205.     char buf[255];
  206.  
  207.     /*
  208.      * based on the family, set the pointer src to the start of the address
  209.      * information to be copied and set len to the number of bytes.
  210.      */
  211.     switch (family) {
  212.       case FamilyLocal:            /* hostname/unix:0 */
  213.                     /* handle unix:0 and :0 specially */
  214.     if (prefix == 0 && (strncmp (fulldpyname, "unix:", 5) == 0 ||
  215.                 fulldpyname[0] == ':')) {
  216.         extern char *get_local_hostname();
  217.  
  218.         if (!get_local_hostname (buf, sizeof buf)) {
  219.         len = 0;
  220.         } else {
  221.         src = buf;
  222.         len = strlen (buf);
  223.         }
  224.     } else {
  225.         src = fulldpyname;
  226.         len = prefix;
  227.     }
  228.     break;
  229.       case FamilyInternet:        /* host:0 */
  230. #ifdef TCPCONN
  231.     if (!get_inet_address (host, &hostinetaddr)) return NULL;
  232.     src = (char *) &hostinetaddr;
  233.     len = 4; /* sizeof inaddr.sin_addr, would fail on Cray */
  234.     break;
  235. #else
  236.     return NULL;
  237. #endif
  238.       case FamilyDECnet:        /* host::0 */
  239. #ifdef DNETCONN
  240.     if (!get_dnet_address (host, &dnaddr)) return NULL;
  241.     src = (char *) &dnaddr;
  242.     len = (sizeof dnaddr);
  243.     break;
  244. #else
  245.     /* fall through since we don't have code for it */
  246. #endif
  247.       default:
  248.     src = NULL;
  249.     len = 0;
  250.     }
  251.  
  252.     /*
  253.      * if source was provided, allocate space and copy it
  254.      */
  255.     if (len == 0 || !src) return NULL;
  256.  
  257.     retval = malloc (len);
  258.     if (retval) {
  259.     bcopy (src, retval, len);
  260.     *lenp = len;
  261.     }
  262.     return retval;
  263. }
  264.