1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include "talk_ctl.h"
41*7c478bd9Sstevel@tonic-gate #include <libintl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef SYSV
45*7c478bd9Sstevel@tonic-gate #define	bcopy(a, b, c)	memcpy((b), (a), (c))
46*7c478bd9Sstevel@tonic-gate #endif /* SYSV */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static int look_for_invite(CTL_RESPONSE *);
49*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE swapresponse();
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	/* see if the local daemon has a invitation for us */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate int
check_local()55*7c478bd9Sstevel@tonic-gate check_local()
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	CTL_RESPONSE response;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	/* the rest of msg was set up in get_names */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	msg.ctl_addr = ctl_addr;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if (!look_for_invite(&response)) {
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 		/* we must be initiating a talk */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 		return (0);
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	/*
71*7c478bd9Sstevel@tonic-gate 	 * there was an invitation waiting for us,
72*7c478bd9Sstevel@tonic-gate 	 * so connect with the other (hopefully waiting) party
73*7c478bd9Sstevel@tonic-gate 	 */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	current_state = gettext("Waiting to connect with caller");
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	response = swapresponse(response);
78*7c478bd9Sstevel@tonic-gate 	while (connect(sockt, (struct sockaddr *)&response.addr,
79*7c478bd9Sstevel@tonic-gate 		sizeof (response.addr)) != 0) {
80*7c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED) {
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 			/*
83*7c478bd9Sstevel@tonic-gate 			 * the caller gave up, but the invitation somehow
84*7c478bd9Sstevel@tonic-gate 			 * was not cleared. Clear it and initiate an
85*7c478bd9Sstevel@tonic-gate 			 * invitation. (We know there are no newer invitations,
86*7c478bd9Sstevel@tonic-gate 			 * the talkd works LIFO.)
87*7c478bd9Sstevel@tonic-gate 			 */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 			ctl_transact(rem_machine_addr, msg, DELETE, &response);
90*7c478bd9Sstevel@tonic-gate 			close(sockt);
91*7c478bd9Sstevel@tonic-gate 			open_sockt();
92*7c478bd9Sstevel@tonic-gate 			return (0);
93*7c478bd9Sstevel@tonic-gate 		} else if (errno == EINTR) {
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		/* we have returned from an interupt handler */
96*7c478bd9Sstevel@tonic-gate 			continue;
97*7c478bd9Sstevel@tonic-gate 		} else {
98*7c478bd9Sstevel@tonic-gate 			p_error(gettext("Unable to connect with initiator"));
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	return (1);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	/* look for an invitation on 'machine' */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate static int
look_for_invite(response)108*7c478bd9Sstevel@tonic-gate look_for_invite(response)
109*7c478bd9Sstevel@tonic-gate CTL_RESPONSE *response;
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	current_state = gettext("Checking for invitation on caller's machine");
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	ctl_transact(rem_machine_addr, msg, LOOK_UP, response);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	/*
116*7c478bd9Sstevel@tonic-gate 	 * switch is for later options, such as multiple invitations
117*7c478bd9Sstevel@tonic-gate 	 */
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	switch (response->answer) {
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	case SUCCESS:
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		msg.id_num = response->id_num;
124*7c478bd9Sstevel@tonic-gate 		return (1);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	default :
127*7c478bd9Sstevel@tonic-gate 		/* there wasn't an invitation waiting for us */
128*7c478bd9Sstevel@tonic-gate 		return (0);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate  * heuristic to detect if need to reshuffle CTL_RESPONSE structure
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
137*7c478bd9Sstevel@tonic-gate struct ctl_response_runrise {
138*7c478bd9Sstevel@tonic-gate 	char type;
139*7c478bd9Sstevel@tonic-gate 	char answer;
140*7c478bd9Sstevel@tonic-gate 	short junk;
141*7c478bd9Sstevel@tonic-gate 	int id_num;
142*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr;
143*7c478bd9Sstevel@tonic-gate };
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE
swapresponse(rsp)146*7c478bd9Sstevel@tonic-gate swapresponse(rsp)
147*7c478bd9Sstevel@tonic-gate 	CTL_RESPONSE rsp;
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	struct ctl_response_runrise swaprsp;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (rsp.addr.sin_family != AF_INET) {
152*7c478bd9Sstevel@tonic-gate 		bcopy(&rsp, &swaprsp, sizeof (CTL_RESPONSE));
153*7c478bd9Sstevel@tonic-gate 		if (swaprsp.addr.sin_family == AF_INET) {
154*7c478bd9Sstevel@tonic-gate 			rsp.addr = swaprsp.addr;
155*7c478bd9Sstevel@tonic-gate 			rsp.type = swaprsp.type;
156*7c478bd9Sstevel@tonic-gate 			rsp.answer = swaprsp.answer;
157*7c478bd9Sstevel@tonic-gate 			rsp.id_num = swaprsp.id_num;
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	return (rsp);
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate #endif
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN)
165*7c478bd9Sstevel@tonic-gate struct ctl_response_sun3 {
166*7c478bd9Sstevel@tonic-gate 	char type;
167*7c478bd9Sstevel@tonic-gate 	char answer;
168*7c478bd9Sstevel@tonic-gate 	unsigned short id_num2;
169*7c478bd9Sstevel@tonic-gate 	unsigned short id_num1;
170*7c478bd9Sstevel@tonic-gate 	short sin_family;
171*7c478bd9Sstevel@tonic-gate 	short sin_port;
172*7c478bd9Sstevel@tonic-gate 	short sin_addr2;
173*7c478bd9Sstevel@tonic-gate 	short sin_addr1;
174*7c478bd9Sstevel@tonic-gate };
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE
swapresponse(rsp)177*7c478bd9Sstevel@tonic-gate swapresponse(rsp)
178*7c478bd9Sstevel@tonic-gate 	CTL_RESPONSE rsp;
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	struct ctl_response_sun3 swaprsp;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	if (rsp.addr.sin_family != AF_INET) {
183*7c478bd9Sstevel@tonic-gate 		bcopy(&rsp, &swaprsp, sizeof (struct ctl_response_sun3));
184*7c478bd9Sstevel@tonic-gate 		if (swaprsp.sin_family == AF_INET) {
185*7c478bd9Sstevel@tonic-gate 			rsp.type = swaprsp.type;
186*7c478bd9Sstevel@tonic-gate 			rsp.answer = swaprsp.answer;
187*7c478bd9Sstevel@tonic-gate 			rsp.id_num = swaprsp.id_num1
188*7c478bd9Sstevel@tonic-gate 				| (swaprsp.id_num2 << 16);
189*7c478bd9Sstevel@tonic-gate 			rsp.addr.sin_family = swaprsp.sin_family;
190*7c478bd9Sstevel@tonic-gate 			rsp.addr.sin_port = swaprsp.sin_port;
191*7c478bd9Sstevel@tonic-gate 			rsp.addr.sin_addr.s_addr =
192*7c478bd9Sstevel@tonic-gate 				(swaprsp.sin_addr2 << 16)| swaprsp.sin_addr1;
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 	return (rsp);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate #endif
198