2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $TOG: tt_host.C /main/9 1999/10/14 18:41:36 mgreess $
30 * @(#)tt_host.C 1.12 93/09/07
32 * Copyright (c) 1990 by Sun Microsystems, Inc.
35 #include <sys/types.h>
36 #include <util/tt_host.h>
37 #include <sys/param.h>
40 /* AIX's arpa/inet.h has a buggy declaration of inet_addr */
41 extern "C" in_addr_t inet_addr(const char *);
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #if !defined(linux) && !defined(CSRG_BASED) && !defined(sun)
52 #define X_INCLUDE_NETDB_H
53 #define XOS_USE_XT_LOCKING
54 #include <X11/Xos_r.h>
56 #include "util/tt_port.h"
58 #if defined(OPT_BUG_SUNOS_4) && defined(__GNUG__)
59 extern "C" { int endhostent(); }
62 #define IP_QUALIFIER "tcp"
63 #define TLI_QUALIFIER "tli"
80 * Destructor. Sets _name to NULL so that reference counting can free up the
93 * Initializes a _Tt_host object from a hostent structure which is the
94 * standard structure returned by gethostbyaddr and gethostbyname.
97 init_from_hostent(hostent *h)
102 if (h != (hostent *)0) {
104 _addr.set((const unsigned char *)h->h_addr, h->h_length);
105 ip_addr = (char *)_addr;
106 sprintf(str, "%d.%d.%d.%d",
107 (int)((unsigned char *)ip_addr)[0],
108 (int)((unsigned char *)ip_addr)[1],
109 (int)((unsigned char *)ip_addr)[2],
110 (int)((unsigned char *)ip_addr)[3]);
111 _string_address = str;
112 // We use the ip address but we qualify it with the
113 // keyword IP_QUALIFIER so that SVR4 clients can
114 // interoperate with this name.
117 _netname = IP_QUALIFIER;
118 _netname = _netname.cat(":").cat(_string_address);
127 * Initializes a _Tt_host object from an IP address.
129 * --> This method should attempt to cache results as much as possible to
130 * avoid repeated calls to gethostbyaddr (which may, in the abscence of
131 * DNS trigger YP lookups). (and in the presence of DNS trigger DNS
132 * lookups which can be just as bad...)
135 init_byaddr(_Tt_string addr)
138 struct hostent *addr_ret;
139 _Xgethostbynameparams addr_buf;
141 memset((char*) &addr_buf, 0, sizeof(_Xgethostbynameparams));
142 addr_ret = _XGethostbyaddr((char *)addr, 4, AF_INET, addr_buf);
143 result = init_from_hostent(addr_ret);
144 #ifdef OPT_BUG_SUNOS_5
145 // gethostbyaddr is effectively built on gethostent. If
146 // endhostent is not called, storage is left around to save the
147 // name service connection, etc. bug 1111175
155 * Initializes a _Tt_host object from a string representing the host
156 * address in Internet '.' notation.
159 init_bystringaddr(_Tt_string addr)
161 unsigned long *ip_address;
162 unsigned long ip_address_buf;
163 struct hostent *addr_ret;
164 _Xgethostbynameparams addr_buf;
166 memset((char*) &addr_buf, 0, sizeof(_Xgethostbynameparams));
167 ip_address = &ip_address_buf;
169 *ip_address = inet_addr((char *)addr);
170 if (*ip_address == INADDR_NONE) {
174 _addr.set((const unsigned char *)ip_address, 4);
175 addr_ret = _XGethostbyaddr((char *)_addr, 4, AF_INET, addr_buf);
176 if (! init_from_hostent(addr_ret)) {
177 // given an ip address we can still communicate with
178 // this host but we may not know it's name
179 _string_address = addr;
180 _netname = IP_QUALIFIER;
181 _netname = _netname.cat(":").cat(_string_address);
182 // we couldn't find out the name of the host so we'll
183 // just call it by it's ip address
186 #ifdef OPT_BUG_SUNOS_5
187 // gethostbyaddr is effectively built on gethostent. If
188 // endhostent is not called, storage is left around to save the
189 // name service connection, etc. bug 1111175
196 * Initializes a _Tt_host object from a host name.
200 init_byname(_Tt_string name)
205 struct hostent *host_ret;
206 _Xgethostbynameparams host_buf;
208 memset((char*) &host_buf, 0, sizeof(_Xgethostbynameparams));
209 if (name.len() == 0) {
210 _name = _tt_gethostname();
212 qual = name.split(':',_name);
213 if (_name.len()== 0) {
217 if (qual == IP_QUALIFIER) {
218 return(init_bystringaddr(_name));
219 } else if (qual == TLI_QUALIFIER) {
220 /* just leave _name alone */;
222 // unknown qualifier!
227 host_ret = _XGethostbyname((char *)_name, host_buf);
228 if (host_ret == NULL) {
229 result = init_bystringaddr(_name);
231 result = init_from_hostent(host_ret);
233 #ifdef OPT_BUG_SUNOS_5
234 // gethostbyname is effectively built on gethostent. If
235 // endhostent is not called, storage is left around to save the
236 // name service connection, etc. bug 1111175
244 * Returns the length of the host address.
256 return(_addr.xdr(xdrs));
260 _tt_host_addr(_Tt_object_ptr &o)
262 return(((_Tt_host *)o.c_pointer())->addr());
267 _tt_host_name(_Tt_object_ptr &o)
269 return(((_Tt_host *)o.c_pointer())->name());