Exemple #1
0
        /// <summary>
        /// Initializes a new instance of tox dns3.
        /// </summary>
        /// <param name="publicKey"></param>
        public ToxDns(string publicKey)
        {
            _toxDns3 = ToxDnsFunctions.New(ToxTools.StringToHexBin(publicKey));

            if (_toxDns3 == null || _toxDns3.IsInvalid)
            {
                throw new Exception("Could not create a new tox_dns3 instance with the provided public_key");
            }
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of tox dns3.
        /// </summary>
        /// <param name="publicKey">The public key that this instance of toxdns should be initialized with.</param>
        public ToxDns(ToxKey publicKey)
        {
            _toxDns3 = ToxDnsFunctions.New(publicKey.GetBytes());

            if (_toxDns3 == null || _toxDns3.IsInvalid)
            {
                throw new Exception("Could not create a new tox_dns3 instance with the provided publicKey");
            }
        }
Exemple #3
0
        /// <summary>
        /// Decodes and decrypts the dns3 string returned by <see cref="GenerateDns3String"/>.
        /// </summary>
        /// <param name="dns3String">String to decrypt.</param>
        /// <param name="requestId">The request id retrieved with GenerateDns3String.</param>
        /// <returns></returns>
        public string DecryptDns3TXT(string dns3String, int requestId)
        {
            ThrowIfDisposed();

            byte[] id            = new byte[ToxConstants.AddressSize];
            byte[] idRecordBytes = Encoding.UTF8.GetBytes(dns3String);

            int result = ToxDnsFunctions.DecryptDns3TXT(_toxDns3, id, idRecordBytes, (uint)idRecordBytes.Length, ToxTools.Map(requestId));

            if (result == 0)
            {
                return(ToxTools.HexBinToString(id));
            }
            else
            {
                throw new Exception("Could not decrypt and decode id_record");
            }
        }
Exemple #4
0
        /// <summary>
        /// Decodes and decrypts the dns3 string returned by <see cref="GenerateDns3String"/>.
        /// </summary>
        /// <param name="dns3String"></param>
        /// <param name="requestId"></param>
        /// <returns></returns>
        public string DecryptDns3TXT(string dns3String, uint requestId)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            byte[] id            = new byte[32 + sizeof(uint) + sizeof(ushort)];
            byte[] idRecordBytes = Encoding.UTF8.GetBytes(dns3String);

            int result = ToxDnsFunctions.DecryptDns3TXT(_toxDns3, id, idRecordBytes, (uint)idRecordBytes.Length, (uint)requestId);

            if (result == 0)
            {
                return(ToxTools.HexBinToString(id));
            }
            else
            {
                throw new Exception("Could not decrypt and decode id_record");
            }
        }
Exemple #5
0
        /// <summary>
        /// Generates a dns3 string used to query the dns server.
        /// </summary>
        /// <param name="name">Name of the registered user.</param>
        /// <param name="requestId">The request id, to be used when calling DecryptDns3TXT.</param>
        /// <returns></returns>
        public string GenerateDns3String(string name, out int requestId)
        {
            ThrowIfDisposed();

            byte[] bytes  = Encoding.UTF8.GetBytes(name);
            byte[] result = new byte[1024];

            uint id     = new uint();
            int  length = ToxDnsFunctions.GenerateDns3String(_toxDns3, result, (ushort)result.Length, ref id, bytes, (byte)bytes.Length);

            requestId = ToxTools.Map(id);

            if (length != -1)
            {
                return(Encoding.UTF8.GetString(result, 0, length));
            }
            else
            {
                throw new Exception("Failed to generate a dns3 string");
            }
        }
Exemple #6
0
        /// <summary>
        /// Generates a dns3 string used to query the dns server.
        /// </summary>
        /// <param name="name">Name of the registered user.</param>
        /// <param name="requestId"></param>
        /// <returns></returns>
        public string GenerateDns3String(string name, out uint requestId)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            byte[] bytes  = Encoding.UTF8.GetBytes(name);
            byte[] result = new byte[1024];

            uint id     = new uint();
            int  length = ToxDnsFunctions.GenerateDns3String(_toxDns3, result, (ushort)result.Length, ref id, bytes, (byte)bytes.Length);

            requestId = id;

            if (length != -1)
            {
                return(Encoding.UTF8.GetString(result, 0, length));
            }
            else
            {
                throw new Exception("Failed to generate a dns3 string");
            }
        }
 /// <summary>
 /// Executes tox_dns3_kill to free the toxdns handle.
 /// </summary>
 /// <returns></returns>
 protected override bool ReleaseHandle()
 {
     ToxDnsFunctions.Kill(handle);
     return(true);
 }