diff --git a/Makefile b/Makefile index ec38678..cc45507 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ client.o: client.cpp client.h server.h exception.h config.h worker.h auth.h time server.o: server.cpp server.h client.h utility.h config.h worker.h auth.h time.h echo.h tun.h tun_dev.h g++ -c server.cpp $(CFLAGS) -auth.o: auth.cpp auth.h sha1.h +auth.o: auth.cpp auth.h sha1.h utility.h g++ -c auth.cpp $(CFLAGS) worker.o: worker.cpp worker.h tun.h exception.h time.h echo.h tun_dev.h diff --git a/auth.cpp b/auth.cpp index 4063990..2721c7a 100644 --- a/auth.cpp +++ b/auth.cpp @@ -19,21 +19,13 @@ #include "auth.h" #include "sha1.h" +#include "utility.h" -#include #include -bool Auth::randInitialized = false; - Auth::Auth(const char *passphrase) { this->passphrase = passphrase; - - if (!randInitialized) - { - srand(time(NULL)); - randInitialized = true; - } } Auth::Response Auth::getResponse(const Challenge &challenge) const @@ -59,7 +51,7 @@ Auth::Challenge Auth::generateChallenge(int length) const challenge.resize(length); for (int i = 0; i < length; i++) - challenge[i] = rand(); + challenge[i] = Utility::rand(); return challenge; } diff --git a/auth.h b/auth.h index 922d145..ce47638 100644 --- a/auth.h +++ b/auth.h @@ -43,8 +43,6 @@ public: protected: std::string passphrase; std::string challenge; - - static bool randInitialized; }; #endif diff --git a/utility.cpp b/utility.cpp index 99bd0b0..bfc1c9e 100644 --- a/utility.cpp +++ b/utility.cpp @@ -19,6 +19,8 @@ #include "utility.h" +#include + using namespace std; string Utility::formatIp(uint32_t ip) @@ -27,3 +29,14 @@ string Utility::formatIp(uint32_t ip) sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); return buffer; } + +int Utility::rand() +{ + static bool init = false; + if (!init) + { + init = true; + srand(time(NULL)); + } + return ::rand(); +} diff --git a/utility.h b/utility.h index a8d210e..d9b09f7 100644 --- a/utility.h +++ b/utility.h @@ -26,6 +26,7 @@ class Utility { public: static std::string formatIp(uint32_t ip); + static int rand(); }; #endif