Commit | Line | Data |
---|---|---|
d0aeaf5a DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
963ce42e | 7 | #ifndef lint |
4fa2ac2d | 8 | static char sccsid[] = "@(#)invite.c 5.2 (Berkeley) %G%"; |
d0aeaf5a | 9 | #endif not lint |
ec7915a9 MK |
10 | |
11 | #include "talk_ctl.h" | |
12 | #include <sys/time.h> | |
13 | #include <signal.h> | |
14 | #include <setjmp.h> | |
963ce42e MK |
15 | |
16 | /* | |
17 | * There wasn't an invitation waiting, so send a request containing | |
18 | * our sockt address to the remote talk daemon so it can invite | |
19 | * him | |
20 | */ | |
21 | ||
22 | /* | |
23 | * The msg.id's for the invitations | |
24 | * on the local and remote machines. | |
25 | * These are used to delete the | |
26 | * invitations. | |
27 | */ | |
28 | int local_id, remote_id; | |
29 | void re_invite(); | |
30 | jmp_buf invitebuf; | |
ec7915a9 MK |
31 | |
32 | invite_remote() | |
33 | { | |
963ce42e MK |
34 | int nfd, read_mask, template, new_sockt; |
35 | struct itimerval itimer; | |
36 | CTL_RESPONSE response; | |
37 | ||
38 | itimer.it_value.tv_sec = RING_WAIT; | |
39 | itimer.it_value.tv_usec = 0; | |
40 | itimer.it_interval = itimer.it_value; | |
41 | if (listen(sockt, 5) != 0) | |
42 | p_error("Error on attempt to listen for caller"); | |
4fa2ac2d KM |
43 | msg.addr = *(struct sockaddr *)&my_addr; |
44 | msg.addr.sa_family = htons(msg.addr.sa_family); | |
45 | msg.id_num = htonl(-1); /* an impossible id_num */ | |
963ce42e MK |
46 | invitation_waiting = 1; |
47 | announce_invite(); | |
ec7915a9 | 48 | /* |
963ce42e | 49 | * Shut off the automatic messages for a while, |
ec7915a9 MK |
50 | * so we can use the interupt timer to resend the invitation |
51 | */ | |
963ce42e MK |
52 | end_msgs(); |
53 | setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); | |
54 | message("Waiting for your party to respond"); | |
55 | signal(SIGALRM, re_invite); | |
56 | (void) setjmp(invitebuf); | |
57 | while ((new_sockt = accept(sockt, 0, 0)) < 0) { | |
58 | if (errno == EINTR) | |
59 | continue; | |
60 | p_error("Unable to connect with your party"); | |
ec7915a9 | 61 | } |
963ce42e MK |
62 | close(sockt); |
63 | sockt = new_sockt; | |
ec7915a9 | 64 | |
963ce42e MK |
65 | /* |
66 | * Have the daemons delete the invitations now that we | |
67 | * have connected. | |
ec7915a9 | 68 | */ |
963ce42e MK |
69 | current_state = "Waiting for your party to respond"; |
70 | start_msgs(); | |
71 | ||
4fa2ac2d | 72 | msg.id_num = htonl(local_id); |
963ce42e | 73 | ctl_transact(my_machine_addr, msg, DELETE, &response); |
4fa2ac2d | 74 | msg.id_num = htonl(remote_id); |
963ce42e MK |
75 | ctl_transact(his_machine_addr, msg, DELETE, &response); |
76 | invitation_waiting = 0; | |
ec7915a9 MK |
77 | } |
78 | ||
963ce42e MK |
79 | /* |
80 | * Routine called on interupt to re-invite the callee | |
81 | */ | |
82 | void | |
83 | re_invite() | |
ec7915a9 | 84 | { |
963ce42e MK |
85 | |
86 | message("Ringing your party again"); | |
87 | current_line++; | |
ec7915a9 | 88 | /* force a re-announce */ |
4fa2ac2d | 89 | msg.id_num = htonl(remote_id + 1); |
963ce42e MK |
90 | announce_invite(); |
91 | longjmp(invitebuf, 1); | |
ec7915a9 MK |
92 | } |
93 | ||
4fa2ac2d KM |
94 | static char *answers[] = { |
95 | "Your party is not logged on", /* NOT_HERE */ | |
96 | "Target machine does not recognize us", /* MACHINE_UNKNOWN */ | |
97 | "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */ | |
98 | "Target machine is too confused to talk to us", /* FAILED */ | |
99 | "Your party is refusing messages", /* PERMISSION_REFUSED */ | |
100 | "Target machine indicates protocol mismatch", /* BADVERSION */ | |
101 | "Target machine indicates protocol botch (addr)",/* BADADDR */ | |
102 | "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */ | |
103 | }; | |
104 | #define NANSWERS (sizeof (answers) / sizeof (answers[0])) | |
105 | ||
963ce42e MK |
106 | /* |
107 | * Transmit the invitation and process the response | |
108 | */ | |
ec7915a9 MK |
109 | announce_invite() |
110 | { | |
963ce42e MK |
111 | CTL_RESPONSE response; |
112 | ||
113 | current_state = "Trying to connect to your party's talk daemon"; | |
114 | ctl_transact(his_machine_addr, msg, ANNOUNCE, &response); | |
115 | remote_id = response.id_num; | |
116 | if (response.answer != SUCCESS) { | |
4fa2ac2d KM |
117 | if (response.answer < NANSWERS) |
118 | message(answers[response.answer]); | |
963ce42e | 119 | quit(); |
ec7915a9 | 120 | } |
ec7915a9 | 121 | /* leave the actual invitation on my talk daemon */ |
963ce42e MK |
122 | ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response); |
123 | local_id = response.id_num; | |
ec7915a9 | 124 | } |
4fa2ac2d | 125 | |
963ce42e MK |
126 | /* |
127 | * Tell the daemon to remove your invitation | |
128 | */ | |
ec7915a9 MK |
129 | send_delete() |
130 | { | |
963ce42e MK |
131 | |
132 | msg.type = DELETE; | |
133 | /* | |
134 | * This is just a extra clean up, so just send it | |
135 | * and don't wait for an answer | |
136 | */ | |
4fa2ac2d | 137 | msg.id_num = htonl(remote_id); |
963ce42e | 138 | daemon_addr.sin_addr = his_machine_addr; |
4fa2ac2d KM |
139 | if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr, |
140 | sizeof (daemon_addr)) != sizeof(msg)) | |
141 | perror("send_delete (remote)"); | |
142 | msg.id_num = htonl(local_id); | |
963ce42e | 143 | daemon_addr.sin_addr = my_machine_addr; |
4fa2ac2d KM |
144 | if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr, |
145 | sizeof (daemon_addr)) != sizeof (msg)) | |
146 | perror("send_delete (local)"); | |
ec7915a9 | 147 | } |