| 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 <sys/types.h> |
| 51 |
#include <sys/socket.h> |
| 52 |
#include <sys/time.h> |
| 53 |
#include <time.h> |
| 54 |
#include <netinet/in.h> |
| 55 |
#include <sys/select.h> |
| 56 |
#include <protocols/talkd.h> |
| 57 |
#include <errno.h> |
| 58 |
#include "talk_ctl.h" |
| 59 |
#include "talk.h" |
| 60 |
|
| 61 |
#define CTL_WAIT 2 /* time to wait for a response, in seconds */ |
| 62 |
|
| 63 |
/* |
| 64 |
* SOCKDGRAM is unreliable, so we must repeat messages if we have |
| 65 |
* not received an acknowledgement within a reasonable amount |
| 66 |
* of time |
| 67 |
*/ |
| 68 |
int |
| 69 |
ctl_transact (struct in_addr target, CTL_MSG msg, int type, CTL_RESPONSE *rp) |
| 70 |
{ |
| 71 |
int nready = 0, cc; |
| 72 |
fd_set read_mask, ctl_mask; |
| 73 |
struct timeval wait; |
| 74 |
|
| 75 |
msg.type = type; |
| 76 |
daemon_addr.sin_family = AF_INET; |
| 77 |
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
| 78 |
daemon_addr.sin_len = sizeof (daemon_addr); |
| 79 |
#endif |
| 80 |
daemon_addr.sin_addr = target; |
| 81 |
daemon_addr.sin_port = htons (daemon_port); |
| 82 |
FD_ZERO (&ctl_mask); |
| 83 |
FD_SET (ctl_sockt, &ctl_mask); |
| 84 |
|
| 85 |
/* |
| 86 |
* Keep sending the message until a response of |
| 87 |
* the proper type is obtained. |
| 88 |
*/ |
| 89 |
do |
| 90 |
{ |
| 91 |
wait.tv_sec = CTL_WAIT; |
| 92 |
wait.tv_usec = 0; |
| 93 |
/* resend message until a response is obtained */ |
| 94 |
do |
| 95 |
{ |
| 96 |
cc = sendto (ctl_sockt, (char *) &msg, sizeof (msg), 0, |
| 97 |
(struct sockaddr *) &daemon_addr, |
| 98 |
sizeof (daemon_addr)); |
| 99 |
if (cc != sizeof (msg)) |
| 100 |
{ |
| 101 |
if (errno == EINTR) |
| 102 |
continue; |
| 103 |
p_error ("Error on write to talk daemon"); |
| 104 |
} |
| 105 |
read_mask = ctl_mask; |
| 106 |
nready = select (32, &read_mask, 0, 0, &wait); |
| 107 |
if (nready < 0) |
| 108 |
{ |
| 109 |
if (errno == EINTR) |
| 110 |
continue; |
| 111 |
p_error ("Error waiting for daemon response"); |
| 112 |
} |
| 113 |
} |
| 114 |
while (nready == 0); |
| 115 |
/* |
| 116 |
* Keep reading while there are queued messages |
| 117 |
* (this is not necessary, it just saves extra |
| 118 |
* request/acknowledgements being sent) |
| 119 |
*/ |
| 120 |
do |
| 121 |
{ |
| 122 |
cc = recv (ctl_sockt, (char *) rp, sizeof (*rp), 0); |
| 123 |
if (cc < 0) |
| 124 |
{ |
| 125 |
if (errno == EINTR) |
| 126 |
continue; |
| 127 |
p_error ("Error on read from talk daemon"); |
| 128 |
} |
| 129 |
read_mask = ctl_mask; |
| 130 |
/* an immediate poll */ |
| 131 |
timerclear (&wait); |
| 132 |
nready = select (32, &read_mask, 0, 0, &wait); |
| 133 |
} |
| 134 |
while (nready > 0 && (rp->vers != TALK_VERSION || rp->type != type)); |
| 135 |
} |
| 136 |
while (rp->vers != TALK_VERSION || rp->type != type); |
| 137 |
rp->id_num = ntohl (rp->id_num); |
| 138 |
rp->addr.sa_family = ntohs (rp->addr.sa_family); |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |