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