|
| 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 <util/fixed_bitmap.hpp> |
| 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 handling a full range of network ports. |
| 34 | + * Generates ephemeral ports and track what ports are bound or not. |
| 35 | + * 1 means free, 0 means bound (occupied) |
| 36 | + */ |
| 37 | +class Port_util { |
| 38 | +public: |
| 39 | + /** |
| 40 | + * @brief Construct a port util with a new generated ephemeral port |
| 41 | + * and a empty port list. |
| 42 | + */ |
| 43 | + Port_util() |
| 44 | + : ports(), |
| 45 | + eph_view{ // set the ephemeral view to be between 49152-65535 |
| 46 | + ports.data() + port_ranges::DYNAMIC_START, |
| 47 | + (port_ranges::DYNAMIC_END - port_ranges::DYNAMIC_START + 1) / sizeof(MemBitmap::word) |
| 48 | + }, |
| 49 | + ephemeral_(net::new_ephemeral_port()), |
| 50 | + eph_count(0) |
| 51 | + { |
| 52 | + // all ports are free |
| 53 | + ports.set_all(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Gets the next ephemeral port. |
| 58 | + * increment_ephemeral may throw |
| 59 | + * |
| 60 | + * @return The next ephemeral port. |
| 61 | + */ |
| 62 | + uint16_t get_next_ephemeral() |
| 63 | + { |
| 64 | + increment_ephemeral(); |
| 65 | + return ephemeral_; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @brief Bind a port, making it reserved. |
| 70 | + * |
| 71 | + * @param[in] port The port |
| 72 | + */ |
| 73 | + void bind(const uint16_t port) noexcept |
| 74 | + { |
| 75 | + ports.reset(port); |
| 76 | + |
| 77 | + if(port_ranges::is_dynamic(port)) ++eph_count; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Unbind a port, making it available. |
| 82 | + * |
| 83 | + * @param[in] port The port |
| 84 | + */ |
| 85 | + void unbind(const uint16_t port) noexcept |
| 86 | + { |
| 87 | + ports.set(port); |
| 88 | + |
| 89 | + if(port_ranges::is_dynamic(port)) --eph_count; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief Determines if the port is bound. |
| 94 | + * |
| 95 | + * @param[in] port The port |
| 96 | + * |
| 97 | + * @return True if bound, False otherwise. |
| 98 | + */ |
| 99 | + bool is_bound(const uint16_t port) const noexcept |
| 100 | + { |
| 101 | + return !ports[port]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief Determines if it has any free ephemeral ports. |
| 106 | + * |
| 107 | + * @return True if has free ephemeral, False otherwise. |
| 108 | + */ |
| 109 | + bool has_free_ephemeral() const noexcept |
| 110 | + { return eph_count < (port_ranges::DYNAMIC_END - port_ranges::DYNAMIC_START); } |
| 111 | + |
| 112 | +private: |
| 113 | + Fixed_bitmap<65536> ports; |
| 114 | + MemBitmap eph_view; |
| 115 | + uint16_t ephemeral_; |
| 116 | + uint16_t eph_count; |
| 117 | + |
| 118 | + /** |
| 119 | + * @brief Increment the ephemeral port by one. |
| 120 | + * Throws if there are no more free ephemeral ports available. |
| 121 | + */ |
| 122 | + void increment_ephemeral() |
| 123 | + { |
| 124 | + if(UNLIKELY( not has_free_ephemeral() )) |
| 125 | + throw Port_error{"All ephemeral ports are taken"}; |
| 126 | + |
| 127 | + ephemeral_++; |
| 128 | + |
| 129 | + // wrap around to dynamic start if end |
| 130 | + if(UNLIKELY(ephemeral_ == 0)) |
| 131 | + ephemeral_ = port_ranges::DYNAMIC_START; |
| 132 | + |
| 133 | + if(UNLIKELY( is_bound(ephemeral_) )) |
| 134 | + { |
| 135 | + auto i = eph_view.first_set(); |
| 136 | + Ensures(i != -1 && "Did not found a free ephemeral even tho has_free_ephemeral() == true..."); |
| 137 | + ephemeral_ = port_ranges::DYNAMIC_START + i; |
| 138 | + } |
| 139 | + |
| 140 | + Expects(not is_bound(ephemeral_) && "Generated ephemeral port is already bound. Please fix me!"); |
| 141 | + } |
| 142 | +}; // < class Port_util |
| 143 | + |
| 144 | +} // < namespace net |
| 145 | + |
| 146 | +#endif |
0 commit comments