1 // Copyright Leandro Lucarella 2008 - 2010.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file COPYING or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef POSIXX_SOCKET_INET_HPP_
8 #define POSIXX_SOCKET_INET_HPP_
10 #include "basic_socket.hpp" // posixx::socket
12 #include <netinet/in.h> // sockaddr_in, htonl, htons, {PF,AF}_INET, INADDR_ANY
13 #include <arpa/inet.h> // inet_addr
14 #include <cstring> // memset
15 #include <string> // std::string
19 namespace posixx { namespace socket {
21 /// Internet Protocol sockets
25 /// Address to accept any incoming messages
26 const uint32_t any = INADDR_ANY;
28 /// Address to send to all hosts
29 const uint32_t broadcast = INADDR_BROADCAST;
33 struct sockaddr: sockaddr_in
37 * Create an IP socket address.
39 * @note The IP address is expected in network byte order but the
40 * port is expected in host byte order. This is because this
41 * constructor is expected to be used with predefined IP
42 * addresses, like any, none, loopback, etc.
44 * @param addr IP address in network byte order
45 * @param port IP port in host byte order
47 * @see any, none, etc. constants
49 explicit sockaddr(uint32_t addr = any, uint16_t port = 0) throw ();
52 * Create an IP socket address.
54 * @param addr String representation of the IP address
55 * @param port IP port in host byte order
57 explicit sockaddr(const std::string& addr, uint16_t port) throw ();
60 * Set the IP address and port.
62 * @param addr IP address in network byte order
63 * @param port IP port in host byte order
65 * @see sockaddr(uint32_t, uint16_t)
67 void set(uint32_t addr, uint16_t port) throw();
70 * Set the IP address and port.
72 * @param addr String representation of the IP address
73 * @param port IP port in host byte order
75 * @see sockaddr(const std::string&, uint16_t)
77 void set(const std::string& addr, uint16_t port) throw();
79 /// Get the IP address as a string
80 std::string addr() const throw ();
82 /// Set the IP address (in network byte order)
83 void addr(uint32_t addr) throw ();
85 /// Set the IP address from a string
86 void addr(const std::string& addr) throw ();
88 /// Get the port number
89 uint16_t port() const throw ();
91 /// Set the port number (port is expected to be in host byte order)
92 void port(uint16_t port) throw ();
94 /// Length of this IP socket address
95 socklen_t length() const throw ();
97 /// Compare two IP socket addresses
98 bool operator == (const sockaddr& other) const throw ();
100 }; // struct sockaddr
105 /// Socket address type.
106 typedef inet::sockaddr sockaddr;
108 enum { PF = PF_INET };
112 typedef posixx::socket::basic_socket< traits > socket;
114 } } } // namespace posixx::socket::inet
120 posixx::socket::inet::sockaddr::sockaddr(uint32_t addr, uint16_t port)
123 memset(this, 0, sizeof(struct sockaddr_in));
124 sin_family = AF_INET;
129 posixx::socket::inet::sockaddr::sockaddr(const std::string& addr, uint16_t port)
132 memset(this, 0, sizeof(struct sockaddr_in));
133 sin_family = AF_INET;
138 void posixx::socket::inet::sockaddr::set(uint32_t addr, uint16_t port)
141 sin_addr.s_addr = addr;
146 void posixx::socket::inet::sockaddr::set(const std::string& addr, uint16_t port)
154 std::string posixx::socket::inet::sockaddr::addr() const throw ()
156 return inet_ntoa(sin_addr);
160 void posixx::socket::inet::sockaddr::addr(uint32_t addr) throw ()
162 sin_addr.s_addr = addr;
166 void posixx::socket::inet::sockaddr::addr(const std::string& addr) throw ()
168 sin_addr.s_addr = inet_addr(addr.c_str());
172 uint16_t posixx::socket::inet::sockaddr::port() const throw ()
174 return ntohs(sin_port);
178 void posixx::socket::inet::sockaddr::port(uint16_t port) throw ()
180 sin_port = htons(port);
184 socklen_t posixx::socket::inet::sockaddr::length() const throw ()
186 return sizeof(sockaddr_in);
190 bool posixx::socket::inet::sockaddr::operator == (const sockaddr& other) const
193 return !memcmp(this, &other, sizeof(*this));
196 #endif // POSIXX_SOCKET_INET_HPP_