textattrs.go 2.8 KB

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