This is an automated email from the ASF dual-hosted git repository.

maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 9a0f6635e8c251ad7fdb44f7f4b6d90204afeaff
Author: Masakazu Kitajo <mas...@apache.org>
AuthorDate: Tue Jun 19 17:27:51 2018 +0900

    Add static accessor methods for QUICPacket header fields
---
 iocore/net/quic/QUICPacket.cc | 104 ++++++++++++++++++++++++++++++++++++------
 iocore/net/quic/QUICPacket.h  |  14 ++++++
 iocore/net/quic/QUICTypes.h   |   6 +++
 3 files changed, 110 insertions(+), 14 deletions(-)

diff --git a/iocore/net/quic/QUICPacket.cc b/iocore/net/quic/QUICPacket.cc
index 1ebed09..ca3bc45 100644
--- a/iocore/net/quic/QUICPacket.cc
+++ b/iocore/net/quic/QUICPacket.cc
@@ -186,18 +186,80 @@ QUICPacketType
 QUICPacketLongHeader::type() const
 {
   if (this->_buf) {
-    uint8_t type = this->_buf.get()[0] & 0x7F;
-    if (this->version() == 0x00) {
-      return QUICPacketType::VERSION_NEGOTIATION;
-    } else {
-      // any other version-specific type?
-      return static_cast<QUICPacketType>(type);
-    }
+    QUICPacketType type;
+    QUICPacketLongHeader::type(type, this->_buf.get(), this->_buf_len);
+    return type;
   } else {
     return this->_type;
   }
 }
 
+bool
+QUICPacketLongHeader::type(QUICPacketType &type, const uint8_t *packet, size_t 
packet_len)
+{
+  if (packet_len < 1) {
+    return false;
+  }
+
+  uint8_t raw_type = packet[0] & 0x7F;
+  QUICVersion version;
+  if (QUICPacketLongHeader::version(version, packet, packet_len) && version == 
0x00) {
+    type = QUICPacketType::VERSION_NEGOTIATION;
+  } else {
+    type = static_cast<QUICPacketType>(raw_type);
+  }
+  return true;
+}
+
+bool
+QUICPacketLongHeader::version(QUICVersion &version, const uint8_t *packet, 
size_t packet_len)
+{
+  if (packet_len < 5) {
+    return false;
+  }
+
+  version = QUICTypeUtil::read_QUICVersion(packet + LONG_HDR_OFFSET_VERSION);
+  return true;
+}
+
+bool
+QUICPacketLongHeader::dcil(uint8_t &dcil, const uint8_t *packet, size_t 
packet_len)
+{
+  if (QUICInvariants::dcil(dcil, packet, packet_len)) {
+    dcil += 3;
+    return true;
+  } else {
+    return false;
+  }
+}
+
+bool
+QUICPacketLongHeader::scil(uint8_t &scil, const uint8_t *packet, size_t 
packet_len)
+{
+  if (QUICInvariants::scil(scil, packet, packet_len)) {
+    scil += 3;
+    return true;
+  } else {
+    return false;
+  }
+}
+
+bool
+QUICPacketLongHeader::payload_length(size_t &length, uint8_t *field_len, const 
uint8_t *packet, size_t packet_len)
+{
+  uint8_t dcil, scil;
+
+  QUICPacketLongHeader::dcil(dcil, packet, packet_len);
+  QUICPacketLongHeader::scil(scil, packet, packet_len);
+
+  size_t payload_len_offset = LONG_HDR_OFFSET_CONNECTION_ID + dcil + scil;
+  length                    = QUICIntUtil::read_QUICVariableInt(packet + 
payload_len_offset);
+  if (field_len) {
+    *field_len = QUICVariableInt::size(packet + payload_len_offset);
+  }
+  return true;
+}
+
 QUICConnectionId
 QUICPacketLongHeader::destination_cid() const
 {
@@ -242,7 +304,9 @@ QUICVersion
 QUICPacketLongHeader::version() const
 {
   if (this->_buf) {
-    return QUICTypeUtil::read_QUICVersion(this->_buf.get() + 
LONG_HDR_OFFSET_VERSION);
+    QUICVersion version;
+    QUICPacketLongHeader::version(version, this->_buf.get(), this->_buf_len);
+    return version;
   } else {
     return this->_version;
   }
@@ -437,16 +501,28 @@ QUICKeyPhase
 QUICPacketShortHeader::key_phase() const
 {
   if (this->_buf) {
-    if (this->_buf.get()[0] & 0x40) {
-      return QUICKeyPhase::PHASE_1;
-    } else {
-      return QUICKeyPhase::PHASE_0;
-    }
+    QUICKeyPhase phase;
+    QUICPacketShortHeader::key_phase(phase, this->_buf.get(), this->_buf_len);
+    return phase;
   } else {
-    return _key_phase;
+    return this->_key_phase;
   }
 }
 
+bool
+QUICPacketShortHeader::key_phase(QUICKeyPhase &phase, const uint8_t *packet, 
size_t packet_len)
+{
+  if (packet_len < 1) {
+    return false;
+  }
+  if (packet[0] & 0x40) {
+    phase = QUICKeyPhase::PHASE_1;
+  } else {
+    phase = QUICKeyPhase::PHASE_0;
+  }
+  return true;
+}
+
 /**
  * Header Length (doesn't include payload length)
  */
diff --git a/iocore/net/quic/QUICPacket.h b/iocore/net/quic/QUICPacket.h
index 0245f3e..bf6cfb7 100644
--- a/iocore/net/quic/QUICPacket.h
+++ b/iocore/net/quic/QUICPacket.h
@@ -190,6 +190,18 @@ public:
   uint16_t size() const;
   void store(uint8_t *buf, size_t *len) const;
 
+  static bool type(QUICPacketType &type, const uint8_t *packet, size_t 
packet_len);
+  static bool version(QUICVersion &version, const uint8_t *packet, size_t 
packet_len);
+  /**
+   * Unlike QUICInvariants::dcil(), this returns actual connection id length
+   */
+  static bool dcil(uint8_t &dcil, const uint8_t *packet, size_t packet_len);
+  /**
+   * Unlike QUICInvariants::scil(), this returns actual connection id length
+   */
+  static bool scil(uint8_t &scil, const uint8_t *packet, size_t packet_len);
+  static bool payload_length(size_t &payload_length, uint8_t *field_len, const 
uint8_t *packet, size_t packet_len);
+
 private:
   QUICPacketNumber _packet_number;
   QUICConnectionId _destination_cid = QUICConnectionId::ZERO();
@@ -224,6 +236,8 @@ public:
   uint16_t size() const;
   void store(uint8_t *buf, size_t *len) const;
 
+  static bool key_phase(QUICKeyPhase &key_phase, const uint8_t *packet, size_t 
packet_len);
+
 private:
   int _packet_number_len;
 };
diff --git a/iocore/net/quic/QUICTypes.h b/iocore/net/quic/QUICTypes.h
index 41b0efd..c7b3f42 100644
--- a/iocore/net/quic/QUICTypes.h
+++ b/iocore/net/quic/QUICTypes.h
@@ -347,7 +347,13 @@ public:
   static bool is_long_header(const uint8_t *buf);
   static bool is_version_negotiation(QUICVersion v);
   static bool version(QUICVersion &dst, const uint8_t *buf, uint64_t buf_len);
+  /**
+   * This function returns the raw value. You'll need to add 3 to the returned 
value to get the actual connection id length.
+   */
   static bool dcil(uint8_t &dst, const uint8_t *buf, uint64_t buf_len);
+  /**
+   * This function returns the raw value. You'll need to add 3 to the returned 
value to get the actual connection id length.
+   */
   static bool scil(uint8_t &dst, const uint8_t *buf, uint64_t buf_len);
   static bool dcid(QUICConnectionId &dst, const uint8_t *buf, uint64_t 
buf_len);
   static bool scid(QUICConnectionId &dst, const uint8_t *buf, uint64_t 
buf_len);

Reply via email to