mutt stable branch with some hacks
1/*
2 * Copyright (C) 2009,2013,2016 Derek Martin <code@pizzashack.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#if HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <string.h>
24#include <unistd.h>
25#include <netdb.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28
29#include "mutt.h"
30
31
32int getdnsdomainname (char *d, size_t len)
33{
34 int ret = -1;
35
36#ifdef HAVE_GETADDRINFO
37 char *node;
38 long node_len;
39 struct addrinfo hints;
40 struct addrinfo *h;
41 char *p;
42
43 *d = '\0';
44 memset(&hints, 0, sizeof (struct addrinfo));
45 hints.ai_flags = AI_CANONNAME;
46 hints.ai_family = AF_UNSPEC;
47
48 /* A DNS name can actually be only 253 octets, string is 256 */
49 if ((node_len = sysconf(_SC_HOST_NAME_MAX)) == -1)
50 node_len = STRING;
51 node = safe_malloc(node_len + 1);
52 if (gethostname(node, node_len))
53 ret = -1;
54 else if (getaddrinfo(node, NULL, &hints, &h))
55 ret = -1;
56 else
57 {
58 if (!h->ai_canonname || !(p = strchr(h->ai_canonname, '.')))
59 ret = -1;
60 else
61 {
62 strfcpy(d, ++p, len);
63 ret = 0;
64 dprint(1, (debugfile, "getdnsdomainname(): %s\n", d));
65 }
66 freeaddrinfo(h);
67 }
68 FREE (&node);
69#endif
70
71 return ret;
72}