| 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 <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
|
| 53 |
#include <sys/types.h> |
| 54 |
#include <sys/socket.h> |
| 55 |
#include <netinet/in.h> |
| 56 |
#include <protocols/talkd.h> |
| 57 |
#include <netdb.h> |
| 58 |
#include <stdio.h> |
| 59 |
#include <unistd.h> |
| 60 |
#if defined HAVE_IDN2_H && defined HAVE_IDN2 |
| 61 |
# include <idn2.h> |
| 62 |
#elif defined HAVE_IDNA_H |
| 63 |
# include <idna.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#include "talk_ctl.h" |
| 67 |
|
| 68 |
int |
| 69 |
get_addrs (char *my_machine_name, char *his_machine_name) |
| 70 |
{ |
| 71 |
#if HAVE_DECL_GETADDRINFO || defined HAVE_IDN || defined HAVE_IDN2 |
| 72 |
int err; |
| 73 |
#endif |
| 74 |
char *lhost, *rhost; |
| 75 |
#if HAVE_DECL_GETADDRINFO |
| 76 |
struct addrinfo hints, *res, *ai; |
| 77 |
#else /* !HAVE_DECL_GETADDRINFO */ |
| 78 |
struct hostent *hp; |
| 79 |
#endif |
| 80 |
struct servent *sp; |
| 81 |
|
| 82 |
#if defined HAVE_IDN || defined HAVE_IDN2 |
| 83 |
err = idna_to_ascii_lz (my_machine_name, &lhost, 0); |
| 84 |
if (err) |
| 85 |
{ |
| 86 |
fprintf (stderr, "talk: %s: %s\n", |
| 87 |
my_machine_name, idna_strerror (err)); |
| 88 |
exit (-1); |
| 89 |
} |
| 90 |
|
| 91 |
err = idna_to_ascii_lz (his_machine_name, &rhost, 0); |
| 92 |
if (err) |
| 93 |
{ |
| 94 |
fprintf (stderr, "talk: %s: %s\n", |
| 95 |
his_machine_name, idna_strerror (err)); |
| 96 |
exit (-1); |
| 97 |
} |
| 98 |
#else /* !HAVE_IDN && !HAVE_IDN2 */ |
| 99 |
lhost = my_machine_name; |
| 100 |
rhost = his_machine_name; |
| 101 |
#endif |
| 102 |
|
| 103 |
msg.pid = htonl (getpid ()); |
| 104 |
|
| 105 |
/* Look up the address of the local host. */ |
| 106 |
|
| 107 |
#if HAVE_DECL_GETADDRINFO |
| 108 |
memset (&hints, 0, sizeof (hints)); |
| 109 |
|
| 110 |
/* The talk-protocol is IPv4 only! */ |
| 111 |
hints.ai_family = AF_INET; |
| 112 |
hints.ai_socktype = SOCK_DGRAM; |
| 113 |
# ifdef AI_IDN |
| 114 |
hints.ai_flags |= AI_IDN; |
| 115 |
# endif |
| 116 |
|
| 117 |
err = getaddrinfo (lhost, NULL, &hints, &res); |
| 118 |
if (err) |
| 119 |
{ |
| 120 |
fprintf (stderr, "talk: %s: %s\n", lhost, gai_strerror (err)); |
| 121 |
exit (-1); |
| 122 |
} |
| 123 |
|
| 124 |
/* Perform all sanity checks available. |
| 125 |
* Reduction of tests? |
| 126 |
*/ |
| 127 |
for (ai = res; ai; ai = ai->ai_next) |
| 128 |
{ |
| 129 |
int f; |
| 130 |
|
| 131 |
if (ai->ai_family != AF_INET) |
| 132 |
continue; |
| 133 |
|
| 134 |
f = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 135 |
if (f < 0) |
| 136 |
continue; |
| 137 |
|
| 138 |
/* Attempt binding to this local address. */ |
| 139 |
if (bind (f, ai->ai_addr, ai->ai_addrlen)) |
| 140 |
{ |
| 141 |
close (f); |
| 142 |
f = -1; |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
/* We have a usable address. */ |
| 147 |
close (f); |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
if (ai) |
| 152 |
memcpy (&my_machine_addr, |
| 153 |
&((struct sockaddr_in *) ai->ai_addr)->sin_addr, |
| 154 |
sizeof (my_machine_addr)); |
| 155 |
|
| 156 |
freeaddrinfo (res); |
| 157 |
if (!ai) |
| 158 |
{ |
| 159 |
fprintf (stderr, "talk: %s: %s\n", lhost, "address not found"); |
| 160 |
exit (-1); |
| 161 |
} |
| 162 |
|
| 163 |
#else /* !HAVE_DECL_GETADDRINFO */ |
| 164 |
hp = gethostbyname (lhost); |
| 165 |
if (hp == NULL) |
| 166 |
{ |
| 167 |
fprintf (stderr, "talk: %s(%s): ", lhost, my_machine_name); |
| 168 |
herror ((char *) NULL); |
| 169 |
exit (-1); |
| 170 |
} |
| 171 |
memmove (&my_machine_addr, hp->h_addr, hp->h_length); |
| 172 |
#endif /* !HAVE_DECL_GETADDRINFO */ |
| 173 |
|
| 174 |
/* |
| 175 |
* If the callee is on-machine, just copy the |
| 176 |
* network address, otherwise do a lookup... |
| 177 |
*/ |
| 178 |
if (strcmp (rhost, lhost)) |
| 179 |
{ |
| 180 |
#if HAVE_DECL_GETADDRINFO |
| 181 |
err = getaddrinfo (rhost, NULL, &hints, &res); |
| 182 |
if (err) |
| 183 |
{ |
| 184 |
fprintf (stderr, "talk: %s: %s\n", rhost, gai_strerror (err)); |
| 185 |
exit (-1); |
| 186 |
} |
| 187 |
|
| 188 |
/* Perform all sanity checks available. */ |
| 189 |
for (ai = res; ai; ai = ai->ai_next) |
| 190 |
{ |
| 191 |
int f; |
| 192 |
|
| 193 |
if (ai->ai_family != AF_INET) |
| 194 |
continue; |
| 195 |
|
| 196 |
f = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 197 |
if (f < 0) |
| 198 |
continue; |
| 199 |
|
| 200 |
/* We have a usable address family! */ |
| 201 |
close (f); |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
if (ai) |
| 206 |
memcpy (&his_machine_addr, |
| 207 |
&((struct sockaddr_in *) ai->ai_addr)->sin_addr, |
| 208 |
sizeof (his_machine_addr)); |
| 209 |
|
| 210 |
freeaddrinfo (res); |
| 211 |
if (!ai) |
| 212 |
{ |
| 213 |
fprintf (stderr, "talk: %s: %s\n", rhost, "address not found"); |
| 214 |
exit (-1); |
| 215 |
} |
| 216 |
|
| 217 |
#else /* !HAVE_DECL_GETADDRINFO */ |
| 218 |
hp = gethostbyname (rhost); |
| 219 |
if (hp == NULL) |
| 220 |
{ |
| 221 |
fprintf (stderr, "talk: %s(%s): ", rhost, his_machine_name); |
| 222 |
herror ((char *) NULL); |
| 223 |
exit (-1); |
| 224 |
} |
| 225 |
memmove (&his_machine_addr, hp->h_addr, hp->h_length); |
| 226 |
#endif /* !HAVE_DECL_GETADDRINFO */ |
| 227 |
} |
| 228 |
else |
| 229 |
his_machine_addr = my_machine_addr; |
| 230 |
|
| 231 |
/* Find the server's port. */ |
| 232 |
sp = getservbyname ("ntalk", "udp"); |
| 233 |
if (sp == 0) |
| 234 |
{ |
| 235 |
fprintf (stderr, "talk: %s/%s: service is not registered.\n", |
| 236 |
"ntalk", "udp"); |
| 237 |
exit (-1); |
| 238 |
} |
| 239 |
daemon_port = ntohs (sp->s_port); |
| 240 |
|
| 241 |
#if defined HAVE_IDN || defined HAVE_IDN2 |
| 242 |
free (lhost); |
| 243 |
free (rhost); |
| 244 |
#endif |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |