xref: /openbsd/usr.bin/talk/invite.c (revision 62e3c252)
1 /*	$OpenBSD: invite.c,v 1.15 2013/03/11 17:40:11 deraadt Exp $	*/
2 /*	$NetBSD: invite.c,v 1.3 1994/12/09 02:14:18 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "talk.h"
34 #include <arpa/inet.h>
35 #include <sys/time.h>
36 #include <netdb.h>
37 #include <errno.h>
38 #include <setjmp.h>
39 #include <unistd.h>
40 #include "talk_ctl.h"
41 
42 #define STRING_LENGTH 158
43 
44 /*
45  * There wasn't an invitation waiting, so send a request containing
46  * our sockt address to the remote talk daemon so it can invite
47  * him
48  */
49 
50 /*
51  * The msg.id's for the invitations
52  * on the local and remote machines.
53  * These are used to delete the
54  * invitations.
55  */
56 int	local_id, remote_id;
57 jmp_buf invitebuf;
58 
59 void
60 invite_remote(void)
61 {
62 	int new_sockt;
63 	struct itimerval itimer;
64 	CTL_RESPONSE response;
65 	struct sockaddr rp;
66 	socklen_t rplen = sizeof(struct sockaddr);
67 	struct hostent *rphost;
68 	char rname[STRING_LENGTH];
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 		quit("Error on attempt to listen for caller", 1);
75 #ifdef MSG_EOR
76 	/* copy new style sockaddr to old, swap family (short in old) */
77 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
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 	 * We no longer turn automatic messages back on to avoid a bonus
89 	 * message after we've connected; this is okay even though end_msgs()
90 	 * gets called again in main().
91 	 */
92 	end_msgs();
93 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
94 	message("Waiting for your party to respond");
95 	signal(SIGALRM, re_invite);
96 	(void) setjmp(invitebuf);
97 	while ((new_sockt = accept(sockt, &rp, &rplen)) == -1) {
98 		if (errno == EINTR || errno == EWOULDBLOCK ||
99 		    errno == ECONNABORTED)
100 			continue;
101 		quit("Unable to connect with your party", 1);
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 	msg.id_num = htonl(local_id);
111 	ctl_transact(my_machine_addr, msg, DELETE, &response);
112 	msg.id_num = htonl(remote_id);
113 	ctl_transact(his_machine_addr, msg, DELETE, &response);
114 	invitation_waiting = 0;
115 
116 	/*
117 	 * Check to see if the other guy is coming from the machine
118 	 * we expect.
119 	 */
120 	if (his_machine_addr.s_addr !=
121 	    ((struct sockaddr_in *)&rp)->sin_addr.s_addr) {
122 		rphost = gethostbyaddr((char *) &((struct sockaddr_in
123 		    *)&rp)->sin_addr, sizeof(struct in_addr), AF_INET);
124 		if (rphost)
125 			snprintf(rname, STRING_LENGTH,
126 			    "Answering talk request from %s@%s", msg.r_name,
127 			    rphost->h_name);
128 		else
129 			snprintf(rname, STRING_LENGTH,
130 			    "Answering talk request from %s@%s", msg.r_name,
131 			    inet_ntoa(((struct sockaddr_in *)&rp)->sin_addr));
132 		message(rname);
133 	}
134 }
135 
136 /*
137  * Routine called on interrupt to re-invite the callee
138  */
139 void
140 re_invite(int dummy)
141 {
142 	message("Ringing your party again");
143 	/* force a re-announce */
144 	msg.id_num = htonl(remote_id + 1);
145 	announce_invite();
146 	longjmp(invitebuf, 1);
147 }
148 
149 static	char *answers[] = {
150 	"answer #0",					/* SUCCESS */
151 	"Your party is not logged on",			/* NOT_HERE */
152 	"Target machine is too confused to talk to us",	/* FAILED */
153 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
154 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
155 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
156 	"Target machine indicates protocol mismatch",	/* BADVERSION */
157 	"Target machine indicates protocol botch (addr)",/* BADADDR */
158 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
159 };
160 #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
161 
162 /*
163  * Transmit the invitation and process the response
164  */
165 void
166 announce_invite(void)
167 {
168 	CTL_RESPONSE response;
169 
170 	current_state = "Trying to connect to your party's talk daemon";
171 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
172 	remote_id = response.id_num;
173 	if (response.answer != SUCCESS)
174 		quit(response.answer < NANSWERS ? answers[response.answer] : NULL, 0);
175 	/* leave the actual invitation on my talk daemon */
176 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
177 	local_id = response.id_num;
178 }
179 
180 /*
181  * Tell the daemon to remove your invitation
182  */
183 void
184 send_delete(void)
185 {
186 
187 	msg.type = DELETE;
188 	/*
189 	 * This is just a extra clean up, so just send it
190 	 * and don't wait for an answer
191 	 */
192 	msg.id_num = htonl(remote_id);
193 	daemon_addr.sin_addr = his_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 (remote)");
198 	msg.id_num = htonl(local_id);
199 	daemon_addr.sin_addr = my_machine_addr;
200 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
201 	    (struct sockaddr *)&daemon_addr,
202 	    sizeof (daemon_addr)) != sizeof (msg))
203 		warn("send_delete (local)");
204 }
205