From 9846217fea7000aefc8b16d8a7a0453b0c9ef5b6 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Fri, 25 May 2018 11:42:05 -0700 Subject: [PATCH] better streamid generator --- Makefile | 20 ++++++++++---------- QnetDVAP.cpp | 10 +++++----- QnetDVRPTR.cpp | 6 ++++-- QnetLink.cpp | 8 ++------ QnetLink.h | 3 +++ QnetRemote.cpp | 5 +++-- QnetVoice.cpp | 5 +++-- Random.cpp | 38 ++++++++++++++++++++++++++++++++++++++ Random.h | 29 +++++++++++++++++++++++++++++ 9 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 Random.cpp create mode 100644 Random.h diff --git a/Makefile b/Makefile index 04ebe27..91bfeaa 100644 --- a/Makefile +++ b/Makefile @@ -43,23 +43,23 @@ all : $(PROGRAMS) qngateway : $(IRCOBJS) QnetGateway.o aprs.o g++ $(CPPFLAGS) -o qngateway QnetGateway.o aprs.o $(IRCOBJS) $(LDFLAGS) -pthread -qnlink : QnetLink.o - g++ $(CPPFLAGS) -o qnlink QnetLink.o $(LDFLAGS) -pthread +qnlink : QnetLink.o Random.o + g++ $(CPPFLAGS) -o qnlink QnetLink.o Random.o $(LDFLAGS) -pthread qnrelay : QnetRelay.o g++ $(CPPFLAGS) -o qnrelay QnetRelay.o $(LDFLAGS) -qndvap : QnetDVAP.o DVAPDongle.o $(DSTROBJS) - g++ $(CPPFLAGS) -o qndvap QnetDVAP.o DVAPDongle.o $(DSTROBJS) $(LDFLAGS) -pthread +qndvap : QnetDVAP.o DVAPDongle.o Random.o $(DSTROBJS) + g++ $(CPPFLAGS) -o qndvap QnetDVAP.o DVAPDongle.o Random.o $(DSTROBJS) $(LDFLAGS) -pthread -qndvrptr : QnetDVRPTR.o $(DSTROBJS) - g++ $(CPPFLAGS) -o qndvrptr QnetDVRPTR.o $(DSTROBJS) $(LDFLAGS) +qndvrptr : QnetDVRPTR.o $(DSTROBJS) Random.o + g++ $(CPPFLAGS) -o qndvrptr QnetDVRPTR.o Random.o $(DSTROBJS) $(LDFLAGS) -qnremote : QnetRemote.o - g++ $(CPPFLAGS) -o qnremote QnetRemote.o $(LDFLAGS) +qnremote : QnetRemote.o Random.o + g++ $(CPPFLAGS) -o qnremote QnetRemote.o Random.o $(LDFLAGS) -qnvoice : QnetVoice.o - g++ $(CPPFLAGS) -o qnvoice QnetVoice.o $(LDFLAGS) +qnvoice : QnetVoice.o Random.o + g++ $(CPPFLAGS) -o qnvoice QnetVoice.o Random.o $(LDFLAGS) %.o : %.cpp g++ $(CPPFLAGS) -MMD -MD -c $< -o $@ diff --git a/QnetDVAP.cpp b/QnetDVAP.cpp index 76648f0..540e97a 100644 --- a/QnetDVAP.cpp +++ b/QnetDVAP.cpp @@ -50,6 +50,7 @@ using namespace libconfig; #include "DVAPDongle.h" #include "QnetTypeDefs.h" +#include "Random.h" #define VERSION DVAP_VERSION #define CALL_SIZE 8 @@ -110,6 +111,7 @@ extern void dstar_dv_init(); extern int dstar_dv_decode(const unsigned char *d, int data[3]); CDVAPDongle dongle; +CRandom Random; static void calcPFCS(unsigned char *packet, unsigned char *pfcs) { @@ -469,7 +471,7 @@ static void readFrom20000() while ((space < 1) && keep_running) usleep(5); SDVAP_REGISTER dr; - stream_id_to_dvap = (rand_r(&aseed) % 65535U) + 1U; + stream_id_to_dvap = Random.NewStreamID(); dr.header = 0xa02f; dr.frame.streamid = stream_id_to_dvap; dr.frame.framepos = 0x80; @@ -734,7 +736,6 @@ static void RptrAckThread(SDVAP_ACK_ARG *parg) struct sigaction act; time_t tnow = 0; unsigned char silence[12] = { 0x4e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x70,0x4f,0x93 }; - unsigned int aseed_ack = 0; act.sa_handler = sig_catch; sigemptyset(&act.sa_mask); @@ -754,9 +755,8 @@ static void RptrAckThread(SDVAP_ACK_ARG *parg) sleep(DELAY_BEFORE); time(&tnow); - aseed_ack = tnow + getpid(); - uint16_t stream_id_to_dvap = (rand_r(&aseed_ack) % 65535U) + 1U; + uint16_t stream_id_to_dvap = Random.NewStreamID(); // HEADER while ((space < 1) && keep_running) @@ -1082,7 +1082,7 @@ static void ReadDVAPThread() net_buf.vpkt.dst_rptr_id = 0x00; net_buf.vpkt.snd_rptr_id = 0x01; net_buf.vpkt.snd_term_id = SND_TERM_ID; - streamid_raw = (rand_r(&aseed) % 65535U) + 1U; + streamid_raw = Random.NewStreamID(); net_buf.vpkt.streamid = streamid_raw; net_buf.vpkt.ctrl = 0x80; sequence = 0; diff --git a/QnetDVRPTR.cpp b/QnetDVRPTR.cpp index 670a646..84533bd 100644 --- a/QnetDVRPTR.cpp +++ b/QnetDVRPTR.cpp @@ -25,6 +25,8 @@ #include #include +#include "Random.h" + using namespace libconfig; #define VERSION DVRPTR_VERSION @@ -2676,7 +2678,7 @@ int main(int argc, const char **argv) dstar_dv_init(); time(&tNow); - srand(tNow + getpid()); + CRandom Random; time(&time_rqst); @@ -2932,7 +2934,7 @@ int main(int argc, const char **argv) Send_Network_Header[11] = 0x00; Send_Network_Header[12] = 0x01; Send_Network_Header[13] = SND_TERM_ID; - streamid_raw = (::rand() % 65535U) + 1U; + streamid_raw = Random.NewStreamID(); Send_Network_Header[14] = streamid_raw / 256U; Send_Network_Header[15] = streamid_raw % 256U; Send_Network_Header[16] = 0x80; diff --git a/QnetLink.cpp b/QnetLink.cpp index 4b032cb..d72c2e3 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -162,7 +162,6 @@ void CQnetLink::RptrAckThread(char *arg) char from_mod = arg[0]; char RADIO_ID[21]; memcpy(RADIO_ID, arg + 1, 21); - unsigned int aseed; time_t tnow = 0; unsigned char silence[12] = { 0x9E, 0x8D, 0x32, 0x88, 0x26, 0x1A, 0x3F, 0x61, 0xE8, 0x16, 0x29, 0xf5 }; struct sigaction act; @@ -180,9 +179,8 @@ void CQnetLink::RptrAckThread(char *arg) } time(&tnow); - aseed = tnow + pthread_self(); - u_int16_t streamid_raw = (::rand_r(&aseed) % 65535U) + 1U; + short int streamid_raw = Random.NewStreamID(); sleep(delay_before); @@ -3377,7 +3375,6 @@ void CQnetLink::AudioNotifyThread(char *arg) char mod; char *p = NULL; u_int16_t streamid_raw = 0; - unsigned int aseed; time_t tnow = 0; struct sigaction act; @@ -3453,7 +3450,6 @@ void CQnetLink::AudioNotifyThread(char *arg) } time(&tnow); - aseed = tnow + pthread_self(); while (keep_running) { /* 2 byte length */ @@ -3462,7 +3458,7 @@ void CQnetLink::AudioNotifyThread(char *arg) break; if (rlen == 56) - streamid_raw = (::rand_r(&aseed) % 65535U) + 1U; + streamid_raw = Random.NewStreamID(); else if (rlen == 27) ; else { diff --git a/QnetLink.h b/QnetLink.h index 887f95d..51a057a 100644 --- a/QnetLink.h +++ b/QnetLink.h @@ -23,6 +23,7 @@ #include #include "versions.h" #include "QnetTypeDefs.h" +#include "Random.h" using namespace libconfig; @@ -165,4 +166,6 @@ private: struct old_sid_tag { unsigned short sid; } old_sid[3]; + + CRandom Random; }; diff --git a/QnetRemote.cpp b/QnetRemote.cpp index c01894a..73fdb62 100644 --- a/QnetRemote.cpp +++ b/QnetRemote.cpp @@ -36,6 +36,7 @@ #include #include "QnetTypeDefs.h" +#include "Random.h" using namespace libconfig; @@ -283,7 +284,7 @@ int main(int argc, char *argv[]) RADIO_ID.resize(20, ' '); time(&tNow); - srand(tNow + getpid()); + CRandom Random; if (dst_open(IP_ADDRESS.c_str(), PORT)) return 1; @@ -306,7 +307,7 @@ int main(int argc, char *argv[]) pkt.vpkt.snd_term_id = 0x02; else pkt.vpkt.snd_term_id = 0x00; - streamid_raw = (unsigned short)(::rand() & 0xFFFF); + streamid_raw = Random.NewStreamID(); pkt.vpkt.streamid = htons(streamid_raw); pkt.vpkt.ctrl = 0x80; pkt.vpkt.hdr.flag[0] = pkt.vpkt.hdr.flag[1] = pkt.vpkt.hdr.flag[2] = 0x00; diff --git a/QnetVoice.cpp b/QnetVoice.cpp index 2f14134..b168634 100644 --- a/QnetVoice.cpp +++ b/QnetVoice.cpp @@ -36,6 +36,7 @@ #include #include "QnetTypeDefs.h" +#include "Random.h" using namespace libconfig; @@ -304,7 +305,7 @@ int main(int argc, char *argv[]) sleep(PLAY_WAIT); time(&tNow); - srand(tNow + getpid()); + CRandom Random; if (dst_open(IP_ADDRESS.c_str(), PORT)) return 1; @@ -318,7 +319,7 @@ int main(int argc, char *argv[]) break; } if (rlen == 56) - streamid_raw = (short)(::rand() & 0xFFFF); + streamid_raw = Random.NewStreamID(); else if (rlen == 27) ; else { diff --git a/Random.cpp b/Random.cpp new file mode 100644 index 0000000..06d2bc1 --- /dev/null +++ b/Random.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2018 by Thomas A. Early N7TAE + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include + +#include "Random.h" + +CRandom::CRandom() +{ + srandom(getpid()); +} + +CRandom::~CRandom() {} + +short CRandom::NewStreamID() +{ + short r = 0; + while (0 == r) + r = 0xffff & random(); + return r; +} diff --git a/Random.h b/Random.h new file mode 100644 index 0000000..cbda881 --- /dev/null +++ b/Random.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018 by Thomas A. Early N7TAE + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +class CRandom +{ +public: + CRandom(); + + ~CRandom(); + + short NewStreamID(); +};