2016-08-16 16:51:22 +02:00
|
|
|
/*
|
2017-11-09 19:34:51 +01:00
|
|
|
* This file is part of the Flowee project
|
2024-01-21 23:16:56 +01:00
|
|
|
* Copyright (C) 2016-2024 Tom Zander <tom@flowee.org>
|
2016-08-16 16:51:22 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2024-01-22 19:32:15 +01:00
|
|
|
#ifndef FLOWEE_NETWORKENDPOINT_H
|
|
|
|
|
#define FLOWEE_NETWORKENDPOINT_H
|
2016-08-16 16:51:22 +02:00
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <cstdint>
|
2019-06-03 21:41:04 +02:00
|
|
|
#include <Logger.h>
|
2016-08-16 16:51:22 +02:00
|
|
|
|
|
|
|
|
#include <boost/asio/ip/address.hpp>
|
|
|
|
|
|
|
|
|
|
/// Describes a remote server.
|
|
|
|
|
struct EndPoint
|
|
|
|
|
{
|
2021-02-22 15:53:32 +01:00
|
|
|
/// Invalid endpoint
|
2016-08-16 16:51:22 +02:00
|
|
|
EndPoint()
|
|
|
|
|
: peerPort(0),
|
2019-06-06 19:44:16 +02:00
|
|
|
announcePort(0)
|
2016-08-16 16:51:22 +02:00
|
|
|
{
|
|
|
|
|
}
|
2021-02-22 15:53:32 +01:00
|
|
|
/**
|
|
|
|
|
* Hostname constructor
|
|
|
|
|
* Port will be used for both peer and announce port members.
|
|
|
|
|
*/
|
2019-05-24 23:32:20 +02:00
|
|
|
EndPoint(const std::string &hostname, std::uint16_t port)
|
|
|
|
|
: hostname(hostname),
|
|
|
|
|
peerPort(port),
|
|
|
|
|
announcePort(port)
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-02-22 15:53:32 +01:00
|
|
|
/**
|
|
|
|
|
* IP address constructor.
|
|
|
|
|
* Port will be used for both peer and announce port members.
|
|
|
|
|
*/
|
2019-05-25 14:31:36 +02:00
|
|
|
EndPoint(const boost::asio::ip::address &ip, std::uint16_t port)
|
|
|
|
|
: ipAddress(ip),
|
|
|
|
|
peerPort(port),
|
|
|
|
|
announcePort(port)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-06-13 16:57:07 +02:00
|
|
|
|
|
|
|
|
bool isValid() const { return announcePort > 0 && (!hostname.empty() || !ipAddress.is_unspecified()); }
|
|
|
|
|
|
2024-01-21 23:16:56 +01:00
|
|
|
std::string toString() const;
|
|
|
|
|
|
2020-03-30 22:17:44 +02:00
|
|
|
/// Implement the P2P format 'addr' of a 16-byte vector encoding the address
|
|
|
|
|
static EndPoint fromAddr(const std::vector<char> &addr, int16_t port);
|
|
|
|
|
|
2023-08-13 13:57:43 +02:00
|
|
|
bool operator==(const EndPoint &other) const;
|
|
|
|
|
bool operator!=(const EndPoint &other) const {
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 12:32:03 +02:00
|
|
|
/// write address to a bytearray of 16 bytes.
|
|
|
|
|
void toAddr(char *addr) const;
|
|
|
|
|
|
2024-01-24 21:03:17 +01:00
|
|
|
/**
|
|
|
|
|
* Return if this is an outgoing connection.
|
|
|
|
|
* In networking an incoming socket will have two ports registered,
|
|
|
|
|
* the one we connected to (remote announce port) and the local port that we get assigned
|
|
|
|
|
* by the OS upon opening the socket.
|
|
|
|
|
*/
|
|
|
|
|
bool isOutgoing() const {
|
|
|
|
|
return announcePort == peerPort;
|
|
|
|
|
}
|
|
|
|
|
/// @see isOutgoing()
|
|
|
|
|
bool isIncoming() const {
|
|
|
|
|
return !isOutgoing();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
boost::asio::ip::address ipAddress;
|
|
|
|
|
std::string hostname;
|
2019-06-06 19:44:16 +02:00
|
|
|
std::uint16_t peerPort = 0;
|
|
|
|
|
std::uint16_t announcePort = 0;
|
2021-02-22 15:53:32 +01:00
|
|
|
/// The connection id refers to the NetworkConnection id, as used by the NetworkManager
|
2019-06-06 19:44:16 +02:00
|
|
|
int connectionId = -1;
|
2024-08-04 14:02:53 +02:00
|
|
|
bool encrypted = false;
|
2016-08-16 16:51:22 +02:00
|
|
|
};
|
|
|
|
|
|
2019-06-03 21:41:04 +02:00
|
|
|
inline Log::Item operator<<(Log::Item item, const EndPoint &ep) {
|
|
|
|
|
if (item.isEnabled()) {
|
|
|
|
|
const bool old = item.useSpace();
|
2024-01-21 23:16:56 +01:00
|
|
|
item.nospace() << "EndPoint(" << ep.toString() << ')';
|
2019-06-03 21:41:04 +02:00
|
|
|
if (old)
|
|
|
|
|
return item.space();
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
inline Log::SilentItem operator<<(Log::SilentItem item, const EndPoint&) { return item; }
|
|
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
#endif
|