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