Browse Source

support I2P peers with ECIES

pull/29/head
orignal 6 years ago
parent
commit
9e59c9b64e
  1. 2
      src/i2psam/i2psam.cpp
  2. 42
      src/netbase.cpp
  3. 2
      src/netbase.h

2
src/i2psam/i2psam.cpp

@ -24,7 +24,7 @@
#endif #endif
#define SAM_BUFSIZE 65536 #define SAM_BUFSIZE 65536
#define I2P_DESTINATION_SIZE 521 // EcDSA, GOST and EdDSA, actual size is 524 with trailing A== #define I2P_DESTINATION_SIZE 521 // EcDSA, GOST and EdDSA, actual size is 524 with trailing AAA== or AAQ==
namespace SAM namespace SAM
{ {

42
src/netbase.cpp

@ -581,13 +581,13 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
void CNetAddr::Init() void CNetAddr::Init()
{ {
memset(ip, 0, sizeof(ip)); memset(ip, 0, sizeof(ip));
memset(i2pDest, 0, NATIVE_I2P_DESTINATION_SIZE); i2pDest = "";
} }
void CNetAddr::SetIP(const CNetAddr& ipIn) void CNetAddr::SetIP(const CNetAddr& ipIn)
{ {
memcpy(ip, ipIn.ip, sizeof(ip)); memcpy(ip, ipIn.ip, sizeof(ip));
memcpy(i2pDest, ipIn.i2pDest, NATIVE_I2P_DESTINATION_SIZE); i2pDest = ipIn.i2pDest;
} }
@ -596,10 +596,15 @@ bool CNetAddr::SetSpecial(const std::string &strName)
const bool isBase32Addr = (strName.size() == NATIVE_I2P_B32ADDR_SIZE) && (strName.substr(strName.size() - 8, 8) == ".b32.i2p"); const bool isBase32Addr = (strName.size() == NATIVE_I2P_B32ADDR_SIZE) && (strName.substr(strName.size() - 8, 8) == ".b32.i2p");
const std::string addr = isBase32Addr ? I2PSession::Instance().namingLookup(strName) : strName; const std::string addr = isBase32Addr ? I2PSession::Instance().namingLookup(strName) : strName;
if ((addr.size() == NATIVE_I2P_DESTINATION_SIZE) && (addr.substr(addr.size() - 5, 5) == "AAA==")) { // last 5 symbols of b64-destination must be AAA== if ((addr.length() == NATIVE_I2P_DESTINATION_SIZE))
memcpy(i2pDest, addr.c_str(), NATIVE_I2P_DESTINATION_SIZE); {
return true; auto trailer = addr.substr(addr.size() - 5, 5);
} if (trailer == "AAA==" || trailer == "AAQ==")
{ // last 5 symbols of b64-destination must be AAA== (ElGamal) or AAQ== (ECIES)
i2pDest = addr;
return true;
}
}
return false; return false;
} }
@ -612,14 +617,14 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
{ {
memcpy(ip, pchIPv4, 12); memcpy(ip, pchIPv4, 12);
memcpy(ip+12, &ipv4Addr, 4); memcpy(ip+12, &ipv4Addr, 4);
memset(i2pDest, 0, NATIVE_I2P_DESTINATION_SIZE); i2pDest = "";
} }
#ifdef USE_IPV6 #ifdef USE_IPV6
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr) CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
{ {
memcpy(ip, &ipv6Addr, 16); memcpy(ip, &ipv6Addr, 16);
memset(i2pDest, 0, NATIVE_I2P_DESTINATION_SIZE); i2pDest = "";
} }
#endif #endif
@ -713,13 +718,12 @@ bool CNetAddr::IsRFC4843() const
bool CNetAddr::IsNativeI2P() const bool CNetAddr::IsNativeI2P() const
{ {
static const unsigned char pchAAA[] = {'A','A','A','=','='}; // EdDSA, TODO: return !i2pDest.empty ();
return (memcmp(i2pDest + NATIVE_I2P_DESTINATION_SIZE - sizeof(pchAAA), pchAAA, sizeof(pchAAA)) == 0);
} }
std::string CNetAddr::GetI2PDestination() const std::string CNetAddr::GetI2PDestination() const
{ {
return std::string(i2pDest, i2pDest + NATIVE_I2P_DESTINATION_SIZE); return i2pDest;
} }
bool CNetAddr::IsLocal() const bool CNetAddr::IsLocal() const
@ -834,17 +838,17 @@ std::string CNetAddr::ToString() const
bool operator==(const CNetAddr& a, const CNetAddr& b) bool operator==(const CNetAddr& a, const CNetAddr& b)
{ {
return (memcmp(a.ip, b.ip, 16) == 0 && memcmp(a.i2pDest, b.i2pDest, NATIVE_I2P_DESTINATION_SIZE) == 0); return (memcmp(a.ip, b.ip, 16) == 0 && a.i2pDest == b.i2pDest);
} }
bool operator!=(const CNetAddr& a, const CNetAddr& b) bool operator!=(const CNetAddr& a, const CNetAddr& b)
{ {
return (memcmp(a.ip, b.ip, 16) != 0 || memcmp(a.i2pDest, b.i2pDest, NATIVE_I2P_DESTINATION_SIZE) != 0); return (memcmp(a.ip, b.ip, 16) != 0 || a.i2pDest != b.i2pDest);
} }
bool operator<(const CNetAddr& a, const CNetAddr& b) bool operator<(const CNetAddr& a, const CNetAddr& b)
{ {
return (memcmp(a.ip, b.ip, 16) < 0 || (memcmp(a.ip, b.ip, 16) == 0 && memcmp(a.i2pDest, b.i2pDest, NATIVE_I2P_DESTINATION_SIZE) < 0)); return (memcmp(a.ip, b.ip, 16) < 0 || (memcmp(a.ip, b.ip, 16) == 0 && a.i2pDest < b.i2pDest));
} }
bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
@ -876,9 +880,9 @@ std::vector<unsigned char> CNetAddr::GetGroup() const
if (IsNativeI2P()) if (IsNativeI2P())
{ {
vchRet.resize(NATIVE_I2P_DESTINATION_SIZE + 1); vchRet.resize(i2pDest.length () + 1);
vchRet[0] = NET_NATIVE_I2P; vchRet[0] = NET_NATIVE_I2P;
memcpy(&vchRet[1], i2pDest, NATIVE_I2P_DESTINATION_SIZE); memcpy(&vchRet[1], i2pDest.c_str (), i2pDest.length ());
return vchRet; return vchRet;
} }
@ -938,7 +942,7 @@ std::vector<unsigned char> CNetAddr::GetGroup() const
uint64 CNetAddr::GetHash() const uint64 CNetAddr::GetHash() const
{ {
uint256 hash = IsNativeI2P() ? Hash(i2pDest, i2pDest + NATIVE_I2P_DESTINATION_SIZE) : Hash(&ip[0], &ip[16]); uint256 hash = IsNativeI2P() ? Hash(i2pDest.c_str (), i2pDest.c_str () + i2pDest.length ()) : Hash(&ip[0], &ip[16]);
uint64 nRet; uint64 nRet;
memcpy(&nRet, &hash, sizeof(nRet)); memcpy(&nRet, &hash, sizeof(nRet));
return nRet; return nRet;
@ -1160,8 +1164,8 @@ std::vector<unsigned char> CService::GetKey() const
std::vector<unsigned char> vKey; std::vector<unsigned char> vKey;
if (IsNativeI2P()) if (IsNativeI2P())
{ {
vKey.resize(NATIVE_I2P_DESTINATION_SIZE); vKey.resize(i2pDest.length ());
memcpy(&vKey[0], i2pDest, NATIVE_I2P_DESTINATION_SIZE); memcpy(&vKey[0], i2pDest.c_str (), i2pDest.length ());
return vKey; return vKey;
} }
vKey.resize(18); vKey.resize(18);

2
src/netbase.h

@ -39,7 +39,7 @@ class CNetAddr
{ {
protected: protected:
unsigned char ip[16]; // in network byte order unsigned char ip[16]; // in network byte order
unsigned char i2pDest[NATIVE_I2P_DESTINATION_SIZE]; // I2P Destination std::string i2pDest; // I2P Destination
public: public:
CNetAddr(); CNetAddr();

Loading…
Cancel
Save