122394Sdist /*
2*62303Sbostic * Copyright (c) 1983, 1993
3*62303Sbostic * The Regents of the University of California. All rights reserved.
434373Sbostic *
542770Sbostic * %sccs.include.redist.c%
622394Sdist */
722394Sdist
816363Skarels #ifndef lint
9*62303Sbostic static char sccsid[] = "@(#)invite.c 8.1 (Berkeley) 06/06/93";
1034373Sbostic #endif /* not lint */
1116350Skarels
1246858Sbostic #include <sys/types.h>
1346858Sbostic #include <sys/socket.h>
1416350Skarels #include <sys/time.h>
1516350Skarels #include <signal.h>
1646858Sbostic #include <netinet/in.h>
1746858Sbostic #include <protocols/talkd.h>
1846858Sbostic #include <errno.h>
1916350Skarels #include <setjmp.h>
2046858Sbostic #include "talk_ctl.h"
2146858Sbostic #include "talk.h"
2216350Skarels
2316363Skarels /*
2416363Skarels * There wasn't an invitation waiting, so send a request containing
2516363Skarels * our sockt address to the remote talk daemon so it can invite
2616363Skarels * him
2716363Skarels */
2816350Skarels
2916363Skarels /*
3016363Skarels * The msg.id's for the invitations
3116363Skarels * on the local and remote machines.
3216363Skarels * These are used to delete the
3316363Skarels * invitations.
3416363Skarels */
3516363Skarels int local_id, remote_id;
3616363Skarels void re_invite();
3716363Skarels jmp_buf invitebuf;
3816363Skarels
invite_remote()3916350Skarels invite_remote()
4016350Skarels {
4116363Skarels int nfd, read_mask, template, new_sockt;
4216363Skarels struct itimerval itimer;
4316363Skarels CTL_RESPONSE response;
4416350Skarels
4516363Skarels itimer.it_value.tv_sec = RING_WAIT;
4616363Skarels itimer.it_value.tv_usec = 0;
4716363Skarels itimer.it_interval = itimer.it_value;
4816363Skarels if (listen(sockt, 5) != 0)
4916363Skarels p_error("Error on attempt to listen for caller");
5038642Skarels #ifdef MSG_EOR
5138642Skarels /* copy new style sockaddr to old, swap family (short in old) */
5238642Skarels msg.addr = *(struct osockaddr *)&my_addr; /* XXX new to old style*/
5338642Skarels msg.addr.sa_family = htons(my_addr.sin_family);
5438642Skarels #else
5526848Smckusick msg.addr = *(struct sockaddr *)&my_addr;
5638642Skarels #endif
5726848Smckusick msg.id_num = htonl(-1); /* an impossible id_num */
5816363Skarels invitation_waiting = 1;
5916363Skarels announce_invite();
6016350Skarels /*
6116363Skarels * Shut off the automatic messages for a while,
6216350Skarels * so we can use the interupt timer to resend the invitation
6316350Skarels */
6416363Skarels end_msgs();
6516363Skarels setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
6616363Skarels message("Waiting for your party to respond");
6716363Skarels signal(SIGALRM, re_invite);
6816363Skarels (void) setjmp(invitebuf);
6916363Skarels while ((new_sockt = accept(sockt, 0, 0)) < 0) {
7016363Skarels if (errno == EINTR)
7116363Skarels continue;
7216363Skarels p_error("Unable to connect with your party");
7316350Skarels }
7416363Skarels close(sockt);
7516363Skarels sockt = new_sockt;
7616350Skarels
7716363Skarels /*
7816363Skarels * Have the daemons delete the invitations now that we
7916363Skarels * have connected.
8016350Skarels */
8116363Skarels current_state = "Waiting for your party to respond";
8216363Skarels start_msgs();
8316350Skarels
8426848Smckusick msg.id_num = htonl(local_id);
8516363Skarels ctl_transact(my_machine_addr, msg, DELETE, &response);
8626848Smckusick msg.id_num = htonl(remote_id);
8716363Skarels ctl_transact(his_machine_addr, msg, DELETE, &response);
8816363Skarels invitation_waiting = 0;
8916350Skarels }
9016350Skarels
9116363Skarels /*
9216363Skarels * Routine called on interupt to re-invite the callee
9316363Skarels */
9416363Skarels void
re_invite()9516363Skarels re_invite()
9616363Skarels {
9716350Skarels
9816363Skarels message("Ringing your party again");
9916363Skarels current_line++;
10016350Skarels /* force a re-announce */
10126848Smckusick msg.id_num = htonl(remote_id + 1);
10216363Skarels announce_invite();
10316363Skarels longjmp(invitebuf, 1);
10416350Skarels }
10516350Skarels
10626848Smckusick static char *answers[] = {
10730540Ssam "answer #0", /* SUCCESS */
10826848Smckusick "Your party is not logged on", /* NOT_HERE */
10930540Ssam "Target machine is too confused to talk to us", /* FAILED */
11026848Smckusick "Target machine does not recognize us", /* MACHINE_UNKNOWN */
11130540Ssam "Your party is refusing messages", /* PERMISSION_REFUSED */
11226848Smckusick "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */
11326848Smckusick "Target machine indicates protocol mismatch", /* BADVERSION */
11426848Smckusick "Target machine indicates protocol botch (addr)",/* BADADDR */
11526848Smckusick "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
11626848Smckusick };
11726848Smckusick #define NANSWERS (sizeof (answers) / sizeof (answers[0]))
11826848Smckusick
11916363Skarels /*
12016363Skarels * Transmit the invitation and process the response
12116363Skarels */
announce_invite()12216350Skarels announce_invite()
12316350Skarels {
12416363Skarels CTL_RESPONSE response;
12516350Skarels
12616363Skarels current_state = "Trying to connect to your party's talk daemon";
12716363Skarels ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
12816363Skarels remote_id = response.id_num;
12916363Skarels if (response.answer != SUCCESS) {
13026848Smckusick if (response.answer < NANSWERS)
13126848Smckusick message(answers[response.answer]);
13216363Skarels quit();
13316350Skarels }
13416350Skarels /* leave the actual invitation on my talk daemon */
13516363Skarels ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
13616363Skarels local_id = response.id_num;
13716350Skarels }
13826848Smckusick
13916363Skarels /*
14016363Skarels * Tell the daemon to remove your invitation
14116363Skarels */
send_delete()14216350Skarels send_delete()
14316350Skarels {
14416350Skarels
14516363Skarels msg.type = DELETE;
14616363Skarels /*
14716363Skarels * This is just a extra clean up, so just send it
14816363Skarels * and don't wait for an answer
14916363Skarels */
15026848Smckusick msg.id_num = htonl(remote_id);
15116363Skarels daemon_addr.sin_addr = his_machine_addr;
15246858Sbostic if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
15346858Sbostic (struct sockaddr *)&daemon_addr,
15426848Smckusick sizeof (daemon_addr)) != sizeof(msg))
15526848Smckusick perror("send_delete (remote)");
15626848Smckusick msg.id_num = htonl(local_id);
15716363Skarels daemon_addr.sin_addr = my_machine_addr;
15846858Sbostic if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
15946858Sbostic (struct sockaddr *)&daemon_addr,
16026848Smckusick sizeof (daemon_addr)) != sizeof (msg))
16126848Smckusick perror("send_delete (local)");
16216350Skarels }
163