deskflow/src/lib/net/NetworkAddress.cpp

223 lines
5.9 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2002 Chris Schoeneman
2012-06-10 16:50:54 +00:00
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
2012-06-10 16:50:54 +00:00
*
* This package 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/>.
*/
#include "net/NetworkAddress.h"
#include "net/XSocket.h"
#include "arch/Arch.h"
#include "arch/XArch.h"
SYNERGY-885 mac client listens on ipv4 only but attempts to connect on ipv6 (#6983) * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix ipv6 port parsing * Fix ipv6 server bind * SYNERGY 885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add ipv6 scope checker * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Update changelog * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Refactor network address logic * SYNERGY-885-Mac client listens on ipv4 only but attempts to connect on ipv6 * Build fix * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix empty ipv4 hostname * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix build * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add test for new logic * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Upgrade network adress parser tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix sonar code smells in tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 *Fix comment Co-authored-by: user <user@users-Mac-mini.local> Co-authored-by: Andrii Batyiev <andrii-external@symless.com>
2021-04-26 07:37:34 +00:00
#include <algorithm>
2012-06-10 16:50:54 +00:00
#include <cstdlib>
//
2014-11-11 13:51:47 +00:00
// NetworkAddress
2012-06-10 16:50:54 +00:00
//
// name re-resolution adapted from a patch by Brent Priddy.
2014-11-11 13:51:47 +00:00
NetworkAddress::NetworkAddress() :
m_address(NULL),
m_hostname(),
m_port(0)
2012-06-10 16:50:54 +00:00
{
// note -- make no calls to Network socket interface here;
// we're often called prior to Network::init().
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
NetworkAddress::NetworkAddress(int port) :
m_address(NULL),
m_hostname(),
m_port(port)
2012-06-10 16:50:54 +00:00
{
checkPort();
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
ARCH->setAddrPort(m_address, m_port);
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
NetworkAddress::NetworkAddress(const NetworkAddress& addr) :
m_address(addr.m_address != NULL ? ARCH->copyAddr(addr.m_address) : NULL),
m_hostname(addr.m_hostname),
m_port(addr.m_port)
2012-06-10 16:50:54 +00:00
{
// do nothing
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
NetworkAddress::NetworkAddress(const String& hostname, int port) :
m_address(NULL),
m_hostname(hostname),
m_port(port)
2012-06-10 16:50:54 +00:00
{
SYNERGY-885 mac client listens on ipv4 only but attempts to connect on ipv6 (#6983) * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix ipv6 port parsing * Fix ipv6 server bind * SYNERGY 885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add ipv6 scope checker * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Update changelog * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Refactor network address logic * SYNERGY-885-Mac client listens on ipv4 only but attempts to connect on ipv6 * Build fix * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix empty ipv4 hostname * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix build * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add test for new logic * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Upgrade network adress parser tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix sonar code smells in tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 *Fix comment Co-authored-by: user <user@users-Mac-mini.local> Co-authored-by: Andrii Batyiev <andrii-external@symless.com>
2021-04-26 07:37:34 +00:00
//detect internet protocol version with colom count
auto isColomPredicate = [](char c){return c == ':';};
auto colomCount = std::count_if(m_hostname.begin(), m_hostname.end(), isColomPredicate);
if(colomCount == 1) {
//ipv4 with port part
auto hostIt = m_hostname.find(':');
try {
m_port = std::stoi(m_hostname.substr(hostIt + 1));
} catch(...) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
}
SYNERGY-885 mac client listens on ipv4 only but attempts to connect on ipv6 (#6983) * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix ipv6 port parsing * Fix ipv6 server bind * SYNERGY 885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add ipv6 scope checker * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Update changelog * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Refactor network address logic * SYNERGY-885-Mac client listens on ipv4 only but attempts to connect on ipv6 * Build fix * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix empty ipv4 hostname * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix build * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add test for new logic * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Upgrade network adress parser tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix sonar code smells in tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 *Fix comment Co-authored-by: user <user@users-Mac-mini.local> Co-authored-by: Andrii Batyiev <andrii-external@symless.com>
2021-04-26 07:37:34 +00:00
auto endHostnameIt = static_cast<int>(hostIt);
m_hostname = m_hostname.substr(0, endHostnameIt > 0 ? endHostnameIt : 0);
}
else if (colomCount > 1) {
//ipv6 part
if (m_hostname[0] == '[') {
//ipv6 with port part
String portDelimeter = "]:";
auto hostIt = m_hostname.find(portDelimeter);
//bad syntax of ipv6 with port
if (hostIt == String::npos) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port);
}
SYNERGY-885 mac client listens on ipv4 only but attempts to connect on ipv6 (#6983) * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix ipv6 port parsing * Fix ipv6 server bind * SYNERGY 885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add ipv6 scope checker * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Update changelog * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Refactor network address logic * SYNERGY-885-Mac client listens on ipv4 only but attempts to connect on ipv6 * Build fix * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix empty ipv4 hostname * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix build * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add test for new logic * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Upgrade network adress parser tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix sonar code smells in tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 *Fix comment Co-authored-by: user <user@users-Mac-mini.local> Co-authored-by: Andrii Batyiev <andrii-external@symless.com>
2021-04-26 07:37:34 +00:00
auto portSuffix = m_hostname.substr(hostIt + portDelimeter.size());
//port is implied but omitted
if (portSuffix.empty()) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
}
try {
m_port = std::stoi(portSuffix);
} catch(...) {
//port is not a number
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
}
auto endHostnameIt = static_cast<int>(hostIt) - 1;
m_hostname = m_hostname.substr(1, endHostnameIt > 0 ? endHostnameIt : 0);
}
SYNERGY-885 mac client listens on ipv4 only but attempts to connect on ipv6 (#6983) * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix ipv6 port parsing * Fix ipv6 server bind * SYNERGY 885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add ipv6 scope checker * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Update changelog * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Refactor network address logic * SYNERGY-885-Mac client listens on ipv4 only but attempts to connect on ipv6 * Build fix * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix empty ipv4 hostname * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix build * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Add test for new logic * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Upgrade network adress parser tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix sonar code smells in tests * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 * Fix code smells * SYNERGY-885 Mac client listens on ipv4 only but attempts to connect on ipv6 *Fix comment Co-authored-by: user <user@users-Mac-mini.local> Co-authored-by: Andrii Batyiev <andrii-external@symless.com>
2021-04-26 07:37:34 +00:00
// ensure that ipv6 link-local adress ended with scope id
if (m_hostname.rfind("fe80:", 0) == 0 && m_hostname.find('%') == String::npos) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port);
}
}
// check port number
checkPort();
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
NetworkAddress::~NetworkAddress()
2012-06-10 16:50:54 +00:00
{
if (m_address != NULL) {
ARCH->closeAddr(m_address);
}
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
NetworkAddress&
NetworkAddress::operator=(const NetworkAddress& addr)
2012-06-10 16:50:54 +00:00
{
ArchNetAddress newAddr = NULL;
if (addr.m_address != NULL) {
newAddr = ARCH->copyAddr(addr.m_address);
}
if (m_address != NULL) {
ARCH->closeAddr(m_address);
}
m_address = newAddr;
m_hostname = addr.m_hostname;
m_port = addr.m_port;
return *this;
2012-06-10 16:50:54 +00:00
}
void
2014-11-11 13:51:47 +00:00
NetworkAddress::resolve()
2012-06-10 16:50:54 +00:00
{
// discard previous address
if (m_address != NULL) {
ARCH->closeAddr(m_address);
m_address = NULL;
}
try {
// if hostname is empty then use wildcard address otherwise look
// up the name.
if (m_hostname.empty()) {
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
}
else {
m_address = ARCH->nameToAddr(m_hostname);
}
}
catch (XArchNetworkNameUnknown&) {
throw XSocketAddress(XSocketAddress::kNotFound, m_hostname, m_port);
}
catch (XArchNetworkNameNoAddress&) {
throw XSocketAddress(XSocketAddress::kNoAddress, m_hostname, m_port);
}
catch (XArchNetworkNameUnsupported&) {
throw XSocketAddress(XSocketAddress::kUnsupported, m_hostname, m_port);
}
catch (XArchNetworkName&) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port);
}
// set port in address
ARCH->setAddrPort(m_address, m_port);
2012-06-10 16:50:54 +00:00
}
bool
2014-11-11 13:51:47 +00:00
NetworkAddress::operator==(const NetworkAddress& addr) const
2012-06-10 16:50:54 +00:00
{
return m_address == addr.m_address || ARCH->isEqualAddr(m_address, addr.m_address);
2012-06-10 16:50:54 +00:00
}
bool
2014-11-11 13:51:47 +00:00
NetworkAddress::operator!=(const NetworkAddress& addr) const
2012-06-10 16:50:54 +00:00
{
return !operator==(addr);
2012-06-10 16:50:54 +00:00
}
bool
2014-11-11 13:51:47 +00:00
NetworkAddress::isValid() const
2012-06-10 16:50:54 +00:00
{
return (m_address != NULL);
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
const ArchNetAddress&
NetworkAddress::getAddress() const
2012-06-10 16:50:54 +00:00
{
return m_address;
2012-06-10 16:50:54 +00:00
}
int
2014-11-11 13:51:47 +00:00
NetworkAddress::getPort() const
2012-06-10 16:50:54 +00:00
{
return m_port;
2012-06-10 16:50:54 +00:00
}
2014-11-11 13:51:47 +00:00
String
NetworkAddress::getHostname() const
2012-06-10 16:50:54 +00:00
{
return m_hostname;
2012-06-10 16:50:54 +00:00
}
void
2014-11-11 13:51:47 +00:00
NetworkAddress::checkPort()
2012-06-10 16:50:54 +00:00
{
// check port number
if (m_port <= 0 || m_port > 65535) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
}
2012-06-10 16:50:54 +00:00
}