]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/ctl_transact.c
Merge with v20030416.
[bsdgames-darwin.git] / hunt / huntd / ctl_transact.c
1 /* $NetBSD: ctl_transact.c,v 1.6 2003/06/11 12:00:22 wiz Exp $ */
2 /*
3 * Copyright (c) 1983-2003, Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * + Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * + Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * + Neither the name of the University of California, San Francisco nor
16 * the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "bsd.h"
34
35 #if defined(TALK_43) || defined(TALK_42)
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)ctl_transact.c 5.2 (Berkeley) 3/13/86";
41 #else
42 __RCSID("$NetBSD: ctl_transact.c,v 1.6 2003/06/11 12:00:22 wiz Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/time.h>
47 #include <unistd.h>
48 #include "hunt.h"
49 #include "talk_ctl.h"
50
51 #define CTL_WAIT 2 /* time to wait for a response, in seconds */
52 #define MAX_RETRY 5
53
54 /*
55 * SOCKDGRAM is unreliable, so we must repeat messages if we have
56 * not received an acknowledgement within a reasonable amount
57 * of time
58 */
59 void
60 ctl_transact(target, msg, type, rp)
61 struct in_addr target;
62 CTL_MSG msg;
63 int type;
64 CTL_RESPONSE *rp;
65 {
66 struct pollfd set[1];
67 int nready, cc, retries;
68
69 nready = 0;
70 msg.type = type;
71 daemon_addr.sin_addr = target;
72 daemon_addr.sin_port = daemon_port;
73 set[0].fd = ctl_sockt;
74 set[0].events = POLLIN;
75
76 /*
77 * Keep sending the message until a response of
78 * the proper type is obtained.
79 */
80 do {
81 /* resend message until a response is obtained */
82 for (retries = MAX_RETRY; retries > 0; retries -= 1) {
83 cc = sendto(ctl_sockt, (char *)&msg, sizeof (msg), 0,
84 &daemon_addr, sizeof (daemon_addr));
85 if (cc != sizeof (msg)) {
86 if (errno == EINTR)
87 continue;
88 p_error("Error on write to talk daemon");
89 }
90 nready = poll(set, 1, CTL_WAIT * 1000);
91 if (nready < 0) {
92 if (errno == EINTR)
93 continue;
94 p_error("Error waiting for daemon response");
95 }
96 if (nready != 0)
97 break;
98 }
99 if (retries <= 0)
100 break;
101 /*
102 * Keep reading while there are queued messages
103 * (this is not necessary, it just saves extra
104 * request/acknowledgements being sent)
105 */
106 do {
107 cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
108 if (cc < 0) {
109 if (errno == EINTR)
110 continue;
111 p_error("Error on read from talk daemon");
112 }
113 /* an immediate poll */
114 nready = poll(set, 1, 0);
115 } while (nready > 0 && (
116 #ifdef TALK_43
117 rp->vers != TALK_VERSION ||
118 #endif
119 rp->type != type));
120 } while (
121 #ifdef TALK_43
122 rp->vers != TALK_VERSION ||
123 #endif
124 rp->type != type);
125 rp->id_num = ntohl(rp->id_num);
126 #ifdef TALK_43
127 rp->addr.sa_family = ntohs(rp->addr.sa_family);
128 # else
129 rp->addr.sin_family = ntohs(rp->addr.sin_family);
130 # endif
131 }
132 #endif