OLD | NEW |
| (Empty) |
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 // | |
5 // Some helpers for quic. | |
6 | |
7 #ifndef NET_QUIC_QUIC_UTILS_H_ | |
8 #define NET_QUIC_QUIC_UTILS_H_ | |
9 | |
10 #include <stddef.h> | |
11 #include <stdint.h> | |
12 | |
13 #include <string> | |
14 | |
15 #include "base/macros.h" | |
16 #include "base/strings/string_piece.h" | |
17 #include "net/base/int128.h" | |
18 #include "net/base/net_export.h" | |
19 #include "net/quic/quic_protocol.h" | |
20 | |
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 | |
31 namespace net { | |
32 | |
33 class NET_EXPORT_PRIVATE QuicUtils { | |
34 public: | |
35 enum Priority { | |
36 LOCAL_PRIORITY, | |
37 PEER_PRIORITY, | |
38 }; | |
39 | |
40 // Returns the 64 bit FNV1a hash of the data. See | |
41 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | |
42 static uint64_t FNV1a_64_Hash(const char* data, int len); | |
43 | |
44 // returns the 128 bit FNV1a hash of the data. See | |
45 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | |
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 | |
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 | |
71 // to |out|. | |
72 static void SerializeUint128Short(uint128 v, uint8_t* out); | |
73 | |
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* | |
81 static const char* EncryptionLevelToString(EncryptionLevel level); | |
82 | |
83 // Returns TransmissionType as a char* | |
84 static const char* TransmissionTypeToString(TransmissionType type); | |
85 | |
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 | |
93 // string in |connection_options|. | |
94 static QuicTagVector ParseQuicConnectionOptions( | |
95 const std::string& connection_options); | |
96 | |
97 // Returns PeerAddressChangeType as a std::string. | |
98 static std::string PeerAddressChangeTypeToString(PeerAddressChangeType type); | |
99 | |
100 static char* AsChars(unsigned char* data) { | |
101 return reinterpret_cast<char*>(data); | |
102 } | |
103 | |
104 // Deletes all the sub-frames contained in |frames|. | |
105 static void DeleteFrames(QuicFrames* frames); | |
106 | |
107 // Deletes all the QuicStreamFrames for the specified |stream_id|. | |
108 static void RemoveFramesForStream(QuicFrames* frames, QuicStreamId stream_id); | |
109 | |
110 // Deletes and clears all the frames and the packet from serialized packet. | |
111 static void ClearSerializedPacket(SerializedPacket* serialized_packet); | |
112 | |
113 // 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 | |
115 // 7 bytes of |packet_number|. | |
116 static uint64_t PackPathIdAndPacketNumber(QuicPathId path_id, | |
117 QuicPacketNumber packet_number); | |
118 | |
119 // Allocates a new char[] of size |packet.encrypted_length| and copies in | |
120 // |packet.encrypted_buffer|. | |
121 static char* CopyBuffer(const SerializedPacket& packet); | |
122 | |
123 // Determines and returns change type of address change from |old_address| to | |
124 // |new_address|. | |
125 static PeerAddressChangeType DetermineAddressChangeType( | |
126 const IPEndPoint& old_address, | |
127 const IPEndPoint& new_address); | |
128 | |
129 // This converts 'num' bytes of binary to a 2*'num'-character hexadecimal | |
130 // representation. Return value: 2*'num' characters of ascii std::string. | |
131 static std::string HexEncode(const char* data, size_t length); | |
132 static std::string HexEncode(base::StringPiece data); | |
133 | |
134 // This converts 2*'num' hexadecimal characters to 'num' binary data. | |
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); | |
138 | |
139 // 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 | |
141 // printed as '.' in the ASCII output. | |
142 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." | |
143 static std::string HexDump(base::StringPiece binary_data); | |
144 | |
145 private: | |
146 DISALLOW_COPY_AND_ASSIGN(QuicUtils); | |
147 }; | |
148 | |
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 | |
159 | |
160 #endif // NET_QUIC_QUIC_UTILS_H_ | |
OLD | NEW |