4 * This file is part of nss-myhostname.
6 * nss-myhostname is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * nss-myhostname is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with nss-myhostname; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <sys/types.h>
32 #define LOCALADDRESS (htonl(0x7F000002))
34 static enum nss_status fill_in_hostent(
36 struct hostent *result,
43 char *r_addr, *r_name, *r_aliases, *r_addr_list;
46 if (buflen < l+1+sizeof(char*)+4+sizeof(char*)*2) {
48 *h_errnop = NO_RECOVERY;
49 return NSS_STATUS_TRYAGAIN;
57 *(char**) (buffer + idx) = NULL;
58 r_aliases = buffer + idx;
61 r_addr = buffer + idx;
62 *(uint32_t*) &buffer[idx] = LOCALADDRESS;
65 r_addr_list = buffer + idx;
66 * (char**) (buffer + idx) = r_addr;
67 * (((char**) (buffer + idx)) +1) = NULL;
69 result->h_name = r_name;
70 result->h_aliases = (char**) r_aliases;
71 result->h_addrtype = AF_INET;
73 result->h_addr_list = (char**) r_addr_list;
75 return NSS_STATUS_SUCCESS;
78 enum nss_status _nss_myhostname_gethostbyname2_r(
81 struct hostent * result,
87 char hn[HOST_NAME_MAX+1];
97 *h_errnop = HOST_NOT_FOUND;
98 return NSS_STATUS_NOTFOUND;
101 if (gethostname(hn, sizeof(hn)-1) < 0) {
103 *h_errnop = NO_RECOVERY;
104 return NSS_STATUS_TRYAGAIN;
107 hn[sizeof(hn)-1] = 0;
109 if (strcasecmp(name, hn) != 0) {
111 *h_errnop = HOST_NOT_FOUND;
112 return NSS_STATUS_NOTFOUND;
115 return fill_in_hostent(hn, result, buffer, buflen, errnop, h_errnop);
118 enum nss_status _nss_myhostname_gethostbyname_r (
120 struct hostent *result,
126 return _nss_myhostname_gethostbyname2_r(
136 enum nss_status _nss_myhostname_gethostbyaddr_r(
140 struct hostent *result,
146 char hn[HOST_NAME_MAX+1];
151 if (af != AF_INET || len < 4 || (*(uint32_t*) addr) != LOCALADDRESS) {
153 *h_errnop = HOST_NOT_FOUND;
154 return NSS_STATUS_NOTFOUND;
157 if (gethostname(hn, sizeof(hn)-1) < 0) {
159 *h_errnop = NO_RECOVERY;
160 return NSS_STATUS_TRYAGAIN;
163 hn[sizeof(hn)-1] = 0;
165 return fill_in_hostent(hn, result, buffer, buflen, errnop, h_errnop);