DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::Host Class Reference

Network host. More...

#include <Host.hpp>

Inheritance diagram for OksSystem::Host:
[legend]

Public Member Functions

 Host (const Host &other)
 copy constructor
 
 Host (const std::string &name)
 constructor from name
 
 Host (struct sockaddr_in ip_addr)
 constructor from ip address
 
virtual ~Host () throw ()
 destructor
 
 operator struct sockaddr_in () const throw ()
 cast to ip address
 
bool equals (const Host &other) const throw ()
 equality method
 
struct sockaddr_in ip () const throw ()
 get ip address
 
const std::string & name () const throw ()
 get name
 
const std::string & full_name () const throw ()
 get fully qualified name
 
std::string ip_string () const throw ()
 get ip in string mode
 

Static Public Member Functions

static struct sockaddr_in resolve (const std::string &name) throw ()
 name to ip conversion
 
static std::string resolve (struct sockaddr_in address) throw ()
 ip to name conversion
 
static std::string expand (const std::string &name) throw ()
 expands to full name
 
static std::string to_string (struct sockaddr_in ip_addr)
 ip to string conversion
 

Protected Member Functions

 Host () throw ()
 constructor for current host
 

Protected Attributes

std::string m_name
 name of the host
 
std::string m_full_name
 cached fully qualified host name
 

Detailed Description

Network host.

This class represents an network host. It offers facilities to translate name to addresses and vice versa.

Author
Matthias Wiesmann
Version
1.0

Definition at line 28 of file Host.hpp.

Constructor & Destructor Documentation

◆ Host() [1/4]

OksSystem::Host::Host ( )
throw ( )
protected

constructor for current host

Constructors for the local host

Definition at line 92 of file Host.cpp.

92{} // Host

◆ Host() [2/4]

OksSystem::Host::Host ( const Host & other)

copy constructor

Definition at line 94 of file Host.cpp.

94 {
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
std::string m_name
name of the host
Definition Host.hpp:30
std::string m_full_name
cached fully qualified host name
Definition Host.hpp:31

◆ Host() [3/4]

OksSystem::Host::Host ( const std::string & s_name)

constructor from name

Constructor for host by name

Parameters
namename of the host

Definition at line 106 of file Host.cpp.

106 {
107 m_name = s_name;
108} // Host

◆ Host() [4/4]

OksSystem::Host::Host ( struct sockaddr_in ip_addr)

constructor from ip address

Definition at line 111 of file Host.cpp.

111 {
112 m_name = resolve(ip_addr);
114} // Host
static struct sockaddr_in resolve(const std::string &name)
name to ip conversion
Definition Host.cpp:32

◆ ~Host()

OksSystem::Host::~Host ( )
throw ( )
virtual

destructor

Definition at line 117 of file Host.cpp.

117 {
118} // OksSystem

Member Function Documentation

◆ equals()

bool OksSystem::Host::equals ( const Host & other) const
throw ( )

equality method

Comparison method We try to expand both name and compare those

Parameters
otherthe host to compare to
Returns
true if both have the same fully qualified name

Definition at line 137 of file Host.cpp.

137 {
138 return full_name()==other.full_name();
139} // equals
const std::string & full_name() const
get fully qualified name
Definition Host.cpp:161

◆ expand()

std::string OksSystem::Host::expand ( const std::string & name)
throw ( )
static

expands to full name

Tries to build a fully qualified name. This is done first by converting the name to ip then the ip to a name. If this fails (basically, we cannot do DNS resolves) the non fully qualified name is returned

Parameters
namethe (partial) name
Returns
fullname

Definition at line 69 of file Host.cpp.

69 {
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
const std::string & name() const
get name
Definition Host.cpp:149

◆ full_name()

const std::string & OksSystem::Host::full_name ( ) const
throw ( )

get fully qualified name

Fully qualified name of the host

Returns
Fully qualified name of the host, or the string "0.0.0.0"

Definition at line 161 of file Host.cpp.

161 {
162 if (m_full_name.empty()) {
164 } // if
165 return m_full_name;
166} // full_name
static std::string expand(const std::string &name)
expands to full name
Definition Host.cpp:69

◆ ip()

struct sockaddr_in OksSystem::Host::ip ( ) const
throw ( )

get ip address

IP address of the host

Returns
IP address of the host, or 0.0.0.0 if it cannot be resolved

Definition at line 155 of file Host.cpp.

155{ return resolve(m_name);}

◆ ip_string()

std::string OksSystem::Host::ip_string ( ) const
throw ( )

get ip in string mode

The IP Address of the host, as a string

Returns
a string containing the IP address of the host in w.x.y.z format

Definition at line 172 of file Host.cpp.

172 {
173 const struct sockaddr_in address = ip();
174 return to_string(address);
175} // ip_string
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

◆ name()

const std::string & OksSystem::Host::name ( ) const
throw ( )

get name

Name (this might be non fully qualified name)

Returns
name of the host

Definition at line 149 of file Host.cpp.

149{ return m_name;}

◆ operator struct sockaddr_in()

OksSystem::Host::operator struct sockaddr_in ( ) const
throw ( )

cast to ip address

Cast conversion into ip address

Returns
ip address

Definition at line 127 of file Host.cpp.

127 {
128 return ip();
129} // struct sockaddr_in

◆ resolve() [1/2]

struct sockaddr_in OksSystem::Host::resolve ( const std::string & name)
throw ( )
static

name to ip conversion

Translates an hostname into an ip address

Parameters
namehostname to translate
Returns
the ip addrress, 0.0.0.0 if name not found.
Note
only returns the first ip address associated with name

Definition at line 32 of file Host.cpp.

32 {
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

◆ resolve() [2/2]

std::string OksSystem::Host::resolve ( struct sockaddr_in address)
throw ( )
static

ip to name conversion

Translates an ip address into an hostname

Parameters
addressthe address to translate
Returns
the hostname an empty string if the hostname could not be resolved

Definition at line 53 of file Host.cpp.

53 {
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

◆ to_string()

std::string OksSystem::Host::to_string ( struct sockaddr_in ip_addr)
static

ip to string conversion

Transliterate an ip address into the canonical dotted text version (w.x.y.z).

Parameters
ip_addrthe address to translate
Returns
string containing the text version
Note
Should use addr2ascii

Definition at line 81 of file Host.cpp.

81 {
82 const char* s = inet_ntoa(ip_addr.sin_addr);
83 return std::string(s);
84} // to_string

Member Data Documentation

◆ m_full_name

std::string OksSystem::Host::m_full_name
mutableprotected

cached fully qualified host name

Definition at line 31 of file Host.hpp.

◆ m_name

std::string OksSystem::Host::m_name
protected

name of the host

Definition at line 30 of file Host.hpp.


The documentation for this class was generated from the following files: