Merge pull request #396 from east/securerand

secure_rand() as replacement for client source port generation
This commit is contained in:
H-M-H 2016-01-02 17:43:23 +01:00
commit eaafe4ca05
3 changed files with 21 additions and 2 deletions

View file

@ -2547,6 +2547,13 @@ void secure_random_fill(void *bytes, size_t length)
#endif #endif
} }
int secure_rand()
{
unsigned int i;
secure_random_fill(&i, sizeof(i));
return (int)(i%RAND_MAX);
}
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View file

@ -1383,6 +1383,12 @@ int secure_random_init();
*/ */
void secure_random_fill(void *bytes, size_t length); void secure_random_fill(void *bytes, size_t length);
/*
Function: secure_rand
Returns random int (replacement for rand()).
*/
int secure_rand();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -2582,10 +2582,10 @@ void CClient::Run()
} }
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
{ {
BindAddr.port = (rand() % 64511) + 1024; BindAddr.port = (secure_rand() % 64511) + 1024;
while(!m_NetClient[i].Open(BindAddr, 0)) while(!m_NetClient[i].Open(BindAddr, 0))
{ {
BindAddr.port = (rand() % 64511) + 1024; BindAddr.port = (secure_rand() % 64511) + 1024;
} }
} }
} }
@ -3255,6 +3255,12 @@ int main(int argc, const char **argv) // ignore_convention
dbg_enable_threaded(); dbg_enable_threaded();
#endif #endif
if(secure_random_init() != 0)
{
dbg_msg("secure", "could not initialize secure RNG");
return -1;
}
CClient *pClient = CreateClient(); CClient *pClient = CreateClient();
IKernel *pKernel = IKernel::Create(); IKernel *pKernel = IKernel::Create();
pKernel->RegisterInterface(pClient); pKernel->RegisterInterface(pClient);