ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
Revision: 192
Committed: 2004-08-22T21:23:29-07:00 (20 years, 10 months ago) by Douglas Thrift
File size: 2514 byte(s)
Log Message:
Meep!

File Contents

# User Rev Content
1 Douglas Thrift 11 // Host Update
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Host.hpp"
8    
9 Douglas Thrift 12 Host::Host(const string& host, const string& name, const string& address, const
10     string& platform)
11 Douglas Thrift 11 {
12 Douglas Thrift 22 memset(&this->address, 0, sizeof(in_addr));
13    
14 Douglas Thrift 11 #ifdef _WIN32
15     if (count == 0)
16     {
17     if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
18     {
19     cerr << "Host(): WSAStartup()\n";
20     exit(1);
21     }
22     }
23    
24     count++;
25     #endif
26    
27     setHost(host);
28     if (name != "") setName(name, address == "");
29     if (address != "") setAddress(address, name == "");
30 Douglas Thrift 12 setPlatform(platform);
31 Douglas Thrift 11 }
32    
33     Host::~Host()
34     {
35     #ifdef _WIN32
36     count--;
37    
38     if (count == 0)
39     {
40     if (WSACleanup() != 0)
41     {
42     cerr << "~Host(): WSACleanup()\n";
43     exit(1);
44     }
45     }
46     #endif
47     }
48    
49     void Host::setName(const string& name, bool lookup)
50     {
51     this->name = name;
52    
53     if (lookup)
54     {
55 Douglas Thrift 192 struct hostent* ent(gethostbyname(this->name.c_str()));
56 Douglas Thrift 11
57 Douglas Thrift 15 memcpy(&address, *(ent->h_addr_list), ent->h_length);
58 Douglas Thrift 11 }
59     }
60    
61     void Host::setAddress(const string& address, bool lookup)
62     {
63     in_addr_t value = inet_addr(address.c_str());
64    
65     if (value == INADDR_NONE)
66     {
67     cerr << "Host.setAddress(): INADDR_NONE\n";
68     }
69     else
70     {
71 Douglas Thrift 15 memcpy(&this->address, &value, sizeof(in_addr_t));
72 Douglas Thrift 11 }
73    
74     if (lookup)
75     {
76 Douglas Thrift 192 struct hostent* ent(gethostbyaddr((char*)(&this->address),
77     sizeof(in_addr), AF_INET));
78 Douglas Thrift 11
79 Douglas Thrift 12 if (ent != NULL) name = ent->h_name;
80 Douglas Thrift 11 }
81     }
82    
83 Douglas Thrift 13 bool Host::operator==(const Host& host) const
84 Douglas Thrift 11 {
85 Douglas Thrift 13 if (this->host == host.host)
86     {
87     if (name == host.name)
88     {
89 Douglas Thrift 15 if (string(inet_ntoa(address)) == inet_ntoa(host.address))
90 Douglas Thrift 13 {
91     return platform == host.platform;
92     }
93     else
94     {
95     return false;
96     }
97     }
98     else
99     {
100     return false;
101     }
102     }
103     else
104     {
105     return false;
106     }
107     }
108    
109     Host Host::operator++()
110     {
111 Douglas Thrift 192 string file(string("hosts") + slash + host);
112 Douglas Thrift 15 ifstream fin(file.c_str());
113    
114 Douglas Thrift 13 if (fin.is_open())
115     {
116     getline(fin, name);
117 Douglas Thrift 15
118     string address;
119 Douglas Thrift 19
120 Douglas Thrift 15 getline(fin, address);
121    
122 Douglas Thrift 192 in_addr_t value(inet_addr(address.c_str()));
123 Douglas Thrift 15
124     if (value == INADDR_NONE)
125     {
126     cerr << "Host.operator++(): INADDR_NONE\n";
127     }
128     else
129     {
130     memcpy(&this->address, &value, sizeof(in_addr_t));
131     }
132    
133 Douglas Thrift 13 getline(fin, platform);
134     fin.close();
135     }
136    
137     return *this;
138 Douglas Thrift 11 }
139    
140 Douglas Thrift 13 Host Host::operator++(int)
141 Douglas Thrift 11 {
142 Douglas Thrift 13 Host old = *this;
143    
144     ++*this;
145    
146     return old;
147     }
148    
149     Host Host::operator--()
150     {
151 Douglas Thrift 15 string file = string("hosts") + slash + host;
152 Douglas Thrift 19
153 Douglas Thrift 15 ofstream fout(file.c_str());
154 Douglas Thrift 11
155 Douglas Thrift 15 fout << name << '\n'
156     << inet_ntoa(address) << '\n'
157     << platform << '\n';
158 Douglas Thrift 11
159     fout.close();
160 Douglas Thrift 13
161     return *this;
162 Douglas Thrift 11 }
163    
164 Douglas Thrift 13 Host Host::operator--(int)
165     {
166     Host old = *this;
167    
168     --*this;
169    
170     return old;
171     }
172    
173 Douglas Thrift 11 #ifdef _WIN32
174    
175 Douglas Thrift 192 unsigned Host::count(0);
176 Douglas Thrift 11 WSADATA Host::data;
177    
178     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id