2 * This file is part of nss-myhostname.
4 * nss-myhostname is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * nss-myhostname 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with nss-myhostname; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #include <sys/types.h>
30 #define LOCALADDRESS (htonl(0x7F0002))
32 static enum nss_status fill_in_hostent(
34 struct hostent *result,
41 char *r_addr, *r_name, *r_aliases, *r_addr_list;
44 if (buflen < l+1+sizeof(char*)+4+sizeof(char*)*2) {
46 *h_errnop = NO_RECOVERY;
47 return NSS_STATUS_TRYAGAIN;
55 *(char**) (buffer + idx) = NULL;
56 r_aliases = buffer + idx;
59 r_addr = buffer + idx;
60 *(uint32_t*) &buffer[idx] = LOCALADDRESS;
63 r_addr_list = buffer + idx;
64 * (char**) (buffer + idx) = r_addr;
65 * (((char**) (buffer + idx)) +1) = NULL;
67 result->h_name = r_name;
68 result->h_aliases = (char**) r_aliases;
69 result->h_addrtype = AF_INET;
71 result->h_addr_list = (char**) r_addr_list;
73 return NSS_STATUS_SUCCESS;
76 enum nss_status _nss_hostname_gethostbyname2_r(
79 struct hostent * result,
85 char hn[HOST_NAME_MAX+1];
95 *h_errnop = HOST_NOT_FOUND;
96 return NSS_STATUS_NOTFOUND;
99 if (gethostname(hn, sizeof(hn)-1) < 0) {
101 *h_errnop = NO_RECOVERY;
102 return NSS_STATUS_TRYAGAIN;
105 hn[sizeof(hn)-1] = 0;
107 if (strcasecmp(name, hn) != 0) {
109 *h_errnop = HOST_NOT_FOUND;
110 return NSS_STATUS_NOTFOUND;
113 return fill_in_hostent(hn, result, buffer, buflen, errnop, h_errnop);
116 enum nss_status _nss_hostname_gethostbyname_r (
118 struct hostent *result,
124 return _nss_hostname_gethostbyname2_r(
134 enum nss_status _nss_hostname_gethostbyaddr_r(
138 struct hostent *result,
144 char hn[HOST_NAME_MAX+1];
149 if (af != AF_INET || len != 4 || (*(uint32_t*) addr) != LOCALADDRESS) {
151 *h_errnop = HOST_NOT_FOUND;
152 return NSS_STATUS_NOTFOUND;
155 if (gethostname(hn, sizeof(hn)-1) < 0) {
157 *h_errnop = NO_RECOVERY;
158 return NSS_STATUS_TRYAGAIN;
161 hn[sizeof(hn)-1] = 0;
163 return fill_in_hostent(hn, result, buffer, buflen, errnop, h_errnop);