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
while (attributeStartIndex < packet->getDataSize()) {
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_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);
QHostAddress newPublicAddress = QHostAddress(stunAddress);
QHostAddress newPublicAddress(stunAddress);
if (newPublicAddress != _publicSockAddr.getAddress() || newPublicPort != _publicSockAddr.getPort()) {
_publicSockAddr = HifiSockAddr(newPublicAddress, newPublicPort);
@ -669,6 +670,9 @@ void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packe
flagTimeForConnectionStep(ConnectionStep::SetPublicSocketFromSTUN);
}
// we're done reading the packet so we can return now
return;
}
} else {
// 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);
adjustPayloadStartAndCapacity();
writeTypeAndVersion();
}
NLPacket::NLPacket(std::unique_ptr<Packet> packet) :
@ -146,11 +148,13 @@ NLPacket& NLPacket::operator=(NLPacket&& other) {
}
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) {
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) {
@ -177,13 +181,15 @@ QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUu
return hash.result();
}
void NLPacket::writePacketTypeAndVersion() {
void NLPacket::writeTypeAndVersion() {
auto headerOffset = Packet::localHeaderSize();
// Pack the packet type
memcpy(_packet.get(), &_type, sizeof(PacketType));
memcpy(_packet.get() + headerOffset, &_type, sizeof(PacketType));
// Pack the packet version
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) {
@ -192,7 +198,7 @@ void NLPacket::setType(PacketType type) {
_type = type;
_version = versionForPacketType(_type);
writePacketTypeAndVersion();
writeTypeAndVersion();
// Setting new packet type with a different header size not currently supported
Q_ASSERT(currentHeaderSize == totalHeadersSize());

View file

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

View file

@ -134,5 +134,6 @@ uint qHash(const PacketType& key, uint seed) {
}
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
int packetSizeWithHeader = _udpSocket.pendingDatagramSize();
std::unique_ptr<char> buffer = std::unique_ptr<char>(new char[packetSizeWithHeader]);
// pull the datagram
_udpSocket.readDatagram(buffer.get(), packetSizeWithHeader,
senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer());