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