xref: /dragonfly/usr.bin/talk/invite.c (revision cf89a63b)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)invite.c	8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.bin/talk/invite.c,v 1.6.2.1 2000/10/05 17:40:38 ru Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <setjmp.h>
38 #include <signal.h>
39 #include <protocols/talkd.h>
40 #include "talk_ctl.h"
41 #include "talk.h"
42 
43 /*
44  * There wasn't an invitation waiting, so send a request containing
45  * our sockt address to the remote talk daemon so it can invite
46  * him
47  */
48 
49 /*
50  * The msg.id's for the invitations
51  * on the local and remote machines.
52  * These are used to delete the
53  * invitations.
54  */
55 int	local_id, remote_id;
56 jmp_buf invitebuf;
57 
58 void
59 invite_remote(void)
60 {
61 	int new_sockt;
62 	struct itimerval itimer;
63 	CTL_RESPONSE response;
64 
65 	itimer.it_value.tv_sec = RING_WAIT;
66 	itimer.it_value.tv_usec = 0;
67 	itimer.it_interval = itimer.it_value;
68 	if (listen(sockt, 5) != 0)
69 		p_error("Error on attempt to listen for caller");
70 #ifdef MSG_EOR
71 	/* copy new style sockaddr to old, swap family (short in old) */
72 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
73 	msg.addr.sa_family = htons(my_addr.sin_family);
74 #else
75 	msg.addr = *(struct sockaddr *)&my_addr;
76 #endif
77 	msg.id_num = htonl(-1);		/* an impossible id_num */
78 	invitation_waiting = 1;
79 	announce_invite();
80 	/*
81 	 * Shut off the automatic messages for a while,
82 	 * so we can use the interupt timer to resend the invitation
83 	 */
84 	end_msgs();
85 	setitimer(ITIMER_REAL, &itimer, NULL);
86 	message("Waiting for your party to respond");
87 	signal(SIGALRM, re_invite);
88 	(void) setjmp(invitebuf);
89 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
90 		if (errno == EINTR)
91 			continue;
92 		p_error("Unable to connect with your party");
93 	}
94 	close(sockt);
95 	sockt = new_sockt;
96 
97 	/*
98 	 * Have the daemons delete the invitations now that we
99 	 * have connected.
100 	 */
101 	current_state = "Waiting for your party to respond";
102 	start_msgs();
103 
104 	msg.id_num = htonl(local_id);
105 	ctl_transact(my_machine_addr, msg, DELETE, &response);
106 	msg.id_num = htonl(remote_id);
107 	ctl_transact(his_machine_addr, msg, DELETE, &response);
108 	invitation_waiting = 0;
109 }
110 
111 /*
112  * Routine called on interupt to re-invite the callee
113  */
114 /* ARGSUSED */
115 void
116 re_invite(int signo __unused)
117 {
118 
119 	message("Ringing your party again");
120 	waddch(my_win.x_win, '\n');
121 	if (current_line < my_win.x_nlines - 1)
122 		current_line++;
123 	/* force a re-announce */
124 	msg.id_num = htonl(remote_id + 1);
125 	announce_invite();
126 	longjmp(invitebuf, 1);
127 }
128 
129 static	const char *answers[] = {
130 	"answer #0",					/* SUCCESS */
131 	"Your party is not logged on",			/* NOT_HERE */
132 	"Target machine is too confused to talk to us",	/* FAILED */
133 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
134 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
135 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
136 	"Target machine indicates protocol mismatch",	/* BADVERSION */
137 	"Target machine indicates protocol botch (addr)",/* BADADDR */
138 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
139 };
140 #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
141 
142 /*
143  * Transmit the invitation and process the response
144  */
145 void
146 announce_invite(void)
147 {
148 	CTL_RESPONSE response;
149 
150 	current_state = "Trying to connect to your party's talk daemon";
151 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
152 	remote_id = response.id_num;
153 	if (response.answer != SUCCESS) {
154 		if (response.answer < NANSWERS)
155 			message(answers[response.answer]);
156 		quit();
157 	}
158 	/* leave the actual invitation on my talk daemon */
159 	current_state = "Trying to connect to local talk daemon";
160 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
161 	local_id = response.id_num;
162 }
163 
164 /*
165  * Tell the daemon to remove your invitation
166  */
167 void
168 send_delete(void)
169 {
170 
171 	msg.type = DELETE;
172 	/*
173 	 * This is just a extra clean up, so just send it
174 	 * and don't wait for an answer
175 	 */
176 	msg.id_num = htonl(remote_id);
177 	daemon_addr.sin_addr = his_machine_addr;
178 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
179 	    (struct sockaddr *)&daemon_addr,
180 	    sizeof (daemon_addr)) != sizeof(msg))
181 		warn("send_delete (remote)");
182 	msg.id_num = htonl(local_id);
183 	daemon_addr.sin_addr = my_machine_addr;
184 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
185 	    (struct sockaddr *)&daemon_addr,
186 	    sizeof (daemon_addr)) != sizeof (msg))
187 		warn("send_delete (local)");
188 }
189