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