Example #1
0
    public static StunMessageType Parse(Bits bits)
    {
      if (bits.Count != 14)
      {
        throw new ArgumentException("The number of bits must be 14.", nameof(bits));
      }

      var methodBits = new Bits(12);
      var classBits = new Bits(2);

      methodBits.AddBits(bits.PopBits(5));
      classBits.AddBit(bits.Pop());
      methodBits.AddBits(bits.PopBits(3));
      classBits.AddBit(bits.Pop());
      methodBits.AddBits(bits);

      return new StunMessageType(StunMessageMethod.Parse(methodBits), StunMessageClass.Parse(classBits));
    }
Example #2
0
    public StunMessageType(StunMessageMethod method, StunMessageClass @class)
    {
      if (!method.PermittedClasses.Contains(@class))
      {
        throw new ArgumentException($"The class {@class} is not permitted by the method {method}.", nameof(@class));
      }
      
      Name = method.Name + " " + @class.Name;

      var methodBits = new Bits(method.Bits);
      var classBits = new Bits(@class.Bits);
      var bits = new Bits(14);
      bits.AddBits(methodBits.PopBits(5));
      bits.AddBit(classBits.Pop());
      bits.AddBits(methodBits.PopBits(3));
      bits.AddBit(classBits.Pop());
      bits.AddBits(methodBits);

      Bits = bits.ToBitArray();
    }
Example #3
0
    public byte[] ToLittleEndianByteArray()
    {
      var bits = new Bits(20);

      bits.AddOffBit();
      bits.AddOffBit();

      bits.AddBits(Type.Bits);

      var length = (ushort)(Attributes.Count() * 4 * 8);
      bits.AddUInt16LittleEndian(length);

      bits.AddUInt32LittleEndian(MagicCookie);

      bits.AddBytesLittleEndian(Id.Bytes);

      return bits.ToLittleEndianByteArray();
    }