2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <protocols/talkd.h>
51 * There wasn't an invitation waiting, so send a request containing
52 * our sockt address to the remote talk daemon so it can invite
57 * The msg.id's for the invitations
58 * on the local and remote machines.
59 * These are used to delete the
62 static int local_id, remote_id;
63 static jmp_buf invitebuf;
69 struct itimerval itimer;
70 CTL_RESPONSE response;
72 itimer.it_value.tv_sec = RING_WAIT;
73 itimer.it_value.tv_usec = 0;
74 itimer.it_interval = itimer.it_value;
75 if (listen(sockt, 5) != 0)
76 p_error("Error on attempt to listen for caller");
77 /* copy new style sockaddr to old, swap family (short in old) */
78 msg.addr = *(struct tsockaddr *)&my_addr;
79 msg.addr.sa_family = htons(my_addr.sin_family);
80 msg.id_num = htonl(-1); /* an impossible id_num */
81 invitation_waiting = 1;
84 * Shut off the automatic messages for a while,
85 * so we can use the interrupt timer to resend the invitation
88 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
89 message("Waiting for your party to respond");
90 signal(SIGALRM, re_invite);
91 (void) setjmp(invitebuf);
92 while ((new_sockt = accept(sockt, 0, 0)) < 0) {
95 p_error("Unable to connect with your party");
101 * Have the daemons delete the invitations now that we
104 current_state = "Waiting for your party to respond";
107 msg.id_num = htonl(local_id);
108 ctl_transact(my_machine_addr, msg, DELETE, &response);
109 msg.id_num = htonl(remote_id);
110 ctl_transact(his_machine_addr, msg, DELETE, &response);
111 invitation_waiting = 0;
115 * Routine called on interrupt to re-invite the callee
119 re_invite(int signo __unused)
122 message("Ringing your party again");
123 waddch(my_win.x_win, '\n');
124 if (current_line < my_win.x_nlines - 1)
126 /* force a re-announce */
127 msg.id_num = htonl(remote_id + 1);
129 longjmp(invitebuf, 1);
132 static const char *answers[] = {
133 "answer #0", /* SUCCESS */
134 "Your party is not logged on", /* NOT_HERE */
135 "Target machine is too confused to talk to us", /* FAILED */
136 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
137 "Your party is refusing messages", /* PERMISSION_REFUSED */
138 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
139 "Target machine indicates protocol mismatch", /* BADVERSION */
140 "Target machine indicates protocol botch (addr)",/* BADADDR */
141 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
143 #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
146 * Transmit the invitation and process the response
149 announce_invite(void)
151 CTL_RESPONSE response;
153 current_state = "Trying to connect to your party's talk daemon";
154 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
155 remote_id = response.id_num;
156 if (response.answer != SUCCESS) {
157 if (response.answer < NANSWERS)
158 message(answers[response.answer]);
161 /* leave the actual invitation on my talk daemon */
162 current_state = "Trying to connect to local talk daemon";
163 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
164 local_id = response.id_num;
168 * Tell the daemon to remove your invitation
176 * This is just an extra clean up, so just send it
177 * and don't wait for an answer
179 msg.id_num = htonl(remote_id);
180 daemon_addr.sin_addr = his_machine_addr;
181 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
182 (struct sockaddr *)&daemon_addr,
183 sizeof (daemon_addr)) != sizeof(msg))
184 warn("send_delete (remote)");
185 msg.id_num = htonl(local_id);
186 daemon_addr.sin_addr = my_machine_addr;
187 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
188 (struct sockaddr *)&daemon_addr,
189 sizeof (daemon_addr)) != sizeof (msg))
190 warn("send_delete (local)");