1//
2// local/detail/impl/endpoint.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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_IMPL_ENDPOINT_IPP
13#define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
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 <cstring>
24#include <boost/asio/detail/socket_ops.hpp>
25#include <boost/asio/detail/throw_error.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/local/detail/endpoint.hpp>
28
29#include <boost/asio/detail/push_options.hpp>
30
31namespace boost {
32namespace asio {
33namespace local {
34namespace detail {
35
36endpoint::endpoint()
37{
38 init(path: "", path_length: 0);
39}
40
41endpoint::endpoint(const char* path_name)
42{
43 using namespace std; // For strlen.
44 init(path: path_name, path_length: strlen(s: path_name));
45}
46
47endpoint::endpoint(const std::string& path_name)
48{
49 init(path: path_name.data(), path_length: path_name.length());
50}
51
52#if defined(BOOST_ASIO_HAS_STRING_VIEW)
53endpoint::endpoint(string_view path_name)
54{
55 init(path: path_name.data(), path_length: path_name.length());
56}
57#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
58
59void endpoint::resize(std::size_t new_size)
60{
61 if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
62 {
63 boost::system::error_code ec(boost::asio::error::invalid_argument);
64 boost::asio::detail::throw_error(err: ec);
65 }
66 else if (new_size == 0)
67 {
68 path_length_ = 0;
69 }
70 else
71 {
72 path_length_ = new_size
73 - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
74
75 // The path returned by the operating system may be NUL-terminated.
76 if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
77 --path_length_;
78 }
79}
80
81std::string endpoint::path() const
82{
83 return std::string(data_.local.sun_path, path_length_);
84}
85
86void endpoint::path(const char* p)
87{
88 using namespace std; // For strlen.
89 init(path: p, path_length: strlen(s: p));
90}
91
92void endpoint::path(const std::string& p)
93{
94 init(path: p.data(), path_length: p.length());
95}
96
97bool operator==(const endpoint& e1, const endpoint& e2)
98{
99 return e1.path() == e2.path();
100}
101
102bool operator<(const endpoint& e1, const endpoint& e2)
103{
104 return e1.path() < e2.path();
105}
106
107void endpoint::init(const char* path_name, std::size_t path_length)
108{
109 if (path_length > sizeof(data_.local.sun_path) - 1)
110 {
111 // The buffer is not large enough to store this address.
112 boost::system::error_code ec(boost::asio::error::name_too_long);
113 boost::asio::detail::throw_error(err: ec);
114 }
115
116 using namespace std; // For memset and memcpy.
117 memset(s: &data_.local, c: 0, n: sizeof(boost::asio::detail::sockaddr_un_type));
118 data_.local.sun_family = AF_UNIX;
119 if (path_length > 0)
120 memcpy(dest: data_.local.sun_path, src: path_name, n: path_length);
121 path_length_ = path_length;
122}
123
124} // namespace detail
125} // namespace local
126} // namespace asio
127} // namespace boost
128
129#include <boost/asio/detail/pop_options.hpp>
130
131#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
132
133#endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
134

source code of boost/libs/asio/include/boost/asio/local/detail/impl/endpoint.ipp