OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Some helpers for quic. | 5 // Some helpers for quic. |
6 | 6 |
7 #ifndef NET_QUIC_CORE_QUIC_UTILS_H_ | 7 #ifndef NET_QUIC_CORE_QUIC_UTILS_H_ |
8 #define NET_QUIC_CORE_QUIC_UTILS_H_ | 8 #define NET_QUIC_CORE_QUIC_UTILS_H_ |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #define QUIC_ALIGNED(X) __declspec(align(X)) | 25 #define QUIC_ALIGNED(X) __declspec(align(X)) |
26 #else | 26 #else |
27 #define QUIC_ALIGN_OF alignof | 27 #define QUIC_ALIGN_OF alignof |
28 #define QUIC_ALIGNED(X) __attribute__((aligned(X))) | 28 #define QUIC_ALIGNED(X) __attribute__((aligned(X))) |
29 #endif // _MSC_VER | 29 #endif // _MSC_VER |
30 | 30 |
31 namespace net { | 31 namespace net { |
32 | 32 |
33 class NET_EXPORT_PRIVATE QuicUtils { | 33 class NET_EXPORT_PRIVATE QuicUtils { |
34 public: | 34 public: |
35 enum Priority { | |
36 LOCAL_PRIORITY, | |
37 PEER_PRIORITY, | |
38 }; | |
39 | |
40 // Returns the 64 bit FNV1a hash of the data. See | 35 // Returns the 64 bit FNV1a hash of the data. See |
41 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | 36 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
42 static uint64_t FNV1a_64_Hash(const char* data, int len); | 37 static uint64_t FNV1a_64_Hash(const char* data, int len); |
43 | 38 |
44 // returns the 128 bit FNV1a hash of the data. See | 39 // returns the 128 bit FNV1a hash of the data. See |
45 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | 40 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
46 static uint128 FNV1a_128_Hash(const char* data1, int len1); | 41 static uint128 FNV1a_128_Hash(const char* data1, int len1); |
47 | 42 |
48 // returns the 128 bit FNV1a hash of the two sequences of data. See | 43 // 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 | 44 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
50 static uint128 FNV1a_128_Hash_Two(const char* data1, | 45 static uint128 FNV1a_128_Hash_Two(const char* data1, |
51 int len1, | 46 int len1, |
52 const char* data2, | 47 const char* data2, |
53 int len2); | 48 int len2); |
54 | 49 |
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 // | |
61 // If |out_index| is non-nullptr and a match is found then the index of that | |
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 | |
70 // SerializeUint128 writes the first 96 bits of |v| in little-endian form | 50 // SerializeUint128 writes the first 96 bits of |v| in little-endian form |
71 // to |out|. | 51 // to |out|. |
72 static void SerializeUint128Short(uint128 v, uint8_t* out); | 52 static void SerializeUint128Short(uint128 v, uint8_t* out); |
73 | 53 |
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* | |
78 static const char* ErrorToString(QuicErrorCode error); | |
79 | |
80 // Returns the level of encryption as a char* | 54 // Returns the level of encryption as a char* |
81 static const char* EncryptionLevelToString(EncryptionLevel level); | 55 static const char* EncryptionLevelToString(EncryptionLevel level); |
82 | 56 |
83 // Returns TransmissionType as a char* | 57 // Returns TransmissionType as a char* |
84 static const char* TransmissionTypeToString(TransmissionType type); | 58 static const char* TransmissionTypeToString(TransmissionType type); |
85 | 59 |
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); | |
91 | |
92 // Returns the list of QUIC tags represented by the comma separated | 60 // Returns the list of QUIC tags represented by the comma separated |
93 // string in |connection_options|. | 61 // string in |connection_options|. |
94 static QuicTagVector ParseQuicConnectionOptions( | 62 static QuicTagVector ParseQuicConnectionOptions( |
95 const std::string& connection_options); | 63 const std::string& connection_options); |
96 | 64 |
97 // Returns PeerAddressChangeType as a std::string. | 65 // Returns PeerAddressChangeType as a std::string. |
98 static std::string PeerAddressChangeTypeToString(PeerAddressChangeType type); | 66 static std::string PeerAddressChangeTypeToString(PeerAddressChangeType type); |
99 | 67 |
100 static char* AsChars(unsigned char* data) { | |
101 return reinterpret_cast<char*>(data); | |
102 } | |
103 | |
104 // Deletes all the sub-frames contained in |frames|. | 68 // Deletes all the sub-frames contained in |frames|. |
105 static void DeleteFrames(QuicFrames* frames); | 69 static void DeleteFrames(QuicFrames* frames); |
106 | 70 |
107 // Deletes all the QuicStreamFrames for the specified |stream_id|. | 71 // Deletes all the QuicStreamFrames for the specified |stream_id|. |
108 static void RemoveFramesForStream(QuicFrames* frames, QuicStreamId stream_id); | 72 static void RemoveFramesForStream(QuicFrames* frames, QuicStreamId stream_id); |
109 | 73 |
110 // Deletes and clears all the frames and the packet from serialized packet. | 74 // Deletes and clears all the frames and the packet from serialized packet. |
111 static void ClearSerializedPacket(SerializedPacket* serialized_packet); | 75 static void ClearSerializedPacket(SerializedPacket* serialized_packet); |
112 | 76 |
113 // Returns a packed representation of |path_id| and |packet_number| in which | 77 // Returns a packed representation of |path_id| and |packet_number| in which |
114 // the highest byte is set to |path_id| and the lower 7 bytes are the lower | 78 // the highest byte is set to |path_id| and the lower 7 bytes are the lower |
115 // 7 bytes of |packet_number|. | 79 // 7 bytes of |packet_number|. |
116 static uint64_t PackPathIdAndPacketNumber(QuicPathId path_id, | 80 static uint64_t PackPathIdAndPacketNumber(QuicPathId path_id, |
117 QuicPacketNumber packet_number); | 81 QuicPacketNumber packet_number); |
118 | 82 |
119 // Allocates a new char[] of size |packet.encrypted_length| and copies in | 83 // Allocates a new char[] of size |packet.encrypted_length| and copies in |
120 // |packet.encrypted_buffer|. | 84 // |packet.encrypted_buffer|. |
121 static char* CopyBuffer(const SerializedPacket& packet); | 85 static char* CopyBuffer(const SerializedPacket& packet); |
122 | 86 |
123 // Determines and returns change type of address change from |old_address| to | 87 // Determines and returns change type of address change from |old_address| to |
124 // |new_address|. | 88 // |new_address|. |
125 static PeerAddressChangeType DetermineAddressChangeType( | 89 static PeerAddressChangeType DetermineAddressChangeType( |
126 const IPEndPoint& old_address, | 90 const IPEndPoint& old_address, |
127 const IPEndPoint& new_address); | 91 const IPEndPoint& new_address); |
128 | 92 |
129 // This converts 'num' bytes of binary to a 2*'num'-character hexadecimal | 93 // This converts |length| bytes of binary to a 2*|length|-character |
130 // representation. Return value: 2*'num' characters of ascii std::string. | 94 // hexadecimal representation. |
| 95 // Return value: 2*|length| characters of ASCII std::string. |
131 static std::string HexEncode(const char* data, size_t length); | 96 static std::string HexEncode(const char* data, size_t length); |
132 static std::string HexEncode(base::StringPiece data); | 97 static std::string HexEncode(base::StringPiece data); |
133 | 98 |
134 // This converts 2*'num' hexadecimal characters to 'num' binary data. | 99 // Converts |data| from a hexadecimal ASCII std::string to binary. |
135 // Return value: 'num' bytes of binary data (via the 'to' argument). | |
136 static std::string HexDecode(const char* data, size_t length); | |
137 static std::string HexDecode(base::StringPiece data); | 100 static std::string HexDecode(base::StringPiece data); |
138 | 101 |
139 // Returns a std::string containing hex and ASCII representations of |binary|, | 102 // Returns a std::string containing hex and ASCII representations of |binary|, |
140 // side-by-side in the style of hexdump. Non-printable characters will be | 103 // side-by-side in the style of hexdump. Non-printable characters will be |
141 // printed as '.' in the ASCII output. | 104 // printed as '.' in the ASCII output. |
142 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." | 105 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." |
143 static std::string HexDump(base::StringPiece binary_data); | 106 static std::string HexDump(base::StringPiece binary_data); |
144 | 107 |
145 private: | 108 private: |
146 DISALLOW_COPY_AND_ASSIGN(QuicUtils); | 109 DISALLOW_COPY_AND_ASSIGN(QuicUtils); |
147 }; | 110 }; |
148 | 111 |
149 // Utility function that returns an QuicIOVector object wrapped around |str|. | |
150 // |str|'s data is stored in |iov|. | |
151 inline QuicIOVector MakeIOVector(base::StringPiece str, struct iovec* iov) { | |
152 iov->iov_base = const_cast<char*>(str.data()); | |
153 iov->iov_len = static_cast<size_t>(str.size()); | |
154 QuicIOVector quic_iov(iov, 1, str.size()); | |
155 return quic_iov; | |
156 } | |
157 | |
158 } // namespace net | 112 } // namespace net |
159 | 113 |
160 #endif // NET_QUIC_CORE_QUIC_UTILS_H_ | 114 #endif // NET_QUIC_CORE_QUIC_UTILS_H_ |
OLD | NEW |