xref: /freebsd/usr.bin/talk/invite.c (revision b02b5aad)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)invite.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <sys/types.h>
399b50d902SRodney W. Grimes #include <sys/socket.h>
409b50d902SRodney W. Grimes #include <sys/time.h>
419b50d902SRodney W. Grimes #include <signal.h>
429b50d902SRodney W. Grimes #include <netinet/in.h>
439b50d902SRodney W. Grimes #include <protocols/talkd.h>
449b50d902SRodney W. Grimes #include <errno.h>
459b50d902SRodney W. Grimes #include <setjmp.h>
469b50d902SRodney W. Grimes #include "talk_ctl.h"
479b50d902SRodney W. Grimes #include "talk.h"
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes /*
509b50d902SRodney W. Grimes  * There wasn't an invitation waiting, so send a request containing
519b50d902SRodney W. Grimes  * our sockt address to the remote talk daemon so it can invite
529b50d902SRodney W. Grimes  * him
539b50d902SRodney W. Grimes  */
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes /*
569b50d902SRodney W. Grimes  * The msg.id's for the invitations
579b50d902SRodney W. Grimes  * on the local and remote machines.
589b50d902SRodney W. Grimes  * These are used to delete the
599b50d902SRodney W. Grimes  * invitations.
609b50d902SRodney W. Grimes  */
619b50d902SRodney W. Grimes int	local_id, remote_id;
629b50d902SRodney W. Grimes void	re_invite();
639b50d902SRodney W. Grimes jmp_buf invitebuf;
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes invite_remote()
669b50d902SRodney W. Grimes {
679b50d902SRodney W. Grimes 	int nfd, read_mask, template, new_sockt;
689b50d902SRodney W. Grimes 	struct itimerval itimer;
699b50d902SRodney W. Grimes 	CTL_RESPONSE response;
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes 	itimer.it_value.tv_sec = RING_WAIT;
729b50d902SRodney W. Grimes 	itimer.it_value.tv_usec = 0;
739b50d902SRodney W. Grimes 	itimer.it_interval = itimer.it_value;
749b50d902SRodney W. Grimes 	if (listen(sockt, 5) != 0)
759b50d902SRodney W. Grimes 		p_error("Error on attempt to listen for caller");
769b50d902SRodney W. Grimes #ifdef MSG_EOR
779b50d902SRodney W. Grimes 	/* copy new style sockaddr to old, swap family (short in old) */
789b50d902SRodney W. Grimes 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
799b50d902SRodney W. Grimes 	msg.addr.sa_family = htons(my_addr.sin_family);
809b50d902SRodney W. Grimes #else
819b50d902SRodney W. Grimes 	msg.addr = *(struct sockaddr *)&my_addr;
829b50d902SRodney W. Grimes #endif
839b50d902SRodney W. Grimes 	msg.id_num = htonl(-1);		/* an impossible id_num */
849b50d902SRodney W. Grimes 	invitation_waiting = 1;
859b50d902SRodney W. Grimes 	announce_invite();
869b50d902SRodney W. Grimes 	/*
879b50d902SRodney W. Grimes 	 * Shut off the automatic messages for a while,
889b50d902SRodney W. Grimes 	 * so we can use the interupt timer to resend the invitation
899b50d902SRodney W. Grimes 	 */
909b50d902SRodney W. Grimes 	end_msgs();
919b50d902SRodney W. Grimes 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
929b50d902SRodney W. Grimes 	message("Waiting for your party to respond");
939b50d902SRodney W. Grimes 	signal(SIGALRM, re_invite);
949b50d902SRodney W. Grimes 	(void) setjmp(invitebuf);
959b50d902SRodney W. Grimes 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
969b50d902SRodney W. Grimes 		if (errno == EINTR)
979b50d902SRodney W. Grimes 			continue;
989b50d902SRodney W. Grimes 		p_error("Unable to connect with your party");
999b50d902SRodney W. Grimes 	}
1009b50d902SRodney W. Grimes 	close(sockt);
1019b50d902SRodney W. Grimes 	sockt = new_sockt;
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	/*
1049b50d902SRodney W. Grimes 	 * Have the daemons delete the invitations now that we
1059b50d902SRodney W. Grimes 	 * have connected.
1069b50d902SRodney W. Grimes 	 */
1079b50d902SRodney W. Grimes 	current_state = "Waiting for your party to respond";
1089b50d902SRodney W. Grimes 	start_msgs();
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1119b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, DELETE, &response);
1129b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1139b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, DELETE, &response);
1149b50d902SRodney W. Grimes 	invitation_waiting = 0;
1159b50d902SRodney W. Grimes }
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes /*
1189b50d902SRodney W. Grimes  * Routine called on interupt to re-invite the callee
1199b50d902SRodney W. Grimes  */
1209b50d902SRodney W. Grimes void
1219b50d902SRodney W. Grimes re_invite()
1229b50d902SRodney W. Grimes {
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 	message("Ringing your party again");
125b02b5aadSAndrey A. Chernov 	waddch(my_win.x_win, '\n');
126b02b5aadSAndrey A. Chernov 	if (current_line < my_win.x_nlines - 1)
1279b50d902SRodney W. Grimes 		current_line++;
1289b50d902SRodney W. Grimes 	/* force a re-announce */
1299b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id + 1);
1309b50d902SRodney W. Grimes 	announce_invite();
1319b50d902SRodney W. Grimes 	longjmp(invitebuf, 1);
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes 
1349b50d902SRodney W. Grimes static	char *answers[] = {
1359b50d902SRodney W. Grimes 	"answer #0",					/* SUCCESS */
1369b50d902SRodney W. Grimes 	"Your party is not logged on",			/* NOT_HERE */
1379b50d902SRodney W. Grimes 	"Target machine is too confused to talk to us",	/* FAILED */
1389b50d902SRodney W. Grimes 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
1399b50d902SRodney W. Grimes 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
1409b50d902SRodney W. Grimes 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
1419b50d902SRodney W. Grimes 	"Target machine indicates protocol mismatch",	/* BADVERSION */
1429b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (addr)",/* BADADDR */
1439b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
1449b50d902SRodney W. Grimes };
1459b50d902SRodney W. Grimes #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes /*
1489b50d902SRodney W. Grimes  * Transmit the invitation and process the response
1499b50d902SRodney W. Grimes  */
1509b50d902SRodney W. Grimes announce_invite()
1519b50d902SRodney W. Grimes {
1529b50d902SRodney W. Grimes 	CTL_RESPONSE response;
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes 	current_state = "Trying to connect to your party's talk daemon";
1559b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
1569b50d902SRodney W. Grimes 	remote_id = response.id_num;
1579b50d902SRodney W. Grimes 	if (response.answer != SUCCESS) {
1589b50d902SRodney W. Grimes 		if (response.answer < NANSWERS)
1599b50d902SRodney W. Grimes 			message(answers[response.answer]);
1609b50d902SRodney W. Grimes 		quit();
1619b50d902SRodney W. Grimes 	}
1629b50d902SRodney W. Grimes 	/* leave the actual invitation on my talk daemon */
1639b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
1649b50d902SRodney W. Grimes 	local_id = response.id_num;
1659b50d902SRodney W. Grimes }
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes /*
1689b50d902SRodney W. Grimes  * Tell the daemon to remove your invitation
1699b50d902SRodney W. Grimes  */
1709b50d902SRodney W. Grimes send_delete()
1719b50d902SRodney W. Grimes {
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes 	msg.type = DELETE;
1749b50d902SRodney W. Grimes 	/*
1759b50d902SRodney W. Grimes 	 * This is just a extra clean up, so just send it
1769b50d902SRodney W. Grimes 	 * and don't wait for an answer
1779b50d902SRodney W. Grimes 	 */
1789b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1799b50d902SRodney W. Grimes 	daemon_addr.sin_addr = his_machine_addr;
1809b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1819b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1829b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof(msg))
1839b50d902SRodney W. Grimes 		perror("send_delete (remote)");
1849b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1859b50d902SRodney W. Grimes 	daemon_addr.sin_addr = my_machine_addr;
1869b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1879b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1889b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof (msg))
1899b50d902SRodney W. Grimes 		perror("send_delete (local)");
1909b50d902SRodney W. Grimes }
191