Ejemplo n.º 1
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x13, 0x00, 0x48, 0x21, 0x12, 0xa4, 0x42, 0xee, 0x29,
                0xdd, 0x2d, 0x7a, 0xe9, 0x9c, 0xf4, 0x00, 0x82, 0xf2, 0x5e,
                0x00, 0x09, 0x00, 0x10, 0x00, 0x00, 0x04, 0x01, 0x55, 0x6e,
                0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64,
                0x00, 0x15, 0x00, 0x10, 0x37, 0x35, 0x64, 0x34, 0x35, 0x34,
                0x31, 0x39, 0x63, 0x33, 0x39, 0x33, 0x34, 0x33, 0x66, 0x65,
                0x00, 0x14, 0x00, 0x0a, 0x74, 0x77, 0x69, 0x6c, 0x69, 0x6f,
                0x2e, 0x63, 0x6f, 0x6d, 0x03, 0x6d, 0x80, 0x22, 0x00, 0x04,
                0x4e, 0x6f, 0x6e, 0x65, 0x80, 0x28, 0x00, 0x04, 0x6e, 0x1e,
                0x55, 0x49,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (AllocateErrorResponse)await StunMessage.Parse(stream);

                Assert.Equal(
                    new byte[]
                {
                    0xee, 0x29, 0xdd, 0x2d, 0x7a, 0xe9, 0x9c, 0xf4, 0x00, 0x82,
                    0xf2, 0x5e,
                },
                    response.TransactionId);
                Assert.Equal(401, response.ErrorCode);
                Assert.Equal("twilio.com", response.Realm);
                Assert.Equal(
                    new byte[]
                {
                    0x37, 0x35, 0x64, 0x34, 0x35, 0x34,
                    0x31, 0x39, 0x63, 0x33, 0x39, 0x33,
                    0x34, 0x33, 0x66, 0x65,
                },
                    response.Nonce
                    );
            }
        }
Ejemplo n.º 2
0
        private async Task ProcessMessage()
        {
            NetworkStream stream = _control.GetStream();

            while (true)
            {
                StunMessage message = await StunMessage.Parse(stream);

                if (_connectAttempted != null &&
                    message is ConnectionAttempt attempt)
                {
                    _connectAttempted.SetResult(attempt);
                }
                else if (_responses.TryGetValue(
                             message.TransactionId,
                             out TaskCompletionSource <StunMessage> tcs))
                {
                    tcs.SetResult(message);
                    _responses.Remove(message.TransactionId);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x00, 0x1c, 0x00, 0x24, 0x21, 0x12, 0xa4, 0x42, 0x8e, 0x3b,
                0x82, 0x73, 0xda, 0x6f, 0xc3, 0xa9, 0x1e, 0x88, 0x8f, 0x0b,
                0x00, 0x2a, 0x00, 0x04, 0x21, 0x5a, 0x86, 0x01, 0x00, 0x12,
                0x00, 0x08, 0x00, 0x01, 0x60, 0xc6, 0x17, 0x53, 0x9b, 0x96,
                0x80, 0x22, 0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65, 0x80, 0x28,
                0x00, 0x04, 0x58, 0x07, 0xfb, 0x8e,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (ConnectionAttempt)await StunMessage.Parse(stream);

                Assert.Equal(
                    new byte[] { 0x21, 0x5a, 0x86, 0x01 },
                    response.ConnectionId);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Does STUN transaction. Returns transaction response or null if transaction failed.
        /// </summary>
        /// <param name="request">STUN message.</param>
        /// <param name="socket">Socket to use for send/receive.</param>
        /// <param name="remoteEndPoint">Remote end point.</param>
        /// <param name="timeout">Timeout in milliseconds.</param>
        /// <returns>Returns transaction response or null if transaction failed.</returns>
        private static StunMessage DoTransaction(StunMessage request, Socket socket, IPEndPoint remoteEndPoint, int timeout)
        {
            var requestBytes = request.ToByteData();
            var startTime    = DateTime.Now;

            // Retransmit with 500 ms.
            while (startTime.AddMilliseconds(timeout) > DateTime.Now)
            {
                try
                {
                    socket.SendTo(requestBytes, remoteEndPoint);

                    // We got response.
                    if (socket.Poll(500 * 1000, SelectMode.SelectRead))
                    {
                        var receiveBuffer = new byte[512];
                        socket.Receive(receiveBuffer);

                        // Parse message
                        var response = new StunMessage();
                        response.Parse(receiveBuffer);

                        // Check that transaction ID matches or not response what we want.
                        if (NetUtils.CompareArray(request.TransactionId, response.TransactionId))
                        {
                            return(response);
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        public async Task <NetworkStream> AcceptRelayedStreamAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            while (true)
            {
                await EnsureConnection();

                ConnectionAttempt attempt =
                    await _connectionAttempts.DequeueAsync(cancellationToken);

                byte[]        id            = attempt.ConnectionId;
                var           bindRequest   = new ConnectionBindRequest(id);
                var           relayedClient = new TcpClient(_host, _port);
                NetworkStream relayedStream = relayedClient.GetStream();

                try
                {
                    await SendMessageAsync(relayedStream, bindRequest, cancellationToken);

                    StunMessage bindResponse = await StunMessage.Parse(relayedStream);

                    if (bindResponse is ConnectionBindSuccessResponse)
                    {
                        _relayedClients.Add(relayedClient);
                        return(relayedStream);
                    }

                    throw new TurnClientException("ConnectionBind failed.", bindResponse);
                }
                catch (IOException e)
                {
                    Log.Warning(e, "The connection seems to disconnect before parsing; ignored.");
                    continue;
                }
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> IsConnectable(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                using var client = new TcpClient();
#pragma warning disable PC001 // API not supported on all platforms
                client.Connect(_host, _port);
#pragma warning restore PC001 // API not supported on all platforms
                NetworkStream stream = client.GetStream();

                var request = new BindingRequest();
                var asBytes = request.Encode(this);
                await stream.WriteAsync(asBytes, 0, asBytes.Length, cancellationToken);

                await StunMessage.Parse(stream);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }