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