1 /* $OpenBSD: invite.c,v 1.14 2009/10/27 23:59:44 deraadt 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 #include "talk.h" 34 #include <arpa/inet.h> 35 #include <sys/time.h> 36 #include <netdb.h> 37 #include <errno.h> 38 #include <setjmp.h> 39 #include <unistd.h> 40 #include "talk_ctl.h" 41 42 #define STRING_LENGTH 158 43 44 /* 45 * There wasn't an invitation waiting, so send a request containing 46 * our sockt address to the remote talk daemon so it can invite 47 * him 48 */ 49 50 /* 51 * The msg.id's for the invitations 52 * on the local and remote machines. 53 * These are used to delete the 54 * invitations. 55 */ 56 int local_id, remote_id; 57 jmp_buf invitebuf; 58 59 void 60 invite_remote(void) 61 { 62 int new_sockt; 63 struct itimerval itimer; 64 CTL_RESPONSE response; 65 struct sockaddr rp; 66 socklen_t rplen = sizeof(struct sockaddr); 67 struct hostent *rphost; 68 char rname[STRING_LENGTH]; 69 70 itimer.it_value.tv_sec = RING_WAIT; 71 itimer.it_value.tv_usec = 0; 72 itimer.it_interval = itimer.it_value; 73 if (listen(sockt, 5) != 0) 74 quit("Error on attempt to listen for caller", 1); 75 #ifdef MSG_EOR 76 /* copy new style sockaddr to old, swap family (short in old) */ 77 msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/ 78 msg.addr.sa_family = htons(my_addr.sin_family); 79 #else 80 msg.addr = *(struct sockaddr *)&my_addr; 81 #endif 82 msg.id_num = htonl(-1); /* an impossible id_num */ 83 invitation_waiting = 1; 84 announce_invite(); 85 /* 86 * Shut off the automatic messages for a while, 87 * so we can use the interrupt timer to resend the invitation. 88 * We no longer turn automatic messages back on to avoid a bonus 89 * message after we've connected; this is okay even though end_msgs() 90 * gets called again in main(). 91 */ 92 end_msgs(); 93 setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); 94 message("Waiting for your party to respond"); 95 signal(SIGALRM, re_invite); 96 (void) setjmp(invitebuf); 97 while ((new_sockt = accept(sockt, &rp, &rplen)) == -1) { 98 if (errno == EINTR || errno == ECONNABORTED) 99 continue; 100 quit("Unable to connect with your party", 1); 101 } 102 close(sockt); 103 sockt = new_sockt; 104 105 /* 106 * Have the daemons delete the invitations now that we 107 * have connected. 108 */ 109 msg.id_num = htonl(local_id); 110 ctl_transact(my_machine_addr, msg, DELETE, &response); 111 msg.id_num = htonl(remote_id); 112 ctl_transact(his_machine_addr, msg, DELETE, &response); 113 invitation_waiting = 0; 114 115 /* 116 * Check to see if the other guy is coming from the machine 117 * we expect. 118 */ 119 if (his_machine_addr.s_addr != 120 ((struct sockaddr_in *)&rp)->sin_addr.s_addr) { 121 rphost = gethostbyaddr((char *) &((struct sockaddr_in 122 *)&rp)->sin_addr, sizeof(struct in_addr), AF_INET); 123 if (rphost) 124 snprintf(rname, STRING_LENGTH, 125 "Answering talk request from %s@%s", msg.r_name, 126 rphost->h_name); 127 else 128 snprintf(rname, STRING_LENGTH, 129 "Answering talk request from %s@%s", msg.r_name, 130 inet_ntoa(((struct sockaddr_in *)&rp)->sin_addr)); 131 message(rname); 132 } 133 } 134 135 /* 136 * Routine called on interrupt to re-invite the callee 137 */ 138 void 139 re_invite(int dummy) 140 { 141 message("Ringing your party again"); 142 /* force a re-announce */ 143 msg.id_num = htonl(remote_id + 1); 144 announce_invite(); 145 longjmp(invitebuf, 1); 146 } 147 148 static char *answers[] = { 149 "answer #0", /* SUCCESS */ 150 "Your party is not logged on", /* NOT_HERE */ 151 "Target machine is too confused to talk to us", /* FAILED */ 152 "Target machine does not recognize us", /* MACHINE_UNKNOWN */ 153 "Your party is refusing messages", /* PERMISSION_REFUSED */ 154 "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */ 155 "Target machine indicates protocol mismatch", /* BADVERSION */ 156 "Target machine indicates protocol botch (addr)",/* BADADDR */ 157 "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */ 158 }; 159 #define NANSWERS (sizeof (answers) / sizeof (answers[0])) 160 161 /* 162 * Transmit the invitation and process the response 163 */ 164 void 165 announce_invite(void) 166 { 167 CTL_RESPONSE response; 168 169 current_state = "Trying to connect to your party's talk daemon"; 170 ctl_transact(his_machine_addr, msg, ANNOUNCE, &response); 171 remote_id = response.id_num; 172 if (response.answer != SUCCESS) 173 quit(response.answer < NANSWERS ? answers[response.answer] : NULL, 0); 174 /* leave the actual invitation on my talk daemon */ 175 ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response); 176 local_id = response.id_num; 177 } 178 179 /* 180 * Tell the daemon to remove your invitation 181 */ 182 void 183 send_delete(void) 184 { 185 186 msg.type = DELETE; 187 /* 188 * This is just a extra clean up, so just send it 189 * and don't wait for an answer 190 */ 191 msg.id_num = htonl(remote_id); 192 daemon_addr.sin_addr = his_machine_addr; 193 if (sendto(ctl_sockt, &msg, sizeof (msg), 0, 194 (struct sockaddr *)&daemon_addr, 195 sizeof (daemon_addr)) != sizeof(msg)) 196 warn("send_delete (remote)"); 197 msg.id_num = htonl(local_id); 198 daemon_addr.sin_addr = my_machine_addr; 199 if (sendto(ctl_sockt, &msg, sizeof (msg), 0, 200 (struct sockaddr *)&daemon_addr, 201 sizeof (daemon_addr)) != sizeof (msg)) 202 warn("send_delete (local)"); 203 } 204