]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.bin/talk/ctl_transact.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.bin / talk / ctl_transact.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31
32 __FBSDID("$FreeBSD$");
33
34 #ifndef lint
35 static const char sccsid[] = "@(#)ctl_transact.c        8.1 (Berkeley) 6/6/93";
36 #endif
37
38 #include <arpa/inet.h>
39
40 #include <errno.h>
41
42 #include "talk.h"
43 #include "talk_ctl.h"
44
45 #define CTL_WAIT 2      /* time to wait for a response, in seconds */
46
47 /*
48  * SOCKDGRAM is unreliable, so we must repeat messages if we have
49  * not recieved an acknowledgement within a reasonable amount
50  * of time
51  */
52 void
53 ctl_transact(struct in_addr target, CTL_MSG lmsg, int type, CTL_RESPONSE *rp)
54 {
55         fd_set read_mask, ctl_mask;
56         int nready = 0, cc;
57         struct timeval wait;
58
59         lmsg.type = type;
60         daemon_addr.sin_addr = target;
61         daemon_addr.sin_port = daemon_port;
62         FD_ZERO(&ctl_mask);
63         FD_SET(ctl_sockt, &ctl_mask);
64
65         /*
66          * Keep sending the message until a response of
67          * the proper type is obtained.
68          */
69         do {
70                 wait.tv_sec = CTL_WAIT;
71                 wait.tv_usec = 0;
72                 /* resend message until a response is obtained */
73                 do {
74                         cc = sendto(ctl_sockt, (char *)&lmsg, sizeof (lmsg), 0,
75                             (struct sockaddr *)&daemon_addr,
76                             sizeof (daemon_addr));
77                         if (cc != sizeof (lmsg)) {
78                                 if (errno == EINTR)
79                                         continue;
80                                 p_error("Error on write to talk daemon");
81                         }
82                         read_mask = ctl_mask;
83                         nready = select(32, &read_mask, 0, 0, &wait);
84                         if (nready < 0) {
85                                 if (errno == EINTR)
86                                         continue;
87                                 p_error("Error waiting for daemon response");
88                         }
89                 } while (nready == 0);
90                 /*
91                  * Keep reading while there are queued messages
92                  * (this is not necessary, it just saves extra
93                  * request/acknowledgements being sent)
94                  */
95                 do {
96                         cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
97                         if (cc < 0) {
98                                 if (errno == EINTR)
99                                         continue;
100                                 p_error("Error on read from talk daemon");
101                         }
102                         read_mask = ctl_mask;
103                         /* an immediate poll */
104                         timerclear(&wait);
105                         nready = select(32, &read_mask, 0, 0, &wait);
106                 } while (nready > 0 && (rp->vers != TALK_VERSION ||
107                     rp->type != type));
108         } while (rp->vers != TALK_VERSION || rp->type != type);
109         rp->id_num = ntohl(rp->id_num);
110         rp->addr.sa_family = ntohs(rp->addr.sa_family);
111 }