| 1 | /* |
| 2 | * Copyright (c) 1983 Regents of the University of California. |
| 3 | * All rights reserved. The Berkeley software License Agreement |
| 4 | * specifies the terms and conditions for redistribution. |
| 5 | */ |
| 6 | |
| 7 | #ifndef lint |
| 8 | static char sccsid[] = "@(#)invite.c 5.2 (Berkeley) %G%"; |
| 9 | #endif not lint |
| 10 | |
| 11 | #include "talk_ctl.h" |
| 12 | #include <sys/time.h> |
| 13 | #include <signal.h> |
| 14 | #include <setjmp.h> |
| 15 | |
| 16 | /* |
| 17 | * There wasn't an invitation waiting, so send a request containing |
| 18 | * our sockt address to the remote talk daemon so it can invite |
| 19 | * him |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * The msg.id's for the invitations |
| 24 | * on the local and remote machines. |
| 25 | * These are used to delete the |
| 26 | * invitations. |
| 27 | */ |
| 28 | int local_id, remote_id; |
| 29 | void re_invite(); |
| 30 | jmp_buf invitebuf; |
| 31 | |
| 32 | invite_remote() |
| 33 | { |
| 34 | int nfd, read_mask, template, new_sockt; |
| 35 | struct itimerval itimer; |
| 36 | CTL_RESPONSE response; |
| 37 | |
| 38 | itimer.it_value.tv_sec = RING_WAIT; |
| 39 | itimer.it_value.tv_usec = 0; |
| 40 | itimer.it_interval = itimer.it_value; |
| 41 | if (listen(sockt, 5) != 0) |
| 42 | p_error("Error on attempt to listen for caller"); |
| 43 | msg.addr = *(struct sockaddr *)&my_addr; |
| 44 | msg.addr.sa_family = htons(msg.addr.sa_family); |
| 45 | msg.id_num = htonl(-1); /* an impossible id_num */ |
| 46 | invitation_waiting = 1; |
| 47 | announce_invite(); |
| 48 | /* |
| 49 | * Shut off the automatic messages for a while, |
| 50 | * so we can use the interupt timer to resend the invitation |
| 51 | */ |
| 52 | end_msgs(); |
| 53 | setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); |
| 54 | message("Waiting for your party to respond"); |
| 55 | signal(SIGALRM, re_invite); |
| 56 | (void) setjmp(invitebuf); |
| 57 | while ((new_sockt = accept(sockt, 0, 0)) < 0) { |
| 58 | if (errno == EINTR) |
| 59 | continue; |
| 60 | p_error("Unable to connect with your party"); |
| 61 | } |
| 62 | close(sockt); |
| 63 | sockt = new_sockt; |
| 64 | |
| 65 | /* |
| 66 | * Have the daemons delete the invitations now that we |
| 67 | * have connected. |
| 68 | */ |
| 69 | current_state = "Waiting for your party to respond"; |
| 70 | start_msgs(); |
| 71 | |
| 72 | msg.id_num = htonl(local_id); |
| 73 | ctl_transact(my_machine_addr, msg, DELETE, &response); |
| 74 | msg.id_num = htonl(remote_id); |
| 75 | ctl_transact(his_machine_addr, msg, DELETE, &response); |
| 76 | invitation_waiting = 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Routine called on interupt to re-invite the callee |
| 81 | */ |
| 82 | void |
| 83 | re_invite() |
| 84 | { |
| 85 | |
| 86 | message("Ringing your party again"); |
| 87 | current_line++; |
| 88 | /* force a re-announce */ |
| 89 | msg.id_num = htonl(remote_id + 1); |
| 90 | announce_invite(); |
| 91 | longjmp(invitebuf, 1); |
| 92 | } |
| 93 | |
| 94 | static char *answers[] = { |
| 95 | "Your party is not logged on", /* NOT_HERE */ |
| 96 | "Target machine does not recognize us", /* MACHINE_UNKNOWN */ |
| 97 | "Target machine can not handle remote talk", /* UNKNOWN_REQUEST */ |
| 98 | "Target machine is too confused to talk to us", /* FAILED */ |
| 99 | "Your party is refusing messages", /* PERMISSION_REFUSED */ |
| 100 | "Target machine indicates protocol mismatch", /* BADVERSION */ |
| 101 | "Target machine indicates protocol botch (addr)",/* BADADDR */ |
| 102 | "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */ |
| 103 | }; |
| 104 | #define NANSWERS (sizeof (answers) / sizeof (answers[0])) |
| 105 | |
| 106 | /* |
| 107 | * Transmit the invitation and process the response |
| 108 | */ |
| 109 | announce_invite() |
| 110 | { |
| 111 | CTL_RESPONSE response; |
| 112 | |
| 113 | current_state = "Trying to connect to your party's talk daemon"; |
| 114 | ctl_transact(his_machine_addr, msg, ANNOUNCE, &response); |
| 115 | remote_id = response.id_num; |
| 116 | if (response.answer != SUCCESS) { |
| 117 | if (response.answer < NANSWERS) |
| 118 | message(answers[response.answer]); |
| 119 | quit(); |
| 120 | } |
| 121 | /* leave the actual invitation on my talk daemon */ |
| 122 | ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response); |
| 123 | local_id = response.id_num; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Tell the daemon to remove your invitation |
| 128 | */ |
| 129 | send_delete() |
| 130 | { |
| 131 | |
| 132 | msg.type = DELETE; |
| 133 | /* |
| 134 | * This is just a extra clean up, so just send it |
| 135 | * and don't wait for an answer |
| 136 | */ |
| 137 | msg.id_num = htonl(remote_id); |
| 138 | daemon_addr.sin_addr = his_machine_addr; |
| 139 | if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr, |
| 140 | sizeof (daemon_addr)) != sizeof(msg)) |
| 141 | perror("send_delete (remote)"); |
| 142 | msg.id_num = htonl(local_id); |
| 143 | daemon_addr.sin_addr = my_machine_addr; |
| 144 | if (sendto(ctl_sockt, &msg, sizeof (msg), 0, &daemon_addr, |
| 145 | sizeof (daemon_addr)) != sizeof (msg)) |
| 146 | perror("send_delete (local)"); |
| 147 | } |