1 /* $NetBSD: ctl_transact.c,v 1.8 2009/07/04 02:37:20 dholland Exp $ */
3 * Copyright (c) 1983-2003, Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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
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.
35 #if defined(TALK_43) || defined(TALK_42)
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)ctl_transact.c 5.2 (Berkeley) 3/13/86";
42 __RCSID("$NetBSD: ctl_transact.c,v 1.8 2009/07/04 02:37:20 dholland Exp $");
51 #define CTL_WAIT 2 /* time to wait for a response, in seconds */
55 * SOCKDGRAM is unreliable, so we must repeat messages if we have
56 * not received an acknowledgement within a reasonable amount
60 ctl_transact(struct in_addr target
, CTL_MSG msg
, int type
, CTL_RESPONSE
*rp
)
63 int nready
, cc
, retries
;
67 daemon_addr
.sin_addr
= target
;
68 daemon_addr
.sin_port
= daemon_port
;
69 set
[0].fd
= ctl_sockt
;
70 set
[0].events
= POLLIN
;
73 * Keep sending the message until a response of
74 * the proper type is obtained.
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
)) {
84 p_error("Error on write to talk daemon");
86 nready
= poll(set
, 1, CTL_WAIT
* 1000);
90 p_error("Error waiting for daemon response");
98 * Keep reading while there are queued messages
99 * (this is not necessary, it just saves extra
100 * request/acknowledgements being sent)
103 cc
= recv(ctl_sockt
, rp
, sizeof (*rp
), 0);
107 p_error("Error on read from talk daemon");
109 /* an immediate poll */
110 nready
= poll(set
, 1, 0);
111 } while (nready
> 0 && (
113 rp
->vers
!= TALK_VERSION
||
118 rp
->vers
!= TALK_VERSION
||
121 rp
->id_num
= ntohl(rp
->id_num
);
123 rp
->addr
.sa_family
= ntohs(rp
->addr
.sa_family
);
125 rp
->addr
.sin_family
= ntohs(rp
->addr
.sin_family
);