NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
ip_address.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
27 
28 #include <cstring>
29 #include <stdexcept>
30 #include <string>
31 
32 using namespace std::string_literals;
33 
34 namespace storage {
35 
36 ip_address::ip_address(std::string address, uint16_t port) : m_addr{std::move(address)}, m_port{port}
37 {
38 }
39 
40 std::string const &ip_address::get_address() const noexcept
41 {
42  return m_addr;
43 }
44 
45 uint16_t ip_address::get_port() const noexcept
46 {
47  return m_port;
48 }
49 
51 {
52  auto const *const end = value + ::strnlen(value, 22);
53  std::string addr;
54  uint32_t port = 0;
55 
56  uint8_t dots = 0;
57  for (auto *iter = value; iter != end; ++iter) {
58  if (std::isspace(*iter))
59  throw std::runtime_error{"Spaces are not permitted in IP address strings" +
60  std::to_string(UINT16_MAX)};
61  if (*iter == '.') {
62  ++dots;
63  } else if (*iter == ':') {
64  if (dots != 3) {
65  throw std::runtime_error{"Invalid ip address: \""s + value +
66  "\" expected 4 octets before the port number %u" +
67  std::to_string(UINT16_MAX)};
68  }
69 
70  addr = std::string{value, iter};
71  port = ::strtoul(iter + 1, nullptr, 10);
72  if (port > UINT16_MAX) {
73  throw std::runtime_error{"Invalid ip address: \""s + value +
74  "\", port number exceeds %u" + std::to_string(UINT16_MAX)};
75  }
76 
77  return ip_address{std::move(addr), static_cast<uint16_t>(port)};
78  }
79  }
80 
81  throw std::runtime_error{"Invalid ip address: \""s + value + "\""};
82 }
83 
84 } /* namespace storage */
uint16_t get_port() const noexcept
Definition: ip_address.cpp:45
std::string const & get_address() const noexcept
Definition: ip_address.cpp:40
uintptr_t addr
type value
std::string to_string(storage::control::message_type type)
ip_address parse_ip_v4_address(char const *value)
Definition: ip_address.cpp:50