Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JNetwork.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM__JNETWORK__
2 #define __JSYSTEM__JNETWORK__
3 
4 #include <unistd.h>
5 #include <netdb.h>
6 #include <arpa/inet.h>
7 
8 #include <string.h>
9 #include <sstream>
10 
11 #include "JLang/JException.hh"
12 
13 /**
14  * \file
15  * Hostname and IP address functions.
16  * \author mdejong
17  */
18 namespace JSYSTEM {}
19 namespace JPP { using namespace JSYSTEM; }
20 
21 namespace JSYSTEM {
22 
23 
24  namespace {
25 
27 
28 
29  /**
30  * Auxiliary class to compare endianess of network and system.
31  */
32  class JCompareEndian {
33  public:
34  /**
35  * Default constructor.
36  */
37  JCompareEndian() :
38  endian(ntohs(0x1234) == 0x1234)
39  {}
40 
41 
42  /**
43  * Function object operator.
44  *
45  * \return true if network and system endianess same; else false
46  */
47  bool operator()() const
48  {
49  return endian;
50  }
51 
52 
53  private:
54  const bool endian;
55  };
56  }
57 
58 
59  /**
60  * Function object operator.
61  *
62  * \return true if network and system endianess same; else false
63  */
64  static const JCompareEndian compareEndian;
65 
66 
67  /**
68  * Get host name.
69  *
70  * Note that to obtain a host name including the domain name,
71  * JSYSTEM::getHostname(JSYSTEM::getIPnumber()) should be used.
72  *
73  * \return host name
74  */
75  inline std::string getHostname()
76  {
77  const size_t MAXIMUM_LENGTH = 256;
78  char buffer[MAXIMUM_LENGTH];
79 
80  if (gethostname(buffer, MAXIMUM_LENGTH) == 0)
81  return std::string(buffer);
82  else
83  throw JSystemException("Unknown host name.");
84  }
85 
86 
87 
88  /**
89  * Get host name.
90  *
91  * \param ip IP number
92  * \return host name
93  */
94  inline std::string getHostname(const int ip)
95  {
96  in_addr buffer;
97 
98  buffer.s_addr = ip;
99 
100  hostent* p = gethostbyaddr(&buffer, sizeof(in_addr), AF_INET);
101 
102  if (p != NULL)
103  return std::string(p->h_name);
104  else
105  throw JSystemException("Unknown IP address.");
106  }
107 
108 
109  /**
110  * Get IP number.
111  *
112  * \param host_name host name
113  * \return IP number
114  */
115  inline int getIPnumber(const std::string& host_name)
116  {
117  int ip = -1;
118 
119  std::string buffer = host_name;
120 
121  if (buffer == "" || buffer == "localhost")
122  buffer = getHostname();
123 
124  const hostent* hp = gethostbyname(buffer.c_str());
125 
126  if (hp != NULL)
127  memcpy((char *) &ip, hp->h_addr, sizeof(int));
128 
129  return ip;
130  }
131 
132 
133  /**
134  * Get IP number.
135  *
136  * \return IP number
137  */
138  inline int getIPnumber()
139  {
140  return getIPnumber(getHostname());
141  }
142 
143 
144  /**
145  * Get IP address (decimal-dot notation).
146  *
147  * \param ip IP number
148  * \return IP address
149  */
150  inline std::string getIPaddress(const int ip)
151  {
152  std::ostringstream os;
153 
154  if (compareEndian())
155  os << ((ip >> 24) & 0xFF) << '.'
156  << ((ip >> 16) & 0xFF) << '.'
157  << ((ip >> 8) & 0xFF) << '.'
158  << ((ip >> 0) & 0xFF);
159  else
160  os << ((ip >> 0) & 0xFF) << '.'
161  << ((ip >> 8) & 0xFF) << '.'
162  << ((ip >> 16) & 0xFF) << '.'
163  << ((ip >> 24) & 0xFF);
164 
165  return os.str();
166  }
167 
168 
169  /**
170  * Get IP address (decimal-dot notation).
171  *
172  * \return IP address
173  */
174  inline std::string getIPaddress()
175  {
176  return getIPaddress(getIPnumber());
177  }
178 
179 
180  /**
181  * Get host identifier within network.
182  *
183  * \param ip IP number
184  * \return ID
185  */
186  inline unsigned short getSubaddress(const int ip)
187  {
188  if (ntohs(0x1234) == 0x1234)
189  return (unsigned short) (ip & 0x0000FFFF);
190  else
191  return (unsigned short) (((ip & 0xFF000000) >> 24) |
192  ((ip & 0x00FF0000) >> 8) );
193  }
194 
195 
196  /**
197  * Get host identifier within network.
198  *
199  * \return ID
200  */
201  inline unsigned short getSubaddress()
202  {
203  return getSubaddress(getIPnumber());
204  }
205 }
206 
207 #endif
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
Exceptions.
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:150
unsigned short getSubaddress(const int ip)
Get host identifier within network.
Definition: JNetwork.hh:186
const bool endian
Definition: JNetwork.hh:54
static const JCompareEndian compareEndian
Function object operator.
Definition: JNetwork.hh:64
std::string getHostname()
Get host name.
Definition: JNetwork.hh:75
Exception for system call.
Definition: JException.hh:486