|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2017 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | +#ifndef NET_PORT_UTIL_HPP |
| 20 | +#define NET_PORT_UTIL_HPP |
| 21 | + |
| 22 | +#include "inet_common.hpp" |
| 23 | +#include <bitset> |
| 24 | + |
| 25 | +namespace net { |
| 26 | + |
| 27 | +struct Port_error : public std::runtime_error { |
| 28 | + using base = std::runtime_error; |
| 29 | + using base::base; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * @brief Class for port utility. |
| 34 | + */ |
| 35 | +class Port_util { |
| 36 | +public: |
| 37 | + /** |
| 38 | + * @brief Construct a port util with a new generated ephemeral port |
| 39 | + * and a empty port list. |
| 40 | + */ |
| 41 | + Port_util() |
| 42 | + : ports(), |
| 43 | + ephemeral_(net::new_ephemeral_port()), |
| 44 | + eph_count(0) |
| 45 | + { |
| 46 | + ports.set(port_ranges::DYNAMIC_END); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Gets the next ephemeral port. |
| 51 | + * increment_ephemeral may throw |
| 52 | + * |
| 53 | + * @return The next ephemeral port. |
| 54 | + */ |
| 55 | + uint16_t get_next_ephemeral() |
| 56 | + { |
| 57 | + increment_ephemeral(); |
| 58 | + return ephemeral_; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief Bind a port, making it reserved. |
| 63 | + * |
| 64 | + * @param[in] port The port |
| 65 | + */ |
| 66 | + void bind(const uint16_t port) noexcept |
| 67 | + { |
| 68 | + Expects(port < port_ranges::DYNAMIC_END); |
| 69 | + ports.set(port); |
| 70 | + |
| 71 | + if(port_ranges::is_dynamic(port)) ++eph_count; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Unbind a port, making it available. |
| 76 | + * |
| 77 | + * @param[in] port The port |
| 78 | + */ |
| 79 | + void unbind(const uint16_t port) noexcept |
| 80 | + { |
| 81 | + Expects(port < port_ranges::DYNAMIC_END); |
| 82 | + ports.reset(port); |
| 83 | + |
| 84 | + if(port_ranges::is_dynamic(port)) --eph_count; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Determines if the port is bound. |
| 89 | + * |
| 90 | + * @param[in] port The port |
| 91 | + * |
| 92 | + * @return True if bound, False otherwise. |
| 93 | + */ |
| 94 | + bool is_bound(const uint16_t port) const noexcept |
| 95 | + { |
| 96 | + Expects(port < port_ranges::DYNAMIC_END); |
| 97 | + return ports[port]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @brief Determines if it has any free ephemeral ports. |
| 102 | + * |
| 103 | + * @return True if has free ephemeral, False otherwise. |
| 104 | + */ |
| 105 | + bool has_free_ephemeral() const noexcept |
| 106 | + { return eph_count < (port_ranges::DYNAMIC_END - port_ranges::DYNAMIC_START); } |
| 107 | + |
| 108 | +private: |
| 109 | + std::bitset<65536> ports; |
| 110 | + uint16_t ephemeral_; |
| 111 | + uint16_t eph_count; |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Increment the ephemeral port by one. |
| 115 | + * Throws if there are no more free ephemeral ports available. |
| 116 | + */ |
| 117 | + void increment_ephemeral() |
| 118 | + { |
| 119 | + if(UNLIKELY( not has_free_ephemeral() )) |
| 120 | + throw Port_error{"All ephemeral ports are taken"}; |
| 121 | + |
| 122 | + ephemeral_++; |
| 123 | + |
| 124 | + // wrap around to dynamic start if end |
| 125 | + if(UNLIKELY(ephemeral_ == port_ranges::DYNAMIC_END)) |
| 126 | + ephemeral_ = port_ranges::DYNAMIC_START; |
| 127 | + |
| 128 | + // TODO: Avoid wrap around, increment ephemeral to next free port. |
| 129 | + // while(is_bound(ephemeral_)) ++ephemeral_; // worst case is like 16k iterations :D |
| 130 | + // need a solution that checks each word of the subset (the dynamic range) |
| 131 | + // FIXME: this may happen... |
| 132 | + if(UNLIKELY( is_bound(ephemeral_) )) |
| 133 | + throw Port_error{"Generated ephemeral port is already bound. Please fix me!"}; |
| 134 | + } |
| 135 | +}; // < class Port_util |
| 136 | + |
| 137 | +} // < namespace net |
| 138 | + |
| 139 | +#endif |
0 commit comments