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