bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

protocol constants enums

Open mkalex777 opened this issue 4 years ago • 0 comments

I'm not sure, is there something stopping to use standard enums instead of int/short/byte consts?

Using enum make life much more easier. It's performance free and you can freely use it since it was supported from the first version of C#.

For example:

    public enum ExtensionType : ushort
    {
        /*
         * RFC 2546 2.3.
         */
        server_name = 0,
        max_fragment_length = 1,
        client_certificate_url = 2,
        trusted_ca_keys = 3,
        truncated_hmac = 4,
        status_request = 5,

        renegotiation_info = 0xff01,
}

when you needs to pass it as int, just add explicit cast:

   ExtensionType type = ExtensionType.max_fragment_length;
   Console.WriteLine("{0} = {1}", type, (int)type);
   
   type = (ExtensionType)77777777;
   Console.WriteLine("{0} = {1}", type, (int)type);
max_fragment_length = 1
77777777 = 77777777

By default enum is derived from int, but in case of needs you can derive it from byte or ushort:

    public enum ContentType : byte
    {
        change_cipher_spec = 20,
        alert = 21,
        handshake = 22,
        application_data = 23,
        heartbeat = 24,

I notice that a new src/tls code has switched from byte to short for ContentType. Is there reason for that? According to the TLS protocol it takes just one byte. Isn't it?

mkalex777 avatar Sep 08 '21 08:09 mkalex777