[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | // |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 5 | // Some helpers for quic. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 6 | |
| 7 | #ifndef NET_QUIC_QUIC_UTILS_H_ |
| 8 | #define NET_QUIC_QUIC_UTILS_H_ |
| 9 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
| 12 | |
mef | cb6537c | 2014-11-12 19:38:50 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 15 | #include "base/macros.h" |
rtenneti | c14c8ab | 2015-06-18 05:47:40 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
[email protected] | 165e075 | 2012-11-16 07:49:44 | [diff] [blame] | 17 | #include "net/base/int128.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 18 | #include "net/base/net_export.h" |
| 19 | #include "net/quic/quic_protocol.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 20 | |
jdorfman | 4ea54a2 | 2016-01-21 22:12:50 | [diff] [blame] | 21 | #ifdef _MSC_VER |
| 22 | // MSVC 2013 and prior don't have alignof or aligned(); they have __alignof and |
| 23 | // a __declspec instead. |
| 24 | #define QUIC_ALIGN_OF __alignof |
| 25 | #define QUIC_ALIGNED(X) __declspec(align(X)) |
| 26 | #else |
| 27 | #define QUIC_ALIGN_OF alignof |
| 28 | #define QUIC_ALIGNED(X) __attribute__((aligned(X))) |
| 29 | #endif // _MSC_VER |
| 30 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 31 | namespace net { |
| 32 | |
| 33 | class NET_EXPORT_PRIVATE QuicUtils { |
| 34 | public: |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 35 | enum Priority { |
| 36 | LOCAL_PRIORITY, |
| 37 | PEER_PRIORITY, |
| 38 | }; |
| 39 | |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 40 | // Returns the 64 bit FNV1a hash of the data. See |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 41 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 42 | static uint64_t FNV1a_64_Hash(const char* data, int len); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 43 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 44 | // returns the 128 bit FNV1a hash of the data. See |
| 45 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 46 | static uint128 FNV1a_128_Hash(const char* data1, int len1); |
| 47 | |
| 48 | // returns the 128 bit FNV1a hash of the two sequences of data. See |
| 49 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
| 50 | static uint128 FNV1a_128_Hash_Two(const char* data1, |
| 51 | int len1, |
| 52 | const char* data2, |
| 53 | int len2); |
| 54 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 55 | // FindMutualTag sets |out_result| to the first tag in the priority list that |
| 56 | // is also in the other list and returns true. If there is no intersection it |
| 57 | // returns false. |
| 58 | // |
| 59 | // Which list has priority is determined by |priority|. |
| 60 | // |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 61 | // If |out_index| is non-nullptr and a match is found then the index of that |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 62 | // match in |their_tags| is written to |out_index|. |
| 63 | static bool FindMutualTag(const QuicTagVector& our_tags, |
| 64 | const QuicTag* their_tags, |
| 65 | size_t num_their_tags, |
| 66 | Priority priority, |
| 67 | QuicTag* out_result, |
| 68 | size_t* out_index); |
| 69 | |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 70 | // SerializeUint128 writes the first 96 bits of |v| in little-endian form |
| 71 | // to |out|. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 72 | static void SerializeUint128Short(uint128 v, uint8_t* out); |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 73 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 74 | // Returns the name of the QuicRstStreamErrorCode as a char* |
| 75 | static const char* StreamErrorToString(QuicRstStreamErrorCode error); |
| 76 | |
| 77 | // Returns the name of the QuicErrorCode as a char* |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 78 | static const char* ErrorToString(QuicErrorCode error); |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 79 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 80 | // Returns the level of encryption as a char* |
| 81 | static const char* EncryptionLevelToString(EncryptionLevel level); |
| 82 | |
[email protected] | 5c34371f | 2014-03-13 21:23:52 | [diff] [blame] | 83 | // Returns TransmissionType as a char* |
| 84 | static const char* TransmissionTypeToString(TransmissionType type); |
| 85 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 86 | // TagToString is a utility function for pretty-printing handshake messages |
| 87 | // that converts a tag to a string. It will try to maintain the human friendly |
| 88 | // name if possible (i.e. kABCD -> "ABCD"), or will just treat it as a number |
| 89 | // if not. |
| 90 | static std::string TagToString(QuicTag tag); |
[email protected] | 502293e | 2013-05-24 09:41:27 | [diff] [blame] | 91 | |
mef | cb6537c | 2014-11-12 19:38:50 | [diff] [blame] | 92 | // Returns the list of QUIC tags represented by the comma separated |
| 93 | // string in |connection_options|. |
| 94 | static QuicTagVector ParseQuicConnectionOptions( |
| 95 | const std::string& connection_options); |
| 96 | |
[email protected] | 502293e | 2013-05-24 09:41:27 | [diff] [blame] | 97 | // Given a binary buffer, return a hex+ASCII dump in the style of |
| 98 | // tcpdump's -X and -XX options: |
| 99 | // "0x0000: 0090 69bd 5400 000d 610f 0189 0800 4500 ..i.T...a.....E.\n" |
| 100 | // "0x0010: 001c fb98 4000 4001 7e18 d8ef 2301 455d ....@.@.~...#.E]\n" |
| 101 | // "0x0020: 7fe2 0800 6bcb 0bc6 806e ....k....n\n" |
| 102 | static std::string StringToHexASCIIDump(base::StringPiece in_buffer); |
[email protected] | 76c35b0 | 2013-08-07 10:06:37 | [diff] [blame] | 103 | |
ianswett | 7b88c0a | 2016-06-10 22:29:58 | [diff] [blame] | 104 | // Returns PeerAddressChangeType as a std::string. |
| 105 | static std::string PeerAddressChangeTypeToString(PeerAddressChangeType type); |
| 106 | |
[email protected] | 76c35b0 | 2013-08-07 10:06:37 | [diff] [blame] | 107 | static char* AsChars(unsigned char* data) { |
| 108 | return reinterpret_cast<char*>(data); |
| 109 | } |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 110 | |
rch | caec424 | 2016-01-22 20:49:52 | [diff] [blame] | 111 | // Deletes all the sub-frames contained in |frames|. |
| 112 | static void DeleteFrames(QuicFrames* frames); |
| 113 | |
| 114 | // Deletes all the QuicStreamFrames for the specified |stream_id|. |
| 115 | static void RemoveFramesForStream(QuicFrames* frames, QuicStreamId stream_id); |
| 116 | |
jri | c533399b | 2016-01-29 07:36:01 | [diff] [blame] | 117 | // Deletes and clears all the frames and the packet from serialized packet. |
| 118 | static void ClearSerializedPacket(SerializedPacket* serialized_packet); |
| 119 | |
rtenneti | f85706db | 2016-02-04 02:43:03 | [diff] [blame] | 120 | // Returns a packed representation of |path_id| and |packet_number| in which |
| 121 | // the highest byte is set to |path_id| and the lower 7 bytes are the lower |
| 122 | // 7 bytes of |packet_number|. |
| 123 | static uint64_t PackPathIdAndPacketNumber(QuicPathId path_id, |
| 124 | QuicPacketNumber packet_number); |
| 125 | |
rtenneti | 2cae207 | 2016-02-05 02:21:33 | [diff] [blame] | 126 | // Allocates a new char[] of size |packet.encrypted_length| and copies in |
| 127 | // |packet.encrypted_buffer|. |
| 128 | static char* CopyBuffer(const SerializedPacket& packet); |
| 129 | |
fayang | a64c1a9 | 2016-02-13 01:55:58 | [diff] [blame] | 130 | // Determines and returns change type of address change from |old_address| to |
| 131 | // |new_address|. |
| 132 | static PeerAddressChangeType DetermineAddressChangeType( |
| 133 | const IPEndPoint& old_address, |
| 134 | const IPEndPoint& new_address); |
| 135 | |
rch | 16fe57a | 2016-05-10 21:34:33 | [diff] [blame] | 136 | // This converts 'num' bytes of binary to a 2*'num'-character hexadecimal |
| 137 | // representation. Return value: 2*'num' characters of ascii std::string. |
| 138 | static std::string HexEncode(const char* data, size_t length); |
| 139 | static std::string HexEncode(base::StringPiece data); |
| 140 | |
| 141 | // This converts 2*'num' hexadecimal characters to 'num' binary data. |
| 142 | // Return value: 'num' bytes of binary data (via the 'to' argument). |
| 143 | static std::string HexDecode(const char* data, size_t length); |
| 144 | static std::string HexDecode(base::StringPiece data); |
| 145 | |
ckrasic | 6567aa5 | 2016-07-08 09:24:35 | [diff] [blame^] | 146 | // Returns a std::string containing hex and ASCII representations of |binary|, |
| 147 | // side-by-side in the style of hexdump. Non-printable characters will be |
| 148 | // printed as '.' in the ASCII output. |
| 149 | // For example: |
| 150 | // "48 65 6c 6c 6f 2c 20 51 55 49 43 21 01 02 03 04 |Hello, QUIC!....|" |
| 151 | static std::string HexDump(base::StringPiece binary_data); |
rch | 16fe57a | 2016-05-10 21:34:33 | [diff] [blame] | 152 | |
[email protected] | b99c0fc | 2014-04-22 07:56:52 | [diff] [blame] | 153 | private: |
| 154 | DISALLOW_COPY_AND_ASSIGN(QuicUtils); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 155 | }; |
| 156 | |
rtenneti | c14c8ab | 2015-06-18 05:47:40 | [diff] [blame] | 157 | // Utility function that returns an QuicIOVector object wrapped around |str|. |
| 158 | // |str|'s data is stored in |iov|. |
| 159 | inline QuicIOVector MakeIOVector(base::StringPiece str, struct iovec* iov) { |
| 160 | iov->iov_base = const_cast<char*>(str.data()); |
| 161 | iov->iov_len = static_cast<size_t>(str.size()); |
| 162 | QuicIOVector quic_iov(iov, 1, str.size()); |
| 163 | return quic_iov; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 166 | } // namespace net |
| 167 | |
| 168 | #endif // NET_QUIC_QUIC_UTILS_H_ |