blob: 726cef42ad7741491f8becb3b3f7b7936667eebd [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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]cc7ec7f2014-01-25 20:32:515// Some helpers for quic.
[email protected]8b37a092012-10-18 21:53:496
7#ifndef NET_QUIC_QUIC_UTILS_H_
8#define NET_QUIC_QUIC_UTILS_H_
9
Avi Drissman13fc8932015-12-20 04:40:4610#include <stddef.h>
11#include <stdint.h>
12
mefcb6537c2014-11-12 19:38:5013#include <string>
14
Avi Drissman13fc8932015-12-20 04:40:4615#include "base/macros.h"
rtennetic14c8ab2015-06-18 05:47:4016#include "base/strings/string_piece.h"
[email protected]165e0752012-11-16 07:49:4417#include "net/base/int128.h"
[email protected]8b37a092012-10-18 21:53:4918#include "net/base/net_export.h"
19#include "net/quic/quic_protocol.h"
[email protected]8b37a092012-10-18 21:53:4920
jdorfman4ea54a22016-01-21 22:12:5021#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]8b37a092012-10-18 21:53:4931namespace net {
32
33class NET_EXPORT_PRIVATE QuicUtils {
34 public:
[email protected]899951652013-05-16 12:52:3935 enum Priority {
36 LOCAL_PRIORITY,
37 PEER_PRIORITY,
38 };
39
[email protected]cc7ec7f2014-01-25 20:32:5140 // Returns the 64 bit FNV1a hash of the data. See
[email protected]c244c5a12013-05-07 20:55:0441 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
Avi Drissman13fc8932015-12-20 04:40:4642 static uint64_t FNV1a_64_Hash(const char* data, int len);
[email protected]c244c5a12013-05-07 20:55:0443
[email protected]8b37a092012-10-18 21:53:4944 // returns the 128 bit FNV1a hash of the data. See
45 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
rtennetifb8763f22015-02-11 18:05:5846 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]899951652013-05-16 12:52:3955 // 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 //
rtennetibe635732014-10-02 22:51:4261 // If |out_index| is non-nullptr and a match is found then the index of that
[email protected]899951652013-05-16 12:52:3962 // 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]752fbe52013-10-14 08:35:3270 // SerializeUint128 writes the first 96 bits of |v| in little-endian form
71 // to |out|.
Avi Drissman13fc8932015-12-20 04:40:4672 static void SerializeUint128Short(uint128 v, uint8_t* out);
[email protected]752fbe52013-10-14 08:35:3273
[email protected]74bda142013-03-31 02:49:1174 // 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]8b37a092012-10-18 21:53:4978 static const char* ErrorToString(QuicErrorCode error);
[email protected]899951652013-05-16 12:52:3979
[email protected]0bbeb6972013-05-23 04:10:2180 // Returns the level of encryption as a char*
81 static const char* EncryptionLevelToString(EncryptionLevel level);
82
[email protected]5c34371f2014-03-13 21:23:5283 // Returns TransmissionType as a char*
84 static const char* TransmissionTypeToString(TransmissionType type);
85
[email protected]899951652013-05-16 12:52:3986 // 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]502293e2013-05-24 09:41:2791
mefcb6537c2014-11-12 19:38:5092 // 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]502293e2013-05-24 09:41:2797 // 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]76c35b02013-08-07 10:06:37103
ianswett7b88c0a2016-06-10 22:29:58104 // Returns PeerAddressChangeType as a std::string.
105 static std::string PeerAddressChangeTypeToString(PeerAddressChangeType type);
106
[email protected]76c35b02013-08-07 10:06:37107 static char* AsChars(unsigned char* data) {
108 return reinterpret_cast<char*>(data);
109 }
[email protected]c05a6d222013-12-16 19:42:03110
rchcaec4242016-01-22 20:49:52111 // 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
jric533399b2016-01-29 07:36:01117 // Deletes and clears all the frames and the packet from serialized packet.
118 static void ClearSerializedPacket(SerializedPacket* serialized_packet);
119
rtennetif85706db2016-02-04 02:43:03120 // 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
rtenneti2cae2072016-02-05 02:21:33126 // Allocates a new char[] of size |packet.encrypted_length| and copies in
127 // |packet.encrypted_buffer|.
128 static char* CopyBuffer(const SerializedPacket& packet);
129
fayanga64c1a92016-02-13 01:55:58130 // 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
rch16fe57a2016-05-10 21:34:33136 // 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
ckrasic6567aa52016-07-08 09:24:35146 // 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);
rch16fe57a2016-05-10 21:34:33152
[email protected]b99c0fc2014-04-22 07:56:52153 private:
154 DISALLOW_COPY_AND_ASSIGN(QuicUtils);
[email protected]8b37a092012-10-18 21:53:49155};
156
rtennetic14c8ab2015-06-18 05:47:40157// Utility function that returns an QuicIOVector object wrapped around |str|.
158// |str|'s data is stored in |iov|.
159inline 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]5dafdb62013-11-14 01:24:26164}
165
[email protected]8b37a092012-10-18 21:53:49166} // namespace net
167
168#endif // NET_QUIC_QUIC_UTILS_H_