|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of clio: https://github.com/XRPLF/clio |
| 4 | + Copyright (c) 2024, the clio developers. |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and distribute this software for any |
| 7 | + purpose with or without fee is hereby granted, provided that the above |
| 8 | + copyright notice and this permission notice appear in all copies. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +*/ |
| 18 | +//============================================================================== |
| 19 | + |
| 20 | +#include "util/AssignRandomPort.hpp" |
| 21 | + |
| 22 | +#include <boost/asio/io_context.hpp> |
| 23 | +#include <boost/asio/ip/tcp.hpp> |
| 24 | + |
| 25 | +#include <cstdint> |
| 26 | + |
| 27 | +using tcp = boost::asio::ip::tcp; |
| 28 | + |
| 29 | +namespace tests::util { |
| 30 | + |
| 31 | +uint32_t |
| 32 | +generateFreePort() |
| 33 | +{ |
| 34 | + boost::asio::io_context io_context; |
| 35 | + tcp::acceptor acceptor(io_context); |
| 36 | + tcp::endpoint const endpoint(tcp::v4(), 0); |
| 37 | + |
| 38 | + acceptor.open(endpoint.protocol()); |
| 39 | + acceptor.set_option(tcp::acceptor::reuse_address(true)); |
| 40 | + acceptor.bind(endpoint); |
| 41 | + |
| 42 | + return acceptor.local_endpoint().port(); |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace tests::util |
0 commit comments