Annotation of src/usr.bin/talk/invite.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: invite.c,v 1.5 1998/04/28 22:13:27 pjanzen Exp $ */
1.1 deraadt 2: /* $NetBSD: invite.c,v 1.3 1994/12/09 02:14:18 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
40: #endif
1.6 ! millert 41: static char rcsid[] = "$OpenBSD: invite.c,v 1.5 1998/04/28 22:13:27 pjanzen Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
1.5 pjanzen 44: #include "talk.h"
45: #include <arpa/inet.h>
1.1 deraadt 46: #include <sys/time.h>
47: #include <signal.h>
1.4 pjanzen 48: #include <netdb.h>
1.1 deraadt 49: #include <errno.h>
50: #include <setjmp.h>
1.5 pjanzen 51: #include <unistd.h>
1.1 deraadt 52: #include "talk_ctl.h"
53:
1.4 pjanzen 54: #define STRING_LENGTH 158
55:
1.1 deraadt 56: /*
57: * There wasn't an invitation waiting, so send a request containing
58: * our sockt address to the remote talk daemon so it can invite
1.6 ! millert 59: * him
1.1 deraadt 60: */
61:
62: /*
63: * The msg.id's for the invitations
64: * on the local and remote machines.
1.6 ! millert 65: * These are used to delete the
1.1 deraadt 66: * invitations.
67: */
68: int local_id, remote_id;
69: jmp_buf invitebuf;
70:
1.5 pjanzen 71: void
1.1 deraadt 72: invite_remote()
73: {
1.5 pjanzen 74: int new_sockt;
1.1 deraadt 75: struct itimerval itimer;
76: CTL_RESPONSE response;
1.4 pjanzen 77: struct sockaddr rp;
78: int rplen = sizeof(struct sockaddr);
79: struct hostent *rphost;
80: char rname[STRING_LENGTH];
1.1 deraadt 81:
82: itimer.it_value.tv_sec = RING_WAIT;
83: itimer.it_value.tv_usec = 0;
84: itimer.it_interval = itimer.it_value;
85: if (listen(sockt, 5) != 0)
86: p_error("Error on attempt to listen for caller");
87: #ifdef MSG_EOR
88: /* copy new style sockaddr to old, swap family (short in old) */
89: msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/
90: msg.addr.sa_family = htons(my_addr.sin_family);
91: #else
92: msg.addr = *(struct sockaddr *)&my_addr;
93: #endif
94: msg.id_num = htonl(-1); /* an impossible id_num */
95: invitation_waiting = 1;
96: announce_invite();
97: /*
98: * Shut off the automatic messages for a while,
1.4 pjanzen 99: * so we can use the interrupt timer to resend the invitation.
100: * We no longer turn automatic messages back on to avoid a bonus
101: * message after we've connected; this is okay even though end_msgs()
102: * gets called again in main().
1.1 deraadt 103: */
104: end_msgs();
105: setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
106: message("Waiting for your party to respond");
107: signal(SIGALRM, re_invite);
108: (void) setjmp(invitebuf);
1.4 pjanzen 109: while ((new_sockt = accept(sockt, &rp, &rplen)) < 0) {
1.1 deraadt 110: if (errno == EINTR)
111: continue;
112: p_error("Unable to connect with your party");
113: }
114: close(sockt);
115: sockt = new_sockt;
116:
117: /*
118: * Have the daemons delete the invitations now that we
119: * have connected.
120: */
121: msg.id_num = htonl(local_id);
122: ctl_transact(my_machine_addr, msg, DELETE, &response);
123: msg.id_num = htonl(remote_id);
124: ctl_transact(his_machine_addr, msg, DELETE, &response);
125: invitation_waiting = 0;
1.4 pjanzen 126:
127: /*
128: * Check to see if the other guy is coming from the machine
129: * we expect.
130: */
131: if (his_machine_addr.s_addr !=
132: ((struct sockaddr_in *)&rp)->sin_addr.s_addr) {
133: rphost = gethostbyaddr((char *) &((struct sockaddr_in
134: *)&rp)->sin_addr, sizeof(struct in_addr), AF_INET);
135: if (rphost)
136: snprintf(rname, STRING_LENGTH,
137: "Answering talk request from %s@%s", msg.r_name,
138: rphost->h_name);
139: else
140: snprintf(rname, STRING_LENGTH,
141: "Answering talk request from %s@%s", msg.r_name,
142: inet_ntoa(((struct sockaddr_in *)&rp)->sin_addr));
143: message(rname);
144: }
1.1 deraadt 145: }
146:
147: /*
148: * Routine called on interupt to re-invite the callee
149: */
150: void
1.5 pjanzen 151: re_invite(dummy)
152: int dummy;
1.1 deraadt 153: {
154: message("Ringing your party again");
155: /* force a re-announce */
156: msg.id_num = htonl(remote_id + 1);
157: announce_invite();
158: longjmp(invitebuf, 1);
159: }
160:
161: static char *answers[] = {
162: "answer #0", /* SUCCESS */
163: "Your party is not logged on", /* NOT_HERE */
164: "Target machine is too confused to talk to us", /* FAILED */
165: "Target machine does not recognize us", /* MACHINE_UNKNOWN */
166: "Your party is refusing messages", /* PERMISSION_REFUSED */
167: "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
168: "Target machine indicates protocol mismatch", /* BADVERSION */
169: "Target machine indicates protocol botch (addr)",/* BADADDR */
170: "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
171: };
1.5 pjanzen 172: #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
1.1 deraadt 173:
174: /*
175: * Transmit the invitation and process the response
176: */
1.5 pjanzen 177: void
1.1 deraadt 178: announce_invite()
179: {
180: CTL_RESPONSE response;
181:
182: current_state = "Trying to connect to your party's talk daemon";
183: ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
184: remote_id = response.id_num;
185: if (response.answer != SUCCESS) {
186: if (response.answer < NANSWERS)
187: message(answers[response.answer]);
188: quit();
189: }
190: /* leave the actual invitation on my talk daemon */
191: ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
192: local_id = response.id_num;
193: }
194:
195: /*
196: * Tell the daemon to remove your invitation
197: */
1.5 pjanzen 198: void
1.1 deraadt 199: send_delete()
200: {
201:
202: msg.type = DELETE;
203: /*
204: * This is just a extra clean up, so just send it
205: * and don't wait for an answer
206: */
207: msg.id_num = htonl(remote_id);
208: daemon_addr.sin_addr = his_machine_addr;
209: if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
210: (struct sockaddr *)&daemon_addr,
211: sizeof (daemon_addr)) != sizeof(msg))
1.6 ! millert 212: warn("send_delete (remote)");
1.1 deraadt 213: msg.id_num = htonl(local_id);
214: daemon_addr.sin_addr = my_machine_addr;
215: if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
216: (struct sockaddr *)&daemon_addr,
217: sizeof (daemon_addr)) != sizeof (msg))
1.6 ! millert 218: warn("send_delete (local)");
1.1 deraadt 219: }