11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
3087710Smarkm#include <sys/cdefs.h>
3187710Smarkm
3287710Smarkm__FBSDID("$FreeBSD$");
3387710Smarkm
341590Srgrimes#ifndef lint
3587710Smarkmstatic const char sccsid[] = "@(#)ctl_transact.c	8.1 (Berkeley) 6/6/93";
3632503Scharnier#endif
371590Srgrimes
3890868Smike#include <arpa/inet.h>
3990868Smike
401590Srgrimes#include <errno.h>
4187710Smarkm
4214443Sjoerg#include "talk.h"
431590Srgrimes#include "talk_ctl.h"
441590Srgrimes
451590Srgrimes#define CTL_WAIT 2	/* time to wait for a response, in seconds */
461590Srgrimes
471590Srgrimes/*
481590Srgrimes * SOCKDGRAM is unreliable, so we must repeat messages if we have
49228992Suqs * not received an acknowledgement within a reasonable amount
501590Srgrimes * of time
511590Srgrimes */
5214443Sjoergvoid
53178642Sdelphijctl_transact(struct in_addr target, CTL_MSG lmsg, int type, CTL_RESPONSE *rp)
541590Srgrimes{
5514443Sjoerg	fd_set read_mask, ctl_mask;
5614443Sjoerg	int nready = 0, cc;
571590Srgrimes	struct timeval wait;
581590Srgrimes
5987710Smarkm	lmsg.type = type;
601590Srgrimes	daemon_addr.sin_addr = target;
611590Srgrimes	daemon_addr.sin_port = daemon_port;
6214443Sjoerg	FD_ZERO(&ctl_mask);
6314443Sjoerg	FD_SET(ctl_sockt, &ctl_mask);
641590Srgrimes
651590Srgrimes	/*
661590Srgrimes	 * Keep sending the message until a response of
671590Srgrimes	 * the proper type is obtained.
681590Srgrimes	 */
691590Srgrimes	do {
701590Srgrimes		wait.tv_sec = CTL_WAIT;
711590Srgrimes		wait.tv_usec = 0;
721590Srgrimes		/* resend message until a response is obtained */
731590Srgrimes		do {
7487710Smarkm			cc = sendto(ctl_sockt, (char *)&lmsg, sizeof (lmsg), 0,
751590Srgrimes			    (struct sockaddr *)&daemon_addr,
761590Srgrimes			    sizeof (daemon_addr));
7787710Smarkm			if (cc != sizeof (lmsg)) {
781590Srgrimes				if (errno == EINTR)
791590Srgrimes					continue;
801590Srgrimes				p_error("Error on write to talk daemon");
811590Srgrimes			}
821590Srgrimes			read_mask = ctl_mask;
831590Srgrimes			nready = select(32, &read_mask, 0, 0, &wait);
841590Srgrimes			if (nready < 0) {
851590Srgrimes				if (errno == EINTR)
861590Srgrimes					continue;
871590Srgrimes				p_error("Error waiting for daemon response");
881590Srgrimes			}
891590Srgrimes		} while (nready == 0);
901590Srgrimes		/*
918874Srgrimes		 * Keep reading while there are queued messages
921590Srgrimes		 * (this is not necessary, it just saves extra
931590Srgrimes		 * request/acknowledgements being sent)
941590Srgrimes		 */
951590Srgrimes		do {
961590Srgrimes			cc = recv(ctl_sockt, (char *)rp, sizeof (*rp), 0);
971590Srgrimes			if (cc < 0) {
981590Srgrimes				if (errno == EINTR)
991590Srgrimes					continue;
1001590Srgrimes				p_error("Error on read from talk daemon");
1011590Srgrimes			}
1021590Srgrimes			read_mask = ctl_mask;
1031590Srgrimes			/* an immediate poll */
1041590Srgrimes			timerclear(&wait);
1051590Srgrimes			nready = select(32, &read_mask, 0, 0, &wait);
1061590Srgrimes		} while (nready > 0 && (rp->vers != TALK_VERSION ||
1071590Srgrimes		    rp->type != type));
1081590Srgrimes	} while (rp->vers != TALK_VERSION || rp->type != type);
1091590Srgrimes	rp->id_num = ntohl(rp->id_num);
1101590Srgrimes	rp->addr.sa_family = ntohs(rp->addr.sa_family);
1111590Srgrimes}
112