mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-11 06:34:36 +00:00
Preliminary IPV6 Support
This commit is contained in:
parent
1a851f167d
commit
704577e051
22 changed files with 1712 additions and 592 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -31,6 +31,8 @@
|
|||
#define MAXPACKETSIZE 1500
|
||||
#endif
|
||||
|
||||
#define TORQUE_NET_DEFAULT_MULTICAST_ADDRESS "ff04::7467::656E::6574::776B"
|
||||
|
||||
typedef S32 NetConnectionId;
|
||||
|
||||
/// Generic network address
|
||||
|
|
@ -41,18 +43,130 @@ struct NetAddress
|
|||
S32 type; ///< Type of address (IPAddress currently)
|
||||
|
||||
/// Acceptable NetAddress types.
|
||||
enum
|
||||
enum Type
|
||||
{
|
||||
IPAddress,
|
||||
IPV6Address,
|
||||
|
||||
IPBroadcastAddress,
|
||||
IPV6MulticastAddress
|
||||
};
|
||||
|
||||
U8 netNum[4]; ///< For IP: sin_addr<br>
|
||||
U8 nodeNum[6]; ///< For IP: Not used.<br>
|
||||
U16 port; ///< For IP: sin_port<br>
|
||||
union
|
||||
{
|
||||
struct {
|
||||
U8 netNum[4];
|
||||
} ipv4;
|
||||
|
||||
struct {
|
||||
U8 netNum[16];
|
||||
U32 netFlow;
|
||||
U32 netScope;
|
||||
} ipv6;
|
||||
|
||||
struct {
|
||||
U8 netNum[16];
|
||||
U8 netFlow[4];
|
||||
U8 netScope[4];
|
||||
} ipv6_raw;
|
||||
|
||||
} address;
|
||||
|
||||
U16 port;
|
||||
|
||||
bool isSameAddress(const NetAddress &other) const
|
||||
{
|
||||
if (type != other.type)
|
||||
return false;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NetAddress::IPAddress:
|
||||
return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
|
||||
break;
|
||||
case NetAddress::IPV6Address:
|
||||
return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
|
||||
break;
|
||||
case NetAddress::IPBroadcastAddress:
|
||||
return true;
|
||||
break;
|
||||
case NetAddress::IPV6MulticastAddress:
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isSameAddressAndPort(const NetAddress &other) const
|
||||
{
|
||||
if (type != other.type)
|
||||
return false;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NetAddress::IPAddress:
|
||||
return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0) && other.port == port;
|
||||
break;
|
||||
case NetAddress::IPV6Address:
|
||||
return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0) && other.port == port;
|
||||
break;
|
||||
case NetAddress::IPBroadcastAddress:
|
||||
return true;
|
||||
break;
|
||||
case NetAddress::IPV6MulticastAddress:
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isEqual(const NetAddress &other) const
|
||||
{
|
||||
if (type != other.type)
|
||||
return false;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NetAddress::IPAddress:
|
||||
return other.port == port && (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
|
||||
break;
|
||||
case NetAddress::IPV6Address:
|
||||
return other.port == port && other.address.ipv6.netFlow == address.ipv6.netFlow && other.address.ipv6.netScope == address.ipv6.netScope && (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
|
||||
break;
|
||||
case NetAddress::IPBroadcastAddress:
|
||||
return other.port == port;
|
||||
break;
|
||||
case NetAddress::IPV6MulticastAddress:
|
||||
return other.port == port;
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
U32 getHash() const;
|
||||
};
|
||||
|
||||
typedef S32 NetSocket;
|
||||
const NetSocket InvalidSocket = -1;
|
||||
class NetSocket
|
||||
{
|
||||
protected:
|
||||
S32 mHandle;
|
||||
|
||||
public:
|
||||
NetSocket() : mHandle(-1) { ; }
|
||||
|
||||
inline void setHandle(S32 handleId) { mHandle = handleId; }
|
||||
inline S32 getHandle() const { return mHandle; }
|
||||
inline U32 getHash() const { return mHandle; }
|
||||
|
||||
bool operator==(const NetSocket &other) const { return mHandle == other.mHandle; }
|
||||
bool operator!=(const NetSocket &other) const { return mHandle != other.mHandle; }
|
||||
|
||||
static NetSocket fromHandle(S32 handleId) { NetSocket ret; ret.mHandle = handleId; return ret; }
|
||||
static NetSocket INVALID;
|
||||
};
|
||||
|
||||
/// void event(NetSocket sock, U32 state)
|
||||
typedef JournaledSignal<void(NetSocket,U32)> ConnectionNotifyEvent;
|
||||
|
|
@ -76,7 +190,8 @@ struct Net
|
|||
InvalidPacketProtocol,
|
||||
WouldBlock,
|
||||
NotASocket,
|
||||
UnknownError
|
||||
UnknownError,
|
||||
NeedHostLookup
|
||||
};
|
||||
|
||||
enum ConnectionState {
|
||||
|
|
@ -87,12 +202,6 @@ struct Net
|
|||
Disconnected
|
||||
};
|
||||
|
||||
enum Protocol
|
||||
{
|
||||
UDPProtocol,
|
||||
TCPProtocol
|
||||
};
|
||||
|
||||
static const S32 MaxPacketDataSize = MAXPACKETSIZE;
|
||||
|
||||
static ConnectionNotifyEvent smConnectionNotify;
|
||||
|
|
@ -100,6 +209,10 @@ struct Net
|
|||
static ConnectionReceiveEvent smConnectionReceive;
|
||||
static PacketReceiveEvent smPacketReceive;
|
||||
|
||||
static bool smMulticastEnabled;
|
||||
static bool smIpv4Enabled;
|
||||
static bool smIpv6Enabled;
|
||||
|
||||
static bool init();
|
||||
static void shutdown();
|
||||
|
||||
|
|
@ -116,13 +229,13 @@ struct Net
|
|||
|
||||
// Reliable net functions (TCP)
|
||||
// all incoming messages come in on the Connected* events
|
||||
static NetSocket openListenPort(U16 port);
|
||||
static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress);
|
||||
static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc.
|
||||
static void closeConnectTo(NetSocket socket);
|
||||
static Error sendtoSocket(NetSocket socket, const U8 *buffer, S32 bufferSize);
|
||||
|
||||
static bool compareAddresses(const NetAddress *a1, const NetAddress *a2);
|
||||
static bool stringToAddress(const char *addressString, NetAddress *address);
|
||||
static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0);
|
||||
static void addressToString(const NetAddress *address, char addressString[256]);
|
||||
|
||||
// lower level socked based network functions
|
||||
|
|
@ -136,15 +249,27 @@ struct Net
|
|||
static Error listen(NetSocket socket, S32 maxConcurrentListens);
|
||||
static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress);
|
||||
|
||||
static Error bind(NetSocket socket, U16 port);
|
||||
static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false);
|
||||
static Error setBufferSize(NetSocket socket, S32 bufferSize);
|
||||
static Error setBroadcast(NetSocket socket, bool broadcastEnable);
|
||||
static Error setBlocking(NetSocket socket, bool blockingIO);
|
||||
|
||||
/// Gets the desired default listen address for a specified address type
|
||||
static bool getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false);
|
||||
static void getIdealListenAddress(NetAddress *address);
|
||||
|
||||
// Multicast for ipv6 local net browsing
|
||||
static void enableMulticast();
|
||||
static void disableMulticast();
|
||||
static bool isMulticastEnabled();
|
||||
|
||||
// Protocol state
|
||||
static bool isAddressTypeAvailable(NetAddress::Type addressType);
|
||||
|
||||
private:
|
||||
static void process();
|
||||
static void processListenSocket(NetSocket socket);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ struct NetAsync::NameLookupRequest
|
|||
|
||||
NameLookupRequest()
|
||||
{
|
||||
sock = InvalidSocket;
|
||||
sock = NetSocket::INVALID;
|
||||
remoteAddr[0] = 0;
|
||||
out_h_addr[0] = 0;
|
||||
out_h_length = -1;
|
||||
|
|
@ -81,9 +81,12 @@ protected:
|
|||
virtual void execute()
|
||||
{
|
||||
#ifndef TORQUE_OS_XENON
|
||||
|
||||
NetAddress address;
|
||||
Net::Error error = Net::stringToAddress(mRequest.remoteAddr, &address, true);
|
||||
|
||||
// do it
|
||||
struct hostent* hostent = gethostbyname(mRequest.remoteAddr);
|
||||
if (hostent == NULL)
|
||||
if (error != Net::NoError)
|
||||
{
|
||||
// oh well! leave the lookup data unmodified (h_length) should
|
||||
// still be -1 from initialization
|
||||
|
|
@ -94,9 +97,9 @@ protected:
|
|||
// copy the stuff we need from the hostent
|
||||
dMemset(mRequest.out_h_addr, 0,
|
||||
sizeof(mRequest.out_h_addr));
|
||||
dMemcpy(mRequest.out_h_addr, hostent->h_addr, hostent->h_length);
|
||||
dMemcpy(mRequest.out_h_addr, &address, sizeof(address));
|
||||
|
||||
mRequest.out_h_length = hostent->h_length;
|
||||
mRequest.out_h_length = sizeof(address);
|
||||
mRequest.complete = true;
|
||||
}
|
||||
#else
|
||||
|
|
@ -159,7 +162,7 @@ void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket)
|
|||
ThreadPool::GLOBAL().queueWorkItem( workItem );
|
||||
}
|
||||
|
||||
bool NetAsync::checkLookup(NetSocket socket, char* out_h_addr,
|
||||
bool NetAsync::checkLookup(NetSocket socket, void* out_h_addr,
|
||||
S32* out_h_length, S32 out_h_addr_size)
|
||||
{
|
||||
bool found = false;
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class NetAsync
|
|||
// out_h_length will be set appropriately. if out_h_length is -1, then
|
||||
// name could not be resolved. otherwise, it provides the number of
|
||||
// address bytes copied into out_h_addr.
|
||||
bool checkLookup(NetSocket socket, char* out_h_addr, int* out_h_length, S32 out_h_addr_size);
|
||||
bool checkLookup(NetSocket socket, void* out_h_addr, int* out_h_length, S32 out_h_addr_size);
|
||||
};
|
||||
|
||||
// the global net async object
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ struct TcpHandle
|
|||
else
|
||||
{
|
||||
Process::requestShutdown();
|
||||
mSocket = NULL;
|
||||
mSocket = NetSocket::INVALID;
|
||||
ASSERT_EQ(Net::Disconnected, state)
|
||||
<< "Ended with a network error!";
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ TEST(Net, TCPRequest)
|
|||
{
|
||||
TcpHandle handler;
|
||||
|
||||
handler.mSocket = InvalidSocket;
|
||||
handler.mSocket = NetSocket::INVALID;
|
||||
handler.mDataReceived = 0;
|
||||
|
||||
// Hook into the signals.
|
||||
|
|
@ -119,7 +119,7 @@ struct JournalHandle
|
|||
else
|
||||
{
|
||||
Process::requestShutdown();
|
||||
mSocket = NULL;
|
||||
mSocket = NetSocket::INVALID;
|
||||
ASSERT_EQ(Net::Disconnected, state)
|
||||
<< "Ended with a network error!";
|
||||
}
|
||||
|
|
@ -135,7 +135,7 @@ struct JournalHandle
|
|||
|
||||
void makeRequest()
|
||||
{
|
||||
mSocket = InvalidSocket;
|
||||
mSocket = NetSocket::INVALID;
|
||||
mDataReceived = 0;
|
||||
|
||||
// Hook into the signals.
|
||||
|
|
@ -175,4 +175,4 @@ TEST(Net, JournalTCPRequest)
|
|||
<< "Didn't get same data back from journal playback.";
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue