1 #ifndef POSIXX_SOCKET_UNIX_HPP_
2 #define POSIXX_SOCKET_UNIX_HPP_
4 #include "basic_socket.hpp" // posixx::socket
6 #include <sys/un.h> // sockaddr_un
7 #include <string> // std::string
8 #include <utility> // std::pair
9 #include <cstring> // memset, memcpy, memcmp
13 namespace posixx { namespace socket {
15 /// Unix (local) sockets
18 /// Unix socket address
19 struct sockaddr: sockaddr_un
22 /// Create an empty unix socket address.
26 * Create a unix socket address.
28 * @param path Path to the socket (should be less than ::PATH_MAX
31 sockaddr(const std::string& path) throw (std::logic_error);
33 /// Length of this unix socket address
34 socklen_t length() const throw ();
36 /// Get the path of this unix socket address
37 std::string path() const throw ();
39 /// Compare two unix socket addresses
40 bool operator == (const sockaddr& other) const throw ();
44 /// Unix socket traits
47 /// Socket address type.
48 typedef posixx::socket::unix::sockaddr sockaddr;
50 enum { PF = PF_UNIX };
54 typedef posixx::socket::basic_socket< traits > socket;
56 /// Pair of unix sockets
57 typedef std::pair< socket*, socket* > pair_type;
59 /// Create a pair of connected unix sockets
60 pair_type pair(type type, int protocol = 0) throw (posixx::error);
62 } } } // namespace posixx::socket::unix
67 posixx::socket::unix::sockaddr::sockaddr() throw ()
73 posixx::socket::unix::sockaddr::sockaddr(const std::string& path)
74 throw (std::logic_error)
76 if (path.size() >= sizeof(sun_path))
77 throw std::logic_error("path too long");
79 memcpy(sun_path, path.c_str(), path.size() + 1); // \0
83 socklen_t posixx::socket::unix::sockaddr::length() const throw ()
85 return sizeof(sun_family) + strlen(sun_path);
89 std::string posixx::socket::unix::sockaddr::path() const throw ()
91 return std::string(sun_path);
95 bool posixx::socket::unix::sockaddr::operator == (const sockaddr& other)
98 return sun_family == other.sun_family
99 && !strncmp(sun_path, other.sun_path, sizeof(sun_path));
103 posixx::socket::unix::pair_type posixx::socket::unix::pair(type type,
104 int protocol) throw (posixx::error)
106 return posixx::socket::pair< socket >(type, protocol);
109 #endif // POSIXX_SOCKET_UNIX_HPP_