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