invite.c revision 1.9 1 /* $NetBSD: invite.c,v 1.9 2011/09/06 18:32:03 joerg Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: invite.c,v 1.9 2011/09/06 18:32:03 joerg Exp $");
38 #endif /* not lint */
39
40 #include "talk.h"
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <errno.h>
44 #include <setjmp.h>
45 #include <unistd.h>
46 #include "talk_ctl.h"
47
48 /*
49 * There wasn't an invitation waiting, so send a request containing
50 * our sockt address to the remote talk daemon so it can invite
51 * him
52 */
53
54 /*
55 * The msg.id's for the invitations
56 * on the local and remote machines.
57 * These are used to delete the
58 * invitations.
59 */
60 static int local_id, remote_id;
61 static jmp_buf invitebuf;
62
63 void
64 invite_remote(void)
65 {
66 int new_sockt;
67 struct itimerval itimer;
68 CTL_RESPONSE response;
69
70 itimer.it_value.tv_sec = RING_WAIT;
71 itimer.it_value.tv_usec = 0;
72 itimer.it_interval = itimer.it_value;
73 if (listen(sockt, 5) != 0)
74 p_error("Error on attempt to listen for caller");
75 #ifdef MSG_EOR
76 /* copy new style sockaddr to old, swap family (short in old) */
77 msg.addr = *(struct talkd_sockaddr *)(void *)&my_addr;
78 msg.addr.sa_family = htons(my_addr.sin_family);
79 #else
80 msg.addr = *(struct sockaddr *)&my_addr;
81 #endif
82 msg.id_num = htonl(-1); /* an impossible id_num */
83 invitation_waiting = 1;
84 announce_invite();
85 /*
86 * Shut off the automatic messages for a while,
87 * so we can use the interrupt timer to resend the invitation
88 */
89 end_msgs();
90 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
91 message("Waiting for your party to respond");
92 signal(SIGALRM, re_invite);
93 (void) setjmp(invitebuf);
94 while ((new_sockt = accept(sockt, 0, 0)) < 0) {
95 if (errno == EINTR)
96 continue;
97 p_error("Unable to connect with your party");
98 }
99 close(sockt);
100 sockt = new_sockt;
101
102 /*
103 * Have the daemons delete the invitations now that we
104 * have connected.
105 */
106 current_state = "Waiting for your party to respond";
107 start_msgs();
108
109 msg.id_num = htonl(local_id);
110 ctl_transact(my_machine_addr, msg, DELETE, &response);
111 msg.id_num = htonl(remote_id);
112 ctl_transact(his_machine_addr, msg, DELETE, &response);
113 invitation_waiting = 0;
114 }
115
116 /*
117 * Routine called on interrupt to re-invite the callee
118 */
119 void
120 re_invite(int dummy)
121 {
122
123 message("Ringing your party again");
124 current_line++;
125 /* force a re-announce */
126 msg.id_num = htonl(remote_id + 1);
127 announce_invite();
128 longjmp(invitebuf, 1);
129 }
130
131 static const char *answers[] = {
132 "answer #0", /* SUCCESS */
133 "Your party is not logged on", /* NOT_HERE */
134 "Target machine is too confused to talk to us", /* FAILED */
135 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
136 "Your party is refusing messages", /* PERMISSION_REFUSED */
137 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
138 "Target machine indicates protocol mismatch", /* BADVERSION */
139 "Target machine indicates protocol botch (addr)",/* BADADDR */
140 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
141 };
142 #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
143
144 /*
145 * Transmit the invitation and process the response
146 */
147 void
148 announce_invite(void)
149 {
150 CTL_RESPONSE response;
151
152 current_state = "Trying to connect to your party's talk daemon";
153 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
154 remote_id = response.id_num;
155 if (response.answer != SUCCESS) {
156 if (response.answer < NANSWERS)
157 message(answers[response.answer]);
158 quit();
159 }
160 /* leave the actual invitation on my talk daemon */
161 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
162 local_id = response.id_num;
163 }
164
165 /*
166 * Tell the daemon to remove your invitation
167 */
168 void
169 send_delete(void)
170 {
171
172 msg.type = DELETE;
173 /*
174 * This is just a extra clean up, so just send it
175 * and don't wait for an answer
176 */
177 msg.id_num = htonl(remote_id);
178 daemon_addr.sin_addr = his_machine_addr;
179 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
180 (struct sockaddr *)&daemon_addr,
181 sizeof (daemon_addr)) != sizeof(msg))
182 perror("send_delete (remote)");
183 msg.id_num = htonl(local_id);
184 daemon_addr.sin_addr = my_machine_addr;
185 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
186 (struct sockaddr *)&daemon_addr,
187 sizeof (daemon_addr)) != sizeof (msg))
188 perror("send_delete (local)");
189 }
190