ec73e869751c798c1d69aec1ac598719ea062c08
[mmh] / mts / smtp / hosts.c
1
2 /*
3  * hosts.c -- find out the official name of a host
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 /*
11  * In the SendMail world, we really don't know what the valid
12  * hosts are.  We could poke around in the sendmail.cf file, but
13  * that still isn't a guarantee.  As a result, we'll say that
14  * everything is a valid host, and let SendMail worry about it.
15  */
16
17 #include <h/mh.h>
18 #include <h/mts.h>
19 #include <netdb.h>
20
21
22 char *
23 OfficialName (char *name)
24 {
25     unsigned char *p;
26     char *q, site[BUFSIZ];
27     struct addrinfo hints, *res;
28
29     static char buffer[BUFSIZ];
30
31     for (p = name, q = site; *p && (q - site < sizeof(site) - 1); p++, q++)
32         *q = isupper (*p) ? tolower (*p) : *p;
33     *q = '\0';
34     q = site;
35
36     if (!mh_strcasecmp (LocalName(), site))
37         return LocalName();
38
39     memset(&hints, 0, sizeof(hints));
40     hints.ai_flags = AI_CANONNAME;
41     hints.ai_family = PF_UNSPEC;
42
43     if (getaddrinfo(q, NULL, &hints, &res) == 0) {
44         strncpy (buffer, res->ai_canonname, sizeof(buffer));
45         buffer[sizeof(buffer) - 1] = '\0';
46         freeaddrinfo(res);
47         return buffer;
48     }
49
50     strncpy (buffer, site, sizeof(buffer));
51     return buffer;
52 }