xref: /netbsd/usr.bin/talk/invite.c (revision bf9ec67e)
1 /*	$NetBSD: invite.c,v 1.4 1997/10/20 00:23:23 lukem 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)invite.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: invite.c,v 1.4 1997/10/20 00:23:23 lukem Exp $");
42 #endif /* not lint */
43 
44 #include "talk.h"
45 #include <sys/time.h>
46 #include <signal.h>
47 #include <errno.h>
48 #include <setjmp.h>
49 #include <unistd.h>
50 #include "talk_ctl.h"
51 
52 /*
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
55  * him
56  */
57 
58 /*
59  * The msg.id's for the invitations
60  * on the local and remote machines.
61  * These are used to delete the
62  * invitations.
63  */
64 int	local_id, remote_id;
65 jmp_buf invitebuf;
66 
67 void
68 invite_remote()
69 {
70 	int new_sockt;
71 	struct itimerval itimer;
72 	CTL_RESPONSE response;
73 
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");
79 #ifdef MSG_EOR
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);
83 #else
84 	msg.addr = *(struct sockaddr *)&my_addr;
85 #endif
86 	msg.id_num = htonl(-1);		/* an impossible id_num */
87 	invitation_waiting = 1;
88 	announce_invite();
89 	/*
90 	 * Shut off the automatic messages for a while,
91 	 * so we can use the interupt timer to resend the invitation
92 	 */
93 	end_msgs();
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) {
99 		if (errno == EINTR)
100 			continue;
101 		p_error("Unable to connect with your party");
102 	}
103 	close(sockt);
104 	sockt = new_sockt;
105 
106 	/*
107 	 * Have the daemons delete the invitations now that we
108 	 * have connected.
109 	 */
110 	current_state = "Waiting for your party to respond";
111 	start_msgs();
112 
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;
118 }
119 
120 /*
121  * Routine called on interupt to re-invite the callee
122  */
123 void
124 re_invite(dummy)
125 	int dummy;
126 {
127 
128 	message("Ringing your party again");
129 	current_line++;
130 	/* force a re-announce */
131 	msg.id_num = htonl(remote_id + 1);
132 	announce_invite();
133 	longjmp(invitebuf, 1);
134 }
135 
136 static	char *answers[] = {
137 	"answer #0",					/* SUCCESS */
138 	"Your party is not logged on",			/* NOT_HERE */
139 	"Target machine is too confused to talk to us",	/* FAILED */
140 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
141 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
142 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
143 	"Target machine indicates protocol mismatch",	/* BADVERSION */
144 	"Target machine indicates protocol botch (addr)",/* BADADDR */
145 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
146 };
147 #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
148 
149 /*
150  * Transmit the invitation and process the response
151  */
152 void
153 announce_invite()
154 {
155 	CTL_RESPONSE response;
156 
157 	current_state = "Trying to connect to your party's talk daemon";
158 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
159 	remote_id = response.id_num;
160 	if (response.answer != SUCCESS) {
161 		if (response.answer < NANSWERS)
162 			message(answers[response.answer]);
163 		quit();
164 	}
165 	/* leave the actual invitation on my talk daemon */
166 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
167 	local_id = response.id_num;
168 }
169 
170 /*
171  * Tell the daemon to remove your invitation
172  */
173 void
174 send_delete()
175 {
176 
177 	msg.type = DELETE;
178 	/*
179 	 * This is just a extra clean up, so just send it
180 	 * and don't wait for an answer
181 	 */
182 	msg.id_num = htonl(remote_id);
183 	daemon_addr.sin_addr = his_machine_addr;
184 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
185 	    (struct sockaddr *)&daemon_addr,
186 	    sizeof (daemon_addr)) != sizeof(msg))
187 		perror("send_delete (remote)");
188 	msg.id_num = htonl(local_id);
189 	daemon_addr.sin_addr = my_machine_addr;
190 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
191 	    (struct sockaddr *)&daemon_addr,
192 	    sizeof (daemon_addr)) != sizeof (msg))
193 		perror("send_delete (local)");
194 }
195