nidas  v1.2-1520
Inet4Address.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_UTIL_INET4ADDRESS
28 #define NIDAS_UTIL_INET4ADDRESS
29 
30 #include "UnknownHostException.h"
31 #include "ThreadSupport.h"
32 
33 #include <arpa/inet.h>
34 
35 #include <string>
36 #include <list>
37 #include <map>
38 
39 namespace nidas { namespace util {
40 
46 class Inet4Address {
47 public:
55  static std::list<Inet4Address> getAllByName(const std::string& hostname)
56  throw(UnknownHostException);
57 
61  static Inet4Address getByName(const std::string& hostname)
62  throw(UnknownHostException);
63 
70  static std::string getHostName(const Inet4Address& addr) throw();
71 
75  Inet4Address();
76 
80  Inet4Address(const struct in_addr*);
81 
85  Inet4Address(unsigned int addr);
86 
90  std::string getHostAddress() const;
91 
96  std::string getHostName() const throw();
97 
101  const struct in_addr* getInAddrPtr() const { return &_inaddr; }
102 
110  const struct in_addr getInAddr() const { return _inaddr; }
111 
115  bool operator < (const Inet4Address& x) const {
116  return ntohl(_inaddr.s_addr) < ntohl(x._inaddr.s_addr);
117  }
118 
122  bool operator == (const Inet4Address& x) const {
123  return _inaddr.s_addr == x._inaddr.s_addr;
124  }
125 
129  bool operator != (const Inet4Address& x) const {
130  return _inaddr.s_addr != x._inaddr.s_addr;
131  }
132 
138  bool isMultiCastAddress() const {
139  return (ntohl(_inaddr.s_addr) & 0xf0000000L) == 0xe0000000L;
140  }
141 
145  int bitsMatch(const Inet4Address& x) const throw();
146 
147 protected:
148 
152  struct in_addr _inaddr;
153 
154 };
155 
156 }} // namespace nidas namespace util
157 
158 #endif
bool operator<(const Inet4Address &x) const
Comparator operator for addresses.
Definition: Inet4Address.h:115
bool isMultiCastAddress() const
Is this address a multicast address? Multicast addresses are in the range 224.0.0.1 to 239.255.255.255, their first four bits are 1110=0xe.
Definition: Inet4Address.h:138
bool operator==(const Inet4Address &x) const
Equality operator for addresses.
Definition: Inet4Address.h:122
bool operator!=(const Inet4Address &x) const
Inequality operator for addresses.
Definition: Inet4Address.h:129
static Inet4Address getByName(const std::string &hostname)
Return an address of a name, the first one found by getAllByName.
Definition: Inet4Address.cc:176
static std::list< Inet4Address > getAllByName(const std::string &hostname)
Static method returning a list of addresses for a name.
Definition: Inet4Address.cc:133
std::string getHostName() const
Return a hostname for this address.
Definition: Inet4Address.cc:62
int bitsMatch(const Inet4Address &x) const
How many leading bits match in the two addresses?
Definition: Inet4Address.cc:189
std::string getHostAddress() const
Return string containing address in dot notation: w.x.y.z.
Definition: Inet4Address.cc:55
Inet4Address()
Default constructor.
Definition: Inet4Address.cc:41
struct in_addr * getInAddrPtr() const
Return const pointer to struct in_addr.
Definition: Inet4Address.h:101
Definition: UnknownHostException.h:35
Support for IP version 4 host address.
Definition: Inet4Address.h:46
struct in_addr getInAddr() const
Get the structure containing the 4 byte internet version 4 address.
Definition: Inet4Address.h:110
struct in_addr _inaddr
The IP address, in network byte order.
Definition: Inet4Address.h:152