stun.go 1.2 KB

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