2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.bin/talk/invite.c,v 1.6.2.1 2000/10/05 17:40:38 ru Exp $";
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <protocols/talkd.h>
53 * There wasn't an invitation waiting, so send a request containing
54 * our sockt address to the remote talk daemon so it can invite
59 * The msg.id's for the invitations
60 * on the local and remote machines.
61 * These are used to delete the
64 int local_id, remote_id;
70 int nfd, read_mask, template, new_sockt;
71 struct itimerval itimer;
72 CTL_RESPONSE response;
74 itimer.it_value.tv_sec = RING_WAIT;
75 itimer.it_value.tv_usec = 0;
76 itimer.it_interval = itimer.it_value;
77 if (listen(sockt, 5) != 0)
78 p_error("Error on attempt to listen for caller");
80 /* copy new style sockaddr to old, swap family (short in old) */
81 msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/
82 msg.addr.sa_family = htons(my_addr.sin_family);
84 msg.addr = *(struct sockaddr *)&my_addr;
86 msg.id_num = htonl(-1); /* an impossible id_num */
87 invitation_waiting = 1;
90 * Shut off the automatic messages for a while,
91 * so we can use the interupt timer to resend the invitation
94 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
95 message("Waiting for your party to respond");
96 signal(SIGALRM, re_invite);
97 (void) setjmp(invitebuf);
98 while ((new_sockt = accept(sockt, 0, 0)) < 0) {
101 p_error("Unable to connect with your party");
107 * Have the daemons delete the invitations now that we
110 current_state = "Waiting for your party to respond";
113 msg.id_num = htonl(local_id);
114 ctl_transact(my_machine_addr, msg, DELETE, &response);
115 msg.id_num = htonl(remote_id);
116 ctl_transact(his_machine_addr, msg, DELETE, &response);
117 invitation_waiting = 0;
121 * Routine called on interupt to re-invite the callee
129 message("Ringing your party again");
130 waddch(my_win.x_win, '\n');
131 if (current_line < my_win.x_nlines - 1)
133 /* force a re-announce */
134 msg.id_num = htonl(remote_id + 1);
136 longjmp(invitebuf, 1);
139 static char *answers[] = {
140 "answer #0", /* SUCCESS */
141 "Your party is not logged on", /* NOT_HERE */
142 "Target machine is too confused to talk to us", /* FAILED */
143 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
144 "Your party is refusing messages", /* PERMISSION_REFUSED */
145 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
146 "Target machine indicates protocol mismatch", /* BADVERSION */
147 "Target machine indicates protocol botch (addr)",/* BADADDR */
148 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
150 #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
153 * Transmit the invitation and process the response
158 CTL_RESPONSE response;
160 current_state = "Trying to connect to your party's talk daemon";
161 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
162 remote_id = response.id_num;
163 if (response.answer != SUCCESS) {
164 if (response.answer < NANSWERS)
165 message(answers[response.answer]);
168 /* leave the actual invitation on my talk daemon */
169 current_state = "Trying to connect to local talk daemon";
170 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
171 local_id = response.id_num;
175 * Tell the daemon to remove your invitation
183 * This is just a extra clean up, so just send it
184 * and don't wait for an answer
186 msg.id_num = htonl(remote_id);
187 daemon_addr.sin_addr = his_machine_addr;
188 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
189 (struct sockaddr *)&daemon_addr,
190 sizeof (daemon_addr)) != sizeof(msg))
191 warn("send_delete (remote)");
192 msg.id_num = htonl(local_id);
193 daemon_addr.sin_addr = my_machine_addr;
194 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
195 (struct sockaddr *)&daemon_addr,
196 sizeof (daemon_addr)) != sizeof (msg))
197 warn("send_delete (local)");