]>
git.saurik.com Git - apple/network_cmds.git/blob - talk.tproj/invite.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/types.h>
59 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <protocols/talkd.h>
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
76 * The msg.id's for the invitations
77 * on the local and remote machines.
78 * These are used to delete the
81 int local_id
, remote_id
;
87 int nfd
, read_mask
, template, new_sockt
;
88 struct itimerval itimer
;
89 CTL_RESPONSE response
;
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");
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
);
101 msg
.addr
= *(struct sockaddr
*)&my_addr
;
103 msg
.id_num
= htonl(-1); /* an impossible id_num */
104 invitation_waiting
= 1;
107 * Shut off the automatic messages for a while,
108 * so we can use the interupt timer to resend the invitation
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) {
118 p_error("Unable to connect with your party");
124 * Have the daemons delete the invitations now that we
127 current_state
= "Waiting for your party to respond";
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;
138 * Routine called on interupt to re-invite the callee
144 message("Ringing your party again");
146 /* force a re-announce */
147 msg
.id_num
= htonl(remote_id
+ 1);
149 longjmp(invitebuf
, 1);
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 */
163 #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
166 * Transmit the invitation and process the response
170 CTL_RESPONSE response
;
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
]);
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
;
186 * Tell the daemon to remove your invitation
193 * This is just a extra clean up, so just send it
194 * and don't wait for an answer
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)");