dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / talk / look_up.c
blob881e0d0c1ae4eeb9a63262f5d32c256f9119c30f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include "talk_ctl.h"
43 #include <libintl.h>
44 #include <sys/isa_defs.h>
46 #ifdef SYSV
47 #define bcopy(a, b, c) memcpy((b), (a), (c))
48 #endif /* SYSV */
50 static int look_for_invite(CTL_RESPONSE *);
51 static CTL_RESPONSE swapresponse();
54 /* see if the local daemon has a invitation for us */
56 int
57 check_local()
59 CTL_RESPONSE response;
61 /* the rest of msg was set up in get_names */
63 msg.ctl_addr = ctl_addr;
65 if (!look_for_invite(&response)) {
67 /* we must be initiating a talk */
69 return (0);
73 * there was an invitation waiting for us,
74 * so connect with the other (hopefully waiting) party
77 current_state = gettext("Waiting to connect with caller");
79 response = swapresponse(response);
80 while (connect(sockt, (struct sockaddr *)&response.addr,
81 sizeof (response.addr)) != 0) {
82 if (errno == ECONNREFUSED) {
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.)
91 ctl_transact(rem_machine_addr, msg, DELETE, &response);
92 close(sockt);
93 open_sockt();
94 return (0);
95 } else if (errno == EINTR) {
97 /* we have returned from an interupt handler */
98 continue;
99 } else {
100 p_error(gettext("Unable to connect with initiator"));
104 return (1);
107 /* look for an invitation on 'machine' */
109 static int
110 look_for_invite(response)
111 CTL_RESPONSE *response;
113 current_state = gettext("Checking for invitation on caller's machine");
115 ctl_transact(rem_machine_addr, msg, LOOK_UP, response);
118 * switch is for later options, such as multiple invitations
121 switch (response->answer) {
123 case SUCCESS:
125 msg.id_num = response->id_num;
126 return (1);
128 default :
129 /* there wasn't an invitation waiting for us */
130 return (0);
135 * heuristic to detect if need to reshuffle CTL_RESPONSE structure
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;
147 static CTL_RESPONSE
148 swapresponse(rsp)
149 CTL_RESPONSE rsp;
151 struct ctl_response_runrise swaprsp;
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;
162 return (rsp);
164 #endif
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;
178 static CTL_RESPONSE
179 swapresponse(rsp)
180 CTL_RESPONSE rsp;
182 struct ctl_response_sun3 swaprsp;
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;
197 return (rsp);
199 #endif