ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
Revision: 12
Committed: 2003-11-06T23:43:01-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 1716 byte(s)
Log Message:
The universe is going to hell! I'm in love!

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 void Host::operator++()
87 {
88 ifstream fin(host.c_str());
89
90 getline(fin, name);
91 fin.read((char*)(address), sizeof(in_addr));
92 getline(fin, platform);
93 fin.close();
94 }
95
96 void Host::operator--()
97 {
98 ofstream fout(host.c_str());
99
100 fout << name << '\n';
101
102 fout.write((char*)(address), sizeof(in_addr));
103
104 fout << platform;
105
106 fout.close();
107 }
108
109 #ifdef _WIN32
110
111 unsigned Host::count = 0;
112 WSADATA Host::data;
113
114 #endif

Properties

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