ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
Revision: 13
Committed: 2003-11-07T12:17:57-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 2226 byte(s)
Log Message:
Goodbye, cruel world, goodbye, cruel lamp...

File Contents

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

Properties

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