1 #ifndef POSIXX_SOCKET_INET_HPP_
2 #define POSIXX_SOCKET_INET_HPP_
4 #include "basic_socket.hpp" // posixx::socket
6 #include <netinet/in.h> // sockaddr_in, htonl, htons, {PF,AF}_INET, INADDR_ANY
7 #include <arpa/inet.h> // inet_addr
8 #include <cstring> // memset
9 #include <string> // std::string
13 namespace posixx { namespace socket {
15 /// Internet Protocol sockets
19 /// Address to accept any incoming messages
20 const uint32_t any = INADDR_ANY;
22 /// Address to send to all hosts
23 const uint32_t broadcast = INADDR_BROADCAST;
27 struct sockaddr: sockaddr_in
31 * Create an IP socket address.
33 * @note The IP address is expected in network byte order but the
34 * port is expected in host byte order. This is because this
35 * constructor is expected to be used with predefined IP
36 * addresses, like any, none, loopback, etc.
38 * @param addr IP address in network byte order
39 * @param port IP port in host byte order
41 * @see any, none, etc. constants
43 explicit sockaddr(uint32_t addr = any, uint16_t port = 0) throw ();
46 * Create an IP socket address.
48 * @param addr String representation of the IP address
49 * @param port IP port in host byte order
51 explicit sockaddr(const std::string& addr, uint16_t port) throw ();
54 * Set the IP address and port.
56 * @param addr IP address in network byte order
57 * @param port IP port in host byte order
59 * @see sockaddr(uint32_t, uint16_t)
61 void set(uint32_t addr, uint16_t port) throw();
64 * Set the IP address and port.
66 * @param addr String representation of the IP address
67 * @param port IP port in host byte order
69 * @see sockaddr(const std::string&, uint16_t)
71 void set(const std::string& addr, uint16_t port) throw();
73 /// Get the IP address as a string
74 std::string addr() const throw ();
76 /// Set the IP address (in network byte order)
77 void addr(uint32_t addr) throw ();
79 /// Set the IP address from a string
80 void addr(const std::string& addr) throw ();
82 /// Get the port number
83 uint16_t port() const throw ();
85 /// Set the port number (port is expected to be in host byte order)
86 void port(uint16_t port) throw ();
88 /// Length of this IP socket address
89 socklen_t length() const throw ();
91 /// Compare two IP socket addresses
92 bool operator == (const sockaddr& other) const throw ();
99 /// Socket address type.
100 typedef inet::sockaddr sockaddr;
102 enum { PF = PF_INET };
106 typedef posixx::socket::basic_socket< traits > socket;
108 } } } // namespace posixx::socket::inet
114 posixx::socket::inet::sockaddr::sockaddr(uint32_t addr, uint16_t port)
117 memset(this, 0, sizeof(struct sockaddr_in));
118 sin_family = AF_INET;
123 posixx::socket::inet::sockaddr::sockaddr(const std::string& addr, uint16_t port)
126 memset(this, 0, sizeof(struct sockaddr_in));
127 sin_family = AF_INET;
132 void posixx::socket::inet::sockaddr::set(uint32_t addr, uint16_t port)
135 sin_addr.s_addr = addr;
140 void posixx::socket::inet::sockaddr::set(const std::string& addr, uint16_t port)
148 std::string posixx::socket::inet::sockaddr::addr() const throw ()
150 return inet_ntoa(sin_addr);
154 void posixx::socket::inet::sockaddr::addr(uint32_t addr) throw ()
156 sin_addr.s_addr = addr;
160 void posixx::socket::inet::sockaddr::addr(const std::string& addr) throw ()
162 sin_addr.s_addr = inet_addr(addr.c_str());
166 uint16_t posixx::socket::inet::sockaddr::port() const throw ()
168 return ntohs(sin_port);
172 void posixx::socket::inet::sockaddr::port(uint16_t port) throw ()
174 sin_port = htons(port);
178 socklen_t posixx::socket::inet::sockaddr::length() const throw ()
180 return sizeof(sockaddr_in);
184 bool posixx::socket::inet::sockaddr::operator == (const sockaddr& other) const
187 return !memcmp(this, &other, sizeof(*this));
190 #endif // POSIXX_SOCKET_INET_HPP_