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 / os2 / tcp_os2.c < prev   
C/C++ Source or Header  |  1998-09-28  |  11KB  |  410 lines

  1. /*
  2.  * Program:    MS-DOS 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:    11 April 1989
  13.  * Last Edited:    28 September 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
  24.  * available "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. static tcptimeout_t tmoh = NIL;    /* TCP timeout handler routine */
  37. static long ttmo_read = 0;    /* TCP timeouts, in seconds */
  38. static long ttmo_write = 0;
  39.  
  40. /* TCP/IP manipulate parameters
  41.  * Accepts: function code
  42.  *        function-dependent value
  43.  * Returns: function-dependent return value
  44.  */
  45.  
  46. void *tcp_parameters (long function,void *value)
  47. {
  48.   switch ((int) function) {
  49.   case SET_TIMEOUT:
  50.     tmoh = (tcptimeout_t) value;
  51.     break;
  52.   case GET_TIMEOUT:
  53.     value = (void *) tmoh;
  54.     break;
  55.   case SET_READTIMEOUT:
  56.     ttmo_read = (long) value;
  57.     break;
  58.   case GET_READTIMEOUT:
  59.     value = (void *) ttmo_read;
  60.     break;
  61.   case SET_WRITETIMEOUT:
  62.     ttmo_write = (long) value;
  63.     break;
  64.   case GET_WRITETIMEOUT:
  65.     value = (void *) ttmo_write;
  66.     break;
  67.   default:
  68.     value = NIL;        /* error case */
  69.     break;
  70.   }
  71.   return value;
  72. }
  73.  
  74. /* TCP/IP open
  75.  * Accepts: host name
  76.  *        contact service name
  77.  *        contact port number
  78.  * Returns: TCP/IP stream if success else NIL
  79.  */
  80.  
  81. TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
  82. {
  83.   TCPSTREAM *stream = NIL;
  84.   struct sockaddr_in sin;
  85.   int sock;
  86.   char *s,tmp[MAILTMPLEN];
  87.   /* The domain literal form is used (rather than simply the dotted decimal
  88.      as with other Unix programs) because it has to be a valid "host name"
  89.      in mailsystem terminology. */
  90.   sin.sin_family = AF_INET;    /* family is always Internet */
  91.                 /* look like domain literal? */
  92.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  93.     strcpy (tmp,host+1);    /* yes, copy number part */
  94.     tmp[strlen (tmp)-1] = '\0';
  95.     if ((sin.sin_addr.s_addr = inet_addr (tmp)) == -1) {
  96.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  97.       mm_log (tmp,ERROR);
  98.       return NIL;
  99.     }
  100.   }
  101.                 /* look up host name */
  102.   else if (!lookuphost (&host,&sin)) {
  103.     sprintf (tmp,"Host not found: %s",host);
  104.     mm_log (tmp,ERROR);
  105.     return NIL;
  106.   }
  107.  
  108.                 /* copy port number in network format */
  109.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  110.                 /* get a TCP stream */
  111.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  112.     sprintf (tmp,"Unable to create TCP socket (%d)",errno);
  113.     mm_log (tmp,ERROR);
  114.     fs_give ((void **) &host);
  115.     return NIL;
  116.   }
  117.                 /* open connection */
  118.   if (connect (sock,(struct sockaddr *) &sin,sizeof (sin)) < 0) {
  119.     switch (errno) {        /* analyze error */
  120.     case ECONNREFUSED:
  121.       s = "Refused";
  122.       break;
  123.     case ENOBUFS:
  124.       s = "Insufficient system resources";
  125.       break;
  126.     case ETIMEDOUT:
  127.       s = "Timed out";
  128.       break;
  129.     default:
  130.       s = "Unknown error";
  131.       break;
  132.     }
  133.     sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",host,port,s,errno);
  134.     mm_log (tmp,ERROR);
  135.     close (sock);
  136.     fs_give ((void **) &host);
  137.     return NIL;
  138.   }
  139.                 /* create TCP/IP stream */
  140.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  141.   stream->host = host;        /* official host name */
  142.   stream->localhost = cpystr (mylocalhost ());
  143.   stream->port = port;        /* port number */
  144.   stream->tcps = sock;        /* init socket */
  145.   stream->ictr = 0;        /* init input counter */
  146.   return stream;        /* return success */
  147. }
  148.   
  149. /* TCP/IP authenticated open
  150.  * Accepts: NETMBX specifier
  151.  *        service name
  152.  *        returned user name buffer
  153.  * Returns: TCP/IP stream if success else NIL
  154.  */
  155.  
  156. TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
  157. {
  158.   return NIL;            /* always NIL on DOS */
  159. }
  160.  
  161. /* TCP/IP receive line
  162.  * Accepts: TCP/IP stream
  163.  * Returns: text line string or NIL if failure
  164.  */
  165.  
  166. char *tcp_getline (TCPSTREAM *stream)
  167. {
  168.   int n,m;
  169.   char *st,*ret,*stp;
  170.   char c = '\0';
  171.   char d;
  172.                 /* make sure have data */
  173.   if (!tcp_getdata (stream)) return NIL;
  174.   st = stream->iptr;        /* save start of string */
  175.   n = 0;            /* init string count */
  176.   while (stream->ictr--) {    /* look for end of line */
  177.     d = *stream->iptr++;    /* slurp another character */
  178.     if ((c == '\015') && (d == '\012')) {
  179.       ret = (char *) fs_get (n--);
  180.       memcpy (ret,st,n);    /* copy into a free storage string */
  181.       ret[n] = '\0';        /* tie off string with null */
  182.       return ret;
  183.     }
  184.     n++;            /* count another character searched */
  185.     c = d;            /* remember previous character */
  186.   }
  187.                 /* copy partial string from buffer */
  188.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  189.                 /* get more data from the net */
  190.   if (!tcp_getdata (stream)) return NIL;
  191.                 /* special case of newline broken by buffer */
  192.   if ((c == '\015') && (*stream->iptr == '\012')) {
  193.     stream->iptr++;        /* eat the line feed */
  194.     stream->ictr--;
  195.     ret[n - 1] = '\0';        /* tie off string with null */
  196.   }
  197.                 /* else recurse to get remainder */
  198.   else if (st = tcp_getline (stream)) {
  199.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  200.     memcpy (ret,stp,n);        /* copy first part */
  201.     memcpy (ret + n,st,m);    /* and second part */
  202.     fs_give ((void **) &stp);    /* flush first part */
  203.     fs_give ((void **) &st);    /* flush second part */
  204.     ret[n + m] = '\0';        /* tie off string with null */
  205.   }
  206.   return ret;
  207. }
  208.  
  209. /* TCP/IP receive buffer
  210.  * Accepts: TCP/IP stream
  211.  *        size in bytes
  212.  *        buffer to read into
  213.  * Returns: T if success, NIL otherwise
  214.  */
  215.  
  216. long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
  217. {
  218.   unsigned long n;
  219.   char *bufptr = buffer;
  220.   while (size > 0) {        /* until request satisfied */
  221.     if (!tcp_getdata (stream)) return NIL;
  222.     n = min (size,stream->ictr);/* number of bytes to transfer */
  223.                 /* do the copy */
  224.     memcpy (bufptr,stream->iptr,n);
  225.     bufptr += n;        /* update pointer */
  226.     stream->iptr +=n;
  227.     size -= n;            /* update # of bytes to do */
  228.     stream->ictr -=n;
  229.   }
  230.   bufptr[0] = '\0';        /* tie off string */
  231.   return T;
  232. }
  233.  
  234. /* TCP/IP receive data
  235.  * Accepts: TCP/IP stream
  236.  * Returns: T if success, NIL otherwise
  237.  */
  238.  
  239. long tcp_getdata (TCPSTREAM *stream)
  240. {
  241.   int i;
  242.   fd_set fds,efds;
  243.   struct timeval tmo;
  244.   time_t t = time (0);
  245.   if (stream->tcpsi < 0) return NIL;
  246.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  247.     time_t tl = time (0);    /* start of request */
  248.     tmo.tv_sec = ttmo_read;    /* read timeout */
  249.     tmo.tv_usec = 0;
  250.     FD_ZERO (&fds);        /* initialize selection vector */
  251.     FD_ZERO (&efds);        /* handle errors too */
  252.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  253.     FD_SET(stream->tcpsi,&efds);/* set bit in error selection vector */
  254.     errno = NIL;        /* block and read */
  255.     while (((i = select (stream->tcpsi+1,&fds,0,&efds,ttmo_read ? &tmo : 0))<0)
  256.        && (errno == EINTR));
  257.     if (!i) {            /* timeout? */
  258.       time_t tc = time (0);
  259.       if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
  260.       else return tcp_abort (stream);
  261.     }
  262.     else if (i < 0) return tcp_abort (stream);
  263.     while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 0) &&
  264.        (errno == EINTR));
  265.     if (i < 1) return tcp_abort (stream);
  266.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  267.     stream->ictr = i;        /* set new byte count */
  268.   }
  269.   return T;
  270. }
  271.  
  272. /* TCP/IP send string as record
  273.  * Accepts: TCP/IP stream
  274.  *        string pointer
  275.  * Returns: T if success else NIL
  276.  */
  277.  
  278. long tcp_soutr (TCPSTREAM *stream,char *string)
  279. {
  280.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  281. }
  282.  
  283.  
  284. /* TCP/IP send string
  285.  * Accepts: TCP/IP stream
  286.  *        string pointer
  287.  *        byte count
  288.  * Returns: T if success else NIL
  289.  */
  290.  
  291. long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
  292. {
  293.   int i;
  294.   fd_set fds;
  295.   struct timeval tmo;
  296.   time_t t = time (0);
  297.   if (stream->tcpso < 0) return NIL;
  298.   while (size > 0) {        /* until request satisfied */
  299.     time_t tl = time (0);    /* start of request */
  300.     tmo.tv_sec = ttmo_write;    /* write timeout */
  301.     tmo.tv_usec = 0;
  302.     FD_ZERO (&fds);        /* initialize selection vector */
  303.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  304.     errno = NIL;        /* block and write */
  305.     while (((i = select (stream->tcpso+1,0,&fds,0,ttmo_write ? &tmo : 0)) < 0)
  306.        && (errno == EINTR));
  307.     if (!i) {            /* timeout? */
  308.       time_t tc = time (0);
  309.       if (tmoh && ((*tmoh) (tc - t,tc - tl))) continue;
  310.       else return tcp_abort (stream);
  311.     }
  312.     else if (i < 0) return tcp_abort (stream);
  313.     while (((i = write (stream->tcpso,string,size)) < 0) && (errno == EINTR));
  314.     if (i < 0) return tcp_abort (stream);
  315.     size -= i;            /* how much we sent */
  316.     string += i;
  317.   }
  318.   return T;            /* all done */
  319. }
  320.  
  321. /* TCP/IP close
  322.  * Accepts: TCP/IP stream
  323.  */
  324.  
  325. void tcp_close (TCPSTREAM *stream)
  326. {
  327.   tcp_abort (stream);        /* nuke the socket */
  328.                 /* flush host names */
  329.   fs_give ((void **) &stream->host);
  330.   fs_give ((void **) &stream->localhost);
  331.   fs_give ((void **) &stream);    /* flush the stream */
  332. }
  333.  
  334.  
  335. /* TCP/IP abort stream
  336.  * Accepts: TCP/IP stream
  337.  * Returns: NIL always
  338.  */
  339.  
  340. long tcp_abort (TCPSTREAM *stream)
  341. {
  342.   if (stream->tcps >= 0) close (stream->tcps);
  343.   stream->tcps = -1;
  344.   return NIL;
  345. }
  346.  
  347. /* TCP/IP get host name
  348.  * Accepts: TCP/IP stream
  349.  * Returns: host name for this stream
  350.  */
  351.  
  352. char *tcp_host (TCPSTREAM *stream)
  353. {
  354.   return stream->host;        /* return host name */
  355. }
  356.  
  357.  
  358. /* TCP/IP get remote host name
  359.  * Accepts: TCP/IP stream
  360.  * Returns: host name for this stream
  361.  */
  362.  
  363. char *tcp_remotehost (TCPSTREAM *stream)
  364. {
  365.   return stream->host;        /* all we can do for now */
  366. }
  367.  
  368.  
  369. /* TCP/IP return port for this stream
  370.  * Accepts: TCP/IP stream
  371.  * Returns: port number for this stream
  372.  */
  373.  
  374. unsigned long tcp_port (TCPSTREAM *stream)
  375. {
  376.   return stream->port;        /* return port number */
  377. }
  378.  
  379.  
  380. /* TCP/IP get local host name
  381.  * Accepts: TCP/IP stream
  382.  * Returns: local host name
  383.  */
  384.  
  385. char *tcp_localhost (TCPSTREAM *stream)
  386. {
  387.   return stream->localhost;    /* return local host name */
  388. }
  389.  
  390.  
  391. /* TCP/IP return canonical form of host name
  392.  * Accepts: host name
  393.  * Returns: canonical form of host name
  394.  */
  395.  
  396. char *tcp_canonical (char *name)
  397. {
  398.   return name;
  399. }
  400.  
  401.  
  402. /* TCP/IP get client host name (server calls only)
  403.  * Returns: client host name
  404.  */
  405.  
  406. char *tcp_clienthost ()
  407. {
  408.   return "UNKNOWN";
  409. }
  410.