textattrs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package stun
  4. // NewUsername returns Username with provided value.
  5. func NewUsername(username string) Username {
  6. return Username(username)
  7. }
  8. // Username represents USERNAME attribute.
  9. //
  10. // RFC 5389 Section 15.3
  11. type Username []byte
  12. func (u Username) String() string {
  13. return string(u)
  14. }
  15. const maxUsernameB = 513
  16. // AddTo adds USERNAME attribute to message.
  17. func (u Username) AddTo(m *Message) error {
  18. return TextAttribute(u).AddToAs(m, AttrUsername, maxUsernameB)
  19. }
  20. // GetFrom gets USERNAME from message.
  21. func (u *Username) GetFrom(m *Message) error {
  22. return (*TextAttribute)(u).GetFromAs(m, AttrUsername)
  23. }
  24. // NewRealm returns Realm with provided value.
  25. // Must be SASL-prepared.
  26. func NewRealm(realm string) Realm {
  27. return Realm(realm)
  28. }
  29. // Realm represents REALM attribute.
  30. //
  31. // RFC 5389 Section 15.7
  32. type Realm []byte
  33. func (n Realm) String() string {
  34. return string(n)
  35. }
  36. const maxRealmB = 763
  37. // AddTo adds NONCE to message.
  38. func (n Realm) AddTo(m *Message) error {
  39. return TextAttribute(n).AddToAs(m, AttrRealm, maxRealmB)
  40. }
  41. // GetFrom gets REALM from message.
  42. func (n *Realm) GetFrom(m *Message) error {
  43. return (*TextAttribute)(n).GetFromAs(m, AttrRealm)
  44. }
  45. const softwareRawMaxB = 763
  46. // Software is SOFTWARE attribute.
  47. //
  48. // RFC 5389 Section 15.10
  49. type Software []byte
  50. func (s Software) String() string {
  51. return string(s)
  52. }
  53. // NewSoftware returns *Software from string.
  54. func NewSoftware(software string) Software {
  55. return Software(software)
  56. }
  57. // AddTo adds Software attribute to m.
  58. func (s Software) AddTo(m *Message) error {
  59. return TextAttribute(s).AddToAs(m, AttrSoftware, softwareRawMaxB)
  60. }
  61. // GetFrom decodes Software from m.
  62. func (s *Software) GetFrom(m *Message) error {
  63. return (*TextAttribute)(s).GetFromAs(m, AttrSoftware)
  64. }
  65. // Nonce represents NONCE attribute.
  66. //
  67. // RFC 5389 Section 15.8
  68. type Nonce []byte
  69. // NewNonce returns new Nonce from string.
  70. func NewNonce(nonce string) Nonce {
  71. return Nonce(nonce)
  72. }
  73. func (n Nonce) String() string {
  74. return string(n)
  75. }
  76. const maxNonceB = 763
  77. // AddTo adds NONCE to message.
  78. func (n Nonce) AddTo(m *Message) error {
  79. return TextAttribute(n).AddToAs(m, AttrNonce, maxNonceB)
  80. }
  81. // GetFrom gets NONCE from message.
  82. func (n *Nonce) GetFrom(m *Message) error {
  83. return (*TextAttribute)(n).GetFromAs(m, AttrNonce)
  84. }
  85. // TextAttribute is helper for adding and getting text attributes.
  86. type TextAttribute []byte
  87. // AddToAs adds attribute with type t to m, checking maximum length. If maxLen
  88. // is less than 0, no check is performed.
  89. func (v TextAttribute) AddToAs(m *Message, t AttrType, maxLen int) error {
  90. if err := CheckOverflow(t, len(v), maxLen); err != nil {
  91. return err
  92. }
  93. m.Add(t, v)
  94. return nil
  95. }
  96. // GetFromAs gets t attribute from m and appends its value to reseted v.
  97. func (v *TextAttribute) GetFromAs(m *Message, t AttrType) error {
  98. a, err := m.Get(t)
  99. if err != nil {
  100. return err
  101. }
  102. *v = a
  103. return nil
  104. }