replaced unweildy GAddress functions with wxSockAddressImpl class, similarly to GSocket -> wxSocketImpl transition; share more code between IPv4 and IPv6 branches

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57635 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-12-29 14:35:48 +00:00
parent f045c7f52d
commit c9bccf239c
7 changed files with 1220 additions and 2018 deletions

View File

@@ -0,0 +1,294 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/sockaddr.h
// Purpose: wxSockAddressImpl
// Author: Vadim Zeitlin
// Created: 2008-12-28
// RCS-ID: $Id$
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_SOCKADDR_H_
#define _WX_PRIVATE_SOCKADDR_H_
#ifdef __WXMSW__
#include "wx/msw/wrapwin.h"
#elif defined(__VMS__)
#include <socket.h>
struct sockaddr_un
{
u_char sun_len; /* sockaddr len including null */
u_char sun_family; /* AF_UNIX */
char sun_path[108]; /* path name (gag) */
};
#else // generic Unix
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#endif // platform
#include <stdlib.h> // for calloc()
// this is a wrapper for sockaddr_storage if it's available or just sockaddr
// otherwise
union wxSockAddressStorage
{
#if wxUSE_IPV6
sockaddr_storage addr_storage;
#endif
sockaddr addr;
};
// ----------------------------------------------------------------------------
// helpers for wxSockAddressImpl
// ----------------------------------------------------------------------------
// helper class mapping sockaddr_xxx types to corresponding AF_XXX values
//
// FIXME-VC6: we could leave the template undefined if not for VC6 which
// absolutely does need to have a generic version defining the
// template "interface" to compile the code below
template <class T> struct AddressFamily { enum { value = AF_UNSPEC }; };
template <> struct AddressFamily<sockaddr_in> { enum { value = AF_INET }; };
#if wxUSE_IPV6
template <> struct AddressFamily<sockaddr_in6> { enum { value = AF_INET6 }; };
#endif // wxUSE_IPV6
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
template <> struct AddressFamily<sockaddr_un> { enum { value = AF_UNIX }; };
#endif // wxHAS_UNIX_DOMAIN_SOCKETS
// ----------------------------------------------------------------------------
// wxSockAddressImpl
// ----------------------------------------------------------------------------
// Represents a socket endpoint, e.g. an (address, port) pair for PF_INET
// sockets. It can be initialized from an existing sockaddr struct and also
// provides access to sockaddr stored internally so that it can be easily used
// with e.g. connect(2).
//
// This class also performs (synchronous, hence potentially long) name lookups
// if necessary, i.e. if the host name strings don't contain addresses in
// numerical form (quad dotted for IPv4 or standard hexadecimal for IPv6).
// Notice that internally the potentially Unicode host names are encoded as
// UTF-8 before being passed to the lookup function but the host names should
// really be ASCII anyhow.
class wxSockAddressImpl
{
public:
// as this is passed to socket() it should be a PF_XXX and not AF_XXX (even
// though they're the same in practice)
enum Family
{
FAMILY_INET = PF_INET,
#if wxUSE_IPV6
FAMILY_INET6 = PF_INET6,
#endif
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
FAMILY_UNIX = PF_UNIX,
#endif
FAMILY_UNSPEC = PF_UNSPEC
};
// default ctor creates uninitialized object, use one of CreateXXX() below
wxSockAddressImpl()
{
InitUnspec();
}
// ctor from an existing sockaddr
wxSockAddressImpl(const sockaddr& addr, int len)
{
switch ( addr.sa_family )
{
case PF_INET:
#if wxUSE_IPV6
case PF_INET6:
#endif
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
case PF_UNIX:
#endif
m_family = static_cast<Family>(addr.sa_family);
break;
default:
wxFAIL_MSG( "unsupported socket address family" );
InitUnspec();
return;
}
InitFromSockaddr(addr, len);
}
// copy ctor and assignment operators
wxSockAddressImpl(const wxSockAddressImpl& other)
{
InitFromOther(other);
}
wxSockAddressImpl& operator=(const wxSockAddressImpl& other)
{
free(m_addr);
InitFromOther(other);
return *this;
}
// dtor frees the memory used by m_addr
~wxSockAddressImpl()
{
free(m_addr);
}
// reset the address to the initial uninitialized state
void Clear()
{
free(m_addr);
InitUnspec();
}
// initialize the address to be of specific address family, it must be
// currently uninitialized (you may call Clear() to achieve this)
void CreateINET();
void CreateINET6();
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
void CreateUnix();
#endif // wxHAS_UNIX_DOMAIN_SOCKETS
// simple accessors
Family GetFamily() const { return m_family; }
bool Is(Family family) const { return m_family == family; }
bool IsOk() const { return m_family != FAMILY_UNSPEC; }
const sockaddr *GetAddr() const { return m_addr; }
sockaddr *GetWritableAddr() { return m_addr; }
int GetLen() const { return m_len; }
// accessors for INET or INET6 address families
#if wxUSE_IPV6
#define CALL_IPV4_OR_6(func, args) \
Is(FAMILY_INET6) ? func##6(args) : func##4(args)
#define CALL_IPV4_OR_6_VOID(func) \
Is(FAMILY_INET6) ? func##6() : func##4()
#else
#define CALL_IPV4_OR_6(func, args) func##4(args)
#define CALL_IPV4_OR_6_VOID(func) func##4()
#endif // IPv6 support on/off
wxString GetHostName() const;
bool SetHostName(const wxString& name)
{
return CALL_IPV4_OR_6(SetHostName, (name));
}
wxUint16 GetPort() const { return CALL_IPV4_OR_6_VOID(GetPort); }
bool SetPort(wxUint16 port) { return CALL_IPV4_OR_6(SetPort, (port)); }
bool SetPortName(const wxString& name, const char *protocol);
bool SetToAnyAddress() { return CALL_IPV4_OR_6_VOID(SetToAnyAddress); }
#undef CALL_IPV4_OR_6
// accessors for INET addresses only
bool GetHostAddress(wxUint32 *address) const;
bool SetHostAddress(wxUint32 address);
bool SetToBroadcastAddress() { return SetHostAddress(INADDR_BROADCAST); }
// accessors for INET6 addresses only
#if wxUSE_IPV6
bool GetHostAddress(in6_addr *address) const;
bool SetHostAddress(const in6_addr& address);
#endif // wxUSE_IPV6
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
// methods valid for Unix address family addresses only
bool SetPath(const wxString& path);
wxString GetPath() const;
#endif // wxHAS_UNIX_DOMAIN_SOCKETS
private:
void DoAlloc(int len)
{
m_addr = static_cast<sockaddr *>(calloc(1, len));
m_len = len;
}
// FIXME-VC6: VC6 doesn't grok Foo<T>() call syntax so we need the extra
// dummy parameter of type T, use the macros in sckaddr.cpp to
// hide it
template <class T>
T *Alloc(T *)
{
DoAlloc(sizeof(T));
return reinterpret_cast<T *>(m_addr);
}
template <class T>
T *Get(T *) const
{
wxCHECK_MSG( static_cast<int>(m_family) == AddressFamily<T>::value,
NULL,
"socket address family mismatch" );
return reinterpret_cast<T *>(m_addr);
}
void InitUnspec()
{
m_family = FAMILY_UNSPEC;
m_addr = NULL;
m_len = 0;
}
void InitFromSockaddr(const sockaddr& addr, int len)
{
DoAlloc(len);
memcpy(m_addr, &addr, len);
}
void InitFromOther(const wxSockAddressImpl& other)
{
m_family = other.m_family;
if ( other.m_addr )
{
InitFromSockaddr(*other.m_addr, other.m_len);
}
else // no address to copy
{
m_addr = NULL;
m_len = 0;
}
}
// IPv4/6 implementations of public functions
bool SetHostName4(const wxString& name);
bool SetPort4(wxUint16 port);
wxUint16 GetPort4() const;
bool SetToAnyAddress4() { return SetHostAddress(INADDR_ANY); }
#if wxUSE_IPV6
bool SetHostName6(const wxString& name);
bool SetPort6(wxUint16 port);
wxUint16 GetPort6() const;
bool SetToAnyAddress6();
#endif // wxUSE_IPV6
Family m_family;
sockaddr *m_addr;
int m_len;
};
#endif // _WX_PRIVATE_SOCKADDR_H_

View File

@@ -42,6 +42,7 @@
#if wxUSE_SOCKETS
#include "wx/socket.h"
#include "wx/private/sckaddr.h"
#include <stddef.h>
@@ -90,23 +91,8 @@
#define SOCKET_ERROR (-1)
#endif
#if wxUSE_IPV6
typedef struct sockaddr_storage wxSockAddr;
#else
typedef struct sockaddr wxSockAddr;
#endif
enum GAddressType
{
wxSOCKET_NOFAMILY = 0,
wxSOCKET_INET,
wxSOCKET_INET6,
wxSOCKET_UNIX
};
typedef int wxSocketEventFlags;
struct GAddress;
class wxSocketImpl;
/*
@@ -200,16 +186,16 @@ public:
m_initialSendBufferSize = send;
}
wxSocketError SetLocal(GAddress *address);
wxSocketError SetPeer(GAddress *address);
wxSocketError SetLocal(const wxSockAddressImpl& address);
wxSocketError SetPeer(const wxSockAddressImpl& address);
// accessors
// ---------
bool IsServer() const { return m_server; }
GAddress *GetLocal();
GAddress *GetPeer();
const wxSockAddressImpl& GetLocal(); // non const as may update m_local
const wxSockAddressImpl& GetPeer() const { return m_peer; }
wxSocketError GetError() const { return m_error; }
bool IsOk() const { return m_error == wxSOCKET_NOERROR; }
@@ -303,8 +289,8 @@ public:
int m_initialRecvBufferSize;
int m_initialSendBufferSize;
GAddress *m_local;
GAddress *m_peer;
wxSockAddressImpl m_local,
m_peer;
wxSocketError m_error;
bool m_stream;
@@ -332,7 +318,7 @@ private:
// check that the socket wasn't created yet and that the given address
// (either m_local or m_peer depending on the socket kind) is valid and
// set m_error and return false if this is not the case
bool PreCreateCheck(GAddress *addr);
bool PreCreateCheck(const wxSockAddressImpl& addr);
// set the given socket option: this just wraps setsockopt(SOL_SOCKET)
int SetSocketOption(int optname, int optval)
@@ -378,79 +364,6 @@ private:
#include "wx/unix/private/sockunix.h"
#endif
/* GAddress */
// TODO: make GAddress a real class instead of this mix of C and C++
// Represents a socket endpoint, i.e. -- in spite of its name -- not an address
// but an (address, port) pair
struct GAddress
{
struct sockaddr *m_addr;
size_t m_len;
GAddressType m_family;
int m_realfamily;
wxSocketError m_error;
};
GAddress *GAddress_new();
GAddress *GAddress_copy(GAddress *address);
void GAddress_destroy(GAddress *address);
void GAddress_SetFamily(GAddress *address, GAddressType type);
GAddressType GAddress_GetFamily(GAddress *address);
/* The use of any of the next functions will set the address family to
* the specific one. For example if you use GAddress_INET_SetHostName,
* address family will be implicitly set to AF_INET.
*/
wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address);
wxSocketError GAddress_INET_SetAnyAddress(GAddress *address);
wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
unsigned long hostaddr);
wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
const char *protocol);
wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
size_t sbuf);
unsigned long GAddress_INET_GetHostAddress(GAddress *address);
unsigned short GAddress_INET_GetPort(GAddress *address);
wxSocketError _GAddress_translate_from(GAddress *address,
struct sockaddr *addr, int len);
wxSocketError _GAddress_translate_to (GAddress *address,
struct sockaddr **addr, int *len);
wxSocketError _GAddress_Init_INET(GAddress *address);
#if wxUSE_IPV6
wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname);
wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address);
wxSocketError GAddress_INET6_SetHostAddress(GAddress *address,
struct in6_addr hostaddr);
wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
const char *protocol);
wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port);
wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname,
size_t sbuf);
wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr);
unsigned short GAddress_INET6_GetPort(GAddress *address);
#endif // wxUSE_IPV6
// these functions are available under all platforms but only implemented under
// Unix ones, elsewhere they just return wxSOCKET_INVADDR
wxSocketError _GAddress_Init_UNIX(GAddress *address);
wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
#endif /* wxUSE_SOCKETS */
#endif /* _WX_PRIVATE_SOCKET_H_ */

View File

@@ -18,18 +18,19 @@
#include "wx/string.h"
struct GAddress;
class wxSockAddressImpl;
// Any socket address kind
class WXDLLIMPEXP_NET wxSockAddress : public wxObject
{
public:
enum
enum Family
{
IPV4 = 1,
IPV6 = 2,
UNIX = 3
} Addr;
NONE,
IPV4,
IPV6,
UNIX
};
wxSockAddress();
wxSockAddress(const wxSockAddress& other);
@@ -38,7 +39,7 @@ public:
wxSockAddress& operator=(const wxSockAddress& other);
virtual void Clear();
virtual int Type() = 0;
virtual Family Type() = 0;
// we need to be able to create copies of the addresses polymorphically
// (i.e. without knowing the exact address class)
@@ -46,11 +47,11 @@ public:
// implementation only, don't use
GAddress *GetAddress() const { return m_address; }
void SetAddress(GAddress *address);
const wxSockAddressImpl& GetAddress() const { return *m_impl; }
void SetAddress(const wxSockAddressImpl& address);
protected:
GAddress *m_address;
wxSockAddressImpl *m_impl;
private:
void Init();
@@ -61,24 +62,47 @@ private:
class WXDLLIMPEXP_NET wxIPaddress : public wxSockAddress
{
public:
wxIPaddress();
wxIPaddress(const wxIPaddress& other);
virtual ~wxIPaddress();
wxIPaddress() : wxSockAddress() { }
wxIPaddress(const wxIPaddress& other)
: wxSockAddress(other),
m_origHostname(other.m_origHostname)
{
}
virtual bool Hostname(const wxString& name) = 0;
virtual bool Service(const wxString& name) = 0;
virtual bool Service(unsigned short port) = 0;
bool operator==(const wxIPaddress& addr) const;
virtual bool LocalHost() = 0;
bool Hostname(const wxString& name);
bool Service(const wxString& name);
bool Service(unsigned short port);
bool LocalHost();
virtual bool IsLocalHost() const = 0;
virtual bool AnyAddress() = 0;
virtual bool BroadcastAddress() = 0;
bool AnyAddress();
virtual wxString IPAddress() const = 0;
virtual wxString Hostname() const = 0;
virtual unsigned short Service() const = 0;
wxString Hostname() const;
unsigned short Service() const;
wxString OrigHostname() const { return m_origHostname; }
protected:
// get m_impl initialized to the right family if it hadn't been done yet
wxSockAddressImpl& GetImpl();
const wxSockAddressImpl& GetImpl() const
{
return const_cast<wxIPaddress *>(this)->GetImpl();
}
// host name originally passed to Hostname()
wxString m_origHostname;
private:
// create the wxSockAddressImpl object of the correct family if it's
// currently uninitialized
virtual void DoInitImpl() = 0;
DECLARE_ABSTRACT_CLASS(wxIPaddress)
};
@@ -87,47 +111,29 @@ public:
class WXDLLIMPEXP_NET wxIPV4address : public wxIPaddress
{
public:
wxIPV4address();
wxIPV4address(const wxIPV4address& other);
virtual ~wxIPV4address();
bool operator==(const wxIPV4address& addr) const;
wxIPV4address() : wxIPaddress() { }
wxIPV4address(const wxIPV4address& other) : wxIPaddress(other) { }
// implement wxSockAddress pure virtuals:
virtual int Type() { return wxSockAddress::IPV4; }
virtual wxSockAddress *Clone() const;
virtual Family Type() { return IPV4; }
virtual wxSockAddress *Clone() const { return new wxIPV4address(*this); }
// implement wxIPaddress pure virtuals:
// handles the usual dotted quad format too
virtual bool Hostname(const wxString& name);
virtual bool Service(const wxString& name);
virtual bool Service(unsigned short port);
// localhost (127.0.0.1)
virtual bool LocalHost();
virtual bool IsLocalHost() const;
// any (0.0.0.0)
virtual bool AnyAddress();
// all (255.255.255.255)
virtual bool BroadcastAddress();
// a.b.c.d
virtual wxString IPAddress() const;
virtual wxString Hostname() const;
virtual unsigned short Service() const;
// IPv4-specific methods:
bool Hostname(unsigned long addr);
wxString OrigHostname() { return m_origHostname; }
bool BroadcastAddress();
using wxIPaddress::Hostname;
private:
wxString m_origHostname;
virtual void DoInitImpl();
DECLARE_DYNAMIC_CLASS(wxIPV4address)
};
@@ -139,78 +145,63 @@ private:
class WXDLLIMPEXP_NET wxIPV6address : public wxIPaddress
{
public:
wxIPV6address();
wxIPV6address(const wxIPV6address& other);
virtual ~wxIPV6address();
wxIPV6address() : wxIPaddress() { }
wxIPV6address(const wxIPV6address& other) : wxIPaddress(other) { }
// implement wxSockAddress pure virtuals:
virtual int Type() { return wxSockAddress::IPV6; }
virtual Family Type() { return IPV6; }
virtual wxSockAddress *Clone() const { return new wxIPV6address(*this); }
// implement wxIPaddress pure virtuals:
virtual bool Hostname(const wxString& name);
virtual bool Service(const wxString& name);
virtual bool Service(unsigned short port);
// localhost (0000:0000:0000:0000:0000:0000:0000:0001 (::1))
virtual bool LocalHost();
virtual bool IsLocalHost() const;
// any (0000:0000:0000:0000:0000:0000:0000:0000 (::))
virtual bool AnyAddress();
// all (?)
virtual bool BroadcastAddress();
// 3ffe:ffff:0100:f101:0210:a4ff:fee3:9566
virtual wxString IPAddress() const;
virtual wxString Hostname() const;
virtual unsigned short Service() const;
// IPv6-specific methods:
bool Hostname(unsigned char addr[16]);
using wxIPaddress::Hostname;
private:
wxString m_origHostname;
virtual void DoInitImpl();
DECLARE_DYNAMIC_CLASS(wxIPV6address)
};
#endif // wxUSE_IPV6
#if defined(__UNIX__) && !defined(__WINE__)
#include <sys/socket.h>
#ifndef __VMS__
#include <sys/un.h>
// Unix domain sockets are only available under, well, Unix
#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
#define wxHAS_UNIX_DOMAIN_SOCKETS
#endif
#ifdef wxHAS_UNIX_DOMAIN_SOCKETS
// A Unix domain socket address
class WXDLLIMPEXP_NET wxUNIXaddress : public wxSockAddress
{
public:
wxUNIXaddress();
wxUNIXaddress(const wxUNIXaddress& other);
virtual ~wxUNIXaddress();
wxUNIXaddress() : wxSockAddress() { }
wxUNIXaddress(const wxUNIXaddress& other) : wxSockAddress(other) { }
void Filename(const wxString& name);
wxString Filename();
wxString Filename() const;
virtual int Type() { return wxSockAddress::UNIX; }
virtual Family Type() { return UNIX; }
virtual wxSockAddress *Clone() const { return new wxUNIXaddress(*this); }
private:
struct sockaddr_un *m_addr;
wxSockAddressImpl& GetUNIX();
const wxSockAddressImpl& GetUNIX() const
{
return const_cast<wxUNIXaddress *>(this)->GetUNIX();
}
DECLARE_DYNAMIC_CLASS(wxUNIXaddress)
};
#endif // __UNIX__
#endif // wxHAS_UNIX_DOMAIN_SOCKETS
#endif // wxUSE_SOCKETS