2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CNetworkAddress.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSocket.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstdlib>
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CNetworkAddress
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CNetworkAddress::CNetworkAddress() :
|
2003-01-04 22:01:32 +00:00
|
|
|
m_address(NULL)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-06-09 16:53:25 +00:00
|
|
|
// note -- make no calls to CNetwork socket interface here;
|
|
|
|
// we're often called prior to CNetwork::init().
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(int port)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-06-09 16:53:25 +00:00
|
|
|
if (port == 0) {
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, "", port);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
|
|
|
|
ARCH->setAddrPort(m_address, port);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(const CNetworkAddress& addr) :
|
|
|
|
m_address(ARCH->copyAddr(addr.m_address)),
|
|
|
|
m_hostname(addr.m_hostname)
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
// do nothing
|
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(const CString& hostname_, int port) :
|
|
|
|
m_hostname(hostname_)
|
|
|
|
{
|
2002-06-09 16:53:25 +00:00
|
|
|
if (port == 0) {
|
2003-01-04 22:01:32 +00:00
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, port);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for port suffix
|
2003-01-04 22:01:32 +00:00
|
|
|
CString hostname(m_hostname);
|
2002-06-09 16:53:25 +00:00
|
|
|
CString::size_type i = hostname.rfind(':');
|
|
|
|
if (i != CString::npos && i + 1 < hostname.size()) {
|
|
|
|
// found a colon. see if it looks like an IPv6 address.
|
|
|
|
bool colonNotation = false;
|
|
|
|
bool dotNotation = false;
|
|
|
|
bool doubleColon = false;
|
|
|
|
for (CString::size_type j = 0; j < i; ++j) {
|
|
|
|
if (hostname[j] == ':') {
|
|
|
|
colonNotation = true;
|
|
|
|
dotNotation = false;
|
|
|
|
if (hostname[j + 1] == ':') {
|
|
|
|
doubleColon = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (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) {
|
|
|
|
char* end;
|
|
|
|
long suffixPort = strtol(hostname.c_str() + i + 1, &end, 10);
|
|
|
|
if (end == hostname.c_str() + i + 1 || *end != '\0' ||
|
|
|
|
suffixPort <= 0 || suffixPort > 65535) {
|
|
|
|
// bogus port
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort,
|
2003-01-04 22:01:32 +00:00
|
|
|
m_hostname, port);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// good port
|
2003-01-04 22:01:32 +00:00
|
|
|
port = static_cast<int>(suffixPort);
|
2002-06-09 16:53:25 +00:00
|
|
|
hostname.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if hostname is empty then use wildcard address
|
2002-10-20 22:36:24 +00:00
|
|
|
if (hostname.empty()) {
|
2003-01-04 22:01:32 +00:00
|
|
|
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
|
|
|
|
ARCH->setAddrPort(m_address, port);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
else {
|
|
|
|
// look up name
|
|
|
|
try {
|
|
|
|
m_address = ARCH->nameToAddr(hostname);
|
|
|
|
ARCH->setAddrPort(m_address, port);
|
|
|
|
}
|
|
|
|
catch (XArchNetworkNameUnknown&) {
|
|
|
|
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
|
|
|
}
|
|
|
|
catch (XArchNetworkNameNoAddress&) {
|
|
|
|
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
|
|
|
}
|
|
|
|
catch (XArchNetworkName&) {
|
|
|
|
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
|
|
|
}
|
2002-06-21 16:29:35 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
}
|
2002-06-21 16:29:35 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CNetworkAddress::~CNetworkAddress()
|
|
|
|
{
|
|
|
|
if (m_address != NULL) {
|
|
|
|
ARCH->closeAddr(m_address);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CNetworkAddress&
|
|
|
|
CNetworkAddress::operator=(const CNetworkAddress& addr)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchNetAddress newAddr = NULL;
|
|
|
|
if (addr.m_address != NULL) {
|
|
|
|
newAddr = ARCH->copyAddr(addr.m_address);
|
|
|
|
}
|
|
|
|
if (m_address != NULL) {
|
|
|
|
ARCH->closeAddr(m_address);
|
|
|
|
}
|
|
|
|
m_hostname = addr.m_hostname;
|
|
|
|
m_address = newAddr;
|
|
|
|
return *this;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
bool
|
|
|
|
CNetworkAddress::operator==(const CNetworkAddress& addr) const
|
|
|
|
{
|
|
|
|
return ARCH->isEqualAddr(m_address, addr.m_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CNetworkAddress::operator!=(const CNetworkAddress& addr) const
|
|
|
|
{
|
|
|
|
return !operator==(addr);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CNetworkAddress::isValid() const
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return (m_address != NULL);
|
2002-06-09 16:53:25 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
const CArchNetAddress&
|
2002-06-10 22:06:45 +00:00
|
|
|
CNetworkAddress::getAddress() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return m_address;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
CNetworkAddress::getPort() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return (m_address == NULL) ? 0 : ARCH->getAddrPort(m_address);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2002-06-09 16:53:25 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
|
|
|
CNetworkAddress::getHostname() const
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
|
|
|
return m_hostname;
|
|
|
|
}
|