handshake.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package binlog
  2. type HandshakePacket struct {
  3. PacketLength uint64
  4. SequenceId uint64
  5. ProtocolVersion uint64
  6. ServerVersion string
  7. ThreadId uint64
  8. AuthPluginDataPart1 []byte
  9. CapabilityFlags1 []byte
  10. Charset uint64
  11. Status []byte
  12. CapabilityFlags2 []byte
  13. AuthPluginDataLength uint64
  14. AuthPluginDataPart2 []byte
  15. AuthPluginName string
  16. CapabilityFlags *CapabilityFlags
  17. StatusFlags *StatusFlags
  18. }
  19. type CapabilityFlags struct {
  20. LongPassword bool
  21. FoundRows bool
  22. LongFlag bool
  23. ConnectWithDb bool
  24. NoSchema bool
  25. Compress bool
  26. Odbc bool
  27. LocalFiles bool
  28. IgnoreSpace bool
  29. Protocol41 bool
  30. Interactive bool
  31. Ssl bool
  32. IgnoreSigpipe bool
  33. Transactions bool
  34. Reserved bool
  35. Reserved2 bool
  36. MultiStatements bool
  37. MultiResults bool
  38. PsMultiResults bool
  39. ConnectAttrs bool
  40. PluginAuthLenEncClientData bool
  41. CanHandleExpiredPasswords bool
  42. SessionTrack bool
  43. DeprecateEOF bool
  44. SslVerifyServerCert bool
  45. OptionalResultSetMetadata bool
  46. RememberOptions bool
  47. }
  48. type StatusFlags struct {
  49. StatusInTrans bool
  50. StatusAutocommit bool
  51. MoreResultsExists bool
  52. QueryNoGoodIndexUsed bool
  53. QueryNoIndexUsed bool
  54. StatusCursorExists bool
  55. StatusLastRowSent bool
  56. StatusDbDropped bool
  57. StatusNoBackslashEscapes bool
  58. StatusMetadataChanged bool
  59. QueryWasSlow bool
  60. PsOutParams bool
  61. StatusInTransReadonly bool
  62. SessionStateChanged bool
  63. }
  64. func (hs *HandshakePacket) decodeCapabilityFlags() {
  65. hs.CapabilityFlags = &CapabilityFlags{
  66. LongPassword: (hs.CapabilityFlags1[0] & 1) > 0,
  67. FoundRows: (hs.CapabilityFlags1[0] & 2) > 0,
  68. LongFlag: (hs.CapabilityFlags1[0] & 4) > 0,
  69. ConnectWithDb: (hs.CapabilityFlags1[0] & 8) > 0,
  70. NoSchema: (hs.CapabilityFlags1[0] & 16) > 0,
  71. Compress: (hs.CapabilityFlags1[0] & 32) > 0,
  72. Odbc: (hs.CapabilityFlags1[0] & 64) > 0,
  73. LocalFiles: (hs.CapabilityFlags1[0] & 128) > 0,
  74. IgnoreSpace: (hs.CapabilityFlags1[1] & 1) > 0,
  75. Protocol41: (hs.CapabilityFlags1[1] & 2) > 0,
  76. Interactive: (hs.CapabilityFlags1[1] & 4) > 0,
  77. Ssl: (hs.CapabilityFlags1[1] & 8) > 0,
  78. IgnoreSigpipe: (hs.CapabilityFlags1[1] & 16) > 0,
  79. Transactions: (hs.CapabilityFlags1[1] & 32) > 0,
  80. Reserved: (hs.CapabilityFlags1[1] & 64) > 0,
  81. Reserved2: (hs.CapabilityFlags1[1] & 128) > 0,
  82. MultiStatements: (hs.CapabilityFlags2[0] & 1) > 0,
  83. MultiResults: (hs.CapabilityFlags2[0] & 2) > 0,
  84. PsMultiResults: (hs.CapabilityFlags2[0] & 4) > 0,
  85. ConnectAttrs: (hs.CapabilityFlags2[0] & 8) > 0,
  86. PluginAuthLenEncClientData: (hs.CapabilityFlags2[0] & 16) > 0,
  87. CanHandleExpiredPasswords: (hs.CapabilityFlags2[0] & 32) > 0,
  88. SessionTrack: (hs.CapabilityFlags2[0] & 64) > 0,
  89. DeprecateEOF: (hs.CapabilityFlags2[1] & 128) > 0,
  90. SslVerifyServerCert: (hs.CapabilityFlags2[1] & 1) > 0,
  91. OptionalResultSetMetadata: (hs.CapabilityFlags2[1] & 2) > 0,
  92. RememberOptions: (hs.CapabilityFlags2[1] & 4) > 0,
  93. }
  94. }
  95. func (hs *HandshakePacket) decodeStatusFlags() {
  96. hs.StatusFlags = &StatusFlags{
  97. StatusInTrans: (hs.Status[0] & 1) > 0,
  98. StatusAutocommit: (hs.Status[0] & 2) > 0,
  99. MoreResultsExists: (hs.Status[0] & 4) > 0,
  100. QueryNoGoodIndexUsed: (hs.Status[0] & 8) > 0,
  101. QueryNoIndexUsed: (hs.Status[0] & 16) > 0,
  102. StatusCursorExists: (hs.Status[0] & 32) > 0,
  103. StatusLastRowSent: (hs.Status[0] & 64) > 0,
  104. StatusDbDropped: (hs.Status[0] & 128) > 0,
  105. StatusNoBackslashEscapes: (hs.Status[1] & 1) > 0,
  106. StatusMetadataChanged: (hs.Status[1] & 2) > 0,
  107. QueryWasSlow: (hs.Status[1] & 4) > 0,
  108. PsOutParams: (hs.Status[1] & 8) > 0,
  109. StatusInTransReadonly: (hs.Status[1] & 16) > 0,
  110. SessionStateChanged: (hs.Status[1] & 32) > 0,
  111. }
  112. }
  113. func (c *Conn) handshakePacket() (*HandshakePacket, error) {
  114. packet := HandshakePacket{}
  115. var err error
  116. packet.PacketLength, err = c.getInt(TypeFixedInt, 3)
  117. if err != nil {
  118. return nil, err
  119. }
  120. packet.SequenceId, err = c.getInt(TypeFixedInt, 1)
  121. if err != nil {
  122. return nil, err
  123. }
  124. packet.ProtocolVersion, err = c.getInt(TypeFixedInt, 1)
  125. if err != nil {
  126. return nil, err
  127. }
  128. packet.ServerVersion, err = c.getString(TypeNullTerminatedString, 0)
  129. if err != nil {
  130. return nil, err
  131. }
  132. packet.ThreadId, err = c.getInt(TypeFixedInt, 4)
  133. if err != nil {
  134. return nil, err
  135. }
  136. packet.AuthPluginDataPart1, err = c.getBytes(8)
  137. if err != nil {
  138. return nil, err
  139. }
  140. err = c.consumeBytes(1)
  141. if err != nil {
  142. return nil, err
  143. }
  144. packet.CapabilityFlags1, err = c.getBytes(2)
  145. if err != nil {
  146. return nil, err
  147. }
  148. packet.Charset, err = c.getInt(TypeFixedInt, 1)
  149. if err != nil {
  150. return nil, err
  151. }
  152. packet.Status, err = c.getBytes(2)
  153. if err != nil {
  154. return nil, err
  155. }
  156. packet.decodeStatusFlags()
  157. packet.CapabilityFlags2, err = c.getBytes(2)
  158. if err != nil {
  159. return nil, err
  160. }
  161. packet.decodeCapabilityFlags()
  162. packet.AuthPluginDataLength, err = c.getInt(TypeFixedInt, 1)
  163. if err != nil {
  164. return nil, err
  165. }
  166. err = c.consumeBytes(10)
  167. if err != nil {
  168. return nil, err
  169. }
  170. packet.AuthPluginDataPart2, err = c.getBytes(packet.AuthPluginDataLength - 8)
  171. if err != nil {
  172. return nil, err
  173. }
  174. packet.AuthPluginName, err = c.getString(TypeNullTerminatedString, 0)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return &packet, nil
  179. }