1 // Copyright (c) 2013 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.
5 #ifndef NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_
6 #define NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_export.h"
14 #include "net/quic/quic_protocol.h"
18 // An intermediate format of a handshake message that's convenient for a
19 // CryptoFramer to serialize from or parse into.
20 class NET_EXPORT_PRIVATE CryptoHandshakeMessage
{
22 CryptoHandshakeMessage();
23 CryptoHandshakeMessage(const CryptoHandshakeMessage
& other
);
24 ~CryptoHandshakeMessage();
26 CryptoHandshakeMessage
& operator=(const CryptoHandshakeMessage
& other
);
31 // GetSerialized returns the serialized form of this message and caches the
32 // result. Subsequently altering the message does not invalidate the cache.
33 const QuicData
& GetSerialized() const;
35 // MarkDirty invalidates the cache created by |GetSerialized|.
38 // SetValue sets an element with the given tag to the raw, memory contents of
40 template<class T
> void SetValue(QuicTag tag
, const T
& v
) {
42 std::string(reinterpret_cast<const char*>(&v
), sizeof(v
));
45 // SetVector sets an element with the given tag to the raw contents of an
46 // array of elements in |v|.
47 template<class T
> void SetVector(QuicTag tag
, const std::vector
<T
>& v
) {
49 tag_value_map_
[tag
] = std::string();
51 tag_value_map_
[tag
] = std::string(reinterpret_cast<const char*>(&v
[0]),
52 v
.size() * sizeof(T
));
56 // Returns the message tag.
57 QuicTag
tag() const { return tag_
; }
58 // Sets the message tag.
59 void set_tag(QuicTag tag
) { tag_
= tag
; }
61 const QuicTagValueMap
& tag_value_map() const { return tag_value_map_
; }
63 // SetTaglist sets an element with the given tag to contain a list of tags,
64 // passed as varargs. The argument list must be terminated with a 0 element.
65 void SetTaglist(QuicTag tag
, ...);
67 void SetStringPiece(QuicTag tag
, base::StringPiece value
);
69 // Erase removes a tag/value, if present, from the message.
70 void Erase(QuicTag tag
);
72 // GetTaglist finds an element with the given tag containing zero or more
73 // tags. If such a tag doesn't exist, it returns false. Otherwise it sets
74 // |out_tags| and |out_len| to point to the array of tags and returns true.
75 // The array points into the CryptoHandshakeMessage and is valid only for as
76 // long as the CryptoHandshakeMessage exists and is not modified.
77 QuicErrorCode
GetTaglist(QuicTag tag
, const QuicTag
** out_tags
,
78 size_t* out_len
) const;
80 bool GetStringPiece(QuicTag tag
, base::StringPiece
* out
) const;
82 // GetNthValue24 interprets the value with the given tag to be a series of
83 // 24-bit, length prefixed values and it returns the subvalue with the given
85 QuicErrorCode
GetNthValue24(QuicTag tag
,
87 base::StringPiece
* out
) const;
88 QuicErrorCode
GetUint32(QuicTag tag
, uint32
* out
) const;
89 QuicErrorCode
GetUint64(QuicTag tag
, uint64
* out
) const;
91 // size returns 4 (message tag) + 2 (uint16, number of entries) +
92 // (4 (tag) + 4 (end offset))*tag_value_map_.size() + ∑ value sizes.
95 // set_minimum_size sets the minimum number of bytes that the message should
96 // consume. The CryptoFramer will add a PAD tag as needed when serializing in
97 // order to ensure this. Setting a value of 0 disables padding.
99 // Padding is useful in order to ensure that messages are a minimum size. A
100 // QUIC server can require a minimum size in order to reduce the
101 // amplification factor of any mirror DoS attack.
102 void set_minimum_size(size_t min_bytes
);
104 size_t minimum_size() const;
106 // DebugString returns a multi-line, string representation of the message
107 // suitable for including in debug output.
108 std::string
DebugString() const;
111 // GetPOD is a utility function for extracting a plain-old-data value. If
112 // |tag| exists in the message, and has a value of exactly |len| bytes then
113 // it copies |len| bytes of data into |out|. Otherwise |len| bytes at |out|
116 // If used to copy integers then this assumes that the machine is
118 QuicErrorCode
GetPOD(QuicTag tag
, void* out
, size_t len
) const;
120 std::string
DebugStringInternal(size_t indent
) const;
123 QuicTagValueMap tag_value_map_
;
125 size_t minimum_size_
;
127 // The serialized form of the handshake message. This member is constructed
129 mutable scoped_ptr
<QuicData
> serialized_
;
134 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_