frames.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package wire
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "sync"
  7. )
  8. type SNACError struct {
  9. Code uint16
  10. TLVRestBlock
  11. }
  12. const (
  13. FLAPFrameSignon uint8 = 0x01
  14. FLAPFrameData uint8 = 0x02
  15. FLAPFrameError uint8 = 0x03
  16. FLAPFrameSignoff uint8 = 0x04
  17. FLAPFrameKeepAlive uint8 = 0x05
  18. )
  19. const (
  20. // FLAPMaxDataSize is the maximum size of a FLAP packet's data (excluding header).
  21. FLAPMaxDataSize uint32 = 0xFFF9
  22. )
  23. type FLAPFrame struct {
  24. StartMarker uint8
  25. FrameType uint8
  26. Sequence uint16
  27. Payload []byte `oscar:"len_prefix=uint16"`
  28. }
  29. // FLAPFrameDisconnect is the last FLAP frame sent to a client before
  30. // disconnection. It differs from FLAPFrame in that there is no payload length
  31. // prefix at the end, which causes pre-multi-conn Windows AIM clients to
  32. // improperly handle server disconnections, as when the regular FLAPFrame type
  33. // is used.
  34. type FLAPFrameDisconnect struct {
  35. StartMarker uint8
  36. FrameType uint8
  37. Sequence uint16
  38. }
  39. type SNACFrame struct {
  40. FoodGroup uint16
  41. SubGroup uint16
  42. Flags uint16
  43. RequestID uint32
  44. }
  45. // ReqIDFromServer is the SNAC frame Request ID value that indicates the SNAC
  46. // is initiated by the server. Some clients, such as the Java AIM 1.1.19,
  47. // completely fail to process some server SNACs if the high bit is not set on
  48. // request ID.
  49. const ReqIDFromServer = 1 << 31
  50. type FLAPSignonFrame struct {
  51. FLAPVersion uint32
  52. TLVRestBlock
  53. }
  54. type SNACMessage struct {
  55. Frame SNACFrame
  56. Body any
  57. }
  58. // NewFlapClient creates a new FLAP client instance. startSeq is the initial
  59. // sequence value, which is typically 0. r receives FLAP messages, w writes
  60. // FLAP messages.
  61. func NewFlapClient(startSeq uint32, r io.Reader, w io.Writer) *FlapClient {
  62. return &FlapClient{
  63. sequence: startSeq,
  64. r: r,
  65. w: w,
  66. mutex: sync.Mutex{},
  67. }
  68. }
  69. // FlapClient sends and receive FLAP frames to and from the server. It ensures
  70. // that the message sequence numbers are properly incremented after sending
  71. // each successive message. It is not safe to use with multiple goroutines
  72. // without synchronization.
  73. type FlapClient struct {
  74. sequence uint32
  75. r io.Reader
  76. w io.Writer
  77. mutex sync.Mutex
  78. }
  79. // Fixes a race condition caused by testify. Yup...
  80. // https://github.com/stretchr/testify/issues/625
  81. func (f *FlapClient) String() string {
  82. return ""
  83. }
  84. // SendSignonFrame sends a signon FLAP frame containing a list of TLVs to
  85. // authenticate or initiate a session.
  86. func (f *FlapClient) SendSignonFrame(tlvs []TLV) error {
  87. signonFrame := FLAPSignonFrame{
  88. FLAPVersion: 1,
  89. }
  90. if len(tlvs) > 0 {
  91. signonFrame.AppendList(tlvs)
  92. }
  93. buf := &bytes.Buffer{}
  94. if err := MarshalBE(signonFrame, buf); err != nil {
  95. return err
  96. }
  97. f.mutex.Lock()
  98. defer f.mutex.Unlock()
  99. flap := FLAPFrame{
  100. StartMarker: 42,
  101. FrameType: FLAPFrameSignon,
  102. Sequence: uint16(f.sequence),
  103. Payload: buf.Bytes(),
  104. }
  105. if err := MarshalBE(flap, f.w); err != nil {
  106. return err
  107. }
  108. f.sequence++
  109. return nil
  110. }
  111. // ReceiveSignonFrame receives a signon FLAP response message.
  112. func (f *FlapClient) ReceiveSignonFrame() (FLAPSignonFrame, error) {
  113. flap := FLAPFrame{}
  114. if err := UnmarshalBE(&flap, f.r); err != nil {
  115. return FLAPSignonFrame{}, err
  116. }
  117. signonFrame := FLAPSignonFrame{}
  118. if err := UnmarshalBE(&signonFrame, bytes.NewBuffer(flap.Payload)); err != nil {
  119. return FLAPSignonFrame{}, err
  120. }
  121. return signonFrame, nil
  122. }
  123. // ReceiveFLAP receives a FLAP frame and body. It only returns a body if the
  124. // FLAP frame is a data frame.
  125. func (f *FlapClient) ReceiveFLAP() (FLAPFrame, error) {
  126. flap := FLAPFrame{}
  127. err := UnmarshalBE(&flap, f.r)
  128. if err != nil {
  129. err = fmt.Errorf("unable to unmarshal FLAP frame: %w", err)
  130. }
  131. return flap, err
  132. }
  133. // OldSignoff sends a signoff FLAP frame for legacy clients that do not
  134. // support multi-connection (Windows AIM 1.x–4.1).
  135. //
  136. // When these clients receive this frame, they display a "connection lost"
  137. // message and close the session. Unlike normal FLAP frames, this variant
  138. // omits the payload size field. If the size field were present, the client
  139. // would hang without displaying any message upon server disconnection.
  140. func (f *FlapClient) OldSignoff() error {
  141. f.mutex.Lock()
  142. defer f.mutex.Unlock()
  143. flap := FLAPFrameDisconnect{
  144. StartMarker: 42,
  145. FrameType: FLAPFrameSignoff,
  146. Sequence: uint16(f.sequence),
  147. }
  148. return MarshalBE(flap, f.w)
  149. }
  150. // NewSignoff sends a signoff FLAP frame for multi-connection clients.
  151. //
  152. // The frame includes a TLV block with additional metadata such as error codes.
  153. // Client behavior depends on the version:
  154. // - AIM 4.3–5.x: the client minimizes and enters a "signed off" state.
  155. // - AIM 6.x–7.x: the client closes and displays a disconnection error.
  156. func (f *FlapClient) NewSignoff(tlvs TLVRestBlock) error {
  157. tlvBuf := &bytes.Buffer{}
  158. if err := MarshalBE(tlvs, tlvBuf); err != nil {
  159. return err
  160. }
  161. f.mutex.Lock()
  162. defer f.mutex.Unlock()
  163. flap := FLAPFrame{
  164. StartMarker: 42,
  165. FrameType: FLAPFrameSignoff,
  166. Sequence: uint16(f.sequence),
  167. Payload: tlvBuf.Bytes(),
  168. }
  169. if err := MarshalBE(flap, f.w); err != nil {
  170. return err
  171. }
  172. f.sequence++
  173. return nil
  174. }
  175. // SendSNAC sends a SNAC message wrapped in a FLAP frame.
  176. func (f *FlapClient) SendSNAC(frame SNACFrame, body any) error {
  177. snacBuf := &bytes.Buffer{}
  178. if err := MarshalBE(frame, snacBuf); err != nil {
  179. return err
  180. }
  181. if err := MarshalBE(body, snacBuf); err != nil {
  182. return err
  183. }
  184. f.mutex.Lock()
  185. defer f.mutex.Unlock()
  186. flap := FLAPFrame{
  187. StartMarker: 42,
  188. FrameType: FLAPFrameData,
  189. Sequence: uint16(f.sequence),
  190. Payload: snacBuf.Bytes(),
  191. }
  192. if err := MarshalBE(flap, f.w); err != nil {
  193. return err
  194. }
  195. f.sequence++
  196. return nil
  197. }
  198. func (f *FlapClient) SendDataFrame(payload []byte) error {
  199. f.mutex.Lock()
  200. defer f.mutex.Unlock()
  201. flap := FLAPFrame{
  202. StartMarker: 42,
  203. FrameType: FLAPFrameData,
  204. Sequence: uint16(f.sequence),
  205. Payload: payload,
  206. }
  207. if err := MarshalBE(flap, f.w); err != nil {
  208. return err
  209. }
  210. f.sequence++
  211. return nil
  212. }
  213. func (f *FlapClient) SendKeepAliveFrame() error {
  214. f.mutex.Lock()
  215. defer f.mutex.Unlock()
  216. flap := FLAPFrame{
  217. StartMarker: 42,
  218. FrameType: FLAPFrameKeepAlive,
  219. Sequence: uint16(f.sequence),
  220. }
  221. if err := MarshalBE(flap, f.w); err != nil {
  222. return err
  223. }
  224. f.sequence++
  225. return nil
  226. }
  227. // ReceiveSNAC receives a SNAC message wrapped in a FLAP frame.
  228. func (f *FlapClient) ReceiveSNAC(frame *SNACFrame, body any) error {
  229. flap := FLAPFrame{}
  230. if err := UnmarshalBE(&flap, f.r); err != nil {
  231. return err
  232. }
  233. buf := bytes.NewBuffer(flap.Payload)
  234. if err := UnmarshalBE(frame, buf); err != nil {
  235. return err
  236. }
  237. return UnmarshalBE(body, buf)
  238. }