Debian Codegraph @

libinetutils/localhost.c

raw
1 /* localhost.c - A slightly more convenient wrapper for gethostname
2 Copyright (C) 1996-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 /* Written by Miles Bader. */
20
21 #include <config.h>
22
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32
33 /* Return the name of the localhost. This is just a wrapper for gethostname,
34 taking care of allocating a big enough buffer, and caching the result
35 after the first call (so the result should be copied before modification).
36 If something goes wrong, 0 is returned, and errno is set.
37 */
38 /* We no longer use static buffers, as it is too dangerous and
39 it causes subtle bugs. */
40 char *
41 localhost (void)
42 {
43 char *buf = NULL;
44 size_t buf_len = 0;
45 int status = 0;
46
47 do
48 {
49 char *tmp;
50 errno = 0;
51
52 buf_len += 256; /* Initial guess */
53 tmp = realloc (buf, buf_len);
54 if (tmp == NULL)
55 {
56 errno = ENOMEM;
57 free (buf);
58 return 0;
59 }
60 else
61 buf = tmp;
62 }
63 while (((status = gethostname (buf, buf_len)) == 0
64 && !memchr (buf, '\0', buf_len))
65 #ifdef ENAMETOOLONG
66 || errno == ENAMETOOLONG
67 #endif
68 );
69
70 if (status != 0 && errno != 0)
71 /* gethostname failed, abort. */
72 {
73 free (buf);
74 buf = 0;
75 }
76 else
77 /* Determine FQDN. */
78 {
79 #if HAVE_DECL_GETADDRINFO
80 int rc;
81 struct addrinfo hints, *res;
82 char fqdn[NI_MAXHOST];
83
84 memset (&hints, 0, sizeof (hints));
85 hints.ai_family = AF_INET;
86
87 rc = getaddrinfo (buf, NULL, &hints, &res);
88 if (!rc)
89 {
90 /* Back resolving as official host name. */
91 rc = getnameinfo (res->ai_addr, res->ai_addrlen,
92 fqdn, sizeof (fqdn), NULL, 0, NI_NAMEREQD);
93 if (!rc)
94 {
95 free (buf);
96 buf = strdup (fqdn);
97 }
98 freeaddrinfo (res);
99 }
100 #else /* !HAVE_DECL_GETADDRINFO */
101 struct hostent *hp = gethostbyname (buf);
102
103 if (hp)
104 {
105 struct in_addr addr;
106 addr.s_addr = *(unsigned int *) hp->h_addr;
107 hp = gethostbyaddr ((char *) &addr, sizeof (addr), AF_INET);
108 if (hp)
109 {
110 free (buf);
111 buf = strdup (hp->h_name);
112 }
113 }
114 #endif /* !HAVE_DECL_GETADDRINFO */
115 }
116 return buf;
117 }