19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 3520868061SPhilippe Charnier #if 0 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93"; 3720868061SPhilippe Charnier #endif 3820868061SPhilippe Charnier static const char rcsid[] = 39c3aac50fSPeter Wemm "$FreeBSD$"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 4220868061SPhilippe Charnier #include <err.h> 439b50d902SRodney W. Grimes #include <errno.h> 449b50d902SRodney W. Grimes #include <setjmp.h> 4520868061SPhilippe Charnier #include <signal.h> 4620868061SPhilippe Charnier #include <sys/types.h> 4720868061SPhilippe Charnier #include <sys/socket.h> 4820868061SPhilippe Charnier #include <protocols/talkd.h> 499b50d902SRodney W. Grimes #include "talk_ctl.h" 509b50d902SRodney W. Grimes #include "talk.h" 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes /* 539b50d902SRodney W. Grimes * There wasn't an invitation waiting, so send a request containing 549b50d902SRodney W. Grimes * our sockt address to the remote talk daemon so it can invite 559b50d902SRodney W. Grimes * him 569b50d902SRodney W. Grimes */ 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes /* 599b50d902SRodney W. Grimes * The msg.id's for the invitations 609b50d902SRodney W. Grimes * on the local and remote machines. 619b50d902SRodney W. Grimes * These are used to delete the 629b50d902SRodney W. Grimes * invitations. 639b50d902SRodney W. Grimes */ 649b50d902SRodney W. Grimes int local_id, remote_id; 659b50d902SRodney W. Grimes jmp_buf invitebuf; 669b50d902SRodney W. Grimes 6728592604SJoerg Wunsch void 689b50d902SRodney W. Grimes invite_remote() 699b50d902SRodney W. Grimes { 709b50d902SRodney W. Grimes int nfd, read_mask, template, new_sockt; 719b50d902SRodney W. Grimes struct itimerval itimer; 729b50d902SRodney W. Grimes CTL_RESPONSE response; 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes itimer.it_value.tv_sec = RING_WAIT; 759b50d902SRodney W. Grimes itimer.it_value.tv_usec = 0; 769b50d902SRodney W. Grimes itimer.it_interval = itimer.it_value; 779b50d902SRodney W. Grimes if (listen(sockt, 5) != 0) 789b50d902SRodney W. Grimes p_error("Error on attempt to listen for caller"); 799b50d902SRodney W. Grimes #ifdef MSG_EOR 809b50d902SRodney W. Grimes /* copy new style sockaddr to old, swap family (short in old) */ 819b50d902SRodney W. Grimes msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/ 829b50d902SRodney W. Grimes msg.addr.sa_family = htons(my_addr.sin_family); 839b50d902SRodney W. Grimes #else 849b50d902SRodney W. Grimes msg.addr = *(struct sockaddr *)&my_addr; 859b50d902SRodney W. Grimes #endif 869b50d902SRodney W. Grimes msg.id_num = htonl(-1); /* an impossible id_num */ 879b50d902SRodney W. Grimes invitation_waiting = 1; 889b50d902SRodney W. Grimes announce_invite(); 899b50d902SRodney W. Grimes /* 909b50d902SRodney W. Grimes * Shut off the automatic messages for a while, 919b50d902SRodney W. Grimes * so we can use the interupt timer to resend the invitation 929b50d902SRodney W. Grimes */ 939b50d902SRodney W. Grimes end_msgs(); 949b50d902SRodney W. Grimes setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); 959b50d902SRodney W. Grimes message("Waiting for your party to respond"); 969b50d902SRodney W. Grimes signal(SIGALRM, re_invite); 979b50d902SRodney W. Grimes (void) setjmp(invitebuf); 989b50d902SRodney W. Grimes while ((new_sockt = accept(sockt, 0, 0)) < 0) { 999b50d902SRodney W. Grimes if (errno == EINTR) 1009b50d902SRodney W. Grimes continue; 1019b50d902SRodney W. Grimes p_error("Unable to connect with your party"); 1029b50d902SRodney W. Grimes } 1039b50d902SRodney W. Grimes close(sockt); 1049b50d902SRodney W. Grimes sockt = new_sockt; 1059b50d902SRodney W. Grimes 1069b50d902SRodney W. Grimes /* 1079b50d902SRodney W. Grimes * Have the daemons delete the invitations now that we 1089b50d902SRodney W. Grimes * have connected. 1099b50d902SRodney W. Grimes */ 1109b50d902SRodney W. Grimes current_state = "Waiting for your party to respond"; 1119b50d902SRodney W. Grimes start_msgs(); 1129b50d902SRodney W. Grimes 1139b50d902SRodney W. Grimes msg.id_num = htonl(local_id); 1149b50d902SRodney W. Grimes ctl_transact(my_machine_addr, msg, DELETE, &response); 1159b50d902SRodney W. Grimes msg.id_num = htonl(remote_id); 1169b50d902SRodney W. Grimes ctl_transact(his_machine_addr, msg, DELETE, &response); 1179b50d902SRodney W. Grimes invitation_waiting = 0; 1189b50d902SRodney W. Grimes } 1199b50d902SRodney W. Grimes 1209b50d902SRodney W. Grimes /* 1219b50d902SRodney W. Grimes * Routine called on interupt to re-invite the callee 1229b50d902SRodney W. Grimes */ 12328592604SJoerg Wunsch /* ARGSUSED */ 1249b50d902SRodney W. Grimes void 12528592604SJoerg Wunsch re_invite(signo) 12628592604SJoerg Wunsch int signo; 1279b50d902SRodney W. Grimes { 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes message("Ringing your party again"); 130b02b5aadSAndrey A. Chernov waddch(my_win.x_win, '\n'); 131b02b5aadSAndrey A. Chernov if (current_line < my_win.x_nlines - 1) 1329b50d902SRodney W. Grimes current_line++; 1339b50d902SRodney W. Grimes /* force a re-announce */ 1349b50d902SRodney W. Grimes msg.id_num = htonl(remote_id + 1); 1359b50d902SRodney W. Grimes announce_invite(); 1369b50d902SRodney W. Grimes longjmp(invitebuf, 1); 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes 1399b50d902SRodney W. Grimes static char *answers[] = { 1409b50d902SRodney W. Grimes "answer #0", /* SUCCESS */ 1419b50d902SRodney W. Grimes "Your party is not logged on", /* NOT_HERE */ 1429b50d902SRodney W. Grimes "Target machine is too confused to talk to us", /* FAILED */ 1439b50d902SRodney W. Grimes "Target machine does not recognize us", /* MACHINE_UNKNOWN */ 1449b50d902SRodney W. Grimes "Your party is refusing messages", /* PERMISSION_REFUSED */ 1459b50d902SRodney W. Grimes "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */ 1469b50d902SRodney W. Grimes "Target machine indicates protocol mismatch", /* BADVERSION */ 1479b50d902SRodney W. Grimes "Target machine indicates protocol botch (addr)",/* BADADDR */ 1489b50d902SRodney W. Grimes "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */ 1499b50d902SRodney W. Grimes }; 1509b50d902SRodney W. Grimes #define NANSWERS (sizeof (answers) / sizeof (answers[0])) 1519b50d902SRodney W. Grimes 1529b50d902SRodney W. Grimes /* 1539b50d902SRodney W. Grimes * Transmit the invitation and process the response 1549b50d902SRodney W. Grimes */ 15528592604SJoerg Wunsch void 1569b50d902SRodney W. Grimes announce_invite() 1579b50d902SRodney W. Grimes { 1589b50d902SRodney W. Grimes CTL_RESPONSE response; 1599b50d902SRodney W. Grimes 1609b50d902SRodney W. Grimes current_state = "Trying to connect to your party's talk daemon"; 1619b50d902SRodney W. Grimes ctl_transact(his_machine_addr, msg, ANNOUNCE, &response); 1629b50d902SRodney W. Grimes remote_id = response.id_num; 1639b50d902SRodney W. Grimes if (response.answer != SUCCESS) { 1649b50d902SRodney W. Grimes if (response.answer < NANSWERS) 1659b50d902SRodney W. Grimes message(answers[response.answer]); 1669b50d902SRodney W. Grimes quit(); 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes /* leave the actual invitation on my talk daemon */ 1699b9c06b0SRuslan Ermilov current_state = "Trying to connect to local talk daemon"; 1709b50d902SRodney W. Grimes ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response); 1719b50d902SRodney W. Grimes local_id = response.id_num; 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes /* 1759b50d902SRodney W. Grimes * Tell the daemon to remove your invitation 1769b50d902SRodney W. Grimes */ 17728592604SJoerg Wunsch void 1789b50d902SRodney W. Grimes send_delete() 1799b50d902SRodney W. Grimes { 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes msg.type = DELETE; 1829b50d902SRodney W. Grimes /* 1839b50d902SRodney W. Grimes * This is just a extra clean up, so just send it 1849b50d902SRodney W. Grimes * and don't wait for an answer 1859b50d902SRodney W. Grimes */ 1869b50d902SRodney W. Grimes msg.id_num = htonl(remote_id); 1879b50d902SRodney W. Grimes daemon_addr.sin_addr = his_machine_addr; 1889b50d902SRodney W. Grimes if (sendto(ctl_sockt, &msg, sizeof (msg), 0, 1899b50d902SRodney W. Grimes (struct sockaddr *)&daemon_addr, 1909b50d902SRodney W. Grimes sizeof (daemon_addr)) != sizeof(msg)) 19120868061SPhilippe Charnier warn("send_delete (remote)"); 1929b50d902SRodney W. Grimes msg.id_num = htonl(local_id); 1939b50d902SRodney W. Grimes daemon_addr.sin_addr = my_machine_addr; 1949b50d902SRodney W. Grimes if (sendto(ctl_sockt, &msg, sizeof (msg), 0, 1959b50d902SRodney W. Grimes (struct sockaddr *)&daemon_addr, 1969b50d902SRodney W. Grimes sizeof (daemon_addr)) != sizeof (msg)) 19720868061SPhilippe Charnier warn("send_delete (local)"); 1989b50d902SRodney W. Grimes } 199