1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_QUIC_CORE_QUIC_TAG_H_ 6 #define QUICHE_QUIC_CORE_QUIC_TAG_H_ 7 8 #include <cstdint> 9 #include <map> 10 #include <string> 11 #include <vector> 12 13 #include "absl/strings/string_view.h" 14 #include "quiche/quic/platform/api/quic_export.h" 15 16 namespace quic { 17 18 // A QuicTag is a 32-bit used as identifiers in the QUIC handshake. The use of 19 // a uint32_t seeks to provide a balance between the tyranny of magic number 20 // registries and the verbosity of strings. As far as the wire protocol is 21 // concerned, these are opaque, 32-bit values. 22 // 23 // Tags will often be referred to by their ASCII equivalent, e.g. EXMP. This is 24 // just a mnemonic for the value 0x504d5845 (little-endian version of the ASCII 25 // string E X M P). 26 using QuicTag = uint32_t; 27 using QuicTagValueMap = std::map<QuicTag, std::string>; 28 using QuicTagVector = std::vector<QuicTag>; 29 30 // MakeQuicTag returns a value given the four bytes. For example: 31 // MakeQuicTag('C', 'H', 'L', 'O'); 32 QUICHE_EXPORT QuicTag MakeQuicTag(uint8_t a, uint8_t b, uint8_t c, uint8_t d); 33 34 // Returns true if |tag_vector| contains |tag|. 35 QUICHE_EXPORT bool ContainsQuicTag(const QuicTagVector& tag_vector, 36 QuicTag tag); 37 38 // Sets |out_result| to the first tag in |our_tags| that is also in |their_tags| 39 // and returns true. If there is no intersection it returns false. 40 // 41 // If |out_index| is non-nullptr and a match is found then the index of that 42 // match in |their_tags| is written to |out_index|. 43 QUICHE_EXPORT bool FindMutualQuicTag(const QuicTagVector& our_tags, 44 const QuicTagVector& their_tags, 45 QuicTag* out_result, size_t* out_index); 46 47 // A utility function that converts a tag to a string. It will try to maintain 48 // the human friendly name if possible (i.e. kABCD -> "ABCD"), or will just 49 // treat it as a number if not. 50 QUICHE_EXPORT std::string QuicTagToString(QuicTag tag); 51 52 // Utility function that converts a string of the form "ABCD" to its 53 // corresponding QuicTag. Note that tags that are less than four characters 54 // long are right-padded with zeroes. Tags that contain non-ASCII characters 55 // are represented as 8-character-long hexadecimal strings. 56 QUICHE_EXPORT QuicTag ParseQuicTag(absl::string_view tag_string); 57 58 // Utility function that converts a string of the form "ABCD,EFGH" to a vector 59 // of the form {kABCD,kEFGH}. Note the caveats on ParseQuicTag. 60 QUICHE_EXPORT QuicTagVector ParseQuicTagVector(absl::string_view tags_string); 61 62 } // namespace quic 63 64 #endif // QUICHE_QUIC_CORE_QUIC_TAG_H_ 65