cleanup, get padding and archive creation right
[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 *
cb956e54 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
963ce42e 8#ifndef lint
a4e94754 9static char sccsid[] = "@(#)invite.c 5.8 (Berkeley) %G%";
f9ac90b4 10#endif /* not lint */
ec7915a9 11
a4e94754
KB
12#include <sys/types.h>
13#include <sys/socket.h>
ec7915a9
MK
14#include <sys/time.h>
15#include <signal.h>
a4e94754
KB
16#include <netinet/in.h>
17#include <protocols/talkd.h>
18#include <errno.h>
ec7915a9 19#include <setjmp.h>
a4e94754
KB
20#include "talk_ctl.h"
21#include "talk.h"
963ce42e
MK
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 */
35int local_id, remote_id;
36void re_invite();
37jmp_buf invitebuf;
ec7915a9
MK
38
39invite_remote()
40{
963ce42e
MK
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");
7038e9c5
MK
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
4fa2ac2d 55 msg.addr = *(struct sockaddr *)&my_addr;
7038e9c5 56#endif
4fa2ac2d 57 msg.id_num = htonl(-1); /* an impossible id_num */
963ce42e
MK
58 invitation_waiting = 1;
59 announce_invite();
ec7915a9 60 /*
963ce42e 61 * Shut off the automatic messages for a while,
ec7915a9
MK
62 * so we can use the interupt timer to resend the invitation
63 */
963ce42e
MK
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");
ec7915a9 73 }
963ce42e
MK
74 close(sockt);
75 sockt = new_sockt;
ec7915a9 76
963ce42e
MK
77 /*
78 * Have the daemons delete the invitations now that we
79 * have connected.
ec7915a9 80 */
963ce42e
MK
81 current_state = "Waiting for your party to respond";
82 start_msgs();
83
4fa2ac2d 84 msg.id_num = htonl(local_id);
963ce42e 85 ctl_transact(my_machine_addr, msg, DELETE, &response);
4fa2ac2d 86 msg.id_num = htonl(remote_id);
963ce42e
MK
87 ctl_transact(his_machine_addr, msg, DELETE, &response);
88 invitation_waiting = 0;
ec7915a9
MK
89}
90
963ce42e
MK
91/*
92 * Routine called on interupt to re-invite the callee
93 */
94void
95re_invite()
ec7915a9 96{
963ce42e
MK
97
98 message("Ringing your party again");
99 current_line++;
ec7915a9 100 /* force a re-announce */
4fa2ac2d 101 msg.id_num = htonl(remote_id + 1);
963ce42e
MK
102 announce_invite();
103 longjmp(invitebuf, 1);
ec7915a9
MK
104}
105
4fa2ac2d 106static char *answers[] = {
665b6bbc 107 "answer #0", /* SUCCESS */
4fa2ac2d 108 "Your party is not logged on", /* NOT_HERE */
4fa2ac2d 109 "Target machine is too confused to talk to us", /* FAILED */
665b6bbc 110 "Target machine does not recognize us", /* MACHINE_UNKNOWN */
4fa2ac2d 111 "Your party is refusing messages", /* PERMISSION_REFUSED */
665b6bbc 112 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
4fa2ac2d
KM
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
963ce42e
MK
119/*
120 * Transmit the invitation and process the response
121 */
ec7915a9
MK
122announce_invite()
123{
963ce42e
MK
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) {
4fa2ac2d
KM
130 if (response.answer < NANSWERS)
131 message(answers[response.answer]);
963ce42e 132 quit();
ec7915a9 133 }
ec7915a9 134 /* leave the actual invitation on my talk daemon */
963ce42e
MK
135 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
136 local_id = response.id_num;
ec7915a9 137}
4fa2ac2d 138
963ce42e
MK
139/*
140 * Tell the daemon to remove your invitation
141 */
ec7915a9
MK
142send_delete()
143{
963ce42e
MK
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 */
4fa2ac2d 150 msg.id_num = htonl(remote_id);
963ce42e 151 daemon_addr.sin_addr = his_machine_addr;
a4e94754
KB
152 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
153 (struct sockaddr *)&daemon_addr,
4fa2ac2d
KM
154 sizeof (daemon_addr)) != sizeof(msg))
155 perror("send_delete (remote)");
156 msg.id_num = htonl(local_id);
963ce42e 157 daemon_addr.sin_addr = my_machine_addr;
a4e94754
KB
158 if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
159 (struct sockaddr *)&daemon_addr,
4fa2ac2d
KM
160 sizeof (daemon_addr)) != sizeof (msg))
161 perror("send_delete (local)");
ec7915a9 162}