A
A
Arkady Baganin2021-02-09 22:21:23
Computer networks
Arkady Baganin, 2021-02-09 22:21:23

What is the working principle of STUN?

Everyone, good day! It became interesting to me how to implement p2p data transfer ... For this I decided to use the tcp / ip protocol. I learned that everything is combined (local PCs in the provider's network) in a NAT server. And for NAT to exchange data (I don’t need local p2p) there are many ways, but the main ones are:
1. Port forwarding (tried Mono.Nat for C #, miniupnp for python) - nothing worked
2. STUN server, like, the code that I found It works on the Internet, but somehow incorrectly, it only sends, but does not receive data. (I'll attach the code below) But I can't understand why STUN is needed? To find out the external ip? So my friend can just throw it, but I won’t be able to connect, because we simply won’t find each other ...

Implementation in c# for the curious

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using LumiSoft.Net.STUN.Client;

namespace Windows_RDR_Host
{
    class Program
    {
        public const string StunServer = "stun4.l.google.com";
        public const int StunServerPort = 19302;
        public static STUN_Result STUN;
        private static Socket socket;

        static void Main(string[] args)
        {
            InitSTUN();
            Console.WriteLine("Your public address: {0}", STUN.PublicEndPoint);

            new Thread(StartSendCycle).Start();
            Console.ReadKey();
        }


        private static void StartSendCycle()
        {
            var data = Encoding.ASCII.GetBytes("Hello, RDP!");

            var sender = new UdpClient(STUN.PublicEndPoint.Port, AddressFamily.InterNetwork);
            sender.AllowNatTraversal(true);

            IPEndPoint ipStun = STUN.PublicEndPoint;

            while (true)
            {
                if (sender.Available > 0)
                {
                    byte[] res = sender.Receive(ref ipStun);

                    Console.WriteLine("Receive: {0}", Encoding.ASCII.GetString(res));
                }

                sender.Send(data, data.Length, STUN.PublicEndPoint);
                Console.WriteLine("Sent " + data.Length + " bytes");
                Thread.Sleep(500);
            }
        }

        public static void InitSTUN()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(new IPEndPoint(IPAddress.Any, 0));
            STUN = STUN_Client.Query(StunServer, StunServerPort, socket);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2021-02-10
@ark_yt

STUN is needed to find out your external ip and, more importantly, the external port associated with the internal one. Another STUN server is needed to find out the type of NAT. If one of you has Symmetric, and the other has RestrictedCone or PortRestrictedCone, then you can forget about P2P. More STUN is not good for anything. In the case of VoIP, clients wishing to connect exchange via SIP addresses received via STUN and direct traffic flow to each other. In the case of restricted NAT, the transmission must start from both sides, otherwise the NAT of the receiving side decides that the packets need to be dropped. Teredo, for example, starts sending dummy packets, the receipt of which symbolizes that the connection has already been established. You can write your own Teredo client from the spec, it's quite simple.
STUN is such a cutie protocol because it is just an addition to SIP. But that doesn't stop you from using it for your needs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question