home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / osdep / amiga / tcp_ami.c < prev    next >
C/C++ Source or Header  |  1998-10-25  |  20KB  |  645 lines

  1. /*
  2.  * Program:    Amiga TCP/IP routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    1 August 1988
  13.  * Last Edited:    25 October 1998
  14.  *
  15.  * Copyright 1998 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made available
  24.  * "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #undef write            /* don't use redefined write() */
  37.  
  38. static tcptimeout_t tmoh = NIL;    /* TCP timeout handler routine */
  39. static long ttmo_open = 0;    /* TCP timeouts, in seconds */
  40. static long ttmo_read = 0;
  41. static long ttmo_write = 0;
  42. static long alarmsave = 0;    /* save alarms */
  43.  
  44.  
  45. /* Local function prototypes */
  46.  
  47. int tcp_socket_open (struct sockaddr_in *sin,char *tmp,int *ctr,char *hst,
  48.              unsigned long port);
  49. long tcp_abort (TCPSTREAM *stream);
  50.  
  51. /* TCP/IP manipulate parameters
  52.  * Accepts: function code
  53.  *        function-dependent value
  54.  * Returns: function-dependent return value
  55.  */
  56.  
  57. void *tcp_parameters (long function,void *value)
  58. {
  59.   switch ((int) function) {
  60.   case SET_TIMEOUT:
  61.     tmoh = (tcptimeout_t) value;
  62.     break;
  63.   case GET_TIMEOUT:
  64.     value = (void *) tmoh;
  65.     break;
  66.   case SET_OPENTIMEOUT:
  67.     ttmo_open = (long) value;
  68.     break;
  69.   case GET_OPENTIMEOUT:
  70.     value = (void *) ttmo_open;
  71.     break;
  72.   case SET_READTIMEOUT:
  73.     ttmo_read = (long) value;
  74.     break;
  75.   case GET_READTIMEOUT:
  76.     value = (void *) ttmo_read;
  77.     break;
  78.   case SET_WRITETIMEOUT:
  79.     ttmo_write = (long) value;
  80.     break;
  81.   case GET_WRITETIMEOUT:
  82.     value = (void *) ttmo_write;
  83.     break;
  84.   case SET_ALARMSAVE:
  85.     alarmsave = (long) value;
  86.     break;
  87.   case GET_ALARMSAVE:
  88.     value = (void *) alarmsave;
  89.     break;
  90.   default:
  91.     value = NIL;        /* error case */
  92.     break;
  93.   }
  94.   return value;
  95. }
  96.  
  97. /* TCP/IP open
  98.  * Accepts: host name
  99.  *        contact service name
  100.  *        contact port number
  101.  * Returns: TCP/IP stream if success else NIL
  102.  */
  103.  
  104. TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
  105. {
  106.   TCPSTREAM *stream = NIL;
  107.   int i,j,sock;
  108.   int ctr = 0;
  109.   char *s;
  110.   struct sockaddr_in sin;
  111.   struct hostent *he;
  112.   char hostname[MAILTMPLEN];
  113.   char tmp[MAILTMPLEN];
  114.   struct servent *sv = NIL;
  115.   if (service) {        /* service specified? */
  116.     if (*service == '*') {    /* yes, special alt driver kludge? */
  117.       ctrp = NIL;        /* yes, don't do open timeout */
  118.       sv = getservbyname (service + 1,"tcp");
  119.     }
  120.     else sv = getservbyname (service,"tcp");
  121.   }
  122.                 /* user service name port */
  123.   if (sv) port = ntohs (sin.sin_port = sv->s_port);
  124.                  /* copy port number in network format */
  125.   else sin.sin_port = htons (port);
  126.   /* The domain literal form is used (rather than simply the dotted decimal
  127.      as with other Unix programs) because it has to be a valid "host name"
  128.      in mailsystem terminology. */
  129.                 /* look like domain literal? */
  130.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  131.     strcpy (hostname,host+1);    /* yes, copy number part */
  132.     hostname[(strlen (hostname))-1] = '\0';
  133.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  134.       sin.sin_family = AF_INET;    /* family is always Internet */
  135.       strcpy (hostname,host);    /* hostname is user's argument */
  136.                 /* get an open socket for this system */
  137.       sock = tcp_socket_open (&sin,tmp,&ctr,hostname,port);
  138.     }
  139.     else {
  140.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  141.       mm_log (tmp,ERROR);
  142.       return NIL;
  143.     }
  144.   }
  145.  
  146.   else {            /* lookup host name */
  147.                 /* quell alarms */
  148.     i = alarmsave ? (int) max (alarm (0),1) : 0;
  149.     if (he = gethostbyname (lcase (strcpy (hostname,host)))) {
  150.       if (i) alarm (i);        /* restore alarms */
  151.                 /* copy address type */
  152.       sin.sin_family = he->h_addrtype;
  153.                 /* copy host name */
  154.       strcpy (hostname,he->h_name);
  155.       for (sock = -1,i = 0; (sock < 0) && (s = he->h_addr_list[i]); i++) {
  156.     if (i) mm_log (tmp,WARN);
  157.     memcpy (&sin.sin_addr,s,he->h_length);
  158.     sock = tcp_socket_open (&sin,tmp,&ctr,hostname,port);
  159.       }
  160.     }
  161.     else {
  162.       if (i) alarm (i);        /* restore alarms */
  163.       sprintf (tmp,"No such host as %.80s",host);
  164.       mm_log (tmp,ERROR);
  165.       return NIL;
  166.     }
  167.   }
  168.   if (sock < 0) mm_log (tmp,ERROR);
  169.   else {            /* won */
  170.     stream = (TCPSTREAM *) memset (fs_get (sizeof (TCPSTREAM)),0,
  171.                    sizeof (TCPSTREAM));
  172.     stream->port = port;    /* port number */
  173.                 /* init sockets */
  174.     stream->tcpsi = stream->tcpso = sock;
  175.                 /* stash in the snuck-in byte */
  176.     if (stream->ictr = ctr) *(stream->iptr = stream->ibuf) = tmp[0];
  177.                 /* copy official host name */
  178.     stream->host = cpystr (hostname);
  179.   }
  180.   return stream;        /* return success */
  181. }
  182.  
  183. /* Open a TCP socket
  184.  * Accepts: Internet socket address block
  185.  *        scratch buffer
  186.  *        pointer to "first byte read in" storage
  187.  *        host name for error message
  188.  *        port number for error message
  189.  * Returns: socket if success, else -1 with error string in scratch buffer
  190.  */
  191.  
  192. int tcp_socket_open (struct sockaddr_in *sin,char *tmp,int *ctr,char *hst,
  193.              unsigned long port)
  194. {
  195.   int i,sock,flgs;
  196.   struct protoent *pt = getprotobyname ("ip");
  197.   fd_set fds,efds;
  198.   struct timeval tmo;
  199.   sprintf (tmp,"Trying IP address [%s]",inet_ntoa (sin->sin_addr));
  200.   mm_log (tmp,NIL);
  201.                 /* make a socket */
  202.   if ((sock = socket (sin->sin_family,SOCK_STREAM,pt ? pt->p_proto : 0)) < 0) {
  203.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  204.     return -1;
  205.   }
  206.   flgs = fcntl (sock,F_GETFL,0);/* get current socket flags */
  207.   fcntl (sock,F_SETFL,flgs | FNDELAY);
  208.                 /* open connection */
  209.   while ((i = connect (sock,(struct sockaddr *) sin,
  210.                sizeof (struct sockaddr_in))) < 0 && errno == EINTR);
  211.   if (i < 0) switch (errno) {    /* failed? */
  212.   case EAGAIN:            /* DG brain damage */
  213.   case EINPROGRESS:        /* what we expect to happen */
  214.   case EISCONN:            /* restart after interrupt? */
  215.   case EADDRINUSE:        /* restart after interrupt? */
  216.     break;            /* well, not really, it was interrupted */
  217.   default:
  218.     i = errno;            /* make sure close() doesn't stomp it */
  219.     close (sock);        /* flush socket */
  220.     errno = i;
  221.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hst,port,strerror (errno));
  222.     return -1;
  223.   }
  224.  
  225.   tmo.tv_sec = ttmo_open;    /* open timeout */
  226.   tmo.tv_usec = 0;
  227.   FD_ZERO (&fds);        /* initialize selection vector */
  228.   FD_ZERO (&efds);        /* handle errors too */
  229.   FD_SET (sock,&fds);        /* block for error or writeable */
  230.   FD_SET (sock,&efds);
  231.                 /* block under timeout */
  232.   while (((i = select (sock+1,0,&fds,&efds,ttmo_open ? &tmo : 0)) < 0) &&
  233.      (errno == EINTR));
  234.   if (i > 0) {            /* success, make sure really connected */
  235.     fcntl (sock,F_SETFL,flgs);    /* restore blocking status */
  236.     /* This used to be a zero-byte read(), but that crashes Solaris */
  237.                 /* get socket status */
  238.     while (((i = *ctr = read (sock,tmp,1)) < 0) && (errno == EINTR));
  239.   }    
  240.   if (i <= 0) {            /* timeout or error? */
  241.     i = i ? errno : ETIMEDOUT;    /* determine error code */
  242.     close (sock);        /* flush socket */
  243.     errno = i;            /* return error code */
  244.     sprintf (tmp,"Connection failed to %.80s,%d: %s",hst,port,strerror(errno));
  245.     return -1;
  246.   }
  247.   return sock;            /* return the socket */
  248. }
  249.   
  250. /* TCP/IP authenticated open
  251.  * Accepts: host name
  252.  *        service name
  253.  *        returned user name buffer
  254.  * Returns: TCP/IP stream if success else NIL
  255.  */
  256.  
  257. TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
  258. {
  259.   return NIL;
  260. }
  261.  
  262. /* TCP/IP receive line
  263.  * Accepts: TCP/IP stream
  264.  * Returns: text line string or NIL if failure
  265.  */
  266.  
  267. char *tcp_getline (TCPSTREAM *stream)
  268. {
  269.   int n,m;
  270.   char *st,*ret,*stp;
  271.   char c = '\0';
  272.   char d;
  273.                 /* make sure have data */
  274.   if (!tcp_getdata (stream)) return NIL;
  275.   st = stream->iptr;        /* save start of string */
  276.   n = 0;            /* init string count */
  277.   while (stream->ictr--) {    /* look for end of line */
  278.     d = *stream->iptr++;    /* slurp another character */
  279.     if ((c == '\015') && (d == '\012')) {
  280.       ret = (char *) fs_get (n--);
  281.       memcpy (ret,st,n);    /* copy into a free storage string */
  282.       ret[n] = '\0';        /* tie off string with null */
  283.       return ret;
  284.     }
  285.     n++;            /* count another character searched */
  286.     c = d;            /* remember previous character */
  287.   }
  288.                 /* copy partial string from buffer */
  289.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  290.                 /* get more data from the net */
  291.   if (!tcp_getdata (stream)) fs_give ((void **) &ret);
  292.                 /* special case of newline broken by buffer */
  293.   else if ((c == '\015') && (*stream->iptr == '\012')) {
  294.     stream->iptr++;        /* eat the line feed */
  295.     stream->ictr--;
  296.     ret[n - 1] = '\0';        /* tie off string with null */
  297.   }
  298.                 /* else recurse to get remainder */
  299.   else if (st = tcp_getline (stream)) {
  300.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  301.     memcpy (ret,stp,n);        /* copy first part */
  302.     memcpy (ret + n,st,m);    /* and second part */
  303.     fs_give ((void **) &stp);    /* flush first part */
  304.     fs_give ((void **) &st);    /* flush second part */
  305.     ret[n + m] = '\0';        /* tie off string with null */
  306.   }
  307.   return ret;
  308. }
  309.  
  310. /* TCP/IP receive buffer
  311.  * Accepts: TCP/IP stream
  312.  *        size in bytes
  313.  *        buffer to read into
  314.  * Returns: T if success, NIL otherwise
  315.  */
  316.  
  317. long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
  318. {
  319.   unsigned long n;
  320.   char *bufptr = buffer;
  321.   while (size > 0) {        /* until request satisfied */
  322.     if (!tcp_getdata (stream)) return NIL;
  323.     n = min (size,stream->ictr);/* number of bytes to transfer */
  324.                 /* do the copy */
  325.     memcpy (bufptr,stream->iptr,n);
  326.     bufptr += n;        /* update pointer */
  327.     stream->iptr +=n;
  328.     size -= n;            /* update # of bytes to do */
  329.     stream->ictr -=n;
  330.   }
  331.   bufptr[0] = '\0';        /* tie off string */
  332.   return T;
  333. }
  334.  
  335. /* TCP/IP receive data
  336.  * Accepts: TCP/IP stream
  337.  * Returns: T if success, NIL otherwise
  338.  */
  339.  
  340. long tcp_getdata (TCPSTREAM *stream)
  341. {
  342.   int i;
  343.   fd_set fds,efds;
  344.   struct timeval tmo;
  345.   time_t t = time (0);
  346.   if (stream->tcpsi < 0) return NIL;
  347.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  348.     time_t tl = time (0);    /* start of request */
  349.     tmo.tv_sec = ttmo_read;    /* read timeout */
  350.     tmo.tv_usec = 0;
  351.     FD_ZERO (&fds);        /* initialize selection vector */
  352.     FD_ZERO (&efds);        /* handle errors too */
  353.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  354.     FD_SET(stream->tcpsi,&efds);/* set bit in error selection vector */
  355.     errno = NIL;        /* block and read */
  356.     while (((i = select (stream->tcpsi+1,&fds,0,&efds,ttmo_read ? &tmo : 0))<0)
  357.        && (errno == EINTR));
  358.     if (!i) {            /* timeout? */
  359.       time_t tc = time (0);
  360.       if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
  361.       else return tcp_abort (stream);
  362.     }
  363.     else if (i < 0) return tcp_abort (stream);
  364.     while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 0) &&
  365.        (errno == EINTR));
  366.     if (i < 1) return tcp_abort (stream);
  367.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  368.     stream->ictr = i;        /* set new byte count */
  369.   }
  370.   return T;
  371. }
  372.  
  373. /* TCP/IP send string as record
  374.  * Accepts: TCP/IP stream
  375.  *        string pointer
  376.  * Returns: T if success else NIL
  377.  */
  378.  
  379. long tcp_soutr (TCPSTREAM *stream,char *string)
  380. {
  381.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  382. }
  383.  
  384.  
  385. /* TCP/IP send string
  386.  * Accepts: TCP/IP stream
  387.  *        string pointer
  388.  *        byte count
  389.  * Returns: T if success else NIL
  390.  */
  391.  
  392. long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
  393. {
  394.   int i;
  395.   fd_set fds;
  396.   struct timeval tmo;
  397.   time_t t = time (0);
  398.   if (stream->tcpso < 0) return NIL;
  399.   while (size > 0) {        /* until request satisfied */
  400.     time_t tl = time (0);    /* start of request */
  401.     tmo.tv_sec = ttmo_write;    /* write timeout */
  402.     tmo.tv_usec = 0;
  403.     FD_ZERO (&fds);        /* initialize selection vector */
  404.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  405.     errno = NIL;        /* block and write */
  406.     while (((i = select (stream->tcpso+1,0,&fds,0,ttmo_write ? &tmo : 0)) < 0)
  407.        && (errno == EINTR));
  408.     if (!i) {            /* timeout? */
  409.       time_t tc = time (0);
  410.       if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
  411.       else return tcp_abort (stream);
  412.     }
  413.     else if (i < 0) return tcp_abort (stream);
  414.     while (((i = write (stream->tcpso,string,size)) < 0) && (errno == EINTR));
  415.     if (i < 0) return tcp_abort (stream);
  416.     size -= i;            /* how much we sent */
  417.     string += i;
  418.   }
  419.   return T;            /* all done */
  420. }
  421.  
  422. /* TCP/IP close
  423.  * Accepts: TCP/IP stream
  424.  */
  425.  
  426. void tcp_close (TCPSTREAM *stream)
  427. {
  428.   tcp_abort (stream);        /* nuke the stream */
  429.                 /* flush host names */
  430.   if (stream->host) fs_give ((void **) &stream->host);
  431.   if (stream->remotehost) fs_give ((void **) &stream->remotehost);
  432.   if (stream->localhost) fs_give ((void **) &stream->localhost);
  433.   fs_give ((void **) &stream);    /* flush the stream */
  434. }
  435.  
  436.  
  437. /* TCP/IP abort stream
  438.  * Accepts: TCP/IP stream
  439.  * Returns: NIL always
  440.  */
  441.  
  442. long tcp_abort (TCPSTREAM *stream)
  443. {
  444.   int i;
  445.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  446.     close (stream->tcpsi);    /* nuke the socket */
  447.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  448.     stream->tcpsi = stream->tcpso = -1;
  449.   }
  450.   return NIL;
  451. }
  452.  
  453. /* TCP/IP get host name
  454.  * Accepts: TCP/IP stream
  455.  * Returns: host name for this stream
  456.  */
  457.  
  458. char *tcp_host (TCPSTREAM *stream)
  459. {
  460.   return stream->host;        /* use tcp_remotehost() if want guarantees */
  461. }
  462.  
  463.  
  464. /* TCP/IP get remote host name
  465.  * Accepts: TCP/IP stream
  466.  * Returns: host name for this stream
  467.  */
  468.  
  469. char *tcp_remotehost (TCPSTREAM *stream)
  470. {
  471.   if (!stream->remotehost) {
  472.     char *s,tmp[MAILTMPLEN];
  473.     struct hostent *he;
  474.     struct sockaddr_in sin;
  475.     int sinlen = sizeof (struct sockaddr_in);
  476.     if (getpeername (stream->tcpsi,(struct sockaddr *) &sin,&sinlen))
  477.       s = stream->host;
  478. #ifndef DISABLE_REVERSE_DNS_LOOKUP
  479.     /* Guarantees that the client will have the same string as the server does
  480.      * from calling tcp_serverhost ().
  481.      */
  482.     else if (he = gethostbyaddr ((char *) &sin.sin_addr,
  483.                  sizeof (struct in_addr),sin.sin_family))
  484.       s = he->h_name;
  485. #else
  486.   /* Not recommended.  In any mechanism (e.g. Kerberos) in which both client
  487.    * and server must agree on the name of the server system, this may cause
  488.    * the client to have a different idea of the server's name from the server.
  489.    * This is particularly important in those cases where a server has multiple
  490.    * CNAMEs; the gethostbyaddr() will canonicalize the name to the proper IP
  491.    * address.
  492.    */
  493. #endif
  494.     else sprintf (s = tmp,"[%s]",inet_ntoa (sin.sin_addr));
  495.     stream->remotehost = cpystr (s);
  496.   }
  497.   return stream->remotehost;
  498. }
  499.  
  500. /* TCP/IP return port for this stream
  501.  * Accepts: TCP/IP stream
  502.  * Returns: port number for this stream
  503.  */
  504.  
  505. unsigned long tcp_port (TCPSTREAM *stream)
  506. {
  507.   return stream->port;        /* return port number */
  508. }
  509.  
  510.  
  511. /* TCP/IP get local host name
  512.  * Accepts: TCP/IP stream
  513.  * Returns: local host name
  514.  */
  515.  
  516. char *tcp_localhost (TCPSTREAM *stream)
  517. {
  518.   if (!stream->localhost) {
  519.     char *s,tmp[MAILTMPLEN];
  520.     struct hostent *he;
  521.     struct sockaddr_in sin;
  522.     int sinlen = sizeof (struct sockaddr_in);
  523.                 /* get socket address */
  524.     if ((stream->port & 0xffff000) ||
  525.     getsockname (stream->tcpsi,(struct sockaddr *) &sin,&sinlen))
  526.       s = mylocalhost ();    /* not a socket or failed, use my name */
  527. #ifndef DISABLE_REVERSE_DNS_LOOKUP
  528.     /* Guarantees that the client will have the same string as the server will
  529.      * get in doing a reverse DNS lookup on the client's IP address.
  530.      */
  531.                 /* translate socket address to name */
  532.     else if (he = gethostbyaddr ((char *) &sin.sin_addr,
  533.                  sizeof (struct in_addr),sin.sin_family)) 
  534.       s = he->h_name;
  535. #else
  536.     /* Not recommended.  In any mechanism (e.g. SMTP or NNTP) in which both
  537.      * client and server must agree on the name of the client system, this may
  538.      * cause the client to use the wrong name.
  539.      */
  540. #endif
  541.     else sprintf (s = tmp,"[%s]",inet_ntoa (sin.sin_addr));
  542.     stream->localhost = cpystr (s);
  543.   }
  544.   return stream->localhost;    /* return local host name */
  545. }
  546.  
  547. /* TCP/IP get client host name (server calls only)
  548.  * Returns: client host name
  549.  */
  550.  
  551. static char *myClientHost = NIL;
  552.  
  553. char *tcp_clienthost ()
  554. {
  555.   if (!myClientHost) {
  556.     char *s,tmp[MAILTMPLEN];
  557.     struct hostent *he;
  558.     struct sockaddr_in sin;
  559.     int sinlen = sizeof (struct sockaddr_in);
  560.     if (getpeername (0,(struct sockaddr *) &sin,&sinlen)) s = "UNKNOWN";
  561. #ifndef DISABLE_REVERSE_DNS_LOOKUP
  562.     /* Includes both client name and IP address in syslog() output. */
  563.     else if (he = gethostbyaddr ((char *) &sin.sin_addr,
  564.                  sizeof (struct in_addr),sin.sin_family)) 
  565.       sprintf (s = tmp,"%s [%s]",he->h_name,inet_ntoa (sin.sin_addr));
  566. #else
  567.     /* Not recommended.  Syslog output will only have the client IP address. */
  568. #endif
  569.     else sprintf (s = tmp,"[%s]",inet_ntoa (sin.sin_addr));
  570.     myClientHost = cpystr (s);
  571.   }
  572.   return myClientHost;
  573. }
  574.  
  575. /* TCP/IP get server host name (server calls only)
  576.  * Returns: server host name
  577.  */
  578.  
  579. static char *myServerHost = NIL;
  580. static long myServerPort = -1;
  581.  
  582. char *tcp_serverhost ()
  583. {
  584.   if (!myServerHost) {
  585.     char *s,tmp[MAILTMPLEN];
  586.     struct hostent *he;
  587.     struct sockaddr_in sin;
  588.     int sinlen = sizeof (struct sockaddr_in);
  589.                 /* get socket address */
  590.     if (getsockname (0,(struct sockaddr *) &sin,(void *) &sinlen))
  591.       s = mylocalhost ();
  592.     else {
  593. #ifndef DISABLE_REVERSE_DNS_LOOKUP
  594.       myServerPort = ntohs (sin.sin_port);
  595.       /* Guarantees that the server will have the same string as the client
  596.        * does from calling tcp_remotehost ().
  597.        */
  598.       if (he = gethostbyaddr ((char *) &sin.sin_addr,
  599.                   sizeof (struct in_addr),sin.sin_family))
  600.     s = he->h_name;
  601.       else
  602. #else
  603.       /* Not recommended.  In any mechanism (e.g. Kerberos) in which both
  604.        * client and server must agree on the name of the server system, this
  605.        * may cause a spurious mismatch.  This is particularly important when
  606.        * multiple server systems are co-located on the same CPU with different
  607.        * IP addresses; the gethostbyaddr() call will return the name of the
  608.        * proper server system name and avoid canonicalizing it to a default
  609.        * name.
  610.        */
  611. #endif
  612.       sprintf (s = tmp,"[%s]",inet_ntoa (sin.sin_addr));
  613.     }
  614.     myServerHost = cpystr (s);
  615.   }
  616.   return myServerHost;
  617. }
  618.  
  619.  
  620. /* TCP/IP get server port number (server calls only)
  621.  * Returns: server port number
  622.  */
  623.  
  624. long tcp_serverport ()
  625. {
  626.   if (!myServerHost) tcp_serverhost ();
  627.   return myServerPort;
  628. }
  629.  
  630. /* TCP/IP return canonical form of host name
  631.  * Accepts: host name
  632.  * Returns: canonical form of host name
  633.  */
  634.  
  635. char *tcp_canonical (char *name)
  636. {
  637.   char host[MAILTMPLEN];
  638.   struct hostent *he;
  639.                 /* look like domain literal? */
  640.   if (name[0] == '[' && name[strlen (name) - 1] == ']') return name;
  641.                 /* note that Unix requires lowercase! */
  642.   else return (he = gethostbyname (lcase (strcpy (host,name)))) ?
  643.     he->h_name : name;
  644. }
  645.