repairs to type/version header write

This commit is contained in:
Stephen Birarda 2015-07-23 15:57:31 -07:00
parent 862c788aec
commit 2f03075750
5 changed files with 22 additions and 11 deletions

View file

@ -624,6 +624,7 @@ void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packe
// enumerate the attributes to find XOR_MAPPED_ADDRESS_TYPE // enumerate the attributes to find XOR_MAPPED_ADDRESS_TYPE
while (attributeStartIndex < packet->getDataSize()) { while (attributeStartIndex < packet->getDataSize()) {
if (memcmp(packet->getData() + attributeStartIndex, &XOR_MAPPED_ADDRESS_TYPE, sizeof(XOR_MAPPED_ADDRESS_TYPE)) == 0) { if (memcmp(packet->getData() + attributeStartIndex, &XOR_MAPPED_ADDRESS_TYPE, sizeof(XOR_MAPPED_ADDRESS_TYPE)) == 0) {
const int NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH = 4; const int NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH = 4;
const int NUM_BYTES_FAMILY_ALIGN = 1; const int NUM_BYTES_FAMILY_ALIGN = 1;
@ -651,8 +652,8 @@ void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packe
uint32_t stunAddress = ntohl(xorMappedAddress) ^ ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER); uint32_t stunAddress = ntohl(xorMappedAddress) ^ ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER);
QHostAddress newPublicAddress = QHostAddress(stunAddress); QHostAddress newPublicAddress(stunAddress);
if (newPublicAddress != _publicSockAddr.getAddress() || newPublicPort != _publicSockAddr.getPort()) { if (newPublicAddress != _publicSockAddr.getAddress() || newPublicPort != _publicSockAddr.getPort()) {
_publicSockAddr = HifiSockAddr(newPublicAddress, newPublicPort); _publicSockAddr = HifiSockAddr(newPublicAddress, newPublicPort);
@ -669,6 +670,9 @@ void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packe
flagTimeForConnectionStep(ConnectionStep::SetPublicSocketFromSTUN); flagTimeForConnectionStep(ConnectionStep::SetPublicSocketFromSTUN);
} }
// we're done reading the packet so we can return now
return;
} }
} else { } else {
// push forward attributeStartIndex by the length of this attribute // push forward attributeStartIndex by the length of this attribute

View file

@ -95,6 +95,8 @@ NLPacket::NLPacket(PacketType type, qint64 size, bool isReliable, bool isPartOfM
Q_ASSERT(size >= 0); Q_ASSERT(size >= 0);
adjustPayloadStartAndCapacity(); adjustPayloadStartAndCapacity();
writeTypeAndVersion();
} }
NLPacket::NLPacket(std::unique_ptr<Packet> packet) : NLPacket::NLPacket(std::unique_ptr<Packet> packet) :
@ -146,11 +148,13 @@ NLPacket& NLPacket::operator=(NLPacket&& other) {
} }
PacketType NLPacket::typeInHeader(const udt::Packet& packet) { PacketType NLPacket::typeInHeader(const udt::Packet& packet) {
return *reinterpret_cast<const PacketType*>(packet.getData()); auto headerOffset = packet.Packet::localHeaderSize();
return *reinterpret_cast<const PacketType*>(packet.getData() + headerOffset);
} }
PacketVersion NLPacket::versionInHeader(const udt::Packet& packet) { PacketVersion NLPacket::versionInHeader(const udt::Packet& packet) {
return *reinterpret_cast<const PacketVersion*>(packet.getData() + sizeof(PacketType)); auto headerOffset = packet.Packet::localHeaderSize();
return *reinterpret_cast<const PacketVersion*>(packet.getData() + headerOffset + sizeof(PacketType));
} }
QUuid NLPacket::sourceIDInHeader(const udt::Packet& packet) { QUuid NLPacket::sourceIDInHeader(const udt::Packet& packet) {
@ -177,13 +181,15 @@ QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUu
return hash.result(); return hash.result();
} }
void NLPacket::writePacketTypeAndVersion() { void NLPacket::writeTypeAndVersion() {
auto headerOffset = Packet::localHeaderSize();
// Pack the packet type // Pack the packet type
memcpy(_packet.get(), &_type, sizeof(PacketType)); memcpy(_packet.get() + headerOffset, &_type, sizeof(PacketType));
// Pack the packet version // Pack the packet version
auto version = versionForPacketType(_type); auto version = versionForPacketType(_type);
memcpy(_packet.get() + sizeof(PacketType), &version, sizeof(version)); memcpy(_packet.get() + headerOffset + sizeof(PacketType), &version, sizeof(version));
} }
void NLPacket::setType(PacketType type) { void NLPacket::setType(PacketType type) {
@ -192,7 +198,7 @@ void NLPacket::setType(PacketType type) {
_type = type; _type = type;
_version = versionForPacketType(_type); _version = versionForPacketType(_type);
writePacketTypeAndVersion(); writeTypeAndVersion();
// Setting new packet type with a different header size not currently supported // Setting new packet type with a different header size not currently supported
Q_ASSERT(currentHeaderSize == totalHeadersSize()); Q_ASSERT(currentHeaderSize == totalHeadersSize());

View file

@ -66,7 +66,7 @@ protected:
NLPacket& operator=(NLPacket&& other); NLPacket& operator=(NLPacket&& other);
// Header writers // Header writers
void writePacketTypeAndVersion(); void writeTypeAndVersion();
// Header readers, used to set member variables after getting a packet from the network // Header readers, used to set member variables after getting a packet from the network
void readType(); void readType();

View file

@ -134,5 +134,6 @@ uint qHash(const PacketType& key, uint seed) {
} }
QDebug operator<<(QDebug debug, const PacketType& type) { QDebug operator<<(QDebug debug, const PacketType& type) {
return debug.nospace() << (uint8_t) type << " (" << qPrintable(nameForPacketType(type)) << ")"; debug.nospace() << (uint8_t) type << " (" << qPrintable(nameForPacketType(type)) << ")";
return debug.space();
} }

View file

@ -83,7 +83,7 @@ void Socket::readPendingDatagrams() {
// setup a buffer to read the packet into // setup a buffer to read the packet into
int packetSizeWithHeader = _udpSocket.pendingDatagramSize(); int packetSizeWithHeader = _udpSocket.pendingDatagramSize();
std::unique_ptr<char> buffer = std::unique_ptr<char>(new char[packetSizeWithHeader]); std::unique_ptr<char> buffer = std::unique_ptr<char>(new char[packetSizeWithHeader]);
// pull the datagram // pull the datagram
_udpSocket.readDatagram(buffer.get(), packetSizeWithHeader, _udpSocket.readDatagram(buffer.get(), packetSizeWithHeader,
senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer()); senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer());