2V1-a: port the EterLib network layer and the Crypto++ packet cipher
NetStream/NetAddress/NetDevice/NetPacketHeaderMap and EterBase/cipher.cpp are mechanical copies. The winsock shim maps Winsock onto BSD sockets and keeps the Winsock semantics CNetworkStream relies on: select ignores nfds, a socket whose connect failed is not writable (40250 times out instead), EINPROGRESS reads as WSAEWOULDBLOCK, and SIGPIPE is ignored. port.net covers loopback connect, send/recv, peer close, the refused connect timeout and the client/server key agreement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
afe63bfa61
commit
cd3b8b506f
@@ -0,0 +1,107 @@
|
||||
#include "StdAfx.h"
|
||||
#include "NetAddress.h"
|
||||
|
||||
#ifndef VC_EXTRALEAN
|
||||
|
||||
bool CNetworkAddress::GetHostName(char* szName, int size)
|
||||
{
|
||||
if (gethostname(szName, size)==SOCKET_ERROR)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
CNetworkAddress::CNetworkAddress()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
CNetworkAddress::~CNetworkAddress()
|
||||
{
|
||||
}
|
||||
|
||||
CNetworkAddress::operator const SOCKADDR_IN&() const
|
||||
{
|
||||
return m_sockAddrIn;
|
||||
}
|
||||
|
||||
void CNetworkAddress::Clear()
|
||||
{
|
||||
memset(&m_sockAddrIn, 0, sizeof(m_sockAddrIn));
|
||||
m_sockAddrIn.sin_family=AF_INET;
|
||||
}
|
||||
|
||||
bool CNetworkAddress::IsIP(const char* c_szAddr)
|
||||
{
|
||||
if (c_szAddr[0]<'0' || c_szAddr[0]>'9')
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CNetworkAddress::Set(const char* c_szAddr, int port)
|
||||
{
|
||||
if (IsIP(c_szAddr))
|
||||
{
|
||||
SetIP(c_szAddr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!SetDNS(c_szAddr))
|
||||
return false;
|
||||
}
|
||||
|
||||
SetPort(port);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CNetworkAddress::SetLocalIP()
|
||||
{
|
||||
SetIP(INADDR_ANY);
|
||||
}
|
||||
|
||||
void CNetworkAddress::SetIP(DWORD ip)
|
||||
{
|
||||
m_sockAddrIn.sin_addr.s_addr=htonl(ip);
|
||||
}
|
||||
|
||||
void CNetworkAddress::SetIP(const char* c_szIP)
|
||||
{
|
||||
m_sockAddrIn.sin_addr.s_addr=inet_addr(c_szIP);
|
||||
}
|
||||
|
||||
bool CNetworkAddress::SetDNS(const char* c_szDNS)
|
||||
{
|
||||
HOSTENT* pHostent=gethostbyname(c_szDNS);
|
||||
if (!pHostent) return false;
|
||||
memcpy(&m_sockAddrIn.sin_addr, pHostent->h_addr, sizeof(m_sockAddrIn.sin_addr));
|
||||
return true;
|
||||
}
|
||||
|
||||
void CNetworkAddress::SetPort(int port)
|
||||
{
|
||||
m_sockAddrIn.sin_port = htons(port);
|
||||
}
|
||||
|
||||
int CNetworkAddress::GetSize()
|
||||
{
|
||||
return sizeof(m_sockAddrIn);
|
||||
}
|
||||
|
||||
DWORD CNetworkAddress::GetIP()
|
||||
{
|
||||
return ntohl(m_sockAddrIn.sin_addr.s_addr);
|
||||
}
|
||||
|
||||
void CNetworkAddress::GetIP(char* szIP, int len)
|
||||
{
|
||||
BYTE IPs[4];
|
||||
*((DWORD*)IPs)=m_sockAddrIn.sin_addr.s_addr;
|
||||
|
||||
_snprintf(szIP, len, "%d.%d.%d.%d", IPs[0], IPs[1], IPs[2], IPs[3]);
|
||||
}
|
||||
|
||||
int CNetworkAddress::GetPort()
|
||||
{
|
||||
return ntohs(m_sockAddrIn.sin_port);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "StdAfx.h"
|
||||
#include "NetDevice.h"
|
||||
|
||||
CNetworkDevice::CNetworkDevice()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
CNetworkDevice::~CNetworkDevice()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void CNetworkDevice::Initialize()
|
||||
{
|
||||
m_isWSA=false;
|
||||
}
|
||||
|
||||
void CNetworkDevice::Destroy()
|
||||
{
|
||||
if (m_isWSA)
|
||||
{
|
||||
WSACleanup();
|
||||
m_isWSA=false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CNetworkDevice::Create()
|
||||
{
|
||||
Destroy();
|
||||
|
||||
Initialize();
|
||||
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(1, 1), &wsaData)!=0)
|
||||
return false;
|
||||
|
||||
m_isWSA=true;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "StdAfx.h"
|
||||
#include "NetPacketHeaderMap.h"
|
||||
|
||||
void CNetworkPacketHeaderMap::Set(int header, TPacketType rPacketType)
|
||||
{
|
||||
m_headerMap[header] = rPacketType;
|
||||
}
|
||||
bool CNetworkPacketHeaderMap::Get(int header, TPacketType * pPacketType)
|
||||
{
|
||||
std::map<int, TPacketType>::iterator f=m_headerMap.find(header);
|
||||
|
||||
if (m_headerMap.end()==f)
|
||||
return false;
|
||||
|
||||
*pPacketType = f->second;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
CNetworkPacketHeaderMap::CNetworkPacketHeaderMap()
|
||||
{
|
||||
}
|
||||
|
||||
CNetworkPacketHeaderMap::~CNetworkPacketHeaderMap()
|
||||
{
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user