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"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CNetworkAddress
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CNetworkAddress::CNetworkAddress() :
|
|
|
|
m_port(0)
|
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
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<
|
|
|
|
struct sockaddr_in*>(&m_address);
|
2001-10-06 14:13:28 +00:00
|
|
|
inetAddress->sin_family = AF_INET;
|
2002-06-09 16:53:25 +00:00
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(m_port);
|
2001-10-06 14:13:28 +00:00
|
|
|
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
2001-10-25 22:17:17 +00:00
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-17 13:31:21 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(UInt16 port) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_port(port)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-06-09 16:53:25 +00:00
|
|
|
if (port == 0) {
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<
|
|
|
|
struct sockaddr_in*>(&m_address);
|
|
|
|
inetAddress->sin_family = AF_INET;
|
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(m_port);
|
|
|
|
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
|
|
|
}
|
|
|
|
|
2002-06-17 13:31:21 +00:00
|
|
|
CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_hostname(hostname_),
|
|
|
|
m_port(port)
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
|
|
|
CString hostname(m_hostname);
|
|
|
|
|
|
|
|
if (port == 0) {
|
|
|
|
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for port suffix
|
|
|
|
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,
|
|
|
|
m_hostname, m_port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// good port
|
|
|
|
port = static_cast<UInt16>(suffixPort);
|
|
|
|
hostname.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if hostname is empty then use wildcard address
|
2002-10-20 22:36:24 +00:00
|
|
|
struct sockaddr_in* inetAddress = reinterpret_cast<
|
2002-06-09 16:53:25 +00:00
|
|
|
struct sockaddr_in*>(&m_address);
|
2002-10-20 22:36:24 +00:00
|
|
|
if (hostname.empty()) {
|
2002-06-09 16:53:25 +00:00
|
|
|
inetAddress->sin_family = AF_INET;
|
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(port);
|
|
|
|
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
|
|
|
return;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-21 16:29:35 +00:00
|
|
|
// convert dot notation to address
|
2002-10-20 22:36:24 +00:00
|
|
|
if (CNetwork::inet_aton(hostname.c_str(), &inetAddress->sin_addr) != 0) {
|
|
|
|
inetAddress->sin_family = AF_INET;
|
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(port);
|
2002-06-21 16:29:35 +00:00
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// look up name
|
2002-10-17 20:56:28 +00:00
|
|
|
CNetwork::CHostInfo hostInfo;
|
|
|
|
switch (CNetwork::gethostbyname(&hostInfo, hostname.c_str())) {
|
|
|
|
case CNetwork::kHOST_OK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CNetwork::kHOST_NOT_FOUND:
|
|
|
|
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
|
|
|
|
|
|
|
case CNetwork::kNO_DATA:
|
|
|
|
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
|
|
|
|
|
|
|
case CNetwork::kNO_RECOVERY:
|
|
|
|
case CNetwork::kTRY_AGAIN:
|
|
|
|
default:
|
|
|
|
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 20:56:28 +00:00
|
|
|
inetAddress->sin_family = hostInfo.m_addressType;
|
2001-11-19 00:33:36 +00:00
|
|
|
inetAddress->sin_port = CNetwork::swaphtons(port);
|
2002-10-17 20:56:28 +00:00
|
|
|
memcpy(&inetAddress->sin_addr, hostInfo.m_addresses[0],
|
|
|
|
hostInfo.m_addressLength);
|
2001-10-25 22:17:17 +00:00
|
|
|
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetworkAddress::~CNetworkAddress()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CNetworkAddress::isValid() const
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
|
|
|
return (m_port != 0);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
const CNetwork::Address*
|
|
|
|
CNetworkAddress::getAddress() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return &m_address;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CNetwork::AddressLength
|
|
|
|
CNetworkAddress::getAddressLength() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return sizeof(m_address);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt16
|
|
|
|
CNetworkAddress::getPort() const
|
2002-06-09 16:53:25 +00:00
|
|
|
{
|
|
|
|
return m_port;
|
|
|
|
}
|