secure_rand() as replacement for client source port generation

This commit is contained in:
east 2016-01-02 15:37:44 +01:00
parent 5a2ee37891
commit f3f33904f7
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()
{
int i;
secure_random_fill(&i, sizeof(i));
return i;
}
#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);