make output line buffered if selecting only some of the entries
[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
7038e9c5 19static char sccsid[] = "@(#)invite.c 5.6 (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");
7038e9c5
MK
54#ifdef MSG_EOR
55 /* copy new style sockaddr to old, swap family (short in old) */
56 msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/
57 msg.addr.sa_family = htons(my_addr.sin_family);
58#else
4fa2ac2d 59 msg.addr = *(struct sockaddr *)&my_addr;
7038e9c5 60#endif
4fa2ac2d 61 msg.id_num = htonl(-1); /* an impossible id_num */
963ce42e
MK
62 invitation_waiting = 1;
63 announce_invite();
ec7915a9 64 /*
963ce42e 65 * Shut off the automatic messages for a while,
ec7915a9
MK
66 * so we can use the interupt timer to resend the invitation
67 */
963ce42e
MK
68 end_msgs();
69 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
70 message("Waiting for your party to respond");
71 signal(SIGALRM, re_invite);
72 (void) setjmp(invitebuf);
73 while ((new_sockt = accept(sockt, 0, 0)) < 0) {
74 if (errno == EINTR)
75 continue;
76 p_error("Unable to connect with your party");
ec7915a9 77 }
963ce42e
MK
78 close(sockt);
79 sockt = new_sockt;
ec7915a9 80
963ce42e
MK
81 /*
82 * Have the daemons delete the invitations now that we
83 * have connected.
ec7915a9 84 */
963ce42e
MK
85 current_state = "Waiting for your party to respond";
86 start_msgs();
87
4fa2ac2d 88 msg.id_num = htonl(local_id);
963ce42e 89 ctl_transact(my_machine_addr, msg, DELETE, &response);
4fa2ac2d 90 msg.id_num = htonl(remote_id);
963ce42e
MK
91 ctl_transact(his_machine_addr, msg, DELETE, &response);
92 invitation_waiting = 0;
ec7915a9
MK
93}
94
963ce42e
MK
95/*
96 * Routine called on interupt to re-invite the callee
97 */
98void
99re_invite()
ec7915a9 100{
963ce42e
MK
101
102 message("Ringing your party again");
103 current_line++;
ec7915a9 104 /* force a re-announce */
4fa2ac2d 105 msg.id_num = htonl(remote_id + 1);
963ce42e
MK
106 announce_invite();
107 longjmp(invitebuf, 1);
ec7915a9
MK
108}
109
4fa2ac2d 110static char *answers[] = {
665b6bbc 111 "answer #0", /* SUCCESS */
4fa2ac2d 112 "Your party is not logged on", /* NOT_HERE */
4fa2ac2d 113 "Target machine is too confused to talk to us", /* FAILED */
665b6bbc 114 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
4fa2ac2d 115 "Your party is refusing messages", /* PERMISSION_REFUSED */
665b6bbc 116 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
4fa2ac2d
KM
117 "Target machine indicates protocol mismatch", /* BADVERSION */
118 "Target machine indicates protocol botch (addr)",/* BADADDR */
119 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
120};
121#define NANSWERS (sizeof (answers) / sizeof (answers[0]))
122
963ce42e
MK
123/*
124 * Transmit the invitation and process the response
125 */
ec7915a9
MK
126announce_invite()
127{
963ce42e
MK
128 CTL_RESPONSE response;
129
130 current_state = "Trying to connect to your party's talk daemon";
131 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
132 remote_id = response.id_num;
133 if (response.answer != SUCCESS) {
4fa2ac2d
KM
134 if (response.answer < NANSWERS)
135 message(answers[response.answer]);
963ce42e 136 quit();
ec7915a9 137 }
ec7915a9 138 /* leave the actual invitation on my talk daemon */
963ce42e
MK
139 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
140 local_id = response.id_num;
ec7915a9 141}
4fa2ac2d 142
963ce42e
MK
143/*
144 * Tell the daemon to remove your invitation
145 */
ec7915a9
MK
146send_delete()
147{
963ce42e
MK
148
149 msg.type = DELETE;
150 /*
151 * This is just a extra clean up, so just send it
152 * and don't wait for an answer
153 */
4fa2ac2d 154 msg.id_num = htonl(remote_id);
963ce42e 155 daemon_addr.sin_addr = his_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 (remote)");
159 msg.id_num = htonl(local_id);
963ce42e 160 daemon_addr.sin_addr = my_machine_addr;
4fa2ac2d
KM
161 if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr,
162 sizeof (daemon_addr)) != sizeof (msg))
163 perror("send_delete (local)");
ec7915a9 164}