| 1 |
/* |
| 2 |
Copyright (C) 1995-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1983, 1993 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include <config.h> |
| 49 |
|
| 50 |
#include <string.h> |
| 51 |
|
| 52 |
#include <sys/types.h> |
| 53 |
#include <sys/socket.h> |
| 54 |
#include <sys/time.h> |
| 55 |
#include <time.h> |
| 56 |
#include <signal.h> |
| 57 |
#include <netinet/in.h> |
| 58 |
#include <protocols/talkd.h> |
| 59 |
#include <errno.h> |
| 60 |
#include <unistd.h> |
| 61 |
#include <setjmp.h> |
| 62 |
#include <attribute.h> |
| 63 |
#include "talk_ctl.h" |
| 64 |
#include "talk.h" |
| 65 |
|
| 66 |
static char *answers[] = { |
| 67 |
"answer #0", /* SUCCESS */ |
| 68 |
"Your party is not logged on", /* NOT_HERE */ |
| 69 |
"Target machine is too confused to talk to us", /* FAILED */ |
| 70 |
"Target machine does not recognize us", /* MACHINE_UNKNOWN */ |
| 71 |
"Your party is refusing messages", /* PERMISSION_REFUSED */ |
| 72 |
"Target machine cannot handle remote talk", /* UNKNOWN_REQUEST */ |
| 73 |
"Target machine indicates protocol mismatch", /* BADVERSION */ |
| 74 |
"Target machine indicates protocol botch (addr)", /* BADADDR */ |
| 75 |
"Target machine indicates protocol botch (ctl_addr)", /* BADCTLADDR */ |
| 76 |
}; |
| 77 |
|
| 78 |
#define NANSWERS (sizeof (answers) / sizeof (answers[0])) |
| 79 |
|
| 80 |
/* |
| 81 |
* The msg.id's for the invitations |
| 82 |
* on the local and remote machines. |
| 83 |
* These are used to delete the |
| 84 |
* invitations. |
| 85 |
*/ |
| 86 |
int local_id, remote_id; |
| 87 |
jmp_buf invitebuf; |
| 88 |
|
| 89 |
/* |
| 90 |
* Transmit the invitation and process the response |
| 91 |
*/ |
| 92 |
int |
| 93 |
announce_invite (void) |
| 94 |
{ |
| 95 |
CTL_RESPONSE response; |
| 96 |
|
| 97 |
current_state = "Trying to connect to your party's talk daemon"; |
| 98 |
ctl_transact (his_machine_addr, msg, ANNOUNCE, &response); |
| 99 |
remote_id = response.id_num; |
| 100 |
if (response.answer != SUCCESS) |
| 101 |
{ |
| 102 |
if (response.answer < NANSWERS) |
| 103 |
message (answers[response.answer]); |
| 104 |
quit (); |
| 105 |
} |
| 106 |
/* leave the actual invitation on my talk daemon */ |
| 107 |
ctl_transact (my_machine_addr, msg, LEAVE_INVITE, &response); |
| 108 |
local_id = response.id_num; |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Routine called on interrupt to re-invite the callee |
| 115 |
*/ |
| 116 |
void |
| 117 |
re_invite (int sig MAYBE_UNUSED) |
| 118 |
{ |
| 119 |
|
| 120 |
message ("Ringing your party again"); |
| 121 |
current_line++; |
| 122 |
/* force a re-announce */ |
| 123 |
msg.id_num = htonl (remote_id + 1); |
| 124 |
announce_invite (); |
| 125 |
longjmp (invitebuf, 1); |
| 126 |
} |
| 127 |
|
| 128 |
int |
| 129 |
invite_remote (void) |
| 130 |
{ |
| 131 |
int new_sockt; |
| 132 |
struct itimerval itimer; |
| 133 |
CTL_RESPONSE response; |
| 134 |
|
| 135 |
itimer.it_value.tv_sec = RING_WAIT; |
| 136 |
itimer.it_value.tv_usec = 0; |
| 137 |
itimer.it_interval = itimer.it_value; |
| 138 |
if (listen (sockt, 5) != 0) |
| 139 |
p_error ("Error on attempt to listen for caller"); |
| 140 |
|
| 141 |
msg.addr.sa_family = htons (my_addr.sin_family); |
| 142 |
memcpy (msg.addr.sa_data, |
| 143 |
((struct sockaddr *) &my_addr)->sa_data, |
| 144 |
sizeof ((struct sockaddr *) & my_addr)->sa_data); |
| 145 |
|
| 146 |
msg.id_num = htonl (-1); /* an impossible id_num */ |
| 147 |
invitation_waiting = 1; |
| 148 |
announce_invite (); |
| 149 |
/* |
| 150 |
* Shut off the automatic messages for a while, |
| 151 |
* so we can use the interrupt timer to resend the invitation |
| 152 |
*/ |
| 153 |
end_msgs (); |
| 154 |
setitimer (ITIMER_REAL, &itimer, (struct itimerval *) 0); |
| 155 |
message ("Waiting for your party to respond"); |
| 156 |
signal (SIGALRM, re_invite); |
| 157 |
setjmp (invitebuf); |
| 158 |
while ((new_sockt = accept (sockt, 0, 0)) < 0) |
| 159 |
{ |
| 160 |
if (errno == EINTR) |
| 161 |
continue; |
| 162 |
p_error ("Unable to connect with your party"); |
| 163 |
} |
| 164 |
close (sockt); |
| 165 |
sockt = new_sockt; |
| 166 |
|
| 167 |
/* |
| 168 |
* Have the daemons delete the invitations now that we |
| 169 |
* have connected. |
| 170 |
*/ |
| 171 |
current_state = "Waiting for your party to respond"; |
| 172 |
start_msgs (); |
| 173 |
|
| 174 |
msg.id_num = htonl (local_id); |
| 175 |
ctl_transact (my_machine_addr, msg, DELETE, &response); |
| 176 |
msg.id_num = htonl (remote_id); |
| 177 |
ctl_transact (his_machine_addr, msg, DELETE, &response); |
| 178 |
invitation_waiting = 0; |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Tell the daemon to remove your invitation |
| 185 |
*/ |
| 186 |
int |
| 187 |
send_delete (void) |
| 188 |
{ |
| 189 |
|
| 190 |
msg.type = DELETE; |
| 191 |
/* |
| 192 |
* This is just a extra clean up, so just send it |
| 193 |
* and don't wait for an answer |
| 194 |
*/ |
| 195 |
msg.id_num = htonl (remote_id); |
| 196 |
daemon_addr.sin_family = AF_INET; |
| 197 |
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
| 198 |
daemon_addr.sin_len = sizeof (daemon_addr); |
| 199 |
#endif |
| 200 |
daemon_addr.sin_addr = his_machine_addr; |
| 201 |
if (sendto (ctl_sockt, (const char *) &msg, sizeof (msg), 0, |
| 202 |
(struct sockaddr *) &daemon_addr, |
| 203 |
sizeof (daemon_addr)) != sizeof (msg)) |
| 204 |
perror ("send_delete (remote)"); |
| 205 |
msg.id_num = htonl (local_id); |
| 206 |
daemon_addr.sin_addr = my_machine_addr; |
| 207 |
if (sendto (ctl_sockt, (const char *) &msg, sizeof (msg), 0, |
| 208 |
(struct sockaddr *) &daemon_addr, |
| 209 |
sizeof (daemon_addr)) != sizeof (msg)) |
| 210 |
perror ("send_delete (local)"); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |