libcommonc++  0.7
SocketAddress.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_SocketAddress_hxx
24 #define __ccxx_SocketAddress_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/ByteOrder.h++>
30 #include <commonc++/String.h++>
31 
32 #ifdef CCXX_OS_POSIX
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #endif
37 
38 namespace ccxx {
39 
47 {
48  public:
49 
51  SocketAddress();
52 
59  SocketAddress(const InetAddress& address, uint16_t port);
60 
62  explicit SocketAddress(const struct sockaddr& other);
63 
65  explicit SocketAddress(const struct sockaddr_in& other);
66 
68  SocketAddress(const SocketAddress& other);
69 
71  ~SocketAddress();
72 
74  SocketAddress& operator=(const struct sockaddr& other);
75 
77  SocketAddress& operator=(const struct sockaddr_in& other);
78 
80  SocketAddress& operator=(const SocketAddress& other);
81 
83  InetAddress getAddress() const;
84 
86  void setAddress(const InetAddress& address);
87 
94  void setHost(const String& host);
95 
97  inline uint16_t getPort() const
98  { return(ByteOrder::networkToHost(_insaddr.sin_port)); }
99 
101  inline void setPort(uint16_t port)
102  { _insaddr.sin_port = ByteOrder::hostToNetwork(port); }
103 
105  inline void setSocketAddress(const struct sockaddr& addr)
106  { _saddr = addr; }
107 
109  inline sockaddr getSocketAddress() const
110  { return(_saddr); }
111 
113  void reset();
114 
116  inline operator sockaddr*()
117  { return(&_saddr); }
118 
120  inline operator sockaddr_in*()
121  { return(&_insaddr); }
122 
124  String toString() const;
125 
127  bool operator==(const SocketAddress& other) const;
128 
130  inline bool operator!=(const SocketAddress& other) const
131  { return(! operator==(other)); }
132 
133  private:
134 
135  union
136  {
137  struct sockaddr _saddr;
138  struct sockaddr_in _insaddr;
139  };
140 };
141 
142 inline std::ostream& operator<<(std::ostream& stream, const SocketAddress& a)
143 {
144  CString cstr_a = a.toString().toUTF8();
145  return(stream << cstr_a.data());
146 }
147 
148 } // namespace ccxx
149 
150 #endif // __ccxx_SocketAddress_hxx
static uint16_t networkToHost(uint16_t n)
Convert an unsigned 16-bit integer from network to host byte-order.
Definition: ByteOrder.h++:55
const char * data() const
Get a pointer to the character array.
Definition: CString.h++:58
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
uint16_t getPort() const
Get the port number for this address.
Definition: SocketAddress.h++:97
sockaddr getSocketAddress() const
Get the address as a sockaddr structure.
Definition: SocketAddress.h++:109
A socket endpoint network address.
Definition: SocketAddress.h++:46
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
static uint16_t hostToNetwork(uint16_t n)
Convert an unsigned 16-bit integer from host to network byte-order.
Definition: ByteOrder.h++:44
bool operator!=(const SocketAddress &other) const
Inequality operator.
Definition: SocketAddress.h++:130
#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
void setSocketAddress(const struct sockaddr &addr)
Set the address from a sockaddr structure.
Definition: SocketAddress.h++:105
CString toUTF8() const
Return the contents of the string as an immutable C string.
Definition: String.c++:1143
An IPv4 Internet address.
Definition: InetAddress.h++:64
Definition: AllocationMap.c++:25
String toString() const
Get a String representation for this address.
Definition: SocketAddress.c++:161
void setPort(uint16_t port)
Set the port number for this address.
Definition: SocketAddress.h++:101