diff --git a/Garlic.cpp b/Garlic.cpp index ec52550c..7a9585a8 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -1,5 +1,5 @@ #include -#include +#include "I2PEndian.h" #include #include #include "ElGamal.h" diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 44862dc0..9395c40c 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -1,5 +1,5 @@ #include -#include +#include "I2PEndian.h" #include #include #include diff --git a/I2PEndian.cpp b/I2PEndian.cpp new file mode 100644 index 00000000..1fccf47f --- /dev/null +++ b/I2PEndian.cpp @@ -0,0 +1,81 @@ +#include "I2PEndian.h" + +// http://habrahabr.ru/post/121811/ +// http://codepad.org/2ycmkz2y + +#include "LittleBigEndian.h" + +uint16_t htobe16(uint16_t int16) +{ + BigEndian u16(int16); + return u16.raw_value; +} + +uint32_t htobe32(uint32_t int32) +{ + BigEndian u32(int32); + return u32.raw_value; +} + +uint64_t htobe64(uint64_t int64) +{ + BigEndian u64(int64); + return u64.raw_value; +} + +uint16_t be16toh(uint16_t big16) +{ + LittleEndian u16(big16); + return u16.raw_value; +} + +uint32_t be32toh(uint32_t big32) +{ + LittleEndian u32(big32); + return u32.raw_value; +} + +uint64_t be64toh(uint64_t big64) +{ + LittleEndian u64(big64); + return u64.raw_value; +} + +/* it can be used in Windows 8 +#include + +uint16_t htobe16(uint16_t int16) +{ + return htons(int16); +} + +uint32_t htobe32(uint32_t int32) +{ + return htonl(int32); +} + +uint64_t htobe64(uint64_t int64) +{ + // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx + //return htonll(int64); + return 0; +} + + +uint16_t be16toh(uint16_t big16) +{ + return ntohs(big16); +} + +uint32_t be32toh(uint32_t big32) +{ + return ntohl(big32); +} + +uint64_t be64toh(uint64_t big64) +{ + // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx + //return ntohll(big64); + return 0; +} +*/ \ No newline at end of file diff --git a/I2PEndian.h b/I2PEndian.h new file mode 100644 index 00000000..01ba73e8 --- /dev/null +++ b/I2PEndian.h @@ -0,0 +1,19 @@ +#ifndef I2PENDIAN_H__ +#define I2PENDIAN_H__ + +#ifndef _WIN32 +#include +#else +#include + +uint16_t htobe16(uint16_t int16); +uint32_t htobe32(uint32_t int32); +uint64_t htobe64(uint64_t int64); + +uint16_t be16toh(uint16_t big16); +uint32_t be32toh(uint32_t big32); +uint64_t be64toh(uint64_t big64); + +#endif + +#endif // I2PENDIAN_H__ \ No newline at end of file diff --git a/Identity.h b/Identity.h index 9e8b62ac..e17b27be 100644 --- a/Identity.h +++ b/Identity.h @@ -35,11 +35,15 @@ namespace data IdentHash (const uint8_t * hash) { memcpy (m_Hash, hash, 32); }; IdentHash (const IdentHash& ) = default; +#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it IdentHash (IdentHash&& ) = default; +#endif IdentHash () = default; IdentHash& operator= (const IdentHash& ) = default; +#ifndef _WIN32 IdentHash& operator= (IdentHash&& ) = default; +#endif uint8_t * operator()() { return m_Hash; }; const uint8_t * operator()() const { return m_Hash; }; diff --git a/LittleBigEndian.h b/LittleBigEndian.h new file mode 100644 index 00000000..ea879729 --- /dev/null +++ b/LittleBigEndian.h @@ -0,0 +1,242 @@ +// LittleBigEndian.h fixed for 64-bits added union +// + +#ifndef LITTLEBIGENDIAN_H +#define LITTLEBIGENDIAN_H + +// Determine Little-Endian or Big-Endian + +#define CURRENT_BYTE_ORDER (*(int *)"\x01\x02\x03\x04") +#define LITTLE_ENDIAN_BYTE_ORDER 0x04030201 +#define BIG_ENDIAN_BYTE_ORDER 0x01020304 +#define PDP_ENDIAN_BYTE_ORDER 0x02010403 + +#define IS_LITTLE_ENDIAN (CURRENT_BYTE_ORDER == LITTLE_ENDIAN_BYTE_ORDER) +#define IS_BIG_ENDIAN (CURRENT_BYTE_ORDER == BIG_ENDIAN_BYTE_ORDER) +#define IS_PDP_ENDIAN (CURRENT_BYTE_ORDER == PDP_ENDIAN_BYTE_ORDER) + +// Forward declaration + +template +struct LittleEndian; + +template +struct BigEndian; + +// Little-Endian template + +#pragma pack(push,1) +template +struct LittleEndian +{ + union + { + unsigned char bytes[sizeof(T)]; + T raw_value; + }; + + LittleEndian(T t = T()) + { + operator =(t); + } + + LittleEndian(const LittleEndian & t) + { + raw_value = t.raw_value; + } + + LittleEndian(const BigEndian & t) + { + for (unsigned i = 0; i < sizeof(T); i++) + bytes[i] = t.bytes[sizeof(T)-1-i]; + } + + operator const T() const + { + T t = T(); + for (unsigned i = 0; i < sizeof(T); i++) + t |= T(bytes[i]) << (i << 3); + return t; + } + + const T operator = (const T t) + { + for (unsigned i = 0; i < sizeof(T); i++) + bytes[i] = t >> (i << 3); + return t; + } + + // operators + + const T operator += (const T t) + { + return (*this = *this + t); + } + + const T operator -= (const T t) + { + return (*this = *this - t); + } + + const T operator *= (const T t) + { + return (*this = *this * t); + } + + const T operator /= (const T t) + { + return (*this = *this / t); + } + + const T operator %= (const T t) + { + return (*this = *this % t); + } + + LittleEndian operator ++ (int) + { + LittleEndian tmp(*this); + operator ++ (); + return tmp; + } + + LittleEndian & operator ++ () + { + for (unsigned i = 0; i < sizeof(T); i++) + { + ++bytes[i]; + if (bytes[i] != 0) + break; + } + return (*this); + } + + LittleEndian operator -- (int) + { + LittleEndian tmp(*this); + operator -- (); + return tmp; + } + + LittleEndian & operator -- () + { + for (unsigned i = 0; i < sizeof(T); i++) + { + --bytes[i]; + if (bytes[i] != (T)(-1)) + break; + } + return (*this); + } +}; +#pragma pack(pop) + +// Big-Endian template + +#pragma pack(push,1) +template +struct BigEndian +{ + union + { + unsigned char bytes[sizeof(T)]; + T raw_value; + }; + + BigEndian(T t = T()) + { + operator =(t); + } + + BigEndian(const BigEndian & t) + { + raw_value = t.raw_value; + } + + BigEndian(const LittleEndian & t) + { + for (unsigned i = 0; i < sizeof(T); i++) + bytes[i] = t.bytes[sizeof(T)-1-i]; + } + + operator const T() const + { + T t = T(); + for (unsigned i = 0; i < sizeof(T); i++) + t |= T(bytes[sizeof(T) - 1 - i]) << (i << 3); + return t; + } + + const T operator = (const T t) + { + for (unsigned i = 0; i < sizeof(T); i++) + bytes[sizeof(T) - 1 - i] = t >> (i << 3); + return t; + } + + // operators + + const T operator += (const T t) + { + return (*this = *this + t); + } + + const T operator -= (const T t) + { + return (*this = *this - t); + } + + const T operator *= (const T t) + { + return (*this = *this * t); + } + + const T operator /= (const T t) + { + return (*this = *this / t); + } + + const T operator %= (const T t) + { + return (*this = *this % t); + } + + BigEndian operator ++ (int) + { + BigEndian tmp(*this); + operator ++ (); + return tmp; + } + + BigEndian & operator ++ () + { + for (unsigned i = 0; i < sizeof(T); i++) + { + ++bytes[sizeof(T) - 1 - i]; + if (bytes[sizeof(T) - 1 - i] != 0) + break; + } + return (*this); + } + + BigEndian operator -- (int) + { + BigEndian tmp(*this); + operator -- (); + return tmp; + } + + BigEndian & operator -- () + { + for (unsigned i = 0; i < sizeof(T); i++) + { + --bytes[sizeof(T) - 1 - i]; + if (bytes[sizeof(T) - 1 - i] != (T)(-1)) + break; + } + return (*this); + } +}; +#pragma pack(pop) + +#endif // LITTLEBIGENDIAN_H \ No newline at end of file diff --git a/NTCPSession.cpp b/NTCPSession.cpp index be50e02b..3174e4f5 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "I2PEndian.h" #include #include #include diff --git a/NetDb.cpp b/NetDb.cpp index 395bd461..7aaa7f42 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -1,4 +1,4 @@ -#include +#include "I2PEndian.h" #include #include #include @@ -94,7 +94,7 @@ namespace data else // if no new DatabaseStore coming, explore it Explore (); - uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + uint64_t ts = i2p::util::GetSecondsSinceEpoch (); if (ts - lastTs >= 60) // save routers every minute { if (lastTs) @@ -169,7 +169,7 @@ namespace data { for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1) { - RouterInfo * r = new RouterInfo (it1->path ().c_str ()); + RouterInfo * r = new RouterInfo (it1->path ().c_str ()); // FIXME!!! path::value_type != char in boost 1_55 on Windows. How to solve? m_RouterInfos[r->GetIdentHash ()] = r; numRouters++; } @@ -277,7 +277,7 @@ namespace data decompressor.Put (buf + offset, size); decompressor.MessageEnd(); uint8_t uncompressed[2048]; - int uncomressedSize = decompressor.MaxRetrievable (); + size_t uncomressedSize = decompressor.MaxRetrievable (); decompressor.Get (uncompressed, uncomressedSize); AddRouterInfo (uncompressed, uncomressedSize); } diff --git a/README.md b/README.md index dc64c4a5..ab64cb4d 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,7 @@ i2pd i2p router for Linux written on C++ Requires gcc 4.6 and higher, boost 1.46 and higher, crypto++ + +on Windows + +Requires msvs2013, boost 1.46 and higher, crypto++ diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 45ab4c20..d804ecad 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "I2PEndian.h" #include #include #include diff --git a/Streaming.cpp b/Streaming.cpp index 73f2ff8a..4ca4b49b 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -1,4 +1,4 @@ -#include +#include "I2PEndian.h" #include #include #include "Log.h" diff --git a/Timestamp.h b/Timestamp.h index f70bb97f..d48cb164 100644 --- a/Timestamp.h +++ b/Timestamp.h @@ -20,7 +20,7 @@ namespace util std::chrono::system_clock::now().time_since_epoch()).count (); } - inline uint32_t GetSecondsSinceEpoch () + inline uint64_t GetSecondsSinceEpoch () { return std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count (); diff --git a/TransitTunnel.cpp b/TransitTunnel.cpp index d92b62a9..6c9a4f6f 100644 --- a/TransitTunnel.cpp +++ b/TransitTunnel.cpp @@ -1,5 +1,5 @@ #include -#include +#include "I2PEndian.h" #include "Log.h" #include "RouterContext.h" #include "I2NPProtocol.h" diff --git a/Tunnel.cpp b/Tunnel.cpp index 76f05a62..4e43e70d 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -1,4 +1,5 @@ -#include +#include "I2PEndian.h" +#include #include #include "RouterContext.h" #include "Log.h" @@ -259,9 +260,9 @@ namespace tunnel void Tunnels::Run () { - sleep (1); // wait for other parts are ready + boost::this_thread::sleep(boost::posix_time::seconds(1)); // wait for other parts are ready - uint32_t lastTs = 0; + uint64_t lastTs = 0; while (m_IsRunning) { try @@ -287,7 +288,7 @@ namespace tunnel msg = m_Queue.Get (); } - uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + uint64_t ts = i2p::util::GetSecondsSinceEpoch (); if (ts - lastTs >= 15) // manage tunnels every 15 seconds { ManageTunnels (); @@ -331,7 +332,7 @@ namespace tunnel void Tunnels::ManageOutboundTunnels () { - uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + uint64_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();) { if (ts > (*it)->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) @@ -377,7 +378,7 @@ namespace tunnel void Tunnels::ManageInboundTunnels () { - uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + uint64_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();) { if (ts > it->second->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) diff --git a/TunnelEndpoint.cpp b/TunnelEndpoint.cpp index 68e741a0..c5ec73b4 100644 --- a/TunnelEndpoint.cpp +++ b/TunnelEndpoint.cpp @@ -1,4 +1,4 @@ -#include +#include "I2PEndian.h" #include #include "Log.h" #include "I2NPProtocol.h" diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index 24f1a0ec..f19084b7 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -1,5 +1,5 @@ #include -#include +#include "I2PEndian.h" #include #include "Log.h" #include "RouterContext.h" diff --git a/i2p.cpp b/i2p.cpp index 884bdbc9..be007d15 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "Log.h" #include "base64.h" #include "Transports.h" @@ -19,7 +20,7 @@ int main( int, char** ) i2p::transports.Start (); i2p::tunnel::tunnels.Start (); - sleep (1000); + boost::this_thread::sleep(boost::posix_time::seconds(1000)); i2p::tunnel::tunnels.Stop (); i2p::transports.Stop (); i2p::data::netdb.Stop (); diff --git a/i2pd.sln b/i2pd.sln new file mode 100644 index 00000000..af0c8110 --- /dev/null +++ b/i2pd.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "i2pd", "i2pd.vcxproj", "{930568EC-31C9-406A-AD1C-9636DF5D8FAA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {930568EC-31C9-406A-AD1C-9636DF5D8FAA}.Debug|Win32.ActiveCfg = Debug|Win32 + {930568EC-31C9-406A-AD1C-9636DF5D8FAA}.Debug|Win32.Build.0 = Debug|Win32 + {930568EC-31C9-406A-AD1C-9636DF5D8FAA}.Release|Win32.ActiveCfg = Release|Win32 + {930568EC-31C9-406A-AD1C-9636DF5D8FAA}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/i2pd.vcxproj b/i2pd.vcxproj new file mode 100644 index 00000000..2c04ff58 --- /dev/null +++ b/i2pd.vcxproj @@ -0,0 +1,127 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {930568EC-31C9-406A-AD1C-9636DF5D8FAA} + i2pd + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + $(BOOST);$(CRYPTOPP);$(IncludePath) + $(BOOST)\stage\lib;$(CRYPTOPP)\cryptopp\Win32\Output\$(Configuration)\;$(LibraryPath) + + + .\boost;.\cryptopp;$(IncludePath) + .\stage-x86\lib;$(LibraryPath) + + + + Level3 + Disabled + true + MultiThreadedDebugDLL + _MBCS;_WIN32_WINNT=0x0501;%(PreprocessorDefinitions) + + + true + cryptlib.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/i2pd.vcxproj.filters b/i2pd.vcxproj.filters new file mode 100644 index 00000000..99d5e78f --- /dev/null +++ b/i2pd.vcxproj.filters @@ -0,0 +1,153 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file