1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-22 01:20:56 -05:00

qrtplib: fixed urandom calls

This commit is contained in:
f4exb 2018-03-29 19:55:03 +02:00
parent 75201ad303
commit b244222667
7 changed files with 128 additions and 50 deletions

View File

@ -70,7 +70,7 @@ RTPPacket::RTPPacket(RTPRawPacket &rawpack) :
RTPPacket::RTPPacket(
uint8_t payloadtype,
const void *payloaddata,
std::size_t payloadlen,
unsigned int payloadlen,
uint16_t seqnr,
uint32_t timestamp,
uint32_t ssrc,
@ -81,7 +81,7 @@ RTPPacket::RTPPacket(
uint16_t extensionid,
uint16_t extensionlen_numwords,
const void *extensiondata,
std::size_t maxpacksize) :
unsigned int maxpacksize) :
receivetime(0, 0)
{
Clear();
@ -106,7 +106,7 @@ RTPPacket::RTPPacket(
RTPPacket::RTPPacket(
uint8_t payloadtype,
const void *payloaddata,
std::size_t payloadlen,
unsigned int payloadlen,
uint16_t seqnr,
uint32_t timestamp,
uint32_t ssrc,
@ -118,7 +118,7 @@ RTPPacket::RTPPacket(
uint16_t extensionlen_numwords,
const void *extensiondata,
void *buffer,
std::size_t buffersize) :
unsigned int buffersize) :
receivetime(0, 0)
{
Clear();
@ -148,7 +148,7 @@ RTPPacket::RTPPacket(
int RTPPacket::ParseRawPacket(RTPRawPacket &rawpack)
{
uint8_t *packetbytes;
std::size_t packetlen;
unsigned int packetlen;
uint8_t payloadtype;
RTPHeader *rtpheader;
bool marker;
@ -264,7 +264,7 @@ uint32_t RTPPacket::GetCSRC(int num) const
int RTPPacket::BuildPacket(
uint8_t payloadtype,
const void *payloaddata,
std::size_t payloadlen,
unsigned int payloadlen,
uint16_t seqnr,
uint32_t timestamp,
uint32_t ssrc,
@ -276,7 +276,7 @@ int RTPPacket::BuildPacket(
uint16_t extensionlen_numwords,
const void *extensiondata,
void *buffer,
std::size_t maxsize)
unsigned int maxsize)
{
if (numcsrcs > RTP_MAXCSRCS)
return ERR_RTP_PACKET_TOOMANYCSRCS;
@ -287,11 +287,11 @@ int RTPPacket::BuildPacket(
return ERR_RTP_PACKET_BADPAYLOADTYPE;
packetlength = sizeof(RTPHeader);
packetlength += sizeof(uint32_t) * ((std::size_t) numcsrcs);
packetlength += sizeof(uint32_t) * ((unsigned int) numcsrcs);
if (gotextension)
{
packetlength += sizeof(RTPExtensionHeader);
packetlength += sizeof(uint32_t) * ((std::size_t) extensionlen_numwords);
packetlength += sizeof(uint32_t) * ((unsigned int) extensionlen_numwords);
}
packetlength += payloadlen;
@ -330,7 +330,7 @@ int RTPPacket::BuildPacket(
RTPPacket::ssrc = ssrc;
RTPPacket::payloadlength = payloadlen;
RTPPacket::extid = extensionid;
RTPPacket::extensionlength = ((std::size_t) extensionlen_numwords) * sizeof(uint32_t);
RTPPacket::extensionlength = ((unsigned int) extensionlen_numwords) * sizeof(uint32_t);
rtphdr = (RTPHeader *) packet;
rtphdr->version = RTP_VERSION;
@ -356,7 +356,7 @@ int RTPPacket::BuildPacket(
for (i = 0; i < numcsrcs; i++, curcsrc++)
*curcsrc = qToBigEndian(csrcs[i]);
payload = packet + sizeof(RTPHeader) + ((std::size_t) numcsrcs) * sizeof(uint32_t);
payload = packet + sizeof(RTPHeader) + ((unsigned int) numcsrcs) * sizeof(uint32_t);
if (gotextension)
{
RTPExtensionHeader *rtpexthdr = (RTPExtensionHeader *) payload;

View File

@ -68,13 +68,38 @@ public:
* \c maxpacksize. The arguments of the constructor are self-explanatory. Note that the size of a header
* extension is specified in a number of 32-bit words. A memory manager can be installed.
*/
RTPPacket(uint8_t payloadtype, const void *payloaddata, std::size_t payloadlen, uint16_t seqnr, uint32_t timestamp, uint32_t ssrc, bool gotmarker, uint8_t numcsrcs,
const uint32_t *csrcs, bool gotextension, uint16_t extensionid, uint16_t extensionlen_numwords, const void *extensiondata, std::size_t maxpacksize);
RTPPacket(uint8_t payloadtype,
const void *payloaddata,
unsigned int payloadlen,
uint16_t seqnr,
uint32_t timestamp,
uint32_t ssrc,
bool gotmarker,
uint8_t numcsrcs,
const uint32_t *csrcs,
bool gotextension,
uint16_t extensionid,
uint16_t extensionlen_numwords,
const void *extensiondata,
unsigned int maxpacksize);
/** This constructor is similar to the other constructor, but here data is stored in an external buffer
* \c buffer with size \c buffersize. */
RTPPacket(uint8_t payloadtype, const void *payloaddata, std::size_t payloadlen, uint16_t seqnr, uint32_t timestamp, uint32_t ssrc, bool gotmarker, uint8_t numcsrcs,
const uint32_t *csrcs, bool gotextension, uint16_t extensionid, uint16_t extensionlen_numwords, const void *extensiondata, void *buffer, std::size_t buffersize);
RTPPacket(uint8_t payloadtype,
const void *payloaddata,
unsigned int payloadlen,
uint16_t seqnr,
uint32_t timestamp,
uint32_t ssrc,
bool gotmarker,
uint8_t numcsrcs,
const uint32_t *csrcs,
bool gotextension,
uint16_t extensionid,
uint16_t extensionlen_numwords,
const void *extensiondata,
void *buffer,
unsigned int buffersize);
virtual ~RTPPacket()
{
@ -163,13 +188,13 @@ public:
}
/** Returns the length of the entire packet. */
std::size_t GetPacketLength() const
unsigned int GetPacketLength() const
{
return packetlength;
}
/** Returns the payload length. */
std::size_t GetPayloadLength() const
unsigned int GetPayloadLength() const
{
return payloadlength;
}
@ -187,7 +212,7 @@ public:
}
/** Returns the length of the header extension data. */
std::size_t GetExtensionLength() const
unsigned int GetExtensionLength() const
{
return extensionlength;
}
@ -204,8 +229,8 @@ public:
private:
void Clear();
int ParseRawPacket(RTPRawPacket &rawpack);
int BuildPacket(uint8_t payloadtype, const void *payloaddata, std::size_t payloadlen, uint16_t seqnr, uint32_t timestamp, uint32_t ssrc, bool gotmarker, uint8_t numcsrcs,
const uint32_t *csrcs, bool gotextension, uint16_t extensionid, uint16_t extensionlen_numwords, const void *extensiondata, void *buffer, std::size_t maxsize);
int BuildPacket(uint8_t payloadtype, const void *payloaddata, unsigned int payloadlen, uint16_t seqnr, uint32_t timestamp, uint32_t ssrc, bool gotmarker, uint8_t numcsrcs,
const uint32_t *csrcs, bool gotextension, uint16_t extensionid, uint16_t extensionlen_numwords, const void *extensiondata, void *buffer, unsigned int maxsize);
RTPEndian m_endian;
int error;
@ -216,11 +241,11 @@ private:
uint8_t payloadtype;
uint32_t extseqnr, timestamp, ssrc;
uint8_t *packet, *payload;
std::size_t packetlength, payloadlength;
unsigned int packetlength, payloadlength;
uint16_t extid;
uint8_t *extension;
std::size_t extensionlength;
unsigned int extensionlength;
bool externalbuffer;

View File

@ -41,9 +41,27 @@ namespace qrtplib
{
RTPPacketBuilder::RTPPacketBuilder(RTPRandom &r) :
rtprnd(r), lastwallclocktime(0, 0)
rtprnd(r),
maxpacksize(0),
buffer(0),
packetlength(0),
lastwallclocktime(0, 0)
{
init = false;
deftsset = false;
defptset = false;
defmarkset = false;
defaultmark = false;
defaulttimestampinc = 0;
ssrc = 0;
timestamp = 0;
seqnr = 0;
prevrtptimestamp = 0;
lastrtptimestamp = 0;
defaultpayloadtype = 0;
numcsrcs = 0;
numpayloadbytes = 0;
numpackets = 0;
timeinit.Dummy();
//std::cout << (void *)(&rtprnd) << std::endl;
@ -54,7 +72,7 @@ RTPPacketBuilder::~RTPPacketBuilder()
Destroy();
}
int RTPPacketBuilder::Init(std::size_t max)
int RTPPacketBuilder::Init(unsigned int max)
{
if (init)
return ERR_RTP_PACKBUILD_ALREADYINIT;
@ -66,6 +84,7 @@ int RTPPacketBuilder::Init(std::size_t max)
if (buffer == 0)
return ERR_RTP_OUTOFMEM;
packetlength = 0;
numpackets = 0;
CreateNewSSRC();
@ -87,7 +106,7 @@ void RTPPacketBuilder::Destroy()
init = false;
}
int RTPPacketBuilder::SetMaximumPacketSize(std::size_t max)
int RTPPacketBuilder::SetMaximumPacketSize(unsigned int max)
{
uint8_t *newbuf;
@ -161,6 +180,8 @@ uint32_t RTPPacketBuilder::CreateNewSSRC()
timestamp = rtprnd.GetRandom32();
seqnr = rtprnd.GetRandom16();
qDebug("RTPPacketBuilder::CreateNewSSRC: timestamp: %u", timestamp);
// p 38: the count SHOULD be reset if the sender changes its SSRC identifier
numpayloadbytes = 0;
numpackets = 0;
@ -186,7 +207,7 @@ uint32_t RTPPacketBuilder::CreateNewSSRC(RTPSources &sources)
return ssrc;
}
int RTPPacketBuilder::BuildPacket(const void *data, std::size_t len)
int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len)
{
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
@ -199,14 +220,14 @@ int RTPPacketBuilder::BuildPacket(const void *data, std::size_t len)
return PrivateBuildPacket(data, len, defaultpayloadtype, defaultmark, defaulttimestampinc, false);
}
int RTPPacketBuilder::BuildPacket(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc)
int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc)
{
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
return PrivateBuildPacket(data, len, pt, mark, timestampinc, false);
}
int RTPPacketBuilder::BuildPacketEx(const void *data, std::size_t len, uint16_t hdrextID, const void *hdrextdata, std::size_t numhdrextwords)
int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
{
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
@ -219,7 +240,7 @@ int RTPPacketBuilder::BuildPacketEx(const void *data, std::size_t len, uint16_t
return PrivateBuildPacket(data, len, defaultpayloadtype, defaultmark, defaulttimestampinc, true, hdrextID, hdrextdata, numhdrextwords);
}
int RTPPacketBuilder::BuildPacketEx(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, std::size_t numhdrextwords)
int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
{
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
@ -227,8 +248,8 @@ int RTPPacketBuilder::BuildPacketEx(const void *data, std::size_t len, uint8_t p
}
int RTPPacketBuilder::PrivateBuildPacket(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID, const void *hdrextdata,
std::size_t numhdrextwords)
int RTPPacketBuilder::PrivateBuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID, const void *hdrextdata,
unsigned int numhdrextwords)
{
RTPPacket p(pt, data, len, seqnr, timestamp, ssrc, mark, numcsrcs, csrcs, gotextension, hdrextID, (uint16_t) numhdrextwords, hdrextdata, buffer, maxpacksize);
int status = p.GetCreationError();
@ -255,6 +276,8 @@ int RTPPacketBuilder::PrivateBuildPacket(const void *data, std::size_t len, uint
timestamp += timestampinc;
seqnr++;
//qDebug("RTPPacketBuilder::PrivateBuildPacket: numpackets: %u timestamp: %u timestampinc: %u seqnr: %u", numpackets, timestamp, timestampinc, seqnr);
return 0;
}

View File

@ -65,7 +65,7 @@ public:
~RTPPacketBuilder();
/** Initializes the builder to only allow packets with a size below \c maxpacksize. */
int Init(std::size_t maxpacksize);
int Init(unsigned int maxpacksize);
/** Cleans up the builder. */
void Destroy();
@ -87,7 +87,7 @@ public:
}
/** Sets the maximum allowed packet size to \c maxpacksize. */
int SetMaximumPacketSize(std::size_t maxpacksize);
int SetMaximumPacketSize(unsigned int maxpacksize);
/** Adds a CSRC to the CSRC list which will be stored in the RTP packets. */
int AddCSRC(uint32_t csrc);
@ -103,14 +103,14 @@ public:
* and timestamp increment used will be those that have been set using the \c SetDefault
* functions below.
*/
int BuildPacket(const void *data, std::size_t len);
int BuildPacket(const void *data, unsigned int len);
/** Builds a packet with payload \c data and payload length \c len.
* Builds a packet with payload \c data and payload length \c len. The payload type will be
* set to \c pt, the marker bit to \c mark and after building this packet, the timestamp will
* be incremented with \c timestamp.
*/
int BuildPacket(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc);
int BuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc);
/** Builds a packet with payload \c data and payload length \c len.
* Builds a packet with payload \c data and payload length \c len. The payload type, marker
@ -119,7 +119,7 @@ public:
* \c hdrextID and data \c hdrextdata. The length of the header extension data is given by
* \c numhdrextwords which expresses the length in a number of 32-bit words.
*/
int BuildPacketEx(const void *data, std::size_t len, uint16_t hdrextID, const void *hdrextdata, std::size_t numhdrextwords);
int BuildPacketEx(const void *data, unsigned int len, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords);
/** Builds a packet with payload \c data and payload length \c len.
* Builds a packet with payload \c data and payload length \c len. The payload type will be set
@ -128,7 +128,7 @@ public:
* with identifier \c hdrextID and data \c hdrextdata. The length of the header extension
* data is given by \c numhdrextwords which expresses the length in a number of 32-bit words.
*/
int BuildPacketEx(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, std::size_t numhdrextwords);
int BuildPacketEx(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords);
/** Returns a pointer to the last built RTP packet data. */
uint8_t *GetPacket()
@ -139,7 +139,7 @@ public:
}
/** Returns the size of the last built RTP packet. */
std::size_t GetPacketLength()
unsigned int GetPacketLength()
{
if (!init)
return 0;
@ -237,13 +237,19 @@ public:
ssrc = s;
}
private:
int PrivateBuildPacket(const void *data, std::size_t len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID = 0, const void *hdrextdata = 0,
std::size_t numhdrextwords = 0);
int PrivateBuildPacket(
const void *data,
unsigned int len, uint8_t pt,
bool mark,
uint32_t timestampinc,
bool gotextension, uint16_t hdrextID = 0,
const void *hdrextdata = 0,
unsigned int numhdrextwords = 0);
RTPRandom &rtprnd;
std::size_t maxpacksize;
unsigned int maxpacksize;
uint8_t *buffer;
std::size_t packetlength;
unsigned int packetlength;
uint32_t numpayloadbytes;
uint32_t numpackets;

View File

@ -44,7 +44,7 @@
#include "export.h"
#define RTPRANDOM_2POWMIN63 1.08420217248550443400745280086994171142578125e-19
#define RTPRANDOM_2POWMIN63 1.08420217248550443400745280086994171142578125e-19
namespace qrtplib
{

View File

@ -31,6 +31,7 @@
*/
#include "rtprandomrand48.h"
#include <Qt>
namespace qrtplib
{
@ -72,6 +73,7 @@ uint32_t RTPRandomRand48::GetRandom32()
{
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
uint32_t x = (uint32_t) ((state >> 16) & 0xffffffffULL);
qDebug("RTPRandomRand48::GetRandom32: %u", x);
return x;
}

View File

@ -32,6 +32,8 @@
#include "rtprandomurandom.h"
#include "rtperrors.h"
#include <Qt>
#include <cstdio>
namespace qrtplib
{
@ -43,18 +45,22 @@ RTPRandomURandom::RTPRandomURandom()
RTPRandomURandom::~RTPRandomURandom()
{
if (device)
if (device) {
fclose(device);
}
}
int RTPRandomURandom::Init()
{
if (device)
if (device) {
return ERR_RTP_RTPRANDOMURANDOM_ALREADYOPEN;
}
device = fopen("/dev/urandom", "rb");
if (device == 0)
if (device == 0) {
return ERR_RTP_RTPRANDOMURANDOM_CANTOPEN;
}
return 0;
}
@ -62,12 +68,16 @@ int RTPRandomURandom::Init()
uint8_t RTPRandomURandom::GetRandom8()
{
if (!device)
{
qWarning("RTPRandomURandom::GetRandom8: no device");
return 0;
}
uint8_t value;
if (fread(&value, sizeof(uint8_t), 1, device) != sizeof(uint8_t))
if (fread(&value, 1, sizeof(uint8_t), device) != sizeof(uint8_t))
{
qWarning("RTPRandomURandom::GetRandom8: cannot read unsigned 8 bit value from device");
return 0;
}
@ -77,12 +87,16 @@ uint8_t RTPRandomURandom::GetRandom8()
uint16_t RTPRandomURandom::GetRandom16()
{
if (!device)
{
qWarning("RTPRandomURandom::GetRandom16: no device");
return 0;
}
uint16_t value;
if (fread(&value, sizeof(uint16_t), 1, device) != sizeof(uint16_t))
if (fread(&value, 1, sizeof(uint16_t), device) != sizeof(uint16_t))
{
qWarning("RTPRandomURandom::GetRandom16: cannot read unsigned 16 bit value from device");
return 0;
}
@ -92,12 +106,16 @@ uint16_t RTPRandomURandom::GetRandom16()
uint32_t RTPRandomURandom::GetRandom32()
{
if (!device)
{
qWarning("RTPRandomURandom::GetRandom32: no device");
return 0;
}
uint32_t value;
if (fread(&value, sizeof(uint32_t), 1, device) != sizeof(uint32_t))
if (fread(&value, 1, sizeof(uint32_t), device) != sizeof(uint32_t))
{
qWarning("RTPRandomURandom::GetRandom32: cannot read unsigned 32 bit value from device");
return 0;
}
@ -107,12 +125,16 @@ uint32_t RTPRandomURandom::GetRandom32()
double RTPRandomURandom::GetRandomDouble()
{
if (!device)
{
qWarning("RTPRandomURandom::GetRandomDouble: no device");
return 0;
}
uint64_t value;
if (fread(&value, sizeof(uint64_t), 1, device) != sizeof(uint64_t))
if (fread(&value, 1, sizeof(uint64_t), device) != sizeof(uint64_t))
{
qWarning("RTPRandomURandom::GetRandomDouble: cannot read unsigned 64 bit value from device");
return 0;
}