From 822883f40f706cd19a92febb70f7f1a92479406d Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 1 Jul 2020 13:31:11 +0100 Subject: [PATCH] Use proper random numbers for the SSRC in the Kenwood network protocol. --- NXDNGateway/KenwoodNetwork.cpp | 10 ++++++++-- NXDNGateway/KenwoodNetwork.h | 2 ++ NXDNGateway/UDPSocket.cpp | 30 +----------------------------- NXDNGateway/UDPSocket.h | 4 +--- NXDNGateway/Version.h | 2 +- NXDNReflector/KenwoodNetwork.cpp | 10 ++++++++-- NXDNReflector/KenwoodNetwork.h | 2 ++ NXDNReflector/UDPSocket.cpp | 28 ---------------------------- NXDNReflector/UDPSocket.h | 4 +--- NXDNReflector/Version.h | 2 +- 10 files changed, 25 insertions(+), 69 deletions(-) diff --git a/NXDNGateway/KenwoodNetwork.cpp b/NXDNGateway/KenwoodNetwork.cpp index c5a4a20..28ab2e2 100644 --- a/NXDNGateway/KenwoodNetwork.cpp +++ b/NXDNGateway/KenwoodNetwork.cpp @@ -55,7 +55,8 @@ m_rtcpTimer(1000U, 0U, 200U), m_hangTimer(1000U, 5U), m_hangType(0U), m_hangSrc(0U), -m_hangDst(0U) +m_hangDst(0U), +m_random() { assert(localPort > 0U); assert(!rptAddress.empty()); @@ -64,6 +65,10 @@ m_hangDst(0U) m_sacch = new unsigned char[10U]; m_address = CUDPSocket::lookup(rptAddress); + + std::random_device rd; + std::mt19937 mt(rd()); + m_random = mt; } CKenwoodNetwork::~CKenwoodNetwork() @@ -86,7 +91,8 @@ bool CKenwoodNetwork::open() return false; } - m_ssrc = m_rtpSocket.getLocalAddress(); + std::uniform_int_distribution dist(0x00000001, 0xfffffffe); + m_ssrc = dist(m_random); return true; } diff --git a/NXDNGateway/KenwoodNetwork.h b/NXDNGateway/KenwoodNetwork.h index aa52d64..b0e1183 100644 --- a/NXDNGateway/KenwoodNetwork.h +++ b/NXDNGateway/KenwoodNetwork.h @@ -25,6 +25,7 @@ #include #include +#include class CKenwoodNetwork : public IRptNetwork { public: @@ -64,6 +65,7 @@ private: unsigned char m_hangType; unsigned short m_hangSrc; unsigned short m_hangDst; + std::mt19937 m_random; bool processIcomVoiceHeader(const unsigned char* data); bool processIcomVoiceData(const unsigned char* data); diff --git a/NXDNGateway/UDPSocket.cpp b/NXDNGateway/UDPSocket.cpp index c105b0b..3c1451b 100644 --- a/NXDNGateway/UDPSocket.cpp +++ b/NXDNGateway/UDPSocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2006-2016 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -261,31 +261,3 @@ void CUDPSocket::close() ::close(m_fd); #endif } - -unsigned long CUDPSocket::getLocalAddress() const -{ - unsigned long address = 0UL; - - char hostname[80U]; - int ret = ::gethostname(hostname, 80); - if (ret == -1) - return 0UL; - - struct hostent* phe = ::gethostbyname(hostname); - if (phe == NULL) - return 0UL; - - if (phe->h_addrtype != AF_INET) - return 0UL; - - for (unsigned int i = 0U; phe->h_addr_list[i] != NULL; i++) { - struct in_addr addr; - ::memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); - if (addr.s_addr != INADDR_LOOPBACK) { - address = addr.s_addr; - break; - } - } - - return address; -} diff --git a/NXDNGateway/UDPSocket.h b/NXDNGateway/UDPSocket.h index ffc3b92..e0af272 100644 --- a/NXDNGateway/UDPSocket.h +++ b/NXDNGateway/UDPSocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,8 +47,6 @@ public: void close(); - unsigned long getLocalAddress() const; - static in_addr lookup(const std::string& hostName); private: diff --git a/NXDNGateway/Version.h b/NXDNGateway/Version.h index 6d4de6d..dc7a96b 100644 --- a/NXDNGateway/Version.h +++ b/NXDNGateway/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200621"; +const char* VERSION = "20200701"; #endif diff --git a/NXDNReflector/KenwoodNetwork.cpp b/NXDNReflector/KenwoodNetwork.cpp index 8a872c0..134ea55 100644 --- a/NXDNReflector/KenwoodNetwork.cpp +++ b/NXDNReflector/KenwoodNetwork.cpp @@ -56,13 +56,18 @@ m_rtcpTimer(1000U, 0U, 200U), m_hangTimer(1000U, 5U), m_hangType(0U), m_hangSrc(0U), -m_hangDst(0U) +m_hangDst(0U), +m_random() { assert(!address.empty()); m_sacch = new unsigned char[10U]; m_address = CUDPSocket::lookup(address); + + std::random_device rd; + std::mt19937 mt(rd()); + m_random = mt; } CKenwoodNetwork::~CKenwoodNetwork() @@ -85,7 +90,8 @@ bool CKenwoodNetwork::open() return false; } - m_ssrc = m_rtpSocket.getLocalAddress(); + std::uniform_int_distribution dist(0x00000001, 0xfffffffe); + m_ssrc = dist(m_random); return true; } diff --git a/NXDNReflector/KenwoodNetwork.h b/NXDNReflector/KenwoodNetwork.h index 0543efe..e597802 100644 --- a/NXDNReflector/KenwoodNetwork.h +++ b/NXDNReflector/KenwoodNetwork.h @@ -25,6 +25,7 @@ #include #include +#include class CKenwoodNetwork : public ICoreNetwork { public: @@ -62,6 +63,7 @@ private: unsigned char m_hangType; unsigned short m_hangSrc; unsigned short m_hangDst; + std::mt19937 m_random; bool processIcomVoiceHeader(const unsigned char* data); bool processIcomVoiceData(const unsigned char* data); diff --git a/NXDNReflector/UDPSocket.cpp b/NXDNReflector/UDPSocket.cpp index d651ab8..eaa2e2f 100644 --- a/NXDNReflector/UDPSocket.cpp +++ b/NXDNReflector/UDPSocket.cpp @@ -260,31 +260,3 @@ void CUDPSocket::close() ::close(m_fd); #endif } - -unsigned long CUDPSocket::getLocalAddress() const -{ - unsigned long address = 0UL; - - char hostname[80U]; - int ret = ::gethostname(hostname, 80); - if (ret == -1) - return 0UL; - - struct hostent* phe = ::gethostbyname(hostname); - if (phe == NULL) - return 0UL; - - if (phe->h_addrtype != AF_INET) - return 0UL; - - for (unsigned int i = 0U; phe->h_addr_list[i] != NULL; i++) { - struct in_addr addr; - ::memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); - if (addr.s_addr != INADDR_LOOPBACK) { - address = addr.s_addr; - break; - } - } - - return address; -} diff --git a/NXDNReflector/UDPSocket.h b/NXDNReflector/UDPSocket.h index 4c21a43..3437b53 100644 --- a/NXDNReflector/UDPSocket.h +++ b/NXDNReflector/UDPSocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX + * Copyright (C) 2009-2011,2013,2015,2016 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,8 +47,6 @@ public: void close(); - unsigned long getLocalAddress() const; - static in_addr lookup(const std::string& hostName); private: diff --git a/NXDNReflector/Version.h b/NXDNReflector/Version.h index 0a67dd6..2cda46b 100644 --- a/NXDNReflector/Version.h +++ b/NXDNReflector/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200427"; +const char* VERSION = "20200701"; #endif