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