1*68168df6Sjoerg /* $NetBSD: ctl_transact.c,v 1.11 2011/09/06 18:32:03 joerg Exp $ */
251207773Sjtc
361f28255Scgd /*
451207773Sjtc * Copyright (c) 1983, 1993
551207773Sjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)ctl_transact.c 8.1 (Berkeley) 6/6/93";
3651207773Sjtc #endif
37*68168df6Sjoerg __RCSID("$NetBSD: ctl_transact.c,v 1.11 2011/09/06 18:32:03 joerg Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd
4032a9e65fSlukem #include "talk.h"
4161f28255Scgd #include <sys/time.h>
4261f28255Scgd #include <errno.h>
4332a9e65fSlukem #include <unistd.h>
446248b528Sitojun #include <poll.h>
4561f28255Scgd #include "talk_ctl.h"
4661f28255Scgd
4761f28255Scgd #define CTL_WAIT 2 /* time to wait for a response, in seconds */
4861f28255Scgd
4961f28255Scgd /*
5061f28255Scgd * SOCKDGRAM is unreliable, so we must repeat messages if we have
510a600be8Swiz * not received an acknowledgement within a reasonable amount
5261f28255Scgd * of time
5361f28255Scgd */
5432a9e65fSlukem void
ctl_transact(struct in_addr target,CTL_MSG tmsg,int type,CTL_RESPONSE * rp)55*68168df6Sjoerg ctl_transact(struct in_addr target, CTL_MSG tmsg, int type, CTL_RESPONSE *rp)
5661f28255Scgd {
57675b884cSmycroft struct pollfd set[1];
5832a9e65fSlukem int nready, cc;
5961f28255Scgd
6032a9e65fSlukem nready = 0;
61c2293a6fSlukem tmsg.type = type;
6261f28255Scgd daemon_addr.sin_addr = target;
6361f28255Scgd daemon_addr.sin_port = daemon_port;
64675b884cSmycroft set[0].fd = ctl_sockt;
65675b884cSmycroft set[0].events = POLLIN;
6661f28255Scgd
6761f28255Scgd /*
6861f28255Scgd * Keep sending the message until a response of
6961f28255Scgd * the proper type is obtained.
7061f28255Scgd */
7161f28255Scgd do {
7261f28255Scgd /* resend message until a response is obtained */
7361f28255Scgd do {
74c2293a6fSlukem cc = sendto(ctl_sockt, (char *)&tmsg, sizeof (tmsg), 0,
7561f28255Scgd (struct sockaddr *)&daemon_addr,
7661f28255Scgd sizeof (daemon_addr));
77c2293a6fSlukem if (cc != sizeof (tmsg)) {
7861f28255Scgd if (errno == EINTR)
7961f28255Scgd continue;
8061f28255Scgd p_error("Error on write to talk daemon");
8161f28255Scgd }
82675b884cSmycroft nready = poll(set, 1, CTL_WAIT * 1000);
8361f28255Scgd if (nready < 0) {
8461f28255Scgd if (errno == EINTR)
8561f28255Scgd continue;
8661f28255Scgd p_error("Error waiting for daemon response");
8761f28255Scgd }
8861f28255Scgd } while (nready == 0);
8961f28255Scgd /*
9061f28255Scgd * Keep reading while there are queued messages
9161f28255Scgd * (this is not necessary, it just saves extra
9261f28255Scgd * request/acknowledgements being sent)
9361f28255Scgd */
9461f28255Scgd do {
9561f28255Scgd cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
9661f28255Scgd if (cc < 0) {
9761f28255Scgd if (errno == EINTR)
9861f28255Scgd continue;
9961f28255Scgd p_error("Error on read from talk daemon");
10061f28255Scgd }
10161f28255Scgd /* an immediate poll */
102675b884cSmycroft nready = poll(set, 1, 0);
10361f28255Scgd } while (nready > 0 && (rp->vers != TALK_VERSION ||
10461f28255Scgd rp->type != type));
10561f28255Scgd } while (rp->vers != TALK_VERSION || rp->type != type);
10661f28255Scgd rp->id_num = ntohl(rp->id_num);
10761f28255Scgd rp->addr.sa_family = ntohs(rp->addr.sa_family);
10861f28255Scgd }
109