summary refs log tree commit diff
path: root/crypto/src/tls/HeartbeatExtension.cs
blob: c44d84a678a0ce75402c8eb8acc76ccf7462441b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.IO;

namespace Org.BouncyCastle.Tls
{
    public sealed class HeartbeatExtension
    {
        private readonly short m_mode;

        public HeartbeatExtension(short mode)
        {
            if (!HeartbeatMode.IsValid(mode))
                throw new ArgumentException("not a valid HeartbeatMode value", "mode");

            this.m_mode = mode;
        }

        public short Mode
        {
            get { return m_mode; }
        }

        /// <summary>Encode this <see cref="HeartbeatExtension"/> to a <see cref="Stream"/>.</summary>
        /// <param name="output">the <see cref="Stream"/> to encode to.</param>
        /// <exception cref="IOException"/>
        public void Encode(Stream output)
        {
            TlsUtilities.WriteUint8(m_mode, output);
        }

        /// <summary>Parse a <see cref="HeartbeatExtension"/> from a <see cref="Stream"/>.</summary>
        /// <param name="input">the <see cref="Stream"/> to parse from.</param>
        /// <returns>a <see cref="HeartbeatExtension"/> object.</returns>
        /// <exception cref="IOException"/>
        public static HeartbeatExtension Parse(Stream input)
        {
            short mode = TlsUtilities.ReadUint8(input);
            if (!HeartbeatMode.IsValid(mode))
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);

            return new HeartbeatExtension(mode);
        }
    }
}