added Utility::rand()

This commit is contained in:
Friedrich Schöller 2009-07-24 01:40:39 +02:00
parent 735b71804d
commit 08e3d4fae3
5 changed files with 17 additions and 13 deletions

View File

@ -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 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) 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) g++ -c auth.cpp $(CFLAGS)
worker.o: worker.cpp worker.h tun.h exception.h time.h echo.h tun_dev.h worker.o: worker.cpp worker.h tun.h exception.h time.h echo.h tun_dev.h

View File

@ -19,21 +19,13 @@
#include "auth.h" #include "auth.h"
#include "sha1.h" #include "sha1.h"
#include "utility.h"
#include <stdlib.h>
#include <arpa/inet.h> #include <arpa/inet.h>
bool Auth::randInitialized = false;
Auth::Auth(const char *passphrase) Auth::Auth(const char *passphrase)
{ {
this->passphrase = passphrase; this->passphrase = passphrase;
if (!randInitialized)
{
srand(time(NULL));
randInitialized = true;
}
} }
Auth::Response Auth::getResponse(const Challenge &challenge) const Auth::Response Auth::getResponse(const Challenge &challenge) const
@ -59,7 +51,7 @@ Auth::Challenge Auth::generateChallenge(int length) const
challenge.resize(length); challenge.resize(length);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
challenge[i] = rand(); challenge[i] = Utility::rand();
return challenge; return challenge;
} }

2
auth.h
View File

@ -43,8 +43,6 @@ public:
protected: protected:
std::string passphrase; std::string passphrase;
std::string challenge; std::string challenge;
static bool randInitialized;
}; };
#endif #endif

View File

@ -19,6 +19,8 @@
#include "utility.h" #include "utility.h"
#include <stdlib.h>
using namespace std; using namespace std;
string Utility::formatIp(uint32_t ip) 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); sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
return buffer; return buffer;
} }
int Utility::rand()
{
static bool init = false;
if (!init)
{
init = true;
srand(time(NULL));
}
return ::rand();
}

View File

@ -26,6 +26,7 @@ class Utility
{ {
public: public:
static std::string formatIp(uint32_t ip); static std::string formatIp(uint32_t ip);
static int rand();
}; };
#endif #endif