frames.go 6.7 KB

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