Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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 <sys/types.h>
7#include <ifaddrs.h>
8#include <string.h>
9#include <string>
10#include <sstream>
11#include <vector>
12
13#include "JLang/JException.hh"
14
15/**
16 * \file
17 * Hostname and IP address functions.
18 * \author mdejong
19 */
20namespace JSYSTEM {}
21namespace JPP { using namespace JSYSTEM; }
22
23namespace JSYSTEM {
24
25
26 namespace {
27
29
30
31 /**
32 * Auxiliary class to compare endianess of network and system.
33 */
34 class JCompareEndian {
35 public:
36 /**
37 * Default constructor.
38 */
39 JCompareEndian() :
40 endian(ntohs(0x1234) == 0x1234)
41 {}
42
43
44 /**
45 * Function object operator.
46 *
47 * \return true if network and system endianess same; else false
48 */
49 bool operator()() const
50 {
51 return endian;
52 }
53
54
55 private:
56 const bool endian;
57 };
58 }
59
60
61 /**
62 * Function object operator.
63 *
64 * \return true if network and system endianess same; else false
65 */
66 static const JCompareEndian compareEndian;
67
68
69 /**
70 * Get host name.
71 *
72 * Note that to obtain a host name including the domain name,
73 * JSYSTEM::getHostname(JSYSTEM::getIPnumber()) should be used.
74 *
75 * \return host name
76 */
77 inline std::string getHostname()
78 {
79 const size_t MAXIMUM_LENGTH = 256;
80 char buffer[MAXIMUM_LENGTH];
81
82 if (gethostname(buffer, MAXIMUM_LENGTH) == 0)
83 return std::string(buffer);
84 else
85 THROW(JSystemException, "Unknown host name.");
86 }
87
88
89
90 /**
91 * Get host name.
92 *
93 * \param ip IP number
94 * \return host name
95 */
96 inline std::string getHostname(const int ip)
97 {
98 in_addr buffer;
99
100 buffer.s_addr = ip;
101
102 hostent* p = gethostbyaddr(&buffer, sizeof(in_addr), AF_INET);
103
104 if (p != NULL)
105 return std::string(p->h_name);
106 else
107 THROW(JSystemException, "Unknown IP address " << ip);
108 }
109
110
111 /**
112 * Get IP number.
113 *
114 * \param host_name host name
115 * \return IP number
116 */
117 inline int getIPnumber(const std::string& host_name)
118 {
119 int ip = -1;
120
121 std::string buffer = host_name;
122
123 if (buffer == "" || buffer == "localhost") {
124 buffer = getHostname();
125 }
126
127 const hostent* hp = gethostbyname(buffer.c_str());
128
129 if (hp != NULL) {
130 memcpy((char *) &ip, hp->h_addr, sizeof(int));
131 }
132
133 return ip;
134 }
135
136
137 /**
138 * Get IP number.
139 *
140 * \return IP number
141 */
142 inline int getIPnumber()
143 {
144 return getIPnumber(getHostname());
145 }
146
147
148 /**
149 * Get IP address (decimal-dot notation).
150 *
151 * \param ip IP number
152 * \return IP address
153 */
154 inline std::string getIPaddress(const int ip)
155 {
156 std::ostringstream os;
157
158 if (compareEndian())
159 os << ((ip >> 24) & 0xFF) << '.'
160 << ((ip >> 16) & 0xFF) << '.'
161 << ((ip >> 8) & 0xFF) << '.'
162 << ((ip >> 0) & 0xFF);
163 else
164 os << ((ip >> 0) & 0xFF) << '.'
165 << ((ip >> 8) & 0xFF) << '.'
166 << ((ip >> 16) & 0xFF) << '.'
167 << ((ip >> 24) & 0xFF);
168
169 return os.str();
170 }
171
172
173 /**
174 * Get IP address (decimal-dot notation).
175 *
176 * \return IP address
177 */
178 inline std::string getIPaddress()
179 {
180 return getIPaddress(getIPnumber());
181 }
182
183
184 /**
185 * Get host identifier within network.
186 *
187 * \param ip IP number
188 * \return ID
189 */
190 inline unsigned short getSubaddress(const int ip)
191 {
192 if (compareEndian())
193 return (unsigned short) (ip & 0x0000FFFF);
194 else
195 return (unsigned short) (((ip & 0xFF000000) >> 24) |
196 ((ip & 0x00FF0000) >> 8) );
197 }
198
199
200 /**
201 * Get host identifier within network.
202 *
203 * \return ID
204 */
205 inline unsigned short getSubaddress()
206 {
207 return getSubaddress(getIPnumber());
208 }
209
210
211 /**
212 * Get list of IP address (decimal-dot notation).
213 *
214 * \return IP address
215 */
217 {
218 using namespace std;
219
220 const size_t MAXIMUM_LENGTH = 256;
221 char buffer[MAXIMUM_LENGTH];
222
223 vector<string> result;
224
225 struct ifaddrs *p = NULL;
226
227 if (getifaddrs(&p) == 0) {
228
229 for ( ; p != NULL; p = p->ifa_next) {
230
231 if (p->ifa_addr != NULL) {
232
233 if (p->ifa_addr->sa_family == AF_INET ||
234 p->ifa_addr->sa_family == AF_INET6) {
235
236 if (getnameinfo( p->ifa_addr,
237 (p->ifa_addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
238 buffer, MAXIMUM_LENGTH,
239 NULL, 0, NI_NUMERICHOST) == 0) {
240
241 result.push_back(buffer);
242 }
243 }
244 }
245 }
246
247 freeifaddrs(p);
248 }
249
250 return result;
251 }
252}
253
254#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
const bool endian
Definition JNetwork.hh:56
Exception for system call.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
static const JCompareEndian compareEndian
Function object operator.
Definition JNetwork.hh:66
int getIPnumber()
Get IP number.
Definition JNetwork.hh:142
std::vector< std::string > getListOfIPaddresses()
Get list of IP address (decimal-dot notation).
Definition JNetwork.hh:216
std::string getIPaddress()
Get IP address (decimal-dot notation).
Definition JNetwork.hh:178
unsigned short getSubaddress()
Get host identifier within network.
Definition JNetwork.hh:205
std::string getHostname()
Get host name.
Definition JNetwork.hh:77