libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
posix_connector.cpp
Go to the documentation of this file.
1/**
2 * @file posix_connector.cpp
3 * @brief POSIX socket connector implementation
4 *
5 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#include "posix_connector.hpp"
10#include "libe3/logger.hpp"
11#include <sys/socket.h>
12#include <sys/stat.h>
13#include <sys/time.h>
14#include <sys/un.h>
15#include <netdb.h>
16#include <netinet/in.h>
17#include <netinet/tcp.h>
18#ifdef LIBE3_ENABLE_SCTP
19#include <netinet/sctp.h>
20#endif
21#include <arpa/inet.h>
22#include <fcntl.h>
23#include <poll.h>
24#include <unistd.h>
25#include <algorithm>
26#include <array>
27#include <cerrno>
28#include <chrono>
29#include <cstring>
30#include <thread>
31
32namespace libe3 {
33
34namespace {
35constexpr const char* LOG_TAG = "PosixConn";
36constexpr const char* IPC_BASE_DIR = "/tmp/dapps";
37// What E3Config holds for setup_endpoint unless the program sets it. The ZeroMQ link
38// shares the string, so the POSIX link maps it to its own default instead of changing it.
39constexpr const char* SHARED_DEFAULT_ENDPOINT = "ipc:///tmp/dapps/setup";
40constexpr const char* DEFAULT_IPC_PATH = "/tmp/dapps/e3";
41constexpr int POLL_TIMEOUT_MS = 500; // How long one read waits, so shutdown is noticed
42constexpr int SEND_TIMEOUT_MS = 500; // Per-peer send timeout (drop a stalled peer)
43constexpr int CONNECT_ATTEMPTS = 10; // The dApp waits for a RAN that is not up yet
44constexpr int CONNECT_PAUSE_MS = 200;
45constexpr int LISTEN_BACKLOG = 64;
46constexpr size_t MAX_PDU = MAX_PROTOCOL_DATA_SIZE * 2; // One limit for every transport and direction
47constexpr uint32_t SCTP_PPID = 0x00E3A001; // Project-private; no payload protocol id is registered for E3AP
48constexpr int SCTP_PROCEDURE_STREAM = 0;
49constexpr int SCTP_INDICATION_STREAM = 1;
50constexpr int64_t SETUP_PUMP_AFTER_NS = 1'000'000'000; // The setup thread reads if receive() has been idle this long
51
52int64_t now_ns() {
53 return std::chrono::duration_cast<std::chrono::nanoseconds>(
54 std::chrono::steady_clock::now().time_since_epoch()).count();
55}
56
57/** @brief Wait for a socket to become readable: 1 if ready, 0 on timeout, -1 on error. */
58int wait_readable(int fd, int timeout_ms) {
59 struct pollfd pfd{fd, POLLIN, 0};
60 return poll(&pfd, 1, timeout_ms);
61}
62
63/**
64 * @brief Strip a URI scheme ("ipc://", "tcp://") from an endpoint, leaving a path.
65 */
66std::string strip_scheme(const std::string& endpoint) {
67 auto pos = endpoint.find("://");
68 return pos == std::string::npos ? endpoint : endpoint.substr(pos + 3);
69}
70
71/**
72 * @brief Create a socket of the SCTP protocol.
73 *
74 * SCTP is optional at build time (LIBE3_ENABLE_SCTP, off by default). The enumerator
75 * stays in the ABI either way, so asking for SCTP against a build without it has to
76 * fail loudly here rather than fall through to TCP, which would look as if it worked
77 * while putting traffic on the wrong protocol. Returns -1 with errno set, like socket().
78 */
79int sctp_socket(int family) {
80#ifdef LIBE3_ENABLE_SCTP
81 return socket(family, SOCK_STREAM, IPPROTO_SCTP);
82#else
83 (void)family;
84 E3_LOG_ERROR(LOG_TAG) << "SCTP transport requested but libe3 was built without it. "
85 "Rebuild with -DLIBE3_ENABLE_SCTP=ON (needs libsctp-dev), "
86 "or use the TCP or IPC transport.";
87 errno = EPROTONOSUPPORT;
88 return -1;
89#endif
90}
91
92void set_timeout(int fd, int option, int timeout_ms, const char* name) {
93 struct timeval tv;
94 tv.tv_sec = timeout_ms / 1000;
95 tv.tv_usec = (timeout_ms % 1000) * 1000;
96 if (setsockopt(fd, SOL_SOCKET, option, &tv, sizeof(tv)) != 0) {
97 E3_LOG_WARN(LOG_TAG) << "Failed to set " << name << ": " << strerror(errno);
98 }
99}
100
101void set_nonblocking(int fd) {
102 int flags = fcntl(fd, F_GETFL, 0);
103 if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
104 E3_LOG_WARN(LOG_TAG) << "Failed to set O_NONBLOCK: " << strerror(errno);
105 }
106}
107
108// ---------------------------------------------------------------------------
109// One PDU per message
110// ---------------------------------------------------------------------------
111
112/** @brief The outcome of looking for the next PDU on a connection. */
113enum class Read { PDU, NONE, CLOSED, ERROR };
114
115/**
116 * @brief Take a length-prefixed PDU off the front of buffered stream bytes (TCP).
117 *
118 * The prefix is four bytes in network order; zero and more than MAX_PDU are refused.
119 */
120Read pop_stream_frame(std::vector<uint8_t>& rx, std::vector<uint8_t>& out) {
121 if (rx.size() < sizeof(uint32_t)) {
122 return Read::NONE;
123 }
124 uint32_t size = 0;
125 std::memcpy(&size, rx.data(), sizeof(size));
126 size = ntohl(size);
127 if (size == 0 || size > MAX_PDU) {
128 E3_LOG_ERROR(LOG_TAG) << "Invalid PDU length " << size;
129 return Read::ERROR;
130 }
131 if (rx.size() < sizeof(uint32_t) + size) {
132 return Read::NONE;
133 }
134 out.assign(rx.begin() + sizeof(uint32_t), rx.begin() + sizeof(uint32_t) + size);
135 rx.erase(rx.begin(), rx.begin() + sizeof(uint32_t) + size);
136 return Read::PDU;
137}
138
139bool would_block(int error) {
140 return error == EINTR || error == EAGAIN || error == EWOULDBLOCK;
141}
142
143/**
144 * @brief Read from @p fd once and say whether a whole PDU is now available in @p out.
145 *
146 * @p rx holds what an earlier call read and did not yet make a PDU. TCP: stream bytes,
147 * parsed by the length prefix. IPC: one recv is one PDU, and a PDU longer than the
148 * buffer is an error rather than a silent cut. SCTP: one recvmsg is one fragment of a
149 * message, joined up to MSG_EOR; notifications are skipped, and a payload protocol id
150 * other than 0 and the one libe3 stamps is an error.
151 */
152Read read_one(int fd, E3TransportLayer transport, std::vector<uint8_t>& rx, std::vector<uint8_t>& out) {
153 if (transport == E3TransportLayer::TCP) {
154 if (Read buffered = pop_stream_frame(rx, out); buffered != Read::NONE) {
155 return buffered;
156 }
157 std::array<uint8_t, 16384> chunk;
158 ssize_t n = ::recv(fd, chunk.data(), chunk.size(), 0);
159 if (n > 0) {
160 rx.insert(rx.end(), chunk.begin(), chunk.begin() + n);
161 return pop_stream_frame(rx, out);
162 }
163 if (n == 0) {
164 return Read::CLOSED;
165 }
166 return would_block(errno) ? Read::NONE : Read::ERROR;
167 }
168
169 if (transport == E3TransportLayer::IPC) {
170 out.resize(MAX_PDU);
171 struct iovec iov{out.data(), out.size()};
172 struct msghdr msg{};
173 msg.msg_iov = &iov;
174 msg.msg_iovlen = 1;
175 ssize_t n = ::recvmsg(fd, &msg, 0);
176 if (n < 0) {
177 return would_block(errno) ? Read::NONE : Read::ERROR;
178 }
179 if (n == 0) {
180 return Read::CLOSED;
181 }
182 if (msg.msg_flags & MSG_TRUNC) {
183 E3_LOG_ERROR(LOG_TAG) << "PDU longer than " << MAX_PDU << " bytes";
184 return Read::ERROR;
185 }
186 out.resize(static_cast<size_t>(n));
187 return Read::PDU;
188 }
189
190#ifdef LIBE3_ENABLE_SCTP
191 static thread_local std::array<uint8_t, MAX_PDU> chunk;
192 alignas(struct cmsghdr) char control[CMSG_SPACE(sizeof(struct sctp_rcvinfo))];
193 struct iovec iov{chunk.data(), chunk.size()};
194 struct msghdr msg{};
195 msg.msg_iov = &iov;
196 msg.msg_iovlen = 1;
197 msg.msg_control = control;
198 msg.msg_controllen = sizeof(control);
199 ssize_t n = ::recvmsg(fd, &msg, 0);
200 if (n < 0) {
201 return would_block(errno) ? Read::NONE : Read::ERROR;
202 }
203 if (n == 0) {
204 return Read::CLOSED;
205 }
206 if (msg.msg_flags & MSG_NOTIFICATION) {
207 return Read::NONE;
208 }
209 if (rx.empty()) {
210 for (struct cmsghdr* c = CMSG_FIRSTHDR(&msg); c != nullptr; c = CMSG_NXTHDR(&msg, c)) {
211 if (c->cmsg_level == IPPROTO_SCTP && c->cmsg_type == SCTP_RCVINFO) {
212 struct sctp_rcvinfo info;
213 std::memcpy(&info, CMSG_DATA(c), sizeof(info));
214 const uint32_t ppid = ntohl(info.rcv_ppid);
215 if (ppid != 0 && ppid != SCTP_PPID) {
216 E3_LOG_ERROR(LOG_TAG) << "Message with payload protocol id 0x" << std::hex << ppid;
217 return Read::ERROR;
218 }
219 }
220 }
221 }
222 rx.insert(rx.end(), chunk.begin(), chunk.begin() + n);
223 if (rx.size() > MAX_PDU) {
224 E3_LOG_ERROR(LOG_TAG) << "PDU longer than " << MAX_PDU << " bytes";
225 return Read::ERROR;
226 }
227 if (msg.msg_flags & MSG_EOR) {
228 out = std::move(rx);
229 rx.clear();
230 return Read::PDU;
231 }
232 return Read::NONE;
233#else
234 (void)rx;
235 (void)out;
236 return Read::ERROR;
237#endif
238}
239
240/**
241 * @brief Send one PDU as one message.
242 *
243 * TCP writes the length prefix and the payload together. IPC and SCTP send the payload
244 * alone: a sequenced-packet message and an SCTP message each keep their boundary. SCTP
245 * stamps the payload protocol id and picks the stream from @p kind.
246 */
247ErrorCode write_pdu(int fd, E3TransportLayer transport, const std::vector<uint8_t>& data, PduClass kind) {
248 if (data.empty() || data.size() > MAX_PDU) {
249 E3_LOG_ERROR(LOG_TAG) << "PDU of " << data.size() << " bytes cannot be sent";
250 return ErrorCode::INVALID_PARAM;
251 }
252
253 if (transport == E3TransportLayer::TCP) {
254 uint32_t prefix = htonl(static_cast<uint32_t>(data.size()));
255 struct iovec iov[2];
256 iov[0].iov_base = &prefix;
257 iov[0].iov_len = sizeof(prefix);
258 iov[1].iov_base = const_cast<uint8_t*>(data.data());
259 iov[1].iov_len = data.size();
260 size_t first = 0;
261 const size_t total = sizeof(prefix) + data.size();
262 size_t sent = 0;
263 while (sent < total) {
264 struct iovec cur[2] = {iov[0], iov[1]};
265 size_t skip = sent;
266 first = 0;
267 while (first < 2 && skip >= cur[first].iov_len) {
268 skip -= cur[first].iov_len;
269 ++first;
270 }
271 cur[first].iov_base = static_cast<uint8_t*>(cur[first].iov_base) + skip;
272 cur[first].iov_len -= skip;
273 struct msghdr msg{};
274 msg.msg_iov = cur + first;
275 msg.msg_iovlen = 2 - first;
276 ssize_t n = ::sendmsg(fd, &msg, MSG_NOSIGNAL);
277 if (n < 0) {
278 if (errno == EINTR) {
279 continue;
280 }
281 E3_LOG_ERROR(LOG_TAG) << "Failed to send: " << strerror(errno);
282 return ErrorCode::TRANSPORT_ERROR;
283 }
284 sent += static_cast<size_t>(n);
285 }
286 return ErrorCode::SUCCESS;
287 }
288
289 if (transport == E3TransportLayer::IPC) {
290 ssize_t n;
291 do {
292 n = ::send(fd, data.data(), data.size(), MSG_NOSIGNAL);
293 } while (n < 0 && errno == EINTR);
294 if (n < 0 || static_cast<size_t>(n) != data.size()) {
295 E3_LOG_ERROR(LOG_TAG) << "Failed to send: " << strerror(errno);
296 return ErrorCode::TRANSPORT_ERROR;
297 }
298 return ErrorCode::SUCCESS;
299 }
300
301#ifdef LIBE3_ENABLE_SCTP
302 alignas(struct cmsghdr) char control[CMSG_SPACE(sizeof(struct sctp_sndinfo))]{};
303 struct iovec iov{const_cast<uint8_t*>(data.data()), data.size()};
304 struct msghdr msg{};
305 msg.msg_iov = &iov;
306 msg.msg_iovlen = 1;
307 msg.msg_control = control;
308 msg.msg_controllen = sizeof(control);
309 struct cmsghdr* c = CMSG_FIRSTHDR(&msg);
310 c->cmsg_level = IPPROTO_SCTP;
311 c->cmsg_type = SCTP_SNDINFO;
312 c->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
313 struct sctp_sndinfo info{};
314 info.snd_sid = kind == PduClass::INDICATION ? SCTP_INDICATION_STREAM : SCTP_PROCEDURE_STREAM;
315 info.snd_ppid = htonl(SCTP_PPID);
316 std::memcpy(CMSG_DATA(c), &info, sizeof(info));
317 ssize_t n;
318 do {
319 n = ::sendmsg(fd, &msg, MSG_NOSIGNAL);
320 } while (n < 0 && errno == EINTR);
321 if (n < 0 || static_cast<size_t>(n) != data.size()) {
322 E3_LOG_ERROR(LOG_TAG) << "Failed to send: " << strerror(errno);
323 return ErrorCode::TRANSPORT_ERROR;
324 }
325 return ErrorCode::SUCCESS;
326#else
327 (void)kind;
328 return ErrorCode::TRANSPORT_ERROR;
329#endif
330}
331
332/** @brief The text of a peer's address, with an IPv4-mapped IPv6 address shown as IPv4. */
333std::string address_text(const sockaddr_storage& ss) {
334 char text[INET6_ADDRSTRLEN] = {};
335 if (ss.ss_family == AF_INET) {
336 const auto* in = reinterpret_cast<const sockaddr_in*>(&ss);
337 inet_ntop(AF_INET, &in->sin_addr, text, sizeof(text));
338 } else if (ss.ss_family == AF_INET6) {
339 const auto* in6 = reinterpret_cast<const sockaddr_in6*>(&ss);
340 if (IN6_IS_ADDR_V4MAPPED(&in6->sin6_addr)) {
341 inet_ntop(AF_INET, &in6->sin6_addr.s6_addr[12], text, sizeof(text));
342 } else {
343 inet_ntop(AF_INET6, &in6->sin6_addr, text, sizeof(text));
344 }
345 }
346 return text;
347}
348} // anonymous namespace
349
350/** @brief One accepted connection of a RAN. Closes its socket when the last reference goes. */
352 int fd; ///< The connection
353 uint64_t token; ///< Never reused by this connector
354 std::vector<uint8_t> rx; ///< Bytes read and not yet a whole PDU; the reader's alone
355 bool seen_first{false}; ///< The first PDU, the setup request, was read; the reader's alone
356 std::mutex write_mutex; ///< One writer at a time
357
358 /**
359 * @brief Take over an accepted connection.
360 * @param f The connected socket, which this object closes
361 * @param t The token that names the connection
362 */
363 Peer(int f, uint64_t t) : fd(f), token(t) {}
364
365 /** @brief Close the socket. */
367 if (fd >= 0) {
368 ::close(fd);
369 }
370 }
371};
372
373/** @brief A PDU read from a peer and waiting for the thread that takes it. */
375 std::shared_ptr<Peer> peer; ///< Who sent it
376 uint64_t token{0}; ///< The sender's token
377 std::vector<uint8_t> data; ///< The PDU
378};
379
382 const std::string& endpoint,
383 uint16_t port,
384 const PosixOptions& options
385)
386 : transport_layer_(transport_layer)
387 , endpoint_uri_(endpoint)
388 , port_(port)
389 , options_(options)
390{
392 setup_endpoint_ = endpoint == SHARED_DEFAULT_ENDPOINT ? DEFAULT_IPC_PATH : strip_scheme(endpoint);
393 } else {
394 setup_endpoint_ = endpoint;
395 }
396 E3_LOG_INFO(LOG_TAG) << "Creating POSIX connector, one endpoint: " << setup_endpoint_
397 << " port " << port_;
398}
399
403
404PosixE3Connector::Target PosixE3Connector::target() const {
405 Target where{"", std::to_string(port_)};
406 std::string rest;
407 for (const char* scheme : {"tcp://", "sctp://"}) {
408 if (endpoint_uri_.rfind(scheme, 0) == 0) {
409 rest = endpoint_uri_.substr(std::strlen(scheme));
410 break;
411 }
412 }
413 if (rest.empty()) {
414 return where;
415 }
416 if (rest[0] == '[') {
417 const auto close = rest.find(']');
418 if (close == std::string::npos) {
419 return where;
420 }
421 where.host = rest.substr(1, close - 1);
422 if (close + 1 < rest.size() && rest[close + 1] == ':') {
423 where.port = rest.substr(close + 2);
424 }
425 } else {
426 const auto colon = rest.rfind(':');
427 if (colon != std::string::npos && rest.find(':') == colon) {
428 where.host = rest.substr(0, colon);
429 where.port = rest.substr(colon + 1);
430 } else {
431 where.host = rest;
432 }
433 }
434 if (where.port.empty()) {
435 where.port = std::to_string(port_);
436 }
437 return where;
438}
439
440int PosixE3Connector::make_socket(int family) const {
441 switch (transport_layer_) {
443 return socket(AF_UNIX, SOCK_SEQPACKET, 0);
445 return sctp_socket(family);
446 default:
447 return socket(family, SOCK_STREAM, 0);
448 }
449}
450
451void PosixE3Connector::tune_socket(int fd, SocketKind kind) const {
452 if (transport_layer_ == E3TransportLayer::IPC) {
453 if (kind == SocketKind::ACCEPTED) {
454 set_timeout(fd, SO_SNDTIMEO, SEND_TIMEOUT_MS, "SO_SNDTIMEO");
455 }
456 return;
457 }
458 const int one = 1;
459 if (kind == SocketKind::LISTENER) {
460 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
461 }
462 if (kind != SocketKind::LISTENER) {
463 set_timeout(fd, SO_SNDTIMEO, SEND_TIMEOUT_MS, "SO_SNDTIMEO");
464 }
465 if (transport_layer_ == E3TransportLayer::TCP) {
466 if (kind != SocketKind::LISTENER && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) {
467 E3_LOG_WARN(LOG_TAG) << "Failed to set TCP_NODELAY: " << strerror(errno);
468 }
469 return;
470 }
471#ifdef LIBE3_ENABLE_SCTP
472 if (kind != SocketKind::ACCEPTED) {
473 // Before the association exists, on both peers: two streams, and buffers and
474 // delayed SACK that do not stall a message larger than the Linux defaults allow.
475 struct sctp_initmsg init{};
476 init.sinit_num_ostreams = 2;
477 init.sinit_max_instreams = 2;
478 if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &init, sizeof(init)) != 0) {
479 E3_LOG_WARN(LOG_TAG) << "Failed to request two SCTP streams: " << strerror(errno);
480 }
481 if (options_.socket_buffer_bytes > 0) {
482 const int size = static_cast<int>(options_.socket_buffer_bytes);
483 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
484 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
485 }
486 if (!options_.sctp_delayed_sack) {
487 struct sctp_sack_info sack{};
488 sack.sack_assoc_id = 0;
489 sack.sack_delay = 0;
490 sack.sack_freq = 1;
491 if (setsockopt(fd, IPPROTO_SCTP, SCTP_DELAYED_SACK, &sack, sizeof(sack)) != 0) {
492 E3_LOG_WARN(LOG_TAG) << "Failed to turn delayed SACK off: " << strerror(errno);
493 }
494 }
495 }
496 if (kind != SocketKind::LISTENER) {
497 if (setsockopt(fd, IPPROTO_SCTP, SCTP_NODELAY, &one, sizeof(one)) != 0) {
498 E3_LOG_WARN(LOG_TAG) << "Failed to set SCTP_NODELAY: " << strerror(errno);
499 }
500 if (setsockopt(fd, IPPROTO_SCTP, SCTP_RECVRCVINFO, &one, sizeof(one)) != 0) {
501 E3_LOG_WARN(LOG_TAG) << "Failed to ask for the SCTP receive info: " << strerror(errno);
502 }
503 }
504#endif
505}
506
507int PosixE3Connector::listen_on(const Target& where) {
508 int fd = -1;
509 int result = -1;
510 if (transport_layer_ == E3TransportLayer::IPC) {
511 struct stat st{};
512 if (stat(IPC_BASE_DIR, &st) == -1 && mkdir(IPC_BASE_DIR, 0777) == -1 && errno != EEXIST) {
513 E3_LOG_ERROR(LOG_TAG) << "Failed to create IPC directory: " << strerror(errno);
514 return -1;
515 }
516 chmod(IPC_BASE_DIR, 0777);
517 if (setup_endpoint_.size() >= sizeof(sockaddr_un::sun_path)) {
518 E3_LOG_ERROR(LOG_TAG) << "IPC path is too long: " << setup_endpoint_;
519 return -1;
520 }
521 fd = make_socket(AF_UNIX);
522 if (fd < 0) {
523 return -1;
524 }
525 struct sockaddr_un addr{};
526 addr.sun_family = AF_UNIX;
527 std::memcpy(addr.sun_path, setup_endpoint_.c_str(), setup_endpoint_.size());
528 unlink(setup_endpoint_.c_str());
529 result = bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
530 } else if (where.host.empty()) {
531 // Any address, both families: a dual-stack socket, or IPv4 alone where there is no IPv6.
532 fd = make_socket(AF_INET6);
533 if (fd >= 0) {
534 const int off = 0;
535 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
536 tune_socket(fd, SocketKind::LISTENER);
537 struct sockaddr_in6 addr{};
538 addr.sin6_family = AF_INET6;
539 addr.sin6_addr = in6addr_any;
540 addr.sin6_port = htons(static_cast<uint16_t>(std::stoi(where.port)));
541 result = bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
542 if (result < 0) {
543 close(fd);
544 fd = -1;
545 }
546 }
547 if (fd < 0) {
548 fd = make_socket(AF_INET);
549 if (fd < 0) {
550 return -1;
551 }
552 tune_socket(fd, SocketKind::LISTENER);
553 struct sockaddr_in addr{};
554 addr.sin_family = AF_INET;
555 addr.sin_addr.s_addr = INADDR_ANY;
556 addr.sin_port = htons(static_cast<uint16_t>(std::stoi(where.port)));
557 result = bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
558 }
559 } else {
560 struct addrinfo hints{};
561 hints.ai_family = AF_UNSPEC;
562 hints.ai_socktype = SOCK_STREAM;
563 hints.ai_flags = AI_PASSIVE;
564 struct addrinfo* found = nullptr;
565 if (getaddrinfo(where.host.c_str(), where.port.c_str(), &hints, &found) != 0 || found == nullptr) {
566 E3_LOG_ERROR(LOG_TAG) << "Cannot resolve " << where.host;
567 return -1;
568 }
569 fd = make_socket(found->ai_family);
570 if (fd >= 0) {
571 tune_socket(fd, SocketKind::LISTENER);
572 result = bind(fd, found->ai_addr, found->ai_addrlen);
573 }
574 freeaddrinfo(found);
575 if (fd < 0) {
576 return -1;
577 }
578 }
579 if (result < 0 || listen(fd, LISTEN_BACKLOG) < 0) {
580 E3_LOG_ERROR(LOG_TAG) << "Failed to listen on " << setup_endpoint_ << ": " << strerror(errno);
581 close(fd);
582 return -1;
583 }
584 set_nonblocking(fd);
585 return fd;
586}
587
588int PosixE3Connector::connect_to(const Target& where) {
589 if (transport_layer_ == E3TransportLayer::IPC) {
590 if (setup_endpoint_.size() >= sizeof(sockaddr_un::sun_path)) {
591 errno = ENAMETOOLONG;
592 return -1;
593 }
594 int fd = make_socket(AF_UNIX);
595 if (fd < 0) {
596 return -1;
597 }
598 struct sockaddr_un addr{};
599 addr.sun_family = AF_UNIX;
600 std::memcpy(addr.sun_path, setup_endpoint_.c_str(), setup_endpoint_.size());
601 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
602 const int saved = errno;
603 close(fd);
604 errno = saved;
605 return -1;
606 }
607 tune_socket(fd, SocketKind::CLIENT);
608 return fd;
609 }
610 struct addrinfo hints{};
611 hints.ai_family = AF_UNSPEC;
612 hints.ai_socktype = SOCK_STREAM;
613 struct addrinfo* found = nullptr;
614 const std::string host = where.host.empty() ? "127.0.0.1" : where.host;
615 if (getaddrinfo(host.c_str(), where.port.c_str(), &hints, &found) != 0 || found == nullptr) {
616 errno = EHOSTUNREACH;
617 return -1;
618 }
619 int fd = -1;
620 int saved = ECONNREFUSED;
621 for (struct addrinfo* a = found; a != nullptr && fd < 0; a = a->ai_next) {
622 int candidate = make_socket(a->ai_family);
623 if (candidate < 0) {
624 saved = errno;
625 continue;
626 }
627 tune_socket(candidate, SocketKind::CLIENT);
628 if (connect(candidate, a->ai_addr, a->ai_addrlen) == 0) {
629 fd = candidate;
630 } else {
631 saved = errno;
632 close(candidate);
633 }
634 }
635 freeaddrinfo(found);
636 if (fd < 0) {
637 errno = saved;
638 }
639 return fd;
640}
641
642bool PosixE3Connector::admit(const void* address) const {
643 if (transport_layer_ == E3TransportLayer::IPC) {
644 return true;
645 }
646 const auto& ss = *static_cast<const sockaddr_storage*>(address);
647 if (!options_.allowed_peers.empty()) {
648 const std::string text = address_text(ss);
649 if (std::find(options_.allowed_peers.begin(), options_.allowed_peers.end(), text)
650 == options_.allowed_peers.end()) {
651 E3_LOG_WARN(LOG_TAG) << "Refused a connection from " << text << ": not in the allowed peers";
652 return false;
653 }
654 }
655 if (options_.maximum_associations > 0) {
656 std::lock_guard<std::mutex> lock(peers_mutex_);
657 if (peers_.size() >= options_.maximum_associations) {
658 E3_LOG_WARN(LOG_TAG) << "Refused a connection from " << address_text(ss)
659 << ": " << options_.maximum_associations << " peers are connected";
660 return false;
661 }
662 }
663 return true;
664}
665
666// ===========================================================================
667// RAN role
668// ===========================================================================
669
671 listener_ = listen_on(target());
672 if (listener_ < 0) {
673 return ErrorCode::CONNECTION_FAILED;
674 }
675 connected_ = true;
676 E3_LOG_INFO(LOG_TAG) << "Listening on " << setup_endpoint_ << " port " << port_;
677 return ErrorCode::SUCCESS;
678}
679
681 // The endpoint that setup_initial_connection() opened carries the inbound traffic too.
682 return listener_ >= 0 ? ErrorCode::SUCCESS : ErrorCode::NOT_INITIALIZED;
683}
684
686 // The same connections carry the outbound traffic.
687 return listener_ >= 0 ? ErrorCode::SUCCESS : ErrorCode::NOT_INITIALIZED;
688}
689
690void PosixE3Connector::accept_pending() {
691 while (listener_ >= 0 && wait_readable(listener_, 0) == 1) {
692 sockaddr_storage address{};
693 socklen_t length = sizeof(address);
694 int fd = accept(listener_, reinterpret_cast<struct sockaddr*>(&address), &length);
695 if (fd < 0) {
696 break; // Nothing more to accept now
697 }
698 if (!admit(&address)) {
699 close(fd);
700 continue;
701 }
702 tune_socket(fd, SocketKind::ACCEPTED);
703 auto peer = std::make_shared<Peer>(fd, next_token_++);
704 size_t count;
705 {
706 std::lock_guard<std::mutex> lock(peers_mutex_);
707 peers_.push_back(peer);
708 count = peers_.size();
709 }
710 E3_LOG_INFO(LOG_TAG) << "Peer connected (" << count << " total)";
711 }
712}
713
714std::shared_ptr<PosixE3Connector::Peer> PosixE3Connector::find_peer(uint64_t token) {
715 std::lock_guard<std::mutex> lock(peers_mutex_);
716 for (const auto& peer : peers_) {
717 if (peer->token == token) {
718 return peer;
719 }
720 }
721 return nullptr;
722}
723
724void PosixE3Connector::drop_peer(const std::shared_ptr<Peer>& peer) {
725 ::shutdown(peer->fd, SHUT_RDWR);
726 size_t remaining;
727 {
728 std::lock_guard<std::mutex> lock(peers_mutex_);
729 peers_.erase(std::remove(peers_.begin(), peers_.end(), peer), peers_.end());
730 remaining = peers_.size();
731 }
732 {
733 std::lock_guard<std::mutex> lock(closed_mutex_);
734 closed_peers_.push_back(peer->token);
735 }
736 if (!peer->seen_first) {
737 E3_LOG_INFO(LOG_TAG) << "A connection closed before it sent a complete setup request. A dApp built "
738 "before libe3 0.6 uses three connections and unframed messages, which "
739 "this endpoint does not serve.";
740 }
741 E3_LOG_INFO(LOG_TAG) << "Peer disconnected (" << remaining << " remain)";
742}
743
744void PosixE3Connector::push_setup(const std::shared_ptr<Peer>& peer, std::vector<uint8_t>&& pdu) {
745 {
746 std::lock_guard<std::mutex> lock(queue_mutex_);
747 setup_queue_.push_back(Queued{peer, peer->token, std::move(pdu)});
748 }
749 setup_ready_.notify_one();
750}
751
752bool PosixE3Connector::receiver_idle() const {
753 return now_ns() - last_receive_ns_.load() > SETUP_PUMP_AFTER_NS;
754}
755
756/**
757 * Look for one PDU for the inbound path, for at most @p timeout_ms. Whoever calls this
758 * holds reader_mutex_. The first PDU of a connection is the setup request and is queued
759 * for recv_setup_request() instead. A connection that closed is dropped only after every
760 * PDU it had sent was handed on.
761 */
762bool PosixE3Connector::pump(std::vector<uint8_t>& out, uint64_t& token, int timeout_ms) {
763 std::vector<std::shared_ptr<Peer>> snapshot;
764 {
765 std::lock_guard<std::mutex> lock(peers_mutex_);
766 snapshot = peers_;
767 }
768
769 // Stream bytes read earlier may already hold a whole PDU.
770 for (const auto& peer : snapshot) {
771 if (transport_layer_ != E3TransportLayer::TCP) {
772 break;
773 }
774 while (!peer->rx.empty()) {
775 Read r = pop_stream_frame(peer->rx, out);
776 if (r == Read::ERROR) {
777 drop_peer(peer);
778 break;
779 }
780 if (r != Read::PDU) {
781 break;
782 }
783 if (!peer->seen_first) {
784 peer->seen_first = true;
785 push_setup(peer, std::move(out));
786 continue;
787 }
788 token = peer->token;
789 return true;
790 }
791 }
792
793 std::vector<struct pollfd> pfds;
794 pfds.reserve(1 + snapshot.size());
795 pfds.push_back({listener_, POLLIN, 0});
796 for (const auto& peer : snapshot) {
797 pfds.push_back({peer->fd, POLLIN, 0});
798 }
799 int ready = poll(pfds.data(), pfds.size(), timeout_ms);
800 if (ready <= 0) {
801 return false;
802 }
803 if (pfds[0].revents & POLLIN) {
804 accept_pending();
805 }
806
807 const size_t count = snapshot.size();
808 for (size_t k = 0; k < count; ++k) {
809 const size_t index = (round_robin_ + k) % count;
810 const auto& peer = snapshot[index];
811 if (!(pfds[index + 1].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL))) {
812 continue;
813 }
814 Read r = read_one(peer->fd, transport_layer_, peer->rx, out);
815 if (r == Read::PDU) {
816 if (!peer->seen_first) {
817 peer->seen_first = true;
818 push_setup(peer, std::move(out));
819 continue;
820 }
821 round_robin_ = index + 1;
822 token = peer->token;
823 return true;
824 }
825 if (r == Read::ERROR && !peer->seen_first && transport_layer_ == E3TransportLayer::TCP) {
826 E3_LOG_INFO(LOG_TAG) << "The first message is not a length-prefixed PDU. A dApp built before "
827 "libe3 0.6 sends its setup request without a prefix.";
828 }
829 if (r == Read::CLOSED || r == Read::ERROR
830 || (r == Read::NONE && (pfds[index + 1].revents & (POLLERR | POLLNVAL)))) {
831 drop_peer(peer);
832 }
833 }
834 if (count > 0) {
835 round_robin_ = (round_robin_ + 1) % count;
836 }
837 return false;
838}
839
840int PosixE3Connector::receive(std::vector<uint8_t>& buffer) {
841 if (role_ == E3Role::DAPP) {
842 return receive_client(buffer);
843 }
844 if (listener_ < 0) {
845 return static_cast<int>(ErrorCode::NOT_CONNECTED);
846 }
847 last_receive_ns_.store(now_ns());
848 {
849 std::lock_guard<std::mutex> lock(queue_mutex_);
850 if (!inbound_queue_.empty()) {
851 Queued item = std::move(inbound_queue_.front());
852 inbound_queue_.pop_front();
853 buffer = std::move(item.data);
854 last_peer_token_ = item.token;
855 return static_cast<int>(buffer.size());
856 }
857 }
858 std::lock_guard<std::mutex> reader(reader_mutex_);
859 uint64_t token = 0;
860 if (pump(buffer, token, POLL_TIMEOUT_MS)) {
861 last_peer_token_ = token;
862 return static_cast<int>(buffer.size());
863 }
864 return 0;
865}
866
867int PosixE3Connector::recv_setup_request(std::vector<uint8_t>& buffer) {
868 if (listener_ < 0) {
869 return static_cast<int>(ErrorCode::NOT_CONNECTED);
870 }
871 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(POLL_TIMEOUT_MS);
872 for (;;) {
873 {
874 std::unique_lock<std::mutex> lock(queue_mutex_);
875 if (!setup_queue_.empty()) {
876 Queued item = std::move(setup_queue_.front());
877 setup_queue_.pop_front();
878 buffer = std::move(item.data);
879 setup_peer_ = item.peer;
880 last_setup_token_ = item.token;
881 return static_cast<int>(buffer.size());
882 }
883 }
884 if (shutdown_requested_.load() || std::chrono::steady_clock::now() >= deadline) {
885 return 0;
886 }
887 // The inbound thread is the reader. When nothing calls receive() (a caller that only
888 // serves setup), this thread reads instead.
889 if (receiver_idle() && reader_mutex_.try_lock()) {
890 std::vector<uint8_t> pdu;
891 uint64_t token = 0;
892 if (pump(pdu, token, 100)) {
893 std::lock_guard<std::mutex> lock(queue_mutex_);
894 inbound_queue_.push_back(Queued{nullptr, token, std::move(pdu)});
895 }
896 reader_mutex_.unlock();
897 } else {
898 std::unique_lock<std::mutex> lock(queue_mutex_);
899 setup_ready_.wait_for(lock, std::chrono::milliseconds(50),
900 [this] { return !setup_queue_.empty() || shutdown_requested_.load(); });
901 }
902 }
903}
904
905ErrorCode PosixE3Connector::send_response(const std::vector<uint8_t>& data) {
906 std::shared_ptr<Peer> peer = setup_peer_;
907 if (!peer) {
908 return ErrorCode::NOT_CONNECTED;
909 }
910 std::lock_guard<std::mutex> lock(peer->write_mutex);
911 return write_pdu(peer->fd, transport_layer_, data, PduClass::PROCEDURE);
912}
913
914ErrorCode PosixE3Connector::send(const std::vector<uint8_t>& data) {
915 return send_to(0, data, PduClass::PROCEDURE);
916}
917
918ErrorCode PosixE3Connector::send_to(uint64_t peer_token, const std::vector<uint8_t>& data, PduClass kind) {
919 // Checked first: shutdown() leaves the fd valid until dispose(), so a send racing it
920 // would otherwise be a real EPIPE, indistinguishable from a transport failure.
921 if (shutdown_requested_.load()) {
922 return ErrorCode::CANCELLED;
923 }
924 if (role_ == E3Role::DAPP) {
925 if (client_fd_ < 0) {
926 return ErrorCode::NOT_CONNECTED;
927 }
928 std::lock_guard<std::mutex> lock(client_write_mutex_);
929 return write_pdu(client_fd_, transport_layer_, data, PduClass::PROCEDURE);
930 }
931 if (listener_ < 0) {
932 return ErrorCode::NOT_CONNECTED;
933 }
934
935 std::vector<std::shared_ptr<Peer>> targets;
936 if (peer_token == 0) {
937 std::lock_guard<std::mutex> lock(peers_mutex_);
938 targets = peers_;
939 } else if (auto peer = find_peer(peer_token)) {
940 targets.push_back(peer);
941 } else {
942 return ErrorCode::NOT_CONNECTED;
943 }
944
945 bool failed = false;
946 size_t sent = 0;
947 for (const auto& peer : targets) {
948 std::lock_guard<std::mutex> lock(peer->write_mutex);
949 if (write_pdu(peer->fd, transport_layer_, data, kind) != ErrorCode::SUCCESS) {
950 // Dead or stalled: closing it makes the reader drop it and report it closed.
951 ::shutdown(peer->fd, SHUT_RDWR);
952 failed = true;
953 } else {
954 ++sent;
955 }
956 }
957 if (failed && sent == 0) {
958 return ErrorCode::TRANSPORT_ERROR;
959 }
960 return ErrorCode::SUCCESS;
961}
962
964 return last_peer_token_;
965}
966
968 return last_setup_token_;
969}
970
971std::vector<uint64_t> PosixE3Connector::take_closed_peers() {
972 std::lock_guard<std::mutex> lock(closed_mutex_);
973 std::vector<uint64_t> closed;
974 closed.swap(closed_peers_);
975 return closed;
976}
977
978// ===========================================================================
979// dApp role: one connection
980// ===========================================================================
981
983 const Target where = target();
984 int fd = -1;
985 for (int attempt = 0; attempt < CONNECT_ATTEMPTS && fd < 0; ++attempt) {
986 fd = connect_to(where);
987 if (fd < 0) {
988 if (shutdown_requested_.load()) {
989 return ErrorCode::CANCELLED;
990 }
991 std::this_thread::sleep_for(std::chrono::milliseconds(CONNECT_PAUSE_MS));
992 }
993 }
994 if (fd < 0) {
995 E3_LOG_ERROR(LOG_TAG) << "Failed to connect to " << setup_endpoint_ << " port " << where.port
996 << ": " << strerror(errno);
997 return ErrorCode::CONNECTION_FAILED;
998 }
999 client_fd_ = fd;
1000 client_rx_.clear();
1001 // Until the setup response is read, the setup thread is the only reader: the inbound
1002 // thread must not take it for an indication.
1003 awaiting_setup_response_.store(true);
1004 connected_ = true;
1005 E3_LOG_INFO(LOG_TAG) << "Connected to " << setup_endpoint_ << " port " << where.port;
1006 return ErrorCode::SUCCESS;
1007}
1008
1009ErrorCode PosixE3Connector::reconnect_client() {
1010 if (client_fd_ >= 0) {
1011 close(client_fd_);
1012 client_fd_ = -1;
1013 }
1015}
1016
1018 if (client_fd_ < 0) {
1019 return ErrorCode::NOT_CONNECTED;
1020 }
1021 // The RAN takes the first PDU of a connection for the setup request, so a second
1022 // attempt needs a connection of its own, as a ZeroMQ request socket is reopened.
1023 if (setup_request_sent_) {
1024 ErrorCode rc = reconnect_client();
1025 if (rc != ErrorCode::SUCCESS) {
1026 return rc;
1027 }
1028 }
1029 setup_request_sent_ = true;
1030 std::lock_guard<std::mutex> lock(client_write_mutex_);
1031 return write_pdu(client_fd_, transport_layer_, data, PduClass::PROCEDURE);
1032}
1033
1034int PosixE3Connector::recv_setup_response_client(std::vector<uint8_t>& buffer) {
1035 if (client_fd_ < 0) {
1036 return static_cast<int>(ErrorCode::NOT_CONNECTED);
1037 }
1038 if (!awaiting_setup_response_.load()) {
1039 std::this_thread::sleep_for(std::chrono::milliseconds(100));
1040 return 0;
1041 }
1042 std::lock_guard<std::mutex> reader(reader_mutex_);
1043 Read r = Read::NONE;
1044 if (transport_layer_ == E3TransportLayer::TCP) {
1045 r = pop_stream_frame(client_rx_, buffer);
1046 }
1047 if (r == Read::NONE) {
1048 const int ready = wait_readable(client_fd_, POLL_TIMEOUT_MS);
1049 if (ready == 0) {
1050 return 0;
1051 }
1052 if (ready < 0) {
1053 return errno == EINTR ? 0 : static_cast<int>(ErrorCode::TRANSPORT_ERROR);
1054 }
1055 r = read_one(client_fd_, transport_layer_, client_rx_, buffer);
1056 }
1057 if (r == Read::PDU) {
1058 awaiting_setup_response_.store(false);
1059 await_cv_.notify_all();
1060 return static_cast<int>(buffer.size());
1061 }
1062 if (r == Read::CLOSED) {
1063 return static_cast<int>(ErrorCode::NOT_CONNECTED);
1064 }
1065 return r == Read::ERROR ? static_cast<int>(ErrorCode::TRANSPORT_ERROR) : 0;
1066}
1067
1069 return client_fd_ >= 0 ? ErrorCode::SUCCESS : ErrorCode::NOT_CONNECTED;
1070}
1071
1073 return client_fd_ >= 0 ? ErrorCode::SUCCESS : ErrorCode::NOT_CONNECTED;
1074}
1075
1076int PosixE3Connector::receive_client(std::vector<uint8_t>& buffer) {
1077 if (client_fd_ < 0) {
1078 return static_cast<int>(ErrorCode::NOT_CONNECTED);
1079 }
1080 if (awaiting_setup_response_.load()) {
1081 std::unique_lock<std::mutex> lock(await_mutex_);
1082 await_cv_.wait_for(lock, std::chrono::milliseconds(POLL_TIMEOUT_MS), [this] {
1083 return !awaiting_setup_response_.load() || shutdown_requested_.load();
1084 });
1085 if (awaiting_setup_response_.load()) {
1086 return 0;
1087 }
1088 }
1089 std::lock_guard<std::mutex> reader(reader_mutex_);
1090 Read r = Read::NONE;
1091 if (transport_layer_ == E3TransportLayer::TCP) {
1092 r = pop_stream_frame(client_rx_, buffer);
1093 }
1094 if (r == Read::NONE) {
1095 const int ready = wait_readable(client_fd_, POLL_TIMEOUT_MS);
1096 if (ready == 0) {
1097 return 0;
1098 }
1099 if (ready < 0) {
1100 return errno == EINTR ? 0 : static_cast<int>(ErrorCode::TRANSPORT_ERROR);
1101 }
1102 r = read_one(client_fd_, transport_layer_, client_rx_, buffer);
1103 }
1104 switch (r) {
1105 case Read::PDU:
1106 return static_cast<int>(buffer.size());
1107 case Read::CLOSED:
1108 return static_cast<int>(ErrorCode::NOT_CONNECTED);
1109 case Read::ERROR:
1110 return static_cast<int>(ErrorCode::TRANSPORT_ERROR);
1111 default:
1112 return 0;
1113 }
1114}
1115
1116// ===========================================================================
1117// Shutdown
1118// ===========================================================================
1119
1121 E3_LOG_DEBUG(LOG_TAG) << "Shutting down POSIX connector";
1122 shutdown_requested_.store(true);
1123 setup_ready_.notify_all();
1124 await_cv_.notify_all();
1125 if (listener_ >= 0) {
1126 ::shutdown(listener_, SHUT_RDWR);
1127 }
1128 {
1129 std::lock_guard<std::mutex> lock(peers_mutex_);
1130 for (const auto& peer : peers_) {
1131 ::shutdown(peer->fd, SHUT_RDWR);
1132 }
1133 }
1134 if (client_fd_ >= 0) {
1135 ::shutdown(client_fd_, SHUT_RDWR);
1136 }
1137}
1138
1140 E3_LOG_DEBUG(LOG_TAG) << "Disposing POSIX connector";
1141 {
1142 std::lock_guard<std::mutex> lock(peers_mutex_);
1143 peers_.clear();
1144 }
1145 {
1146 std::lock_guard<std::mutex> lock(queue_mutex_);
1147 setup_queue_.clear();
1148 inbound_queue_.clear();
1149 }
1150 setup_peer_.reset();
1151 {
1152 std::lock_guard<std::mutex> lock(closed_mutex_);
1153 closed_peers_.clear();
1154 }
1155 if (client_fd_ >= 0) {
1156 close(client_fd_);
1157 client_fd_ = -1;
1158 }
1159 if (listener_ >= 0) {
1160 close(listener_);
1161 listener_ = -1;
1162 // Only the RAN, which binds, has a socket file to remove. The directory is shared
1163 // between agents and tests and stays, so that removing it cannot race a bind in it.
1164 if (transport_layer_ == E3TransportLayer::IPC) {
1165 unlink(setup_endpoint_.c_str());
1166 }
1167 }
1168 connected_ = false;
1169 E3_LOG_INFO(LOG_TAG) << "POSIX connector disposed";
1170}
1171
1172} // namespace libe3
E3Role role_
Role of this peer.
std::string setup_endpoint_
Endpoint of the setup channel.
ErrorCode send_setup_request_client(const std::vector< uint8_t > &data) override
Send a SetupRequest on the setup channel (dApp role).
~PosixE3Connector() override
Close every socket.
uint64_t last_peer_token() const override
Which peer supplied the bytes the last receive() returned (RAN role).
int recv_setup_request(std::vector< uint8_t > &buffer) override
Receive a setup request.
uint64_t last_setup_peer_token() const override
Which peer sent the setup request the last recv_setup_request() returned (RAN role).
void dispose() override
Clean up and release all resources.
int receive(std::vector< uint8_t > &buffer) override
Receive data from the inbound channel.
ErrorCode send(const std::vector< uint8_t > &data) override
Send data on the outbound channel.
ErrorCode setup_initial_connection_client() override
Connect the setup channel (dApp role: REQ connect).
E3TransportLayer transport_layer() const noexcept override
Get the transport layer type.
ErrorCode send_to(uint64_t peer_token, const std::vector< uint8_t > &data, PduClass kind) override
Send data to one peer, or to all of them (RAN role).
int recv_setup_response_client(std::vector< uint8_t > &buffer) override
Receive a SetupResponse on the setup channel (dApp role).
ErrorCode send_response(const std::vector< uint8_t > &data) override
Send a setup response.
PosixE3Connector(E3TransportLayer transport_layer, const std::string &endpoint, uint16_t port, const PosixOptions &options)
Create a POSIX connector.
void shutdown() override
Interrupt blocking operations for shutdown.
ErrorCode setup_outbound_connection_client() override
Connect the outbound channel (dApp role: PUB connect to the RAN's subscriber endpoint).
ErrorCode setup_inbound_connection_client() override
Connect the inbound channel (dApp role: SUB connect to the RAN's publisher endpoint).
std::vector< uint64_t > take_closed_peers() override
Peers whose connection has closed since the last call (RAN role).
ErrorCode setup_initial_connection() override
Set up the initial connection (setup channel).
ErrorCode setup_inbound_connection() override
Set up the inbound connection (SUB channel).
ErrorCode setup_outbound_connection() override
Set up the outbound connection (PUB channel).
Logging facility for libe3.
#define E3_LOG_WARN(component)
Definition logger.hpp:226
#define E3_LOG_DEBUG(component)
Definition logger.hpp:228
#define E3_LOG_INFO(component)
Definition logger.hpp:227
#define E3_LOG_ERROR(component)
Definition logger.hpp:225
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
E3TransportLayer
Transport layer types (matches Python E3TransportLayer).
Definition types.hpp:85
@ SCTP
SCTP (O-RAN standard).
Definition types.hpp:86
@ IPC
Unix Domain Sockets / IPC.
Definition types.hpp:88
@ DAPP
dApp (connects to a RAN agent)
Definition types.hpp:79
constexpr size_t MAX_PROTOCOL_DATA_SIZE
Largest protocolData of an indication, in bytes.
Definition types.hpp:200
PduClass
What a PDU is for, so that a link that has streams can pick one.
@ PROCEDURE
A request, a response, a control, a release: anything but an indication.
@ INDICATION
An indication message.
POSIX socket-based E3 Connector.
One accepted connection of a RAN. Closes its socket when the last reference goes.
std::vector< uint8_t > rx
Bytes read and not yet a whole PDU; the reader's alone.
Peer(int f, uint64_t t)
Take over an accepted connection.
bool seen_first
The first PDU, the setup request, was read; the reader's alone.
uint64_t token
Never reused by this connector.
std::mutex write_mutex
One writer at a time.
A PDU read from a peer and waiting for the thread that takes it.
std::shared_ptr< Peer > peer
Who sent it.
std::vector< uint8_t > data
The PDU.
uint64_t token
The sender's token.
Socket and admission options of the POSIX link.