BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.bin / talk / invite.c
CommitLineData
d0aeaf5a 1/*
ad787160
C
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
f9ac90b4 4 *
ad787160
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
d0aeaf5a
DF
32 */
33
963ce42e 34#ifndef lint
ad787160 35static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
f9ac90b4 36#endif /* not lint */
ec7915a9 37
a4e94754
KB
38#include <sys/types.h>
39#include <sys/socket.h>
ec7915a9
MK
40#include <sys/time.h>
41#include <signal.h>
a4e94754
KB
42#include <netinet/in.h>
43#include <protocols/talkd.h>
44#include <errno.h>
ec7915a9 45#include <setjmp.h>
a4e94754
KB
46#include "talk_ctl.h"
47#include "talk.h"
963ce42e
MK
48
49/*
50 * There wasn't an invitation waiting, so send a request containing
51 * our sockt address to the remote talk daemon so it can invite
52 * him
53 */
54
55/*
56 * The msg.id's for the invitations
57 * on the local and remote machines.
58 * These are used to delete the
59 * invitations.
60 */
61int local_id, remote_id;
62void re_invite();
63jmp_buf invitebuf;
ec7915a9
MK
64
65invite_remote()
66{
963ce42e
MK
67 int nfd, read_mask, template, new_sockt;
68 struct itimerval itimer;
69 CTL_RESPONSE response;
70
71 itimer.it_value.tv_sec = RING_WAIT;
72 itimer.it_value.tv_usec = 0;
73 itimer.it_interval = itimer.it_value;
74 if (listen(sockt, 5) != 0)
75 p_error("Error on attempt to listen for caller");
7038e9c5
MK
76#ifdef MSG_EOR
77 /* copy new style sockaddr to old, swap family (short in old) */
78 msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/
79 msg.addr.sa_family = htons(my_addr.sin_family);
80#else
4fa2ac2d 81 msg.addr = *(struct sockaddr *)&my_addr;
7038e9c5 82#endif
4fa2ac2d 83 msg.id_num = htonl(-1); /* an impossible id_num */
963ce42e
MK
84 invitation_waiting = 1;
85 announce_invite();
ec7915a9 86 /*
963ce42e 87 * Shut off the automatic messages for a while,
ec7915a9
MK
88 * so we can use the interupt timer to resend the invitation
89 */
963ce42e
MK
90 end_msgs();
91 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
92 message("Waiting for your party to respond");
93 signal(SIGALRM, re_invite);
94 (void) setjmp(invitebuf);
95 while ((new_sockt = accept(sockt, 0, 0)) < 0) {
96 if (errno == EINTR)
97 continue;
98 p_error("Unable to connect with your party");
ec7915a9 99 }
963ce42e
MK
100 close(sockt);
101 sockt = new_sockt;
ec7915a9 102
963ce42e
MK
103 /*
104 * Have the daemons delete the invitations now that we
105 * have connected.
ec7915a9 106 */
963ce42e
MK
107 current_state = "Waiting for your party to respond";
108 start_msgs();
109
4fa2ac2d 110 msg.id_num = htonl(local_id);
963ce42e 111 ctl_transact(my_machine_addr, msg, DELETE, &response);
4fa2ac2d 112 msg.id_num = htonl(remote_id);
963ce42e
MK
113 ctl_transact(his_machine_addr, msg, DELETE, &response);
114 invitation_waiting = 0;
ec7915a9
MK
115}
116
963ce42e
MK
117/*
118 * Routine called on interupt to re-invite the callee
119 */
120void
121re_invite()
ec7915a9 122{
963ce42e
MK
123
124 message("Ringing your party again");
125 current_line++;
ec7915a9 126 /* force a re-announce */
4fa2ac2d 127 msg.id_num = htonl(remote_id + 1);
963ce42e
MK
128 announce_invite();
129 longjmp(invitebuf, 1);
ec7915a9
MK
130}
131
4fa2ac2d 132static char *answers[] = {
665b6bbc 133 "answer #0", /* SUCCESS */
4fa2ac2d 134 "Your party is not logged on", /* NOT_HERE */
4fa2ac2d 135 "Target machine is too confused to talk to us", /* FAILED */
665b6bbc 136 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
4fa2ac2d 137 "Your party is refusing messages", /* PERMISSION_REFUSED */
665b6bbc 138 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
4fa2ac2d
KM
139 "Target machine indicates protocol mismatch", /* BADVERSION */
140 "Target machine indicates protocol botch (addr)",/* BADADDR */
141 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
142};
143#define NANSWERS (sizeof (answers) / sizeof (answers[0]))
144
963ce42e
MK
145/*
146 * Transmit the invitation and process the response
147 */
ec7915a9
MK
148announce_invite()
149{
963ce42e
MK
150 CTL_RESPONSE response;
151
152 current_state = "Trying to connect to your party's talk daemon";
153 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
154 remote_id = response.id_num;
155 if (response.answer != SUCCESS) {
4fa2ac2d
KM
156 if (response.answer < NANSWERS)
157 message(answers[response.answer]);
963ce42e 158 quit();
ec7915a9 159 }
ec7915a9 160 /* leave the actual invitation on my talk daemon */
963ce42e
MK
161 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
162 local_id = response.id_num;
ec7915a9 163}
4fa2ac2d 164
963ce42e
MK
165/*
166 * Tell the daemon to remove your invitation
167 */
ec7915a9
MK
168send_delete()
169{
963ce42e
MK
170
171 msg.type = DELETE;
172 /*
173 * This is just a extra clean up, so just send it
174 * and don't wait for an answer
175 */
4fa2ac2d 176 msg.id_num = htonl(remote_id);
963ce42e 177 daemon_addr.sin_addr = his_machine_addr;
a4e94754
KB
178 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
179 (struct sockaddr *)&daemon_addr,
4fa2ac2d
KM
180 sizeof (daemon_addr)) != sizeof(msg))
181 perror("send_delete (remote)");
182 msg.id_num = htonl(local_id);
963ce42e 183 daemon_addr.sin_addr = my_machine_addr;
a4e94754
KB
184 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
185 (struct sockaddr *)&daemon_addr,
4fa2ac2d
KM
186 sizeof (daemon_addr)) != sizeof (msg))
187 perror("send_delete (local)");
ec7915a9 188}