]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/ctl_transact.c
ANSIfy function declarations.
[bsdgames-darwin.git] / hunt / huntd / ctl_transact.c
1 /* $NetBSD: ctl_transact.c,v 1.8 2009/07/04 02:37:20 dholland 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.8 2009/07/04 02:37:20 dholland 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(struct in_addr target, CTL_MSG msg, int type, CTL_RESPONSE *rp)
61 {
62 struct pollfd set[1];
63 int nready, cc, retries;
64
65 nready = 0;
66 msg.type = type;
67 daemon_addr.sin_addr = target;
68 daemon_addr.sin_port = daemon_port;
69 set[0].fd = ctl_sockt;
70 set[0].events = POLLIN;
71
72 /*
73 * Keep sending the message until a response of
74 * the proper type is obtained.
75 */
76 do {
77 /* resend message until a response is obtained */
78 for (retries = MAX_RETRY; retries > 0; retries -= 1) {
79 cc = sendto(ctl_sockt, &msg, sizeof (msg), 0,
80 &daemon_addr, sizeof (daemon_addr));
81 if (cc != sizeof (msg)) {
82 if (errno == EINTR)
83 continue;
84 p_error("Error on write to talk daemon");
85 }
86 nready = poll(set, 1, CTL_WAIT * 1000);
87 if (nready < 0) {
88 if (errno == EINTR)
89 continue;
90 p_error("Error waiting for daemon response");
91 }
92 if (nready != 0)
93 break;
94 }
95 if (retries <= 0)
96 break;
97 /*
98 * Keep reading while there are queued messages
99 * (this is not necessary, it just saves extra
100 * request/acknowledgements being sent)
101 */
102 do {
103 cc = recv(ctl_sockt, rp, sizeof (*rp), 0);
104 if (cc < 0) {
105 if (errno == EINTR)
106 continue;
107 p_error("Error on read from talk daemon");
108 }
109 /* an immediate poll */
110 nready = poll(set, 1, 0);
111 } while (nready > 0 && (
112 #ifdef TALK_43
113 rp->vers != TALK_VERSION ||
114 #endif
115 rp->type != type));
116 } while (
117 #ifdef TALK_43
118 rp->vers != TALK_VERSION ||
119 #endif
120 rp->type != type);
121 rp->id_num = ntohl(rp->id_num);
122 #ifdef TALK_43
123 rp->addr.sa_family = ntohs(rp->addr.sa_family);
124 # else
125 rp->addr.sin_family = ntohs(rp->addr.sin_family);
126 # endif
127 }
128 #endif