xref: /original-bsd/usr.bin/talk/invite.c (revision b424313c)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)invite.c	5.4 (Berkeley) 05/20/88";
15 #endif /* not lint */
16 
17 #include "talk_ctl.h"
18 #include <sys/time.h>
19 #include <signal.h>
20 #include <setjmp.h>
21 
22 /*
23  * There wasn't an invitation waiting, so send a request containing
24  * our sockt address to the remote talk daemon so it can invite
25  * him
26  */
27 
28 /*
29  * The msg.id's for the invitations
30  * on the local and remote machines.
31  * These are used to delete the
32  * invitations.
33  */
34 int	local_id, remote_id;
35 void	re_invite();
36 jmp_buf invitebuf;
37 
38 invite_remote()
39 {
40 	int nfd, read_mask, template, new_sockt;
41 	struct itimerval itimer;
42 	CTL_RESPONSE response;
43 
44 	itimer.it_value.tv_sec = RING_WAIT;
45 	itimer.it_value.tv_usec = 0;
46 	itimer.it_interval = itimer.it_value;
47 	if (listen(sockt, 5) != 0)
48 		p_error("Error on attempt to listen for caller");
49 	msg.addr = *(struct sockaddr *)&my_addr;
50 	msg.addr.sa_family = htons(msg.addr.sa_family);
51 	msg.id_num = htonl(-1);		/* an impossible id_num */
52 	invitation_waiting = 1;
53 	announce_invite();
54 	/*
55 	 * Shut off the automatic messages for a while,
56 	 * so we can use the interupt timer to resend the invitation
57 	 */
58 	end_msgs();
59 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
60 	message("Waiting for your party to respond");
61 	signal(SIGALRM, re_invite);
62 	(void) setjmp(invitebuf);
63 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
64 		if (errno == EINTR)
65 			continue;
66 		p_error("Unable to connect with your party");
67 	}
68 	close(sockt);
69 	sockt = new_sockt;
70 
71 	/*
72 	 * Have the daemons delete the invitations now that we
73 	 * have connected.
74 	 */
75 	current_state = "Waiting for your party to respond";
76 	start_msgs();
77 
78 	msg.id_num = htonl(local_id);
79 	ctl_transact(my_machine_addr, msg, DELETE, &response);
80 	msg.id_num = htonl(remote_id);
81 	ctl_transact(his_machine_addr, msg, DELETE, &response);
82 	invitation_waiting = 0;
83 }
84 
85 /*
86  * Routine called on interupt to re-invite the callee
87  */
88 void
89 re_invite()
90 {
91 
92 	message("Ringing your party again");
93 	current_line++;
94 	/* force a re-announce */
95 	msg.id_num = htonl(remote_id + 1);
96 	announce_invite();
97 	longjmp(invitebuf, 1);
98 }
99 
100 static	char *answers[] = {
101 	"answer #0",					/* SUCCESS */
102 	"Your party is not logged on",			/* NOT_HERE */
103 	"Target machine is too confused to talk to us",	/* FAILED */
104 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
105 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
106 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
107 	"Target machine indicates protocol mismatch",	/* BADVERSION */
108 	"Target machine indicates protocol botch (addr)",/* BADADDR */
109 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
110 };
111 #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
112 
113 /*
114  * Transmit the invitation and process the response
115  */
116 announce_invite()
117 {
118 	CTL_RESPONSE response;
119 
120 	current_state = "Trying to connect to your party's talk daemon";
121 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
122 	remote_id = response.id_num;
123 	if (response.answer != SUCCESS) {
124 		if (response.answer < NANSWERS)
125 			message(answers[response.answer]);
126 		quit();
127 	}
128 	/* leave the actual invitation on my talk daemon */
129 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
130 	local_id = response.id_num;
131 }
132 
133 /*
134  * Tell the daemon to remove your invitation
135  */
136 send_delete()
137 {
138 
139 	msg.type = DELETE;
140 	/*
141 	 * This is just a extra clean up, so just send it
142 	 * and don't wait for an answer
143 	 */
144 	msg.id_num = htonl(remote_id);
145 	daemon_addr.sin_addr = his_machine_addr;
146 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr,
147 	    sizeof (daemon_addr)) != sizeof(msg))
148 		perror("send_delete (remote)");
149 	msg.id_num = htonl(local_id);
150 	daemon_addr.sin_addr = my_machine_addr;
151 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr,
152 	    sizeof (daemon_addr)) != sizeof (msg))
153 		perror("send_delete (local)");
154 }
155