xref: /original-bsd/usr.bin/talk/invite.c (revision c829ecf6)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)invite.c	5.7 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include "talk_ctl.h"
13 #include <sys/time.h>
14 #include <signal.h>
15 #include <setjmp.h>
16 
17 /*
18  * There wasn't an invitation waiting, so send a request containing
19  * our sockt address to the remote talk daemon so it can invite
20  * him
21  */
22 
23 /*
24  * The msg.id's for the invitations
25  * on the local and remote machines.
26  * These are used to delete the
27  * invitations.
28  */
29 int	local_id, remote_id;
30 void	re_invite();
31 jmp_buf invitebuf;
32 
33 invite_remote()
34 {
35 	int nfd, read_mask, template, new_sockt;
36 	struct itimerval itimer;
37 	CTL_RESPONSE response;
38 
39 	itimer.it_value.tv_sec = RING_WAIT;
40 	itimer.it_value.tv_usec = 0;
41 	itimer.it_interval = itimer.it_value;
42 	if (listen(sockt, 5) != 0)
43 		p_error("Error on attempt to listen for caller");
44 #ifdef MSG_EOR
45 	/* copy new style sockaddr to old, swap family (short in old) */
46 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
47 	msg.addr.sa_family = htons(my_addr.sin_family);
48 #else
49 	msg.addr = *(struct sockaddr *)&my_addr;
50 #endif
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