libcommonc++  0.7
InetAddress.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_InetAddress_hxx
24 #define __ccxx_InetAddress_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/ByteOrder.h++>
29 #include <commonc++/String.h++>
30 
31 #ifdef CCXX_OS_POSIX
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #endif
35 
36 namespace ccxx {
37 
45 {
46  public:
47 
53 
54  inline virtual void write(std::ostream& stream) const
55  { _write(stream, "HostNotFoundException"); }
56 
57 };
58 
65 {
66  public:
67 
69  InetAddress();
70 
79  explicit InetAddress(const String& host);
80 
87  explicit InetAddress(uint32_t addr);
88 
94  explicit InetAddress(byte_t addr[4]);
95 
97  ~InetAddress();
98 
105  void setHost(const String& host);
106 
112  void setAddress(uint32_t addr);
113 
119  inline uint32_t getAddress() const
120  { return(_addr); }
121 
128  char getNetworkClass() const;
129 
134  bool isMulticast() const;
135 
140  bool isNonroutable() const;
141 
143  bool isLinkLocal() const;
144 
152  String getHost() const;
153 
160  void resolve();
161 
163  inline bool isResolved() const
164  { return(_resolved); }
165 
167  String toString() const;
168 
170  String toIPString() const;
171 
173  bool operator==(const InetAddress& other) const;
174 
176  inline bool operator!=(const InetAddress& other) const
177  { return(!operator==(other)); }
178 
180  InetAddress& operator|=(const InetAddress& other);
181 
183  InetAddress& operator&=(const InetAddress& other);
184 
186  static const InetAddress ANY;
187 
188  private:
189 
190  bool _fromDotSeparated();
191 
192  uint32_t _addr;
193  mutable String _host;
194  mutable bool _resolved;
195 };
196 
197 inline std::ostream& operator<<(std::ostream& stream, const InetAddress& a)
198 {
199  CString cstr_a = a.toString().toUTF8();
200  return(stream << cstr_a.data());
201 }
202 
203 } // namespace ccxx
204 
205 #endif // __ccxx_InetAddress_hxx
uint32_t getAddress() const
Get the host IP address for this address.
Definition: InetAddress.h++:119
bool operator!=(const InetAddress &other) const
Inequality operator.
Definition: InetAddress.h++:176
String toString() const
Get aString representation for this address.
Definition: InetAddress.c++:213
static const String empty
The empty string.
Definition: String.h++:1134
const char * data() const
Get a pointer to the character array.
Definition: CString.h++:58
static const InetAddress ANY
A value representing the wildcard address, 0.0.0.0.
Definition: InetAddress.h++:186
An exception indicating an I/O error.
Definition: IOException.h++:36
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
An exception indicating a hostname resolution failure or malformed IP address string.
Definition: InetAddress.h++:44
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
An implicitly shared, reference-counted container for an immutable, NUL-terminated C string...
Definition: CString.h++:39
CString toUTF8() const
Return the contents of the string as an immutable C string.
Definition: String.c++:1143
bool isResolved() const
Test if this InetAddress has been successfully resolved.
Definition: InetAddress.h++:163
virtual void write(std::ostream &stream) const
Write a textual representation of the exception to a stream.
Definition: InetAddress.h++:54
An IPv4 Internet address.
Definition: InetAddress.h++:64
Definition: AllocationMap.c++:25
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68