Annotation of fam/fam/ServerHostRef.c++, Revision 1.1.1.1
1.1 trev 1: // Copyright (C) 1999 Silicon Graphics, Inc. All Rights Reserved.
2: //
3: // This program is free software; you can redistribute it and/or modify it
4: // under the terms of version 2 of the GNU General Public License as
5: // published by the Free Software Foundation.
6: //
7: // This program is distributed in the hope that it would be useful, but
8: // WITHOUT ANY WARRANTY; without even the implied warranty of
9: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Further, any
10: // license provided herein, whether implied or otherwise, is limited to
11: // this program in accordance with the express provisions of the GNU
12: // General Public License. Patent licenses, if any, provided herein do not
13: // apply to combinations of this program with other product or programs, or
14: // any other product whatsoever. This program is distributed without any
15: // warranty that the program is delivered free of the rightful claim of any
16: // third person by way of infringement or the like. See the GNU General
17: // Public License for more details.
18: //
19: // You should have received a copy of the GNU General Public License along
20: // with this program; if not, write the Free Software Foundation, Inc., 59
21: // Temple Place - Suite 330, Boston MA 02111-1307, USA.
22:
23: #include "ServerHostRef.h"
24:
25: #include <assert.h>
26: #include <netdb.h>
27:
28: unsigned int ServerHostRef::count;
29: StringTable<ServerHost *> *ServerHostRef::hosts_by_name;
30:
31: ///////////////////////////////////////////////////////////////////////////////
32: // ServerHostRef methods simply maintain refcounts on ServerHosts.
33:
34: ServerHostRef&
35: ServerHostRef::operator = (const ServerHostRef& that)
36: {
37: if (this != &that)
38: {
39: if (p && !--p->refcount)
40: delete p;
41: p = that.p;
42: if (p)
43: p->refcount++;
44: }
45: return *this;
46: }
47:
48: ServerHostRef::~ServerHostRef()
49: {
50: if (!--p->refcount)
51: {
52: // Remove from hosts_by_name.
53:
54: const char *name;
55: for (unsigned i = 0; ((name = hosts_by_name->key(i)) != NULL); )
56: if (hosts_by_name->value(i) == p)
57: hosts_by_name->remove(name);
58: else
59: i++;
60:
61: delete p;
62: }
63:
64: // When the last reference is gone, delete hosts_by_name.
65:
66: if (!--count && hosts_by_name)
67: { assert(!hosts_by_name->size());
68: delete hosts_by_name;
69: hosts_by_name = NULL;
70: }
71: }
72:
73: ServerHost *
74: ServerHostRef::find(const char *name)
75: {
76: // Create hosts_by_name first time.
77:
78: if (!hosts_by_name)
79: hosts_by_name = new StringTable<ServerHost *>;
80:
81: // Look in list of existing hosts.
82:
83: ServerHost *host = hosts_by_name->find(name);
84: if (host)
85: return host;
86:
87: // Look up hostname via gethostbyname().
88:
89: hostent *hp = gethostbyname(name);
90: char *fake_haliases[1];
91: in_addr fake_haddr;
92: in_addr *fake_haddrlist[2];
93: hostent fake_hostent;
94:
95: if (!hp)
96: {
97: // gethostbyname() failed for some reason (e.g., ypbind
98: // is down or NIS map just changed, ethernet came unplugged,
99: // or something equally tragic).
100: //
101: // Fake it by building a hostent with an illegal address.
102:
103: fake_haliases[0] = NULL;
104: fake_haddr.s_addr = 0;
105: fake_haddrlist[0] = &fake_haddr;
106: fake_haddrlist[1] = NULL;
107: fake_hostent.h_name = (char *) name;
108: fake_hostent.h_aliases = fake_haliases;
109: fake_hostent.h_addrtype = 0;
110: fake_hostent.h_length = sizeof fake_haddr;
111: fake_hostent.h_addr_list = (char **) fake_haddrlist;
112: hp = &fake_hostent;
113: }
114:
115: assert(hp->h_length == sizeof (struct in_addr));
116:
117: // Create new host.
118:
119: host = new ServerHost(*hp);
120:
121: // Add this host to hosts_by_name.
122:
123: hosts_by_name->insert(name, host);
124: hosts_by_name->insert(hp->h_name, host);
125: for (char *const*p = hp->h_aliases; *p; p++)
126: hosts_by_name->insert(*p, host);
127:
128: return host;
129: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>