Skip to content

Commit 6a680e2

Browse files
mihajlo-k4og
authored andcommitted
Introduce lib network
1 parent baab441 commit 6a680e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+7428
-0
lines changed

score/network/BUILD

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-License-Identifier: Apache-2.0
12+
# *******************************************************************************
13+
load("@score_baselibs//third_party/itf:py_unittest_qnx_test.bzl", "py_unittest_qnx_test")
14+
15+
test_suite(
16+
name = "unit_tests",
17+
tests = [
18+
"@score_baselibs//score/network/test:unit_tests",
19+
],
20+
visibility = ["//platform/aas/lib:__pkg__"],
21+
)
22+
23+
py_unittest_qnx_test(
24+
name = "unit_tests_qnx",
25+
tags = ["manual"],
26+
test_suites = ["@score_baselibs//score/network/test:unit_tests_qnx"],
27+
visibility = ["//platform/aas/lib:__pkg__"],
28+
)
29+
30+
cc_library(
31+
name = "ipv4_address",
32+
srcs = ["ipv4_address.cpp"],
33+
hdrs = ["ipv4_address.h"],
34+
features = [
35+
"treat_warnings_as_errors",
36+
"additional_warnings",
37+
"strict_warnings",
38+
],
39+
visibility = ["//visibility:public"],
40+
)
41+
42+
cc_library(
43+
name = "udp_socket",
44+
srcs = ["udp_socket.cpp"],
45+
hdrs = ["udp_socket.h"],
46+
features = [
47+
"treat_warnings_as_errors",
48+
"additional_warnings",
49+
"strict_warnings",
50+
],
51+
# The visibility is not public because it is still work in progress and
52+
# not ready to be used.
53+
visibility = [
54+
"//platform/aas/network/tunneling:__subpackages__",
55+
"//platform/aas/network/xnm:__subpackages__",
56+
"@score_baselibs//score/network:__subpackages__",
57+
],
58+
deps = [
59+
":ipv4_address",
60+
"@score_baselibs//score/language/futurecpp",
61+
"@score_baselibs//score/os:errno",
62+
"@score_baselibs//score/os:fcntl",
63+
"@score_baselibs//score/os:socket",
64+
"@score_baselibs//score/os:unistd",
65+
],
66+
)
67+
68+
cc_library(
69+
name = "vlan",
70+
srcs = ["vlan.cpp"],
71+
hdrs = ["vlan.h"],
72+
features = [
73+
"treat_warnings_as_errors",
74+
"additional_warnings",
75+
"strict_warnings",
76+
],
77+
visibility = ["//visibility:public"],
78+
deps = [
79+
"@score_baselibs//score/language/futurecpp",
80+
"@score_baselibs//score/os:socket",
81+
],
82+
)
83+
84+
cc_library(
85+
name = "netutils",
86+
srcs = [
87+
"netutils.cpp",
88+
"netutils_impl.cpp",
89+
"netutils_impl.h",
90+
] + select({
91+
"@platforms//os:linux": ["netutils_impl_linux.cpp"],
92+
"@platforms//os:qnx": ["netutils_impl_qnx.cpp"],
93+
"//conditions:default": ["netutils_impl_linux.cpp"],
94+
}),
95+
hdrs = ["netutils.h"],
96+
visibility = ["//visibility:public"],
97+
deps = [
98+
":ipv4_address",
99+
"@score_baselibs//score/mw/log:frontend",
100+
"@score_baselibs//score/os:errno",
101+
"@score_baselibs//score/os:ifaddrs",
102+
"@score_baselibs//score/os:ioctl",
103+
"@score_baselibs//score/os:object_seam",
104+
"@score_baselibs//score/os:socket",
105+
"@score_baselibs//score/os:unistd",
106+
],
107+
)
108+
109+
cc_library(
110+
name = "socket_interface",
111+
hdrs = ["i_socket.h"],
112+
visibility = ["//visibility:public"],
113+
deps = [
114+
"@score_baselibs//score/language/futurecpp",
115+
"@score_baselibs//score/os:socket",
116+
"@score_baselibs//score/result",
117+
],
118+
)

score/network/i_socket.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
#ifndef SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_
14+
#define SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_
15+
16+
#include "score/network/sock_async/net_endpoint.h"
17+
#include "score/os/socket.h"
18+
#include <score/callback.hpp>
19+
#include <score/span.hpp>
20+
#include <score/variant.hpp>
21+
#include <sys/time.h> /* KW_SUPPRESS:MISRA.INCL.UNSAFE:time.h libarary needed for TimeoutOption */
22+
#include <cstdint>
23+
24+
namespace score
25+
{
26+
27+
namespace os
28+
{
29+
/* KW_SUPPRESS_START:AUTOSAR.STYLE.SINGLE_STMT_PER_LINE: False Positive */
30+
/* KW_SUPPRESS_START:MISRA.ONEDEFRULE.VAR: False Positive */
31+
/* KW_SUPPRESS_START:MISRA.VAR.NEEDS.CONST: False Positive */
32+
constexpr const std::int32_t ENABLE_OPTION = 1;
33+
/* KW_SUPPRESS_END:MISRA.VAR.NEEDS.CONST: False Positive */
34+
/* KW_SUPPRESS_END:MISRA.ONEDEFRULE.VAR: False Positive */
35+
/* KW_SUPPRESS_END:AUTOSAR.STYLE.SINGLE_STMT_PER_LINE: False Positive */
36+
class SockOption
37+
{
38+
public:
39+
virtual ~SockOption() = default;
40+
virtual score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const = 0;
41+
};
42+
43+
class ReuseAddrOption : public SockOption
44+
{
45+
public:
46+
/* KW_SUPPRESS_START:MISRA.CTOR.BASE: Pure virtual base, no ctor */
47+
ReuseAddrOption() : optval_(ENABLE_OPTION) {}
48+
/* KW_SUPPRESS_END:MISRA.CTOR.BASE */
49+
50+
score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override
51+
{
52+
return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval_, sizeof(optval_));
53+
}
54+
55+
private:
56+
std::int32_t optval_;
57+
};
58+
59+
class TimeoutOption : public SockOption
60+
{
61+
public:
62+
TimeoutOption(const std::int32_t seconds) : SockOption(), seconds_(seconds) {}
63+
64+
score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override
65+
{
66+
struct timeval timeout;
67+
timeout.tv_sec = seconds_;
68+
timeout.tv_usec = 0;
69+
return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
70+
}
71+
72+
private:
73+
std::int32_t seconds_;
74+
};
75+
76+
class BufferSizeOption : public SockOption
77+
{
78+
public:
79+
BufferSizeOption(const std::int32_t size) : SockOption(), size_(size) {}
80+
81+
score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override
82+
{
83+
return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size_, sizeof(size_));
84+
}
85+
86+
private:
87+
std::int32_t size_;
88+
};
89+
90+
using AsyncCallback = score::cpp::callback<void(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>>, ssize_t)>;
91+
using AsyncConnectCallback = score::cpp::callback<void(std::int16_t)>;
92+
using Endpoint = score::os::NetEndpoint;
93+
using SockOptionVariant = score::cpp::variant<ReuseAddrOption, TimeoutOption, BufferSizeOption>;
94+
class ISocket
95+
{
96+
public:
97+
virtual void SetOption(const SockOptionVariant optionVariant) noexcept = 0;
98+
virtual std::int32_t WriteAsync(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer,
99+
AsyncCallback cb) noexcept = 0;
100+
virtual score::cpp::expected<ssize_t, score::os::Error> WriteSync(
101+
std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer) const noexcept = 0;
102+
virtual std::int32_t ReadAsync(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer,
103+
AsyncCallback cb) noexcept = 0;
104+
virtual score::cpp::variant<ssize_t, std::tuple<ssize_t, score::os::Ipv4Address>, score::os::Error> ReadSync(
105+
std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer) noexcept = 0;
106+
107+
virtual std::int32_t ConnectAsync(AsyncConnectCallback cb) noexcept = 0;
108+
// virtual void Unblock() noexcept = 0;
109+
virtual ~ISocket() noexcept = default;
110+
};
111+
112+
} // namespace os
113+
} // namespace score
114+
115+
#endif // SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_

0 commit comments

Comments
 (0)