Flow-IPC 1.0.2
Flow-IPC project: Full implementation reference.
asio_local_stream_socket.cpp
Go to the documentation of this file.
1/* Flow-IPC: Core
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
21#include <flow/error/error.hpp>
22
24{
25
26// Implementations.
27
28Endpoint endpoint_at_shared_name(flow::log::Logger* logger_ptr,
29 const Shared_name& absolute_name, Error_code* err_code)
30{
31 namespace bind_ns = flow::util::bind_ns;
32 using boost::system::system_error;
33 using std::string;
34
35 FLOW_ERROR_EXEC_FUNC_AND_THROW_ON_ERROR(Endpoint, endpoint_at_shared_name,
36 logger_ptr, bind_ns::cref(absolute_name), _1);
37 // ^-- Call ourselves and return if err_code is null. If got to present line, err_code is not null.
38
39 FLOW_LOG_SET_CONTEXT(logger_ptr, Log_component::S_TRANSPORT);
40
41 /* Make the name "string" as required to construct the endpoint. This is more subtle than one might expect.
42 * POSIX-y Unix domain sockets are either anonymous (not OK for us), or the endpoints live as files in the
43 * file system. Linux adds the delightful extension, "abstract namespace," wherein instead the endpoints are
44 * named in a cosmically present namespace unconnected to the file system. This avoids the file system issues,
45 * such as permissions and directories and ..., so we want to use that. How to specify it, though?
46 * Well, more or less, at the native level, the sockaddr_un member sun_path (a `char` array) is used either way;
47 * to specify an abstract name one sets the first byte to NUL (which wouldn't be a valid value otherwise); then
48 * the rest of sun_path[] (up to the specified size of the sockaddr_un) is taken as the abstract-namespace name.
49 * Fine... but we use boost.asio; how to get its `endpoint` to behave that way? Bad news is it's not explicitly
50 * documented all the way. Good news is it's quite doable. Firstly, endpoint::data() lets one manually set
51 * the whole sockaddr_un structure ourselves -- that part being no prettier than coding this stuff sans boost.asio
52 * but overall still nicer than all-native code. However, turns out we need not do that either (though this is
53 * the part that isn't explicitly documented but clearly intended if one checks the Boost header code).
54 * Namely: `endpoint` ctor, and the parallel counterpart endpoint::path() mutator overloads, have 2 forms:
55 * one taking `const char*`; and one taking `string[_view]`. `const char*` is clearly insufficient (proof left
56 * as exercise to comment reader). Fascinatingly, string[_view] *is* sufficient: A string[_view] can perfectly
57 * safely store multiple NUL characters; and indeed one can see in the Boost header code that it *specifically*
58 * supports the weird NUL-led abstract-namespace use case (again, I leave details for you to verify). */
59
60 string abstract_namespace_name(size_t(1), '\0'); // Start with NUL!
61 abstract_namespace_name += absolute_name.str(); // After the NUL append the real deal!
62 FLOW_LOG_TRACE("Abstract-namespace (Linux extension) name above consists of 1 NUL + the name "
63 "[" << absolute_name << "]; total of [" << abstract_namespace_name.size() << "] bytes.");
64
65 Endpoint endpoint;
66 auto& sys_err_code = *err_code;
67 try
68 {
69 /* Throws on error. (It's annoying there's no error-code-returning API; a bit odd actually.)
70 * In practice it'll throw only on too-long name (as of this writing; Boost 1.76), but we don't rely on this. */
71 endpoint.path(abstract_namespace_name);
72 sys_err_code.clear(); // If it didn't throw.
73 }
74 catch (const system_error& exc)
75 {
76 FLOW_LOG_WARNING("Unable to set up native local stream endpoint structure; "
77 "could be due to name length; details logged below.");
78 sys_err_code = exc.code();
79 FLOW_ERROR_SYS_ERROR_LOG_WARNING();
80 }
81
82 return endpoint; // sys_err_code is set; `endpoint` might be empty.
83} // endpoint_at_shared_name()
84
85} // namespace ipc::transport::asio_local_stream_socket
String-wrapping abstraction representing a name uniquely distinguishing a kernel-persistent entity fr...
const std::string & str() const
Returns (sans copying) ref to immutable entire wrapped name string, suitable to pass into sys calls w...
size_t size() const
Returns str().size().
Additional (versus boost.asio) APIs for advanced work with local stream (Unix domain) sockets includi...
Protocol::endpoint Endpoint
Short-hand for boost.asio Unix domain peer stream-socket endpoint.
Endpoint endpoint_at_shared_name(flow::log::Logger *logger_ptr, const Shared_name &absolute_name, Error_code *err_code)
Returns an Endpoint corresponding to the given absolute Shared_name, so that an Acceptor or Peer_sock...
flow::Error_code Error_code
Short-hand for flow::Error_code which is very common.
Definition: common.hpp:298