Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Endpoint.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Endpoint.hpp"
18
20
21using namespace std;
22
23namespace toolbox {
24inline namespace net {
25namespace {
26
28{
29 // Reverse find for compatibility with ipv6 address notation.
30 const auto pos = addr.find_last_of(delim);
31 string node, service;
32 if (pos == string::npos) {
33 node = addr;
34 } else {
35 node = addr.substr(0, pos);
36 service = addr.substr(pos + 1);
37 }
38 // Remove square braces around ipv6 address.
39 if (node.size() >= 2 && node.front() == '[' && node.back() == ']') {
40 node = node.substr(1, node.size() - 2);
41 }
42 return {node, service};
43}
44
46{
47 const auto pos = uri.find("://");
48 string scheme, addr;
49 if (pos == string::npos) {
50 addr = uri;
51 } else {
52 scheme = uri.substr(0, pos);
53 addr = uri.substr(pos + 3);
54 }
55 return {scheme, addr};
56}
57} // namespace
58
59AddrInfoPtr parse_endpoint(string_view uri, int type)
60{
61 int family{-1}, protocol{0};
62 const auto [scheme, addr] = split_uri(uri);
63 if (scheme.empty()) {
64 family = AF_UNSPEC;
65 } else if (scheme == "ip4") {
66 family = AF_INET;
67 } else if (scheme == "ip6") {
68 family = AF_INET6;
69 } else if (scheme == "tcp4") {
70 if (type == SOCK_STREAM) {
71 family = AF_INET;
72 protocol = IPPROTO_TCP;
73 }
74 } else if (scheme == "tcp6") {
75 if (type == SOCK_STREAM) {
76 family = AF_INET6;
77 protocol = IPPROTO_TCP;
78 }
79 } else if (scheme == "udp4") {
80 if (type == SOCK_DGRAM) {
81 family = AF_INET;
82 protocol = IPPROTO_UDP;
83 }
84 } else if (scheme == "udp6") {
85 if (type == SOCK_DGRAM) {
86 family = AF_INET6;
87 protocol = IPPROTO_UDP;
88 }
89 } else if (scheme == "unix") {
90 return get_unix_addrinfo(addr, type);
91 }
92 if (family < 0) {
93 throw invalid_argument{make_string("invalid uri: ", uri)};
94 }
95 auto [node, service] = split_ip_addr(addr, ':');
96 return os::getaddrinfo(!node.empty() ? node.c_str() : nullptr,
97 !service.empty() ? service.c_str() : nullptr, family, type, protocol);
98}
99
100istream& operator>>(istream& is, DgramEndpoint& ep)
101{
102 string uri;
103 if (is >> uri) {
105 }
106 return is;
107}
108
109istream& operator>>(istream& is, StreamEndpoint& ep)
110{
111 string uri;
112 if (is >> uri) {
114 }
115 return is;
116}
117
118} // namespace net
119} // namespace toolbox
STL namespace.
BasicEndpoint< DgramProtocol > DgramEndpoint
Definition Endpoint.hpp:35
istream & operator>>(istream &is, DgramEndpoint &ep)
Definition Endpoint.cpp:100
AddrInfoPtr parse_endpoint(string_view uri, int type)
Definition Endpoint.cpp:59
std::unique_ptr< addrinfo, void(*)(addrinfo *)> AddrInfoPtr
Definition Socket.hpp:30
BasicEndpoint< StreamProtocol > StreamEndpoint
Definition Endpoint.hpp:36
AddrInfoPtr get_unix_addrinfo(string_view path, int type)
Create an addrinfo structure for a Unix domain address.
Definition Socket.cpp:35
DgramEndpoint parse_dgram_endpoint(std::string_view uri)
Definition Endpoint.hpp:52
StreamEndpoint parse_stream_endpoint(std::string_view uri)
Definition Endpoint.hpp:58
AddrInfoPtr getaddrinfo(const char *node, const char *service, const addrinfo &hints, std::error_code &ec) noexcept
Get network address from Internet host and service.
Definition Socket.hpp:35
std::string make_string(ArgsT &&... args)
Definition String.hpp:357
constexpr auto bind() noexcept
Definition Slot.hpp:92