1 //
2 // local/detail/endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Derived from a public domain implementation written by Daniel Casimiro.
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 
12 #ifndef BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP
13 #define BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP
14 
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 
19 #include <boost/asio/detail/config.hpp>
20 
21 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
22 
23 #include <cstddef>
24 #include <string>
25 #include <boost/asio/detail/socket_types.hpp>
26 #include <boost/asio/detail/string_view.hpp>
27 
28 #include <boost/asio/detail/push_options.hpp>
29 
30 namespace boost {
31 namespace asio {
32 namespace local {
33 namespace detail {
34 
35 // Helper class for implementing a UNIX domain endpoint.
36 class endpoint
37 {
38 public:
39   // Default constructor.
40   BOOST_ASIO_DECL endpoint();
41 
42   // Construct an endpoint using the specified path name.
43   BOOST_ASIO_DECL endpoint(const char* path_name);
44 
45   // Construct an endpoint using the specified path name.
46   BOOST_ASIO_DECL endpoint(const std::string& path_name);
47 
48   #if defined(BOOST_ASIO_HAS_STRING_VIEW)
49   // Construct an endpoint using the specified path name.
50   BOOST_ASIO_DECL endpoint(string_view path_name);
51   #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
52 
53   // Copy constructor.
endpoint(const endpoint & other)54   endpoint(const endpoint& other)
55     : data_(other.data_),
56       path_length_(other.path_length_)
57   {
58   }
59 
60   // Assign from another endpoint.
operator =(const endpoint & other)61   endpoint& operator=(const endpoint& other)
62   {
63     data_ = other.data_;
64     path_length_ = other.path_length_;
65     return *this;
66   }
67 
68   // Get the underlying endpoint in the native type.
data()69   boost::asio::detail::socket_addr_type* data()
70   {
71     return &data_.base;
72   }
73 
74   // Get the underlying endpoint in the native type.
data() const75   const boost::asio::detail::socket_addr_type* data() const
76   {
77     return &data_.base;
78   }
79 
80   // Get the underlying size of the endpoint in the native type.
size() const81   std::size_t size() const
82   {
83     return path_length_
84       + offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
85   }
86 
87   // Set the underlying size of the endpoint in the native type.
88   BOOST_ASIO_DECL void resize(std::size_t size);
89 
90   // Get the capacity of the endpoint in the native type.
capacity() const91   std::size_t capacity() const
92   {
93     return sizeof(boost::asio::detail::sockaddr_un_type);
94   }
95 
96   // Get the path associated with the endpoint.
97   BOOST_ASIO_DECL std::string path() const;
98 
99   // Set the path associated with the endpoint.
100   BOOST_ASIO_DECL void path(const char* p);
101 
102   // Set the path associated with the endpoint.
103   BOOST_ASIO_DECL void path(const std::string& p);
104 
105   // Compare two endpoints for equality.
106   BOOST_ASIO_DECL friend bool operator==(
107       const endpoint& e1, const endpoint& e2);
108 
109   // Compare endpoints for ordering.
110   BOOST_ASIO_DECL friend bool operator<(
111       const endpoint& e1, const endpoint& e2);
112 
113 private:
114   // The underlying UNIX socket address.
115   union data_union
116   {
117     boost::asio::detail::socket_addr_type base;
118     boost::asio::detail::sockaddr_un_type local;
119   } data_;
120 
121   // The length of the path associated with the endpoint.
122   std::size_t path_length_;
123 
124   // Initialise with a specified path.
125   BOOST_ASIO_DECL void init(const char* path, std::size_t path_length);
126 };
127 
128 } // namespace detail
129 } // namespace local
130 } // namespace asio
131 } // namespace boost
132 
133 #include <boost/asio/detail/pop_options.hpp>
134 
135 #if defined(BOOST_ASIO_HEADER_ONLY)
136 # include <boost/asio/local/detail/impl/endpoint.ipp>
137 #endif // defined(BOOST_ASIO_HEADER_ONLY)
138 
139 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
140 
141 #endif // BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP
142