Skip to content

Commit

Permalink
Reject universes sent to incorrect mulicast
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbirkin committed Mar 8, 2023
1 parent 34c3c21 commit 7b8d198
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
21 changes: 13 additions & 8 deletions src/sacn/sacnlistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,31 @@ void sACNListener::readPendingDatagrams()
* Unicast for this interface - Allowed (Universe checked later)
* Broadcast - Rejected
*/
if (datagram.destinationAddress().isBroadcast())
break;

QList<QHostAddress> interfaceAddress;
for (const auto &address : m_socket->getBoundInterface().addressEntries())
interfaceAddress << address.ip();

CIPAddr addr;
GetUniverseAddress(m_universe, addr);

if (
(datagram.destinationAddress().isMulticast() && datagram.destinationAddress() == addr.ToQHostAddress()) ||
// Relevant Multicast
(datagram.destinationAddress().isMulticast() && datagram.destinationAddress() == m_socket->getMulticastAddr())
||
// Unicast for this interface
interfaceAddress.contains(QHostAddress(datagram.destinationAddress()))
)
{
processDatagram(
datagram.data(),
m_socket->localAddress(),
datagram.destinationAddress(),
datagram.senderAddress());
}
}
}
}

void sACNListener::processDatagram(QByteArray data, QHostAddress destination, QHostAddress sender)
void sACNListener::processDatagram(const QByteArray &data, const QHostAddress &destination, const QHostAddress &sender)
{
if(QThread::currentThread()!=this->thread())
{
Expand Down Expand Up @@ -279,7 +282,7 @@ void sACNListener::processDatagram(QByteArray data, QHostAddress destination, QH
if(m_universe != universe)
{
// Was it unicast? Send to correct listener (if listening)
if (!destination.isMulticast())
if (!destination.isMulticast() && !destination.isBroadcast())
{
// Unicast, send to correct listener!
decltype(sACNManager::getInstance()->getListenerList()) listenerList
Expand All @@ -289,7 +292,9 @@ void sACNListener::processDatagram(QByteArray data, QHostAddress destination, QH
return;
} else {
// Log and discard
qDebug() << "sACNListener" << QThread::currentThreadId() << ": Wrong Universe and is multicast";
qDebug() << "sACNListener" << QThread::currentThreadId()
<< ": Rejecting universe" << universe << "sent to" << destination;
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sacn/sacnlistener.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class sACNListener : public QObject
* This allows other listeners to pass on unicast datagrams for other universes
*
*/
Q_INVOKABLE void processDatagram(QByteArray data, QHostAddress destination, QHostAddress sender);
Q_INVOKABLE void processDatagram(const QByteArray &data, const QHostAddress &destination, const QHostAddress &sender);

// Diagnostic - the number of merge operations per second

Expand Down
3 changes: 2 additions & 1 deletion src/sacn/sacnsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sACNRxSocket::sBindStatus sACNRxSocket::bind(quint16 universe)

CIPAddr multicastAddr;
GetUniverseAddress(universe, multicastAddr);
m_multicastAddr = QHostAddress(multicastAddr.GetV4Address());

QHostAddress listenAddr = QHostAddress::AnyIPv4;
ok = QUdpSocket::bind(listenAddr,
Expand All @@ -55,7 +56,7 @@ sACNRxSocket::sBindStatus sACNRxSocket::bind(quint16 universe)
#endif
#endif
setMulticastInterface(m_interface);
ok |= joinMulticastGroup(QHostAddress(multicastAddr.GetV4Address()), m_interface);
ok |= joinMulticastGroup(m_multicastAddr, m_interface);
status.multicast = ok ? BIND_OK : BIND_FAILED;
}

Expand Down
6 changes: 4 additions & 2 deletions src/sacn/sacnsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ class sACNRxSocket : public QUdpSocket
};

sBindStatus bind(quint16 universe);
int getBoundUniverse() { return m_universe; }
QNetworkInterface getBoundInterface() { return m_interface; }
int getBoundUniverse() const { return m_universe; }
QNetworkInterface getBoundInterface() const{ return m_interface; }
const QHostAddress &getMulticastAddr() const {return m_multicastAddr; }

private:
QNetworkInterface m_interface;
int m_universe;
QHostAddress m_multicastAddr;
};

class sACNTxSocket : public QUdpSocket
Expand Down

0 comments on commit 7b8d198

Please sign in to comment.