home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / talk / ctl_transact.c,v next >
Encoding:
Text File  |  1994-07-16  |  4.1 KB  |  139 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     florian:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.07.16.09.40.47;    author florian;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/*
  25.  * Copyright (c) 1983 Regents of the University of California.
  26.  * All rights reserved.
  27.  *
  28.  * Redistribution and use in source and binary forms, with or without
  29.  * modification, are permitted provided that the following conditions
  30.  * are met:
  31.  * 1. Redistributions of source code must retain the above copyright
  32.  *    notice, this list of conditions and the following disclaimer.
  33.  * 2. Redistributions in binary form must reproduce the above copyright
  34.  *    notice, this list of conditions and the following disclaimer in the
  35.  *    documentation and/or other materials provided with the distribution.
  36.  * 3. All advertising materials mentioning features or use of this software
  37.  *    must display the following acknowledgement:
  38.  *    This product includes software developed by the University of
  39.  *    California, Berkeley and its contributors.
  40.  * 4. Neither the name of the University nor the names of its contributors
  41.  *    may be used to endorse or promote products derived from this software
  42.  *    without specific prior written permission.
  43.  *
  44.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54.  * SUCH DAMAGE.
  55.  */
  56.  
  57. #ifndef lint
  58. /*static char sccsid[] = "from: @@(#)ctl_transact.c    5.8 (Berkeley) 3/1/91";*/
  59. static char rcsid[] = "$Id: ctl_transact.c,v 1.2 1993/08/01 18:07:54 mycroft Exp $";
  60. #endif /* not lint */
  61.  
  62. #include <sys/types.h>
  63. #include <sys/socket.h>
  64. #include <sys/time.h>
  65. #include <netinet/in.h>
  66. #include <protocols/talkd.h>
  67. #include <errno.h>
  68. #include "talk_ctl.h"
  69.  
  70. #define CTL_WAIT 2    /* time to wait for a response, in seconds */
  71.  
  72. /*
  73.  * SOCKDGRAM is unreliable, so we must repeat messages if we have
  74.  * not recieved an acknowledgement within a reasonable amount
  75.  * of time
  76.  */
  77. ctl_transact(target, msg, type, rp)
  78.     struct in_addr target;
  79.     CTL_MSG msg;
  80.     int type;
  81.     CTL_RESPONSE *rp;
  82. {
  83.     int read_mask, ctl_mask, nready, cc;
  84.     struct timeval wait;
  85.  
  86.     msg.type = type;
  87.     daemon_addr.sin_addr = target;
  88.     daemon_addr.sin_port = daemon_port;
  89.     ctl_mask = 1 << ctl_sockt;
  90.  
  91.     /*
  92.      * Keep sending the message until a response of
  93.      * the proper type is obtained.
  94.      */
  95.     do {
  96.         wait.tv_sec = CTL_WAIT;
  97.         wait.tv_usec = 0;
  98.         /* resend message until a response is obtained */
  99.         do {
  100.             cc = sendto(ctl_sockt, (char *)&msg, sizeof (msg), 0,
  101.                 (struct sockaddr *)&daemon_addr,
  102.                 sizeof (daemon_addr));
  103.             if (cc != sizeof (msg)) {
  104.                 if (errno == EINTR)
  105.                     continue;
  106.                 p_error("Error on write to talk daemon");
  107.             }
  108.             read_mask = ctl_mask;
  109.             nready = select(32, &read_mask, 0, 0, &wait);
  110.             if (nready < 0) {
  111.                 if (errno == EINTR)
  112.                     continue;
  113.                 p_error("Error waiting for daemon response");
  114.             }
  115.         } while (nready == 0);
  116.         /*
  117.          * Keep reading while there are queued messages 
  118.          * (this is not necessary, it just saves extra
  119.          * request/acknowledgements being sent)
  120.          */
  121.         do {
  122.             cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
  123.             if (cc < 0) {
  124.                 if (errno == EINTR)
  125.                     continue;
  126.                 p_error("Error on read from talk daemon");
  127.             }
  128.             read_mask = ctl_mask;
  129.             /* an immediate poll */
  130.             timerclear(&wait);
  131.             nready = select(32, &read_mask, 0, 0, &wait);
  132.         } while (nready > 0 && (rp->vers != TALK_VERSION ||
  133.             rp->type != type));
  134.     } while (rp->vers != TALK_VERSION || rp->type != type);
  135.     rp->id_num = ntohl(rp->id_num);
  136.     rp->addr.sa_family = ntohs(rp->addr.sa_family);
  137. }
  138. @
  139.