diff --git a/src/utils.cpp b/src/utils.cpp index d70caeb72c..327a991854 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,6 +1,9 @@ #include "utils.h" #include "uint256.h" +#include +#include + // // Time support // We have our own epoch. @@ -123,6 +126,36 @@ DH* DH_der_load(const std::string& strDer) return d2i_DHparams(NULL, &pbuf, strDer.size()); } +// +// IP Port parsing +// +// <-- iPort: "" = -1 +bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort) +{ + boost::smatch smMatch; + bool bValid = false; + + static boost::regex reEndpoint("\\`\\s*(\\S+)(?:\\s+(\\d+))?\\s*\\'"); + + if (boost::regex_match(strSource, smMatch, reEndpoint)) + { + boost::system::error_code err; + std::string strIPRaw = smMatch[1]; + std::string strPortRaw = smMatch[2]; + + boost::asio::ip::address addrIP = boost::asio::ip::address::from_string(strIPRaw, err); + + bValid = !err; + if (bValid) + { + strIP = addrIP.to_string(); + iPort = strPortRaw.empty() ? -1 : boost::lexical_cast(strPortRaw); + } + } + + return bValid; +} + /* void intIPtoStr(int ip,std::string& retStr) { @@ -130,7 +163,7 @@ void intIPtoStr(int ip,std::string& retStr) bytes[0] = ip & 0xFF; bytes[1] = (ip >> 8) & 0xFF; bytes[2] = (ip >> 16) & 0xFF; - bytes[3] = (ip >> 24) & 0xFF; + bytes[3] = (ip >> 24) & 0xFF; retStr=str(boost::format("%d.%d.%d.%d") % bytes[3] % bytes[2] % bytes[1] % bytes[0] ); } @@ -145,7 +178,7 @@ int strIPtoInt(std::string& ipStr) #include //#include "Winsock2.h" -//#include +//#include // from: http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c // but we don't need to check the endianness uint64_t htobe64(uint64_t value) diff --git a/src/utils.h b/src/utils.h index a069c96fc7..302d5cfc37 100644 --- a/src/utils.h +++ b/src/utils.h @@ -8,9 +8,11 @@ #include "types.h" -#define nothing() do {} while (0) -#define fallthru() do {} while (0) -#define NUMBER(x) (sizeof(x)/sizeof((x)[0])) +#define nothing() do {} while (0) +#define fallthru() do {} while (0) +#define NUMBER(x) (sizeof(x)/sizeof((x)[0])) +#define ADDRESS(p) strHex(uint64( ((char*) p) - ((char*) 0))) +#define ADDRESS_SHARED(p) strHex(uint64( ((char*) (p).get()) - ((char*) 0))) #ifndef MAX #define MAX(x,y) ((x) < (y) ? (y) : (x)) @@ -36,7 +38,7 @@ int strIPtoInt(std::string& ipStr); template std::string strJoin(Iterator first, Iterator last, std::string strSeperator) { - std::ostringstream ossValues; + std::ostringstream ossValues; for (Iterator start = first; first != last; first++) { @@ -104,6 +106,8 @@ std::vector strUnHex(const std::string& strSrc); std::vector strCopy(const std::string& strSrc); std::string strCopy(const std::vector& vucSrc); +bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort); + DH* DH_der_load(const std::string& strDer); std::string DH_der_gen(int iKeyLength);