DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Host.cpp
Go to the documentation of this file.
1/*
2 * Host.cxx
3 * OksSystem
4 *
5 * Created by Matthias Wiesmann on 03.02.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <sys/utsname.h>
14#include <netdb.h>
15
16#include <unistd.h>
17
18#include <iostream>
19#include <sstream>
20#include <strings.h>
21
22#include "okssystem/Host.hpp"
23
24#define BUFFER_SIZE 256
25
32struct sockaddr_in OksSystem::Host::resolve(const std::string &name) throw() {
33 struct addrinfo *info_ptr;
34 const char *str = name.c_str();
35 const int status = getaddrinfo(str,0,0,&info_ptr);
36 struct sockaddr_in address;
37 bzero(&address,sizeof(address));
38 address.sin_family = AF_INET;
39 address.sin_addr.s_addr=0;
40 if (status==0) {
41 struct sockaddr_in *ptr = (sockaddr_in *) info_ptr->ai_addr;
42 address = *ptr;
43 freeaddrinfo(info_ptr);
44 } // if
45 return address;
46} // resolve
47
53std::string OksSystem::Host::resolve(struct sockaddr_in address) throw() {
54 char buffer[NI_MAXHOST];
55 const struct sockaddr *ptr = (const struct sockaddr *) &address;
56 const int status= getnameinfo(ptr,sizeof(address),buffer,sizeof(buffer),0,0,0);
57 if (status!=0) return to_string(address);
58 return std::string(buffer);
59} // resolve
60
69std::string OksSystem::Host::expand(const std::string &name) throw() {
70 const struct sockaddr_in address = resolve(name);
71 if (address.sin_addr.s_addr!=0) return resolve(address);
72 return std::string(name) ;
73} // expand
74
81std::string OksSystem::Host::to_string(struct sockaddr_in ip_addr) {
82 const char* s = inet_ntoa(ip_addr.sin_addr);
83 return std::string(s);
84} // to_string
85
86// Constructors, destructors
87// --------------------------
88
92OksSystem::Host::Host() throw() {} // Host
93
95 m_name = other.m_name;
96 if (! other.m_full_name.empty()) {
97 m_full_name = other.m_full_name;
98 } // if
99} // Host
100
101
106OksSystem::Host::Host(const std::string &s_name) {
107 m_name = s_name;
108} // Host
109
110
111OksSystem::Host::Host(struct sockaddr_in ip_addr) {
112 m_name = resolve(ip_addr);
113 m_full_name = m_name;
114} // Host
115
116
118} // OksSystem
119
120// Operator
121// --------------------------
122
127OksSystem::Host::operator struct sockaddr_in() const throw() {
128 return ip();
129} // struct sockaddr_in
130
137bool OksSystem::Host::equals(const Host &other) const throw() {
138 return full_name()==other.full_name();
139} // equals
140
141
142// Methods
143// --------------------------
144
149const std::string & OksSystem::Host::name() const throw() { return m_name;}
150
155struct sockaddr_in OksSystem::Host::ip() const throw() { return resolve(m_name);}
156
161const std::string & OksSystem::Host::full_name() const throw() {
162 if (m_full_name.empty()) {
163 m_full_name = expand(m_name);
164 } // if
165 return m_full_name;
166} // full_name
167
172std::string OksSystem::Host::ip_string() const throw() {
173 const struct sockaddr_in address = ip();
174 return to_string(address);
175} // ip_string
176
177
178
179bool OksSystem::operator ==(const Host &a, const Host &b) throw() {
180 return a.equals(b);
181} // operator ==
182
183bool OksSystem::operator !=(const Host &a, const Host &b) throw() {
184 return ! a.equals(b);
185} // operator ≠
186
187// Local Host
188// --------------------------
189
195
202const std::string & OksSystem::LocalHost::local_name() throw() {
203 return instance()->name();
204} // localhostname
205
211const std::string & OksSystem::LocalHost::full_local_name() throw() {
212 return instance()->full_name();
213} // fulllocalhostname
214
222 if (0==s_instance) {
223 s_instance = new LocalHost();
224 } //
225 return s_instance;
226} // instance
227
234 struct utsname u_name_data;
235 const int status = ::uname(&u_name_data);
236 if (status==0) {
237 m_name = u_name_data.nodename;
238 m_os_name = u_name_data.sysname;
239 m_release = u_name_data.release;
240 m_version = u_name_data.version;
241 m_machine = u_name_data.machine;
242 } else {
243 char buffer[NI_MAXHOST];
244 const int host_status = gethostname(buffer,sizeof(buffer));
245 if (host_status==0) {
246 m_name = buffer;
247 } else { // both uname and gethostname screwed
248 m_name.clear();
249 } // check for host_name
250 } // uname failed.
251} // LocalHost
252
254 // delete s_instance;
255}
256
259const std::string & OksSystem::LocalHost::os_name() const throw() {
260 return m_os_name;
261} // os_name
262
265const std::string & OksSystem::LocalHost::os_release() const throw() {
266 return m_release;
267} // os_release
268
271const std::string & OksSystem::LocalHost::os_version() const throw() {
272 return m_version;
273} // os_version
274
277const std::string & OksSystem::LocalHost::machine() const throw() {
278 return m_machine;
279} // machine
280
286const std::string & OksSystem::LocalHost::description() const throw() {
287 if (m_description.empty()) {
288 std::ostringstream stream;
289 stream << m_os_name << " " << m_release << "/" << m_machine;
290 m_description = stream.str();
291 }
292 return m_description;
293} // description
294
302const char* OksSystem::getfullhost() throw() {
303 const std::string & str = OksSystem::LocalHost::full_local_name();
304 return str.c_str();
305} // full_host_name
306
313bool OksSystem::operator ==(const LocalHost &, const LocalHost & ) throw()
314{
315 return true;
316}
317
324bool OksSystem::operator !=(const LocalHost &, const LocalHost & ) throw()
325{
326 return false;
327}
328
329std::ostream& operator<<(std::ostream& stream, const OksSystem::Host& host) {
330 stream << host.full_name();
331 return stream;
332} // operator<<
333
334
335
std::ostream & operator<<(std::ostream &stream, const OksSystem::File &file)
Definition File.cpp:852
Network host.
Definition Host.hpp:28
virtual ~Host()
destructor
Definition Host.cpp:117
std::string ip_string() const
get ip in string mode
Definition Host.cpp:172
static struct sockaddr_in resolve(const std::string &name)
name to ip conversion
Definition Host.cpp:32
const std::string & name() const
get name
Definition Host.cpp:149
static std::string expand(const std::string &name)
expands to full name
Definition Host.cpp:69
struct sockaddr_in ip() const
get ip address
Definition Host.cpp:155
static std::string to_string(struct sockaddr_in ip_addr)
ip to string conversion
Definition Host.cpp:81
bool equals(const Host &other) const
equality method
Definition Host.cpp:137
const std::string & full_name() const
get fully qualified name
Definition Host.cpp:161
std::string m_name
name of the host
Definition Host.hpp:30
Host()
constructor for current host
Definition Host.cpp:92
std::string m_full_name
cached fully qualified host name
Definition Host.hpp:31
Network host - localhost.
Definition Host.hpp:62
const std::string & description() const
machine type *‍/
Definition Host.cpp:286
static LocalHost * s_instance
singleton for local host
Definition Host.hpp:65
const std::string & os_release() const
release of the operating system
Definition Host.cpp:265
static const std::string & full_local_name()
fully qualified local host name
Definition Host.cpp:211
const std::string & os_name() const
name of the operating system
Definition Host.cpp:259
static const LocalHost * instance()
pointer to the singleton local host
Definition Host.cpp:221
const std::string & os_version() const
version of the operating system
Definition Host.cpp:271
static const std::string & local_name()
localhostname
Definition Host.cpp:202
const std::string & machine() const
version of the operating system
Definition Host.cpp:277
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:183
const char * getfullhost()
get fully qualified host name
Definition Host.cpp:302
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:179