stun.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. // Package stun implements Session Traversal Utilities for NAT (STUN) RFC 5389.
  4. //
  5. // The stun package is intended to use by package that implements extension
  6. // to STUN (e.g. TURN) or client/server applications.
  7. //
  8. // Most methods are designed to be zero allocations. If it is not enough,
  9. // low-level methods are available. On other hand, there are helpers that
  10. // reduce code repeat.
  11. //
  12. // See examples for Message for basic usage, or https://github.com/pion/turn
  13. // package for example of stun extension implementation.
  14. package stun
  15. import (
  16. "encoding/binary"
  17. "io"
  18. )
  19. // bin is shorthand to binary.BigEndian.
  20. var bin = binary.BigEndian //nolint:gochecknoglobals
  21. func readFullOrPanic(r io.Reader, v []byte) int {
  22. n, err := io.ReadFull(r, v)
  23. if err != nil {
  24. panic(err) //nolint
  25. }
  26. return n
  27. }
  28. func writeOrPanic(w io.Writer, v []byte) int {
  29. n, err := w.Write(v)
  30. if err != nil {
  31. panic(err) //nolint
  32. }
  33. return n
  34. }
  35. // IANA assigned ports for "stun" protocol.
  36. const (
  37. DefaultPort = 3478
  38. DefaultTLSPort = 5349
  39. )
  40. type transactionIDSetter struct{}
  41. func (transactionIDSetter) AddTo(m *Message) error {
  42. return m.NewTransactionID()
  43. }
  44. // TransactionID is Setter for m.TransactionID.
  45. var TransactionID Setter = transactionIDSetter{} //nolint:gochecknoglobals