Example #1
0
        private void HandleStunRequest(IPEndPoint from, byte[] data)
        {
            try
            {
                if (StunPacket.TryParse(data, out var packet) != StunPacket.ResultCodes.Ok)
                {
                    return;
                }

                if (packet.MessageType != StunPacket.MessageTypes.BindingRequest || packet.MessageIntegrity != null)
                {
                    return;
                }

                var answer = new StunPacket
                {
                    MessageType      = StunPacket.MessageTypes.BindingSuccessResponse,
                    TransactionId    = packet.TransactionId,
                    XorMappedAddress = from,
                    Fingerprint      = true,
                };

                _stunServer.Send(from, answer.Pack());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Example #2
0
        private Client FindClientAndCheckIntegrity(StunPacket packet)
        {
            if (packet.MessageIntegrity == null || packet.Username == null)
            {
                return(null);
            }

            var client = Clients.Find(a => a.Ufrag == packet.Username.Split(':')[1]);

            if (client == null)
            {
                return(null);
            }

            if (!packet.VerifyIntegrity(IcePasswordBytes))
            {
                return(null);
            }

            return(client);
        }
Example #3
0
        private void HandleRtpPacket(IPEndPoint from, byte[] data)
        {
            try
            {
                var client = FindClient(from);

                if (client != null &&
                    client.SrtpContext.TryParseSrtpPacket(data, out var rtp) == RtpPacket.ResultCodes.Ok)
                {
                    _debugFile.Write(rtp.Payload, 0, rtp.Payload.Length);

                    foreach (var another in Clients)
                    {
                        IPEndPoint remoteEp;
                        byte[]     packet;

                        rtp.Ssrc = client.ClientId;

                        if (client == another)
                        {
                            continue;
                        }
                        if ((remoteEp = another.RemoteRtp) == null)
                        {
                            continue;
                        }
                        if ((packet = another.SrtpContext.PackSrtpPacket(rtp)) == null)
                        {
                            continue;
                        }

                        _rtpServer.Send(remoteEp, packet);
                    }
                }
                else if (StunPacket.TryParse(data, out var stun) == StunPacket.ResultCodes.Ok)
                {
                    if (stun.MessageType != StunPacket.MessageTypes.BindingRequest)
                    {
                        return;
                    }

                    client = FindClientAndCheckIntegrity(stun);
                    if (client == null)
                    {
                        Console.Error.WriteLineAsync($"Cannot found client with username {stun.Username}");
                        // TODO implement error
                        //var answer = new StunPacket
                        //{
                        //    MessageType = StunPacket.MessageTypes.BindingSuccessResponse,
                        //    TransactionId = packet.TransactionId,
                        //    Fingerprint = true,
                        //};

                        //_rtpServer.Send(from, answer.Pack());
                        return;
                    }

                    var answer = new StunPacket
                    {
                        MessageType         = StunPacket.MessageTypes.BindingSuccessResponse,
                        TransactionId       = stun.TransactionId,
                        XorMappedAddress    = from,
                        MessageIntegrityKey = IcePasswordBytes,
                        Fingerprint         = true,
                    };

                    client.SetRtpParam(from);
                    _rtpServer.Send(from, answer.Pack());
                    client.Dtls.DoHandshake(() => client.SrtpContext.SetMasterKeys(client.Dtls.Ssl, true));
                }
                else if (DtlsPacket.CheckPacket(data, 0) == DtlsPacket.ResultCodes.Ok)
                {
                    if (client == null)
                    {
                        Console.Error.WriteLineAsync($"Cannot found client with ip {from}");
                        return;
                    }
                    client.Dtls.Add(data);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }