NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
getdomain.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <assert.h>
34#include <netdb.h>
35#include <string.h>
36#include <sys/socket.h>
37#include <time.h>
38#include <unistd.h>
39#include "mutt/lib.h"
40#include "lib.h"
41
42#ifdef HAVE_GETADDRINFO_A
52static struct addrinfo *mutt_getaddrinfo_a(const char *node, const struct addrinfo *hints)
53{
54 assert(node);
55 assert(hints);
56 struct addrinfo *result = NULL;
57
58 /* Allow 0.1 seconds to get the FQDN (fully-qualified domain name).
59 * If it takes longer, the system is mis-configured and the network is not
60 * working properly, so... */
61 struct timespec timeout = { 0, 100000000 };
62 struct gaicb req = { 0 };
63 req.ar_name = node;
64 req.ar_request = hints;
65 struct gaicb *reqs[1] = { &req };
66 if (getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL) == 0)
67 {
68 gai_suspend((const struct gaicb *const *) reqs, 1, &timeout);
69 const int status = gai_error(reqs[0]);
70 if (status == 0)
71 {
72 result = reqs[0]->ar_result;
73 }
74 else if (status == EAI_INPROGRESS)
75 {
76 mutt_debug(LL_DEBUG1, "timeout\n");
77 /* request is not finished, cancel it to free it safely */
78 if (gai_cancel(reqs[0]) == EAI_NOTCANCELED)
79 {
80 // try once more for half-a-second, then bail out
81 timeout.tv_nsec = 50000000;
82 gai_suspend((const struct gaicb *const *) reqs, 1, &timeout);
83 }
84 }
85 else
86 {
87 mutt_debug(LL_DEBUG1, "fail: (%d) %s\n", status, gai_strerror(status));
88 }
89 }
90 return result;
91}
92
93#elif defined(HAVE_GETADDRINFO)
103static struct addrinfo *mutt_getaddrinfo(const char *node, const struct addrinfo *hints)
104{
105 assert(node);
106 assert(hints);
107 struct addrinfo *result = NULL;
108 mutt_debug(LL_DEBUG3, "before getaddrinfo\n");
109 int rc = getaddrinfo(node, NULL, hints, &result);
110 mutt_debug(LL_DEBUG3, "after getaddrinfo\n");
111
112 if (rc != 0)
113 result = NULL;
114
115 return result;
116}
117#endif
118
125int getdnsdomainname(struct Buffer *result)
126{
127 assert(result);
128 int rc = -1;
129
130#if defined(HAVE_GETADDRINFO) || defined(HAVE_GETADDRINFO_A)
131 char node[256] = { 0 };
132 if (gethostname(node, sizeof(node)) != 0)
133 return rc;
134
135 struct addrinfo *lookup_result = NULL;
136 struct addrinfo hints = { 0 };
137
138 buf_reset(result);
139 hints.ai_flags = AI_CANONNAME;
140 hints.ai_family = AF_UNSPEC;
141
142#ifdef HAVE_GETADDRINFO_A
143 lookup_result = mutt_getaddrinfo_a(node, &hints);
144#else
145 lookup_result = mutt_getaddrinfo(node, &hints);
146#endif
147
148 if (lookup_result && lookup_result->ai_canonname)
149 {
150 const char *hostname = strchr(lookup_result->ai_canonname, '.');
151 if (hostname && hostname[1] != '\0')
152 {
153 buf_strcpy(result, ++hostname);
154 rc = 0;
155 }
156 }
157
158 if (lookup_result)
159 freeaddrinfo(lookup_result);
160#endif
161
162 return rc;
163}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
int getdnsdomainname(struct Buffer *result)
Lookup the host's name using DNS.
Definition: getdomain.c:125
static struct addrinfo * mutt_getaddrinfo(const char *node, const struct addrinfo *hints)
Lookup the host's name using getaddrinfo()
Definition: getdomain.c:103
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
Convenience wrapper for the library headers.
Key value store.
String manipulation buffer.
Definition: buffer.h:36
Time value with nanosecond precision.
Definition: file.h:51
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:53