2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include "private/hostandport.hpp"
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <boost/lexical_cast.hpp>
30 using namespace shareddatalayer;
34 uint16_t stringToPort(const std::string& port)
37 throw HostAndPort::EmptyPort();
40 return htons(boost::lexical_cast<uint16_t>(port));
42 catch (const boost::bad_lexical_cast&)
45 servent resultbuf = { };
46 servent *result(nullptr);
47 getservbyname_r(port.c_str(), nullptr, &resultbuf, buf, sizeof(buf), &result);
48 if (result == nullptr)
49 throw HostAndPort::InvalidPort(port);
50 return static_cast<uint16_t>(result->s_port);
54 bool isInBrackets(const std::string& str)
56 return ((!str.empty()) && (str.front() == '[') && (str.back() == ']'));
59 std::string removeBrackets(const std::string& str)
61 return str.substr(1U, str.size() - 2U);
64 bool isLiteralIPv6(const std::string& str)
67 return inet_pton(AF_INET6, str.c_str(), &tmp) == 1;
70 std::string buildInvalidPortError(const std::string& port)
72 std::ostringstream os;
73 os << "invalid port: " << port;
78 HostAndPort::InvalidPort::InvalidPort(const std::string& port):
79 Exception(buildInvalidPortError(port))
83 HostAndPort::EmptyPort::EmptyPort():
84 Exception("empty port")
88 HostAndPort::EmptyHost::EmptyHost():
89 Exception("empty host")
93 HostAndPort::HostAndPort(const std::string& addressAndOptionalPort, uint16_t defaultPort)
95 if (isInBrackets(addressAndOptionalPort))
96 parse(removeBrackets(addressAndOptionalPort), defaultPort);
98 parse(addressAndOptionalPort, defaultPort);
101 void HostAndPort::parse(const std::string& addressAndOptionalPort, uint16_t defaultPort)
103 const auto pos(addressAndOptionalPort.rfind(':'));
104 if (pos == std::string::npos)
106 host = addressAndOptionalPort;
110 else if (isLiteralIPv6(addressAndOptionalPort))
112 host = addressAndOptionalPort;
118 host = addressAndOptionalPort.substr(0, pos);
119 if (isInBrackets(host))
120 host = removeBrackets(host);
121 literalIPv6 = isLiteralIPv6(host);
122 port = stringToPort(addressAndOptionalPort.substr(pos + 1U, addressAndOptionalPort.size() - pos - 1U));
128 const std::string& HostAndPort::getHost() const
133 uint16_t HostAndPort::getPort() const
138 std::string HostAndPort::getString() const
140 std::ostringstream os;
142 os << '[' << host << "]:" << ntohs(port);
144 os << host << ':' << ntohs(port);
148 bool HostAndPort::operator==(const HostAndPort& hp) const
150 return this->getPort() == hp.getPort() && this->getHost() == hp.getHost();
153 bool HostAndPort::operator!=(const HostAndPort& hp) const
155 return !(hp == *this);
158 bool HostAndPort::operator<(const HostAndPort& hp) const
160 if (this->getHost() == hp.getHost())
161 return this->getPort() < hp.getPort();
163 return this->getHost() < hp.getHost();
166 std::ostream& shareddatalayer::operator << (std::ostream& os, const HostAndPort& hostAndPort)
168 os << hostAndPort.getHost() << ":" << hostAndPort.getPort();