search
HomeWeb Front-endJS TutorialStun Protocol, Port and Traffic

STUN is a protocol that is designed to overcome barriers in communication that are introduced by NAT.

STUN protocol enables devices that are behind a NAT to discover their own public IP address and port number.

The devices communicate with a STUN server that is on the internet, and the STUN server provides this information to the client device

The device can then share this information with another device or devices on the Internet with which it wants to communicate

This allows external devices to communicate with each other directly, effectively traversing the NAT

STUN is important in facilitating peer-to-peer communication in real time.

If you want to know more about what is a STUN server, then you can refer to our article: Stun Server: What is Session Traversal Utilities for NAT?


Stun Protocol, Port and Traffic

Understanding the NAT Problem

Explanation of Network Address Translation (NAT) and its widespread use.

Routers map multiple private IP addresses to a single IP public IP address using a technique called NAT or Network Address Translation

This allows multiple devices that are on the local network to connect to the internet using a single public IP.

Thus conserving the limited number of IPv4 addresses.

NAT become widespread due to exponential growth in devices that were connected to the internet and there was a delay in adopting IPv6 which offers a larger address space.

Issues Introduced by NAT in peer-to-peer communication

NAT blocks direct communication between devices by blocking inbound traffic, altering the port mappings and hiding the public IP address from the devices that are behind the NAT

Thus overcoming barriers created by NAT is important for enabling direct communication between devices that are on different networks across the internet

NAT transversal solutions like STUN protocol allow devices to discover their own public IP address and negotiate connections through NAT routers

Many times STUN servers are not enough for NAT traversal and you need to fallback on TURN servers

If you are implementing one to one communication and need a reliable STUN and TURN servers then you can consider

Open Relay Project: Free TURN / STUN servers

Metered.ca TURN servers: Premium TURN servers with global reach

Services like VoIP and Video conferencing require STUN and TURN servers.


STUN Protocol: Solution for NAT Traversal

What is STUN?

STUN or Session Traversal Utilities for NAT is a standardized protocol that is defined in the RFC 5389 that enables devices that are behind a NAT or firewall to discover their own public IP address and PORT number

STUN also lets devices and applications discover what kind of NAT they are behind and obtain the necessary information to establish a direct communication channel with other devices on the internet

The core functionalities of STUN include:

  1. Public IP address discovery: STUN allows a client device to learn its public IP address

  2. Port Mapping: STUN helps the client device know what port number it has been assigned by the NAT device

  3. NAT type detection: The STUN server helps client device know what kind of NAT device it is behind. NAT types include full cone NAT, restricted cone NAT, symmetric NAT etc

  4. Facilitating Peer-to-Peer Communication: Thus the STUN server facilitates peer-to-peer communication between devices


How STUN Works

How STUN helps devices to Discover their own Public IP Address and Port number

When a client device sends a request to the STUN server, which is on the internet. The STUN server can see the public IP address and port number from which the request is coming from.

The STUN server then sends this information back to the client. This is how the STUN server helps devices discover their own public IP and port number that is assigned to them by the NAT router.

The process enables the client to

  • Learn its public endpoint: Understand how other devices see it on the internet

  • Share correction details with peers: Once the client device gets the details from the STUN server, it can then share it with other devices on the internet with which it wants to start communication

  • Adapt NAT behaviour: Adjust strategies on how to traverse the NAT based on what type of NAT the client device is behind

Step by Step how STUN works

  1. Client Initiation

    1. The client device sends a request to the STUN server using the UDP protocol to learn its own public IP address and port number
    2. It sends a STUN binding request over UDP User datagram protocol
  2. STUN server reception

    1. The STUN server accepts the request and notes the source IP and port number from which the request is coming from.
    2. The STUN server then responds back with the IP address and port number, back to the client
  3. Binding response

    1. The STUN server contracts a STUN binding response message
    2. This response includes a MAPPED-ADDRESS attribute, client public IP address and port.
  4. Client receipt

    1. The client device then receives the binding response from the server
    2. The client device then retrieves the Public IP and port number from the MAPPED-ADDRESS attribute
  5. NAT type discovery (optional):

    1. The client can then perform additional tests from different STUN servers to determine what type of NAT the client is behind
    2. This involves sending different requests from different ports and evaluating the response
  6. Establishing communication

    1. With the public IP address and port number, the client device can then share this information with other devices on the internet to establish communication

STUN vs TURN and ICE Protocol

STUN vs other NAT Traversal Methods

  1. STUN ( Session Traversal Utilities for NAT)

    1. Purpose: Enables the client devices to discover their public IP and port number
    2. Use-Case: Good for when NAT and firewalls allow for peer-to-peer direct communication after public IP addresses are known.
    3. Limitations: Does not work when NAT and firewall rules are restrictive.
  2. TURN (Traversal Using Relays around NAT)

    1. Purpose: Provides a fallback mechanism by relaying the data through a TURN server when peer to peer direct communication is not possible
    2. Use-Case: Essential when restrictive NAT and firewall rules does not allow direct communication between devices on the internet
    3. Operation: All the traffic is end to end encrypted and send through the TURN server, which relays the data to each client
    4. Trade-Offs: You need turn servers near the client devices, in order to reduce latency and improve communication
  3. ICE (Interactive Connectivity Establishment)

    1. Purpose:ICE is a framework that combines STUN and TURN to establish the best possible connection between clients.
    2. Use-Case: Used in WebRTC and other real time communication systems in order to handle various network configurations
    3. Operation

      1. Candidate Gathering: Clients first gathers candidates to see possible endpoint connections, these are found using host, reflexive via STUN and TURN
      2. Connectivity checks: Clients then perform checks to see using which candidates they can establish a connection
      3. Candidate selection: The optimal path that is the one with the lowest latency is selected by the ICE framework for establishing the communication
    4. Advantages: Maximizes the chances of establishing a connection while optimizing for best performance

When to use STUN over other methods

  1. Use STUN alone when

    1. Both the clients are behind NAT and firewall that enable direct communication after discovering the IP address and port number
    2. Both the clients have good internet connection and the network environment is predictable, for example devices are not changing their networks like in the case of mobile phones etc
  2. Use STUN TURN and ICE when

    1. The devices are behind restrictive NAT and firewall rules
    2. The network environment is changing like in case of mobile devices etc
    3. Reliability is important, you cannot have devices disconnecting for some reason or the other

Summary of Considerations

  • Performance

    • STUN memberikan prestasi yang baik apabila menyambungkan pelanggan berdekatan tetapi ia tidak boleh dipercayai juga, tetapi ia adalah pilihan percuma
    • TURN memberikan prestasi terbaik tetapi memerlukan pelayan yang lebih dekat dengan peranti klien untuk mengurangkan kependaman. Pada masa kini, anda mempunyai perkhidmatan pelayan giliran global seperti metered.ca yang berfungsi dengan baik
  • Kerumitan:

    • STUN agak mudah untuk dilaksanakan dan digunakan
    • TURN: Ia agak ringkas dan mudah digunakan
  • Kadar Kejayaan:

    • STUN: Ia akan gagal dalam peraturan NAT dan firewall yang ketat
    • TURN: Lebih dipercayai dan berfungsi dengan peraturan NAT dan firewall yang ketat

Pelabuhan STUN dan pengendalian trafik

Port lalai yang digunakan oleh STUN

Port UDP dan TCP yang biasa digunakan ialah 3478 dan 5349

Protokol STUN menggunakan port rangkaian khusus untuk berkomunikasi antara pelanggan dan pelayan STUN

  • Pelabuhan UDP

    • Port 3478: Ini ialah port lalai untuk STUN untuk protokol UDP, STUN biasanya beroperasi melalui protokol UDP kerana sifat overhednya yang rendah dan port 3478 biasanya digunakan
    • Port 5349: Ini biasanya port untuk UDP melalui DTLS, yang menyediakan penyulitan untuk komunikasi UDP
  • Pelabuhan TCP

    • Port 3478: STUN : STUN boleh beroperasi pada port yang sama iaitu 3478 untuk TCP apabila UDP tidak sesuai atau disekat
    • Port 5349: Ini digunakan untuk STUN dengan TLS iaitu Transport Layer Security yang menawarkan sambungan TCP
  • Port ganti

    • Walaupun ini adalah port lalai untuk pelayan STUN, mana-mana port lain juga boleh dikonfigurasikan untuk digunakan untuk pelayan STUN dan TURN.
    • Atas sebab keselamatan, selalunya dinasihatkan untuk menjalankan pelayan STUN pada port lain.

Pertimbangan Keselamatan dengan penggunaan Port

  1. Pendedahan kepada pengimbasan port

    1. Penyerang sering mengimbas internet untuk mencari port lalai untuk pelayan STUN, mengekalkan pelayan STUN pada port lalai mungkin menjadikan lebih terdedah kepada aktiviti peninjauan sedemikian
  2. Tatarajah Firewall

    1. Trafik masuk

      1. Secara lalai, banyak tembok api menyekat trafik masuk yang tidak diminta pada port STUN untuk menghalang akses tanpa kebenaran
      2. Tembok api hanya membenarkan trafik masuk, jika ia sebagai tindak balas kepada permintaan sah yang telah dimulakan oleh peranti di dalam rangkaian
    2. Trafik keluar:

      1. Izinkan trafik keluar hanya kepada pelayan STUN dan TURN yang dipercayai dan pada port yang diperlukan.
  3. Menggunakan Penyulitan

    1. STUN melalui TLS/DTLS (port 5349): Semasa trafik pelayan TURN disulitkan hujung ke hujung. Trafik pelayan STUN bukan, tetapi anda boleh mendayakan penyulitan STUN juga.
  4. Kemas Kini dan tampalan Biasa: Apabila anda menjalankan pelayan STUN/TURN anda sendiri, anda perlu mengemas kininya secara kerap dan menampalnya untuk keselamatan.

  5. Log dan pemantauan: Sentiasa log dan pantau pelayan STUN dan TURN jika anda menjalankan pelayan STUN/TURN anda sendiri untuk kelemahan keselamatan


Pelayan STUN berasaskan awan lwn Penyelesaian Dihoskan Sendiri

Pelayan TURN berasaskan awan

  • Pelayan TURN/STUN bermeter: Menawarkan perkhidmatan STUN dan TURN Global, boleh skala, terurus dan TURN dengan API

  • Pelayan STUN Awam Google: Google menawarkan senarai pelayan STUN percuma, anda boleh mendapatkan senarai pelayan Google STUN

  • Pelayan TURN / STUN Relay Terbuka: Pelayan TURN percuma untuk orang ramai

Penyelesaian Dihoskan Sendiri

  • Pelayan Coturn STUN / TURN: Anda boleh menjalankan pelayan giliran anda sendiri dengan kapas projek sumber terbuka. Berikut ialah panduan tentang : Bagaimana untuk menyediakan dan mengkonfigurasi pelayan TURN menggunakan coTURN?

  • Pelayan AWS TURN: Anda juga boleh menjalankan coturn pada AWS: Berikut ialah panduan mengenainya: Pelayan AWS TURN: Dalam 7 Langkah Mudah

  • Pelayan Azure TURN: Anda juga boleh menjalankan pelayan TURN pada Azure. Berikut ialah panduan mengenainya: Pelayan Azure TURN: Panduan Langkah demi Langkah.

  • TURN kos pelayan: berikut ialah panduan tentang potensi kos dan pertimbangan apabila menjalankan pelayan TURN anda sendiri: TURN Kos Pelayan: Panduan Lengkap


Stun Protocol, Port and Traffic

Pelayan TURN bermeter

  1. API: HIDUPKAN pengurusan pelayan dengan API berkuasa. Anda boleh melakukan perkara seperti Tambah/Alih keluar bukti kelayakan melalui API, Dapatkan Setiap Pengguna / Bukti kelayakan dan metrik Pengguna melalui API, Dayakan/ Lumpuhkan bukti kelayakan melalui API, Dapatkan semula data Penggunaan mengikut tarikh melalui API.

  2. Penyasaran Geo-Lokasi Global: Menghalakan trafik ke pelayan terdekat secara automatik, untuk kependaman serendah mungkin dan prestasi kualiti tertinggi. kependaman kurang daripada 50 ms di mana-mana sahaja di seluruh dunia

  3. Pelayan di semua Wilayah di dunia: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapura, Sydney, Seoul, Dallas, New York

  4. Kependaman Rendah: kurang daripada 50 ms kependaman, di mana-mana sahaja di seluruh dunia.

  5. Kos Efektif: harga bayar semasa anda pergi dengan lebar jalur dan diskaun volum tersedia.

  6. Pentadbiran Mudah: Dapatkan log penggunaan, e-mel apabila akaun mencapai had ambang, rekod pengebilan dan sokongan e-mel serta telefon.

  7. Patuh Standard: Mematuhi RFC 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 59244, TLS dan TLS. 🎜>

  8. Berbilang‑Penyewaan: Buat berbilang bukti kelayakan dan asingkan penggunaan mengikut pelanggan atau apl yang berbeza. Dapatkan log Penggunaan, rekod pengebilan dan makluman ambang.

  9. Kebolehpercayaan Perusahaan: 99.999% Masa Beroperasi dengan SLA.

  10. Skala Perusahaan: Tanpa had pada trafik serentak atau jumlah trafik. Pelayan TURN Bermeter menyediakan Kebolehskalaan Perusahaan

  11. 5 GB/bln Percuma: Dapatkan 5 GB penggunaan pelayan TURN percuma setiap bulan dengan Pelan Percuma

  12. Berjalan pada port 80 dan 443

  13. Sokongan MENGUBAH SSL untuk membenarkan sambungan melalui tembok api pemeriksaan paket dalam.

  14. Menyokong kedua-dua TCP dan UDP

  15. STUN Tanpa Had Percuma

The above is the detailed content of Stun Protocol, Port and Traffic. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)