1*0be3040aSchristos /* $NetBSD: invite.c,v 1.10 2012/12/29 23:44:23 christos Exp $ */
251207773Sjtc
361f28255Scgd /*
451207773Sjtc * Copyright (c) 1983, 1993
551207773Sjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 6/6/93";
3651207773Sjtc #endif
37*0be3040aSchristos __RCSID("$NetBSD: invite.c,v 1.10 2012/12/29 23:44:23 christos Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd
4032a9e65fSlukem #include "talk.h"
4161f28255Scgd #include <sys/time.h>
4261f28255Scgd #include <signal.h>
4361f28255Scgd #include <errno.h>
4461f28255Scgd #include <setjmp.h>
4532a9e65fSlukem #include <unistd.h>
46*0be3040aSchristos #include <err.h>
4761f28255Scgd #include "talk_ctl.h"
4861f28255Scgd
4961f28255Scgd /*
5061f28255Scgd * There wasn't an invitation waiting, so send a request containing
5161f28255Scgd * our sockt address to the remote talk daemon so it can invite
5261f28255Scgd * him
5361f28255Scgd */
5461f28255Scgd
5561f28255Scgd /*
5661f28255Scgd * The msg.id's for the invitations
5761f28255Scgd * on the local and remote machines.
5861f28255Scgd * These are used to delete the
5961f28255Scgd * invitations.
6061f28255Scgd */
6168168df6Sjoerg static int local_id, remote_id;
6268168df6Sjoerg static jmp_buf invitebuf;
6361f28255Scgd
6432a9e65fSlukem void
invite_remote(void)6568168df6Sjoerg invite_remote(void)
6661f28255Scgd {
6732a9e65fSlukem int new_sockt;
6861f28255Scgd struct itimerval itimer;
6961f28255Scgd CTL_RESPONSE response;
7061f28255Scgd
7161f28255Scgd itimer.it_value.tv_sec = RING_WAIT;
7261f28255Scgd itimer.it_value.tv_usec = 0;
7361f28255Scgd itimer.it_interval = itimer.it_value;
7461f28255Scgd if (listen(sockt, 5) != 0)
7561f28255Scgd p_error("Error on attempt to listen for caller");
7661f28255Scgd #ifdef MSG_EOR
7761f28255Scgd /* copy new style sockaddr to old, swap family (short in old) */
78c4445bc7Schristos msg.addr = *(struct talkd_sockaddr *)(void *)&my_addr;
7961f28255Scgd msg.addr.sa_family = htons(my_addr.sin_family);
8061f28255Scgd #else
8161f28255Scgd msg.addr = *(struct sockaddr *)&my_addr;
8261f28255Scgd #endif
8361f28255Scgd msg.id_num = htonl(-1); /* an impossible id_num */
8461f28255Scgd invitation_waiting = 1;
8561f28255Scgd announce_invite();
8661f28255Scgd /*
8761f28255Scgd * Shut off the automatic messages for a while,
887e681f70Swiz * so we can use the interrupt timer to resend the invitation
8961f28255Scgd */
9061f28255Scgd end_msgs();
9161f28255Scgd setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
9261f28255Scgd message("Waiting for your party to respond");
9361f28255Scgd signal(SIGALRM, re_invite);
9461f28255Scgd (void) setjmp(invitebuf);
9561f28255Scgd while ((new_sockt = accept(sockt, 0, 0)) < 0) {
9661f28255Scgd if (errno == EINTR)
9761f28255Scgd continue;
9861f28255Scgd p_error("Unable to connect with your party");
9961f28255Scgd }
10061f28255Scgd close(sockt);
10161f28255Scgd sockt = new_sockt;
10261f28255Scgd
10361f28255Scgd /*
10461f28255Scgd * Have the daemons delete the invitations now that we
10561f28255Scgd * have connected.
10661f28255Scgd */
10761f28255Scgd current_state = "Waiting for your party to respond";
10861f28255Scgd start_msgs();
10961f28255Scgd
11061f28255Scgd msg.id_num = htonl(local_id);
11161f28255Scgd ctl_transact(my_machine_addr, msg, DELETE, &response);
11261f28255Scgd msg.id_num = htonl(remote_id);
11361f28255Scgd ctl_transact(his_machine_addr, msg, DELETE, &response);
11461f28255Scgd invitation_waiting = 0;
11561f28255Scgd }
11661f28255Scgd
11761f28255Scgd /*
1187e681f70Swiz * Routine called on interrupt to re-invite the callee
11961f28255Scgd */
12061f28255Scgd void
re_invite(int dummy)12168168df6Sjoerg re_invite(int dummy)
12261f28255Scgd {
12361f28255Scgd
12461f28255Scgd message("Ringing your party again");
12561f28255Scgd current_line++;
12661f28255Scgd /* force a re-announce */
12761f28255Scgd msg.id_num = htonl(remote_id + 1);
12861f28255Scgd announce_invite();
12961f28255Scgd longjmp(invitebuf, 1);
13061f28255Scgd }
13161f28255Scgd
132c2293a6fSlukem static const char *answers[] = {
13361f28255Scgd "answer #0", /* SUCCESS */
13461f28255Scgd "Your party is not logged on", /* NOT_HERE */
13561f28255Scgd "Target machine is too confused to talk to us", /* FAILED */
13661f28255Scgd "Target machine does not recognize us", /* MACHINE_UNKNOWN */
13761f28255Scgd "Your party is refusing messages", /* PERMISSION_REFUSED */
13861f28255Scgd "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
13961f28255Scgd "Target machine indicates protocol mismatch", /* BADVERSION */
14061f28255Scgd "Target machine indicates protocol botch (addr)",/* BADADDR */
14161f28255Scgd "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
14261f28255Scgd };
14361f28255Scgd #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
14461f28255Scgd
14561f28255Scgd /*
14661f28255Scgd * Transmit the invitation and process the response
14761f28255Scgd */
14832a9e65fSlukem void
announce_invite(void)14968168df6Sjoerg announce_invite(void)
15061f28255Scgd {
15161f28255Scgd CTL_RESPONSE response;
15261f28255Scgd
15361f28255Scgd current_state = "Trying to connect to your party's talk daemon";
15461f28255Scgd ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
15561f28255Scgd remote_id = response.id_num;
15661f28255Scgd if (response.answer != SUCCESS) {
15761f28255Scgd if (response.answer < NANSWERS)
15861f28255Scgd message(answers[response.answer]);
15961f28255Scgd quit();
16061f28255Scgd }
16161f28255Scgd /* leave the actual invitation on my talk daemon */
16261f28255Scgd ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
16361f28255Scgd local_id = response.id_num;
16461f28255Scgd }
16561f28255Scgd
16661f28255Scgd /*
16761f28255Scgd * Tell the daemon to remove your invitation
16861f28255Scgd */
16932a9e65fSlukem void
send_delete(void)17068168df6Sjoerg send_delete(void)
17161f28255Scgd {
17261f28255Scgd
17361f28255Scgd msg.type = DELETE;
17461f28255Scgd /*
17561f28255Scgd * This is just a extra clean up, so just send it
17661f28255Scgd * and don't wait for an answer
17761f28255Scgd */
17861f28255Scgd msg.id_num = htonl(remote_id);
17961f28255Scgd daemon_addr.sin_addr = his_machine_addr;
18061f28255Scgd if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
18161f28255Scgd (struct sockaddr *)&daemon_addr,
18261f28255Scgd sizeof (daemon_addr)) != sizeof(msg))
183*0be3040aSchristos warn("send_delete (remote)");
18461f28255Scgd msg.id_num = htonl(local_id);
18561f28255Scgd daemon_addr.sin_addr = my_machine_addr;
18661f28255Scgd if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
18761f28255Scgd (struct sockaddr *)&daemon_addr,
18861f28255Scgd sizeof (daemon_addr)) != sizeof (msg))
189*0be3040aSchristos warn("send_delete (local)");
19061f28255Scgd }
191