17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bdstevel@tonic-gate * The Regents of the University of California
337c478bdstevel@tonic-gate * All Rights Reserved
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bdstevel@tonic-gate * contributors.
387c478bdstevel@tonic-gate */
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#include "talk_ctl.h"
437c478bdstevel@tonic-gate#include <libintl.h>
447c478bdstevel@tonic-gate#include <sys/isa_defs.h>
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate#ifdef SYSV
477c478bdstevel@tonic-gate#define	bcopy(a, b, c)	memcpy((b), (a), (c))
487c478bdstevel@tonic-gate#endif /* SYSV */
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gatestatic int look_for_invite(CTL_RESPONSE *);
517c478bdstevel@tonic-gatestatic CTL_RESPONSE swapresponse();
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate	/* see if the local daemon has a invitation for us */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gateint
577c478bdstevel@tonic-gatecheck_local()
587c478bdstevel@tonic-gate{
597c478bdstevel@tonic-gate	CTL_RESPONSE response;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	/* the rest of msg was set up in get_names */
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	msg.ctl_addr = ctl_addr;
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate	if (!look_for_invite(&response)) {
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate		/* we must be initiating a talk */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate		return (0);
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	/*
737c478bdstevel@tonic-gate	 * there was an invitation waiting for us,
747c478bdstevel@tonic-gate	 * so connect with the other (hopefully waiting) party
757c478bdstevel@tonic-gate	 */
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate	current_state = gettext("Waiting to connect with caller");
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	response = swapresponse(response);
807c478bdstevel@tonic-gate	while (connect(sockt, (struct sockaddr *)&response.addr,
817c478bdstevel@tonic-gate		sizeof (response.addr)) != 0) {
827c478bdstevel@tonic-gate		if (errno == ECONNREFUSED) {
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate			/*
857c478bdstevel@tonic-gate			 * the caller gave up, but the invitation somehow
867c478bdstevel@tonic-gate			 * was not cleared. Clear it and initiate an
877c478bdstevel@tonic-gate			 * invitation. (We know there are no newer invitations,
887c478bdstevel@tonic-gate			 * the talkd works LIFO.)
897c478bdstevel@tonic-gate			 */
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate			ctl_transact(rem_machine_addr, msg, DELETE, &response);
927c478bdstevel@tonic-gate			close(sockt);
937c478bdstevel@tonic-gate			open_sockt();
947c478bdstevel@tonic-gate			return (0);
957c478bdstevel@tonic-gate		} else if (errno == EINTR) {
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate		/* we have returned from an interupt handler */
987c478bdstevel@tonic-gate			continue;
997c478bdstevel@tonic-gate		} else {
1007c478bdstevel@tonic-gate			p_error(gettext("Unable to connect with initiator"));
1017c478bdstevel@tonic-gate		}
1027c478bdstevel@tonic-gate	}
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	return (1);
1057c478bdstevel@tonic-gate}
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	/* look for an invitation on 'machine' */
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gatestatic int
1107c478bdstevel@tonic-gatelook_for_invite(response)
1117c478bdstevel@tonic-gateCTL_RESPONSE *response;
1127c478bdstevel@tonic-gate{
1137c478bdstevel@tonic-gate	current_state = gettext("Checking for invitation on caller's machine");
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	ctl_transact(rem_machine_addr, msg, LOOK_UP, response);
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gate	/*
1187c478bdstevel@tonic-gate	 * switch is for later options, such as multiple invitations
1197c478bdstevel@tonic-gate	 */
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	switch (response->answer) {
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate	case SUCCESS:
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate		msg.id_num = response->id_num;
1267c478bdstevel@tonic-gate		return (1);
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate	default :
1297c478bdstevel@tonic-gate		/* there wasn't an invitation waiting for us */
1307c478bdstevel@tonic-gate		return (0);
1317c478bdstevel@tonic-gate	}
1327c478bdstevel@tonic-gate}
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate/*
1357c478bdstevel@tonic-gate * heuristic to detect if need to reshuffle CTL_RESPONSE structure
1367c478bdstevel@tonic-gate */
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate#if defined(_LITTLE_ENDIAN)
1397c478bdstevel@tonic-gatestruct ctl_response_runrise {
1407c478bdstevel@tonic-gate	char type;
1417c478bdstevel@tonic-gate	char answer;
1427c478bdstevel@tonic-gate	short junk;
1437c478bdstevel@tonic-gate	int id_num;
1447c478bdstevel@tonic-gate	struct sockaddr_in addr;
1457c478bdstevel@tonic-gate};
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gatestatic CTL_RESPONSE
1487c478bdstevel@tonic-gateswapresponse(rsp)
1497c478bdstevel@tonic-gate	CTL_RESPONSE rsp;
1507c478bdstevel@tonic-gate{
1517c478bdstevel@tonic-gate	struct ctl_response_runrise swaprsp;
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	if (rsp.addr.sin_family != AF_INET) {
1547c478bdstevel@tonic-gate		bcopy(&rsp, &swaprsp, sizeof (CTL_RESPONSE));
1557c478bdstevel@tonic-gate		if (swaprsp.addr.sin_family == AF_INET) {
1567c478bdstevel@tonic-gate			rsp.addr = swaprsp.addr;
1577c478bdstevel@tonic-gate			rsp.type = swaprsp.type;
1587c478bdstevel@tonic-gate			rsp.answer = swaprsp.answer;
1597c478bdstevel@tonic-gate			rsp.id_num = swaprsp.id_num;
1607c478bdstevel@tonic-gate		}
1617c478bdstevel@tonic-gate	}
1627c478bdstevel@tonic-gate	return (rsp);
1637c478bdstevel@tonic-gate}
1647c478bdstevel@tonic-gate#endif
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate#if defined(_BIG_ENDIAN)
1677c478bdstevel@tonic-gatestruct ctl_response_sun3 {
1687c478bdstevel@tonic-gate	char type;
1697c478bdstevel@tonic-gate	char answer;
1707c478bdstevel@tonic-gate	unsigned short id_num2;
1717c478bdstevel@tonic-gate	unsigned short id_num1;
1727c478bdstevel@tonic-gate	short sin_family;
1737c478bdstevel@tonic-gate	short sin_port;
1747c478bdstevel@tonic-gate	short sin_addr2;
1757c478bdstevel@tonic-gate	short sin_addr1;
1767c478bdstevel@tonic-gate};
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gatestatic CTL_RESPONSE
1797c478bdstevel@tonic-gateswapresponse(rsp)
1807c478bdstevel@tonic-gate	CTL_RESPONSE rsp;
1817c478bdstevel@tonic-gate{
1827c478bdstevel@tonic-gate	struct ctl_response_sun3 swaprsp;
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate	if (rsp.addr.sin_family != AF_INET) {
1857c478bdstevel@tonic-gate		bcopy(&rsp, &swaprsp, sizeof (struct ctl_response_sun3));
1867c478bdstevel@tonic-gate		if (swaprsp.sin_family == AF_INET) {
1877c478bdstevel@tonic-gate			rsp.type = swaprsp.type;
1887c478bdstevel@tonic-gate			rsp.answer = swaprsp.answer;
1897c478bdstevel@tonic-gate			rsp.id_num = swaprsp.id_num1
1907c478bdstevel@tonic-gate				| (swaprsp.id_num2 << 16);
1917c478bdstevel@tonic-gate			rsp.addr.sin_family = swaprsp.sin_family;
1927c478bdstevel@tonic-gate			rsp.addr.sin_port = swaprsp.sin_port;
1937c478bdstevel@tonic-gate			rsp.addr.sin_addr.s_addr =
1947c478bdstevel@tonic-gate				(swaprsp.sin_addr2 << 16)| swaprsp.sin_addr1;
1957c478bdstevel@tonic-gate		}
1967c478bdstevel@tonic-gate	}
1977c478bdstevel@tonic-gate	return (rsp);
1987c478bdstevel@tonic-gate}
1997c478bdstevel@tonic-gate#endif
200