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 #pragma ident "%Z%%M% %I% %E% SMI"
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate #include "talk_ctl.h"
43*7c478bd9Sstevel@tonic-gate #include <libintl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #ifdef SYSV
47*7c478bd9Sstevel@tonic-gate #define bcopy(a, b, c) memcpy((b), (a), (c))
48*7c478bd9Sstevel@tonic-gate #endif /* SYSV */
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate static int look_for_invite(CTL_RESPONSE *);
51*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE swapresponse();
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /* see if the local daemon has a invitation for us */
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate int
check_local()57*7c478bd9Sstevel@tonic-gate check_local()
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate CTL_RESPONSE response;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate /* the rest of msg was set up in get_names */
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate msg.ctl_addr = ctl_addr;
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate if (!look_for_invite(&response)) {
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate /* we must be initiating a talk */
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate return (0);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate * there was an invitation waiting for us,
74*7c478bd9Sstevel@tonic-gate * so connect with the other (hopefully waiting) party
75*7c478bd9Sstevel@tonic-gate */
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate current_state = gettext("Waiting to connect with caller");
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate response = swapresponse(response);
80*7c478bd9Sstevel@tonic-gate while (connect(sockt, (struct sockaddr *)&response.addr,
81*7c478bd9Sstevel@tonic-gate sizeof (response.addr)) != 0) {
82*7c478bd9Sstevel@tonic-gate if (errno == ECONNREFUSED) {
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate * the caller gave up, but the invitation somehow
86*7c478bd9Sstevel@tonic-gate * was not cleared. Clear it and initiate an
87*7c478bd9Sstevel@tonic-gate * invitation. (We know there are no newer invitations,
88*7c478bd9Sstevel@tonic-gate * the talkd works LIFO.)
89*7c478bd9Sstevel@tonic-gate */
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate ctl_transact(rem_machine_addr, msg, DELETE, &response);
92*7c478bd9Sstevel@tonic-gate close(sockt);
93*7c478bd9Sstevel@tonic-gate open_sockt();
94*7c478bd9Sstevel@tonic-gate return (0);
95*7c478bd9Sstevel@tonic-gate } else if (errno == EINTR) {
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate /* we have returned from an interupt handler */
98*7c478bd9Sstevel@tonic-gate continue;
99*7c478bd9Sstevel@tonic-gate } else {
100*7c478bd9Sstevel@tonic-gate p_error(gettext("Unable to connect with initiator"));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate return (1);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate /* look for an invitation on 'machine' */
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate static int
look_for_invite(response)110*7c478bd9Sstevel@tonic-gate look_for_invite(response)
111*7c478bd9Sstevel@tonic-gate CTL_RESPONSE *response;
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate current_state = gettext("Checking for invitation on caller's machine");
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate ctl_transact(rem_machine_addr, msg, LOOK_UP, response);
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate * switch is for later options, such as multiple invitations
119*7c478bd9Sstevel@tonic-gate */
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate switch (response->answer) {
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate case SUCCESS:
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate msg.id_num = response->id_num;
126*7c478bd9Sstevel@tonic-gate return (1);
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate default :
129*7c478bd9Sstevel@tonic-gate /* there wasn't an invitation waiting for us */
130*7c478bd9Sstevel@tonic-gate return (0);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate * heuristic to detect if need to reshuffle CTL_RESPONSE structure
136*7c478bd9Sstevel@tonic-gate */
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
139*7c478bd9Sstevel@tonic-gate struct ctl_response_runrise {
140*7c478bd9Sstevel@tonic-gate char type;
141*7c478bd9Sstevel@tonic-gate char answer;
142*7c478bd9Sstevel@tonic-gate short junk;
143*7c478bd9Sstevel@tonic-gate int id_num;
144*7c478bd9Sstevel@tonic-gate struct sockaddr_in addr;
145*7c478bd9Sstevel@tonic-gate };
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE
swapresponse(rsp)148*7c478bd9Sstevel@tonic-gate swapresponse(rsp)
149*7c478bd9Sstevel@tonic-gate CTL_RESPONSE rsp;
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate struct ctl_response_runrise swaprsp;
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate if (rsp.addr.sin_family != AF_INET) {
154*7c478bd9Sstevel@tonic-gate bcopy(&rsp, &swaprsp, sizeof (CTL_RESPONSE));
155*7c478bd9Sstevel@tonic-gate if (swaprsp.addr.sin_family == AF_INET) {
156*7c478bd9Sstevel@tonic-gate rsp.addr = swaprsp.addr;
157*7c478bd9Sstevel@tonic-gate rsp.type = swaprsp.type;
158*7c478bd9Sstevel@tonic-gate rsp.answer = swaprsp.answer;
159*7c478bd9Sstevel@tonic-gate rsp.id_num = swaprsp.id_num;
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate return (rsp);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate #endif
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN)
167*7c478bd9Sstevel@tonic-gate struct ctl_response_sun3 {
168*7c478bd9Sstevel@tonic-gate char type;
169*7c478bd9Sstevel@tonic-gate char answer;
170*7c478bd9Sstevel@tonic-gate unsigned short id_num2;
171*7c478bd9Sstevel@tonic-gate unsigned short id_num1;
172*7c478bd9Sstevel@tonic-gate short sin_family;
173*7c478bd9Sstevel@tonic-gate short sin_port;
174*7c478bd9Sstevel@tonic-gate short sin_addr2;
175*7c478bd9Sstevel@tonic-gate short sin_addr1;
176*7c478bd9Sstevel@tonic-gate };
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE
swapresponse(rsp)179*7c478bd9Sstevel@tonic-gate swapresponse(rsp)
180*7c478bd9Sstevel@tonic-gate CTL_RESPONSE rsp;
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate struct ctl_response_sun3 swaprsp;
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate if (rsp.addr.sin_family != AF_INET) {
185*7c478bd9Sstevel@tonic-gate bcopy(&rsp, &swaprsp, sizeof (struct ctl_response_sun3));
186*7c478bd9Sstevel@tonic-gate if (swaprsp.sin_family == AF_INET) {
187*7c478bd9Sstevel@tonic-gate rsp.type = swaprsp.type;
188*7c478bd9Sstevel@tonic-gate rsp.answer = swaprsp.answer;
189*7c478bd9Sstevel@tonic-gate rsp.id_num = swaprsp.id_num1
190*7c478bd9Sstevel@tonic-gate | (swaprsp.id_num2 << 16);
191*7c478bd9Sstevel@tonic-gate rsp.addr.sin_family = swaprsp.sin_family;
192*7c478bd9Sstevel@tonic-gate rsp.addr.sin_port = swaprsp.sin_port;
193*7c478bd9Sstevel@tonic-gate rsp.addr.sin_addr.s_addr =
194*7c478bd9Sstevel@tonic-gate (swaprsp.sin_addr2 << 16)| swaprsp.sin_addr1;
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate return (rsp);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate #endif
200