summaryrefslogtreecommitdiff
path: root/Source/Core/Common/TraversalServer.cpp
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2018-06-08 00:41:08 +0900
committerGitHub <noreply@github.com>2018-06-08 00:41:08 +0900
commitde0fe0e210ad72b6ef10afa5e092d486de578557 (patch)
tree5e59440206a80f70d310a129ac05171900e21b42 /Source/Core/Common/TraversalServer.cpp
parentb51fbaa5b084edc638b66ebd3be8093f4887ee14 (diff)
parentfff1db973096a893475f3ec96ee6f28e35c4a67c (diff)
Merge pull request #6928 from leoetlino/random
Common: Add Random utilities
Diffstat (limited to 'Source/Core/Common/TraversalServer.cpp')
-rw-r--r--Source/Core/Common/TraversalServer.cpp31
1 files changed, 3 insertions, 28 deletions
diff --git a/Source/Core/Common/TraversalServer.cpp b/Source/Core/Common/TraversalServer.cpp
index 5da45a05fe..097418b143 100644
--- a/Source/Core/Common/TraversalServer.cpp
+++ b/Source/Core/Common/TraversalServer.cpp
@@ -20,6 +20,7 @@
#include <systemd/sd-daemon.h>
#endif
+#include "Common/Random.h"
#include "Common/TraversalProto.h"
#define DEBUG 0
@@ -114,7 +115,6 @@ struct hash<TraversalHostId>
}
static int sock;
-static int urandomFd;
static std::unordered_map<TraversalRequestId, OutgoingPacketInfo> outgoingPackets;
static std::unordered_map<TraversalHostId, EvictEntry<TraversalInetAddress>> connectedClients;
@@ -166,28 +166,11 @@ static sockaddr_in6 MakeSinAddr(const TraversalInetAddress& addr)
return result;
}
-static void GetRandomBytes(void* output, size_t size)
-{
- static u8 bytes[8192];
- static size_t bytesLeft = 0;
- if (bytesLeft < size)
- {
- ssize_t rv = read(urandomFd, bytes, sizeof(bytes));
- if (rv != sizeof(bytes))
- {
- perror("read from /dev/urandom");
- exit(1);
- }
- bytesLeft = sizeof(bytes);
- }
- memcpy(output, bytes + (bytesLeft -= size), size);
-}
-
static void GetRandomHostId(TraversalHostId* hostId)
{
char buf[9];
u32 num;
- GetRandomBytes(&num, sizeof(num));
+ Common::Random::Generate(&num, sizeof(num));
sprintf(buf, "%08x", num);
memcpy(hostId->data(), buf, 8);
}
@@ -215,7 +198,7 @@ static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr)
static TraversalPacket* AllocPacket(const sockaddr_in6& dest, TraversalRequestId misc = 0)
{
TraversalRequestId requestId;
- GetRandomBytes(&requestId, sizeof(requestId));
+ Common::Random::Generate(&requestId, sizeof(requestId));
OutgoingPacketInfo* info = &outgoingPackets[requestId];
info->dest = dest;
info->misc = misc;
@@ -376,14 +359,6 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
int main()
{
int rv;
-
- urandomFd = open("/dev/urandom", O_RDONLY);
- if (urandomFd < 0)
- {
- perror("open /dev/urandom");
- return 1;
- }
-
sock = socket(PF_INET6, SOCK_DGRAM, 0);
if (sock == -1)
{