reqtrans.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package proto
  2. import (
  3. "strconv"
  4. "github.com/pion/stun"
  5. )
  6. // Protocol is IANA assigned protocol number.
  7. type Protocol byte
  8. const (
  9. // ProtoUDP is IANA assigned protocol number for UDP.
  10. ProtoUDP Protocol = 17
  11. )
  12. func (p Protocol) String() string {
  13. switch p {
  14. case ProtoUDP:
  15. return "UDP"
  16. default:
  17. return strconv.Itoa(int(p))
  18. }
  19. }
  20. // RequestedTransport represents REQUESTED-TRANSPORT attribute.
  21. //
  22. // This attribute is used by the client to request a specific transport
  23. // protocol for the allocated transport address. RFC 5766 only allows the use of
  24. // codepoint 17 (User Datagram Protocol).
  25. //
  26. // RFC 5766 Section 14.7
  27. type RequestedTransport struct {
  28. Protocol Protocol
  29. }
  30. func (t RequestedTransport) String() string {
  31. return "protocol: " + t.Protocol.String()
  32. }
  33. const requestedTransportSize = 4
  34. // AddTo adds REQUESTED-TRANSPORT to message.
  35. func (t RequestedTransport) AddTo(m *stun.Message) error {
  36. v := make([]byte, requestedTransportSize)
  37. v[0] = byte(t.Protocol)
  38. // b[1:4] is RFFU = 0.
  39. // The RFFU field MUST be set to zero on transmission and MUST be
  40. // ignored on reception. It is reserved for future uses.
  41. m.Add(stun.AttrRequestedTransport, v)
  42. return nil
  43. }
  44. // GetFrom decodes REQUESTED-TRANSPORT from message.
  45. func (t *RequestedTransport) GetFrom(m *stun.Message) error {
  46. v, err := m.Get(stun.AttrRequestedTransport)
  47. if err != nil {
  48. return err
  49. }
  50. if err = stun.CheckSize(stun.AttrRequestedTransport, len(v), requestedTransportSize); err != nil {
  51. return err
  52. }
  53. t.Protocol = Protocol(v[0])
  54. return nil
  55. }