2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2012 Bolton Software 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 COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "net/NetworkAddress.h"
|
|
|
|
|
|
|
|
#include "net/XSocket.h"
|
|
|
|
#include "arch/Arch.h"
|
|
|
|
#include "arch/XArch.h"
|
|
|
|
|
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() :
|
2012-06-10 16:50:54 +00:00
|
|
|
m_address(NULL),
|
|
|
|
m_hostname(),
|
|
|
|
m_port(0)
|
|
|
|
{
|
2014-11-11 13:51:47 +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) :
|
2012-06-10 16:50:54 +00:00
|
|
|
m_address(NULL),
|
|
|
|
m_hostname(),
|
|
|
|
m_port(port)
|
|
|
|
{
|
|
|
|
checkPort();
|
|
|
|
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
|
|
|
|
ARCH->setAddrPort(m_address, m_port);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress::NetworkAddress(const NetworkAddress& addr) :
|
2012-06-10 16:50:54 +00:00
|
|
|
m_address(addr.m_address != NULL ? ARCH->copyAddr(addr.m_address) : NULL),
|
|
|
|
m_hostname(addr.m_hostname),
|
|
|
|
m_port(addr.m_port)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress::NetworkAddress(const String& hostname, int port) :
|
2012-06-10 16:50:54 +00:00
|
|
|
m_address(NULL),
|
|
|
|
m_hostname(hostname),
|
|
|
|
m_port(port)
|
|
|
|
{
|
|
|
|
// check for port suffix
|
2014-11-11 13:51:47 +00:00
|
|
|
String::size_type i = m_hostname.rfind(':');
|
|
|
|
if (i != String::npos && i + 1 < m_hostname.size()) {
|
2012-06-10 16:50:54 +00:00
|
|
|
// found a colon. see if it looks like an IPv6 address.
|
|
|
|
bool colonNotation = false;
|
|
|
|
bool dotNotation = false;
|
|
|
|
bool doubleColon = false;
|
2014-11-11 13:51:47 +00:00
|
|
|
for (String::size_type j = 0; j < i; ++j) {
|
2012-06-10 16:50:54 +00:00
|
|
|
if (m_hostname[j] == ':') {
|
|
|
|
colonNotation = true;
|
|
|
|
dotNotation = false;
|
|
|
|
if (m_hostname[j + 1] == ':') {
|
|
|
|
doubleColon = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_hostname[j] == '.' && colonNotation) {
|
|
|
|
dotNotation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// port suffix is ambiguous with IPv6 notation if there's
|
|
|
|
// a double colon and the end of the address is not in dot
|
|
|
|
// notation. in that case we assume it's not a port suffix.
|
|
|
|
// the user can replace the double colon with zeros to
|
|
|
|
// disambiguate.
|
|
|
|
if ((!doubleColon || dotNotation) || !colonNotation) {
|
|
|
|
// parse port from hostname
|
|
|
|
char* end;
|
|
|
|
const char* chostname = m_hostname.c_str();
|
|
|
|
long suffixPort = strtol(chostname + i + 1, &end, 10);
|
|
|
|
if (end == chostname + i + 1 || *end != '\0') {
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort,
|
|
|
|
m_hostname, m_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim port from hostname
|
|
|
|
m_hostname.erase(i);
|
|
|
|
|
|
|
|
// save port
|
|
|
|
m_port = static_cast<int>(suffixPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check port number
|
|
|
|
checkPort();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress&
|
|
|
|
NetworkAddress::operator=(const NetworkAddress& addr)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2014-11-11 13:51:47 +00:00
|
|
|
ArchNetAddress newAddr = NULL;
|
2012-06-10 16:50:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress::operator==(const NetworkAddress& addr) const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return ARCH->isEqualAddr(m_address, addr.m_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress::isValid() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return (m_address != NULL);
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
const ArchNetAddress&
|
|
|
|
NetworkAddress::getAddress() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return m_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-11-11 13:51:47 +00:00
|
|
|
NetworkAddress::getPort() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return m_port;
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
String
|
|
|
|
NetworkAddress::getHostname() const
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
return m_hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|