packets.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "bytes"
  11. "crypto/tls"
  12. "database/sql/driver"
  13. "encoding/binary"
  14. "errors"
  15. "fmt"
  16. "io"
  17. "math"
  18. "time"
  19. )
  20. // Packets documentation:
  21. // http://dev.mysql.com/doc/internals/en/client-server-protocol.html
  22. // Read packet to buffer 'data'
  23. func (mc *mysqlConn) readPacket() ([]byte, error) {
  24. var prevData []byte
  25. for {
  26. // read packet header
  27. data, err := mc.buf.readNext(4)
  28. if err != nil {
  29. if cerr := mc.canceled.Value(); cerr != nil {
  30. return nil, cerr
  31. }
  32. errLog.Print(err)
  33. mc.Close()
  34. return nil, driver.ErrBadConn
  35. }
  36. // packet length [24 bit]
  37. pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
  38. // check packet sync [8 bit]
  39. if data[3] != mc.sequence {
  40. if data[3] > mc.sequence {
  41. return nil, ErrPktSyncMul
  42. }
  43. return nil, ErrPktSync
  44. }
  45. mc.sequence++
  46. // packets with length 0 terminate a previous packet which is a
  47. // multiple of (2^24)−1 bytes long
  48. if pktLen == 0 {
  49. // there was no previous packet
  50. if prevData == nil {
  51. errLog.Print(ErrMalformPkt)
  52. mc.Close()
  53. return nil, driver.ErrBadConn
  54. }
  55. return prevData, nil
  56. }
  57. // read packet body [pktLen bytes]
  58. data, err = mc.buf.readNext(pktLen)
  59. if err != nil {
  60. if cerr := mc.canceled.Value(); cerr != nil {
  61. return nil, cerr
  62. }
  63. errLog.Print(err)
  64. mc.Close()
  65. return nil, driver.ErrBadConn
  66. }
  67. // return data if this was the last packet
  68. if pktLen < maxPacketSize {
  69. // zero allocations for non-split packets
  70. if prevData == nil {
  71. return data, nil
  72. }
  73. return append(prevData, data...), nil
  74. }
  75. prevData = append(prevData, data...)
  76. }
  77. }
  78. // Write packet buffer 'data'
  79. func (mc *mysqlConn) writePacket(data []byte) error {
  80. pktLen := len(data) - 4
  81. if pktLen > mc.maxAllowedPacket {
  82. return ErrPktTooLarge
  83. }
  84. for {
  85. var size int
  86. if pktLen >= maxPacketSize {
  87. data[0] = 0xff
  88. data[1] = 0xff
  89. data[2] = 0xff
  90. size = maxPacketSize
  91. } else {
  92. data[0] = byte(pktLen)
  93. data[1] = byte(pktLen >> 8)
  94. data[2] = byte(pktLen >> 16)
  95. size = pktLen
  96. }
  97. data[3] = mc.sequence
  98. // Write packet
  99. if mc.writeTimeout > 0 {
  100. if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
  101. return err
  102. }
  103. }
  104. n, err := mc.netConn.Write(data[:4+size])
  105. if err == nil && n == 4+size {
  106. mc.sequence++
  107. if size != maxPacketSize {
  108. return nil
  109. }
  110. pktLen -= size
  111. data = data[size:]
  112. continue
  113. }
  114. // Handle error
  115. if err == nil { // n != len(data)
  116. mc.cleanup()
  117. errLog.Print(ErrMalformPkt)
  118. } else {
  119. if cerr := mc.canceled.Value(); cerr != nil {
  120. return cerr
  121. }
  122. mc.cleanup()
  123. errLog.Print(err)
  124. }
  125. return driver.ErrBadConn
  126. }
  127. }
  128. /******************************************************************************
  129. * Initialisation Process *
  130. ******************************************************************************/
  131. // Handshake Initialization Packet
  132. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
  133. func (mc *mysqlConn) readInitPacket() ([]byte, error) {
  134. data, err := mc.readPacket()
  135. if err != nil {
  136. return nil, err
  137. }
  138. if data[0] == iERR {
  139. return nil, mc.handleErrorPacket(data)
  140. }
  141. // protocol version [1 byte]
  142. if data[0] < minProtocolVersion {
  143. return nil, fmt.Errorf(
  144. "unsupported protocol version %d. Version %d or higher is required",
  145. data[0],
  146. minProtocolVersion,
  147. )
  148. }
  149. // server version [null terminated string]
  150. // connection id [4 bytes]
  151. pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
  152. // first part of the password cipher [8 bytes]
  153. cipher := data[pos : pos+8]
  154. // (filler) always 0x00 [1 byte]
  155. pos += 8 + 1
  156. // capability flags (lower 2 bytes) [2 bytes]
  157. mc.flags = clientFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  158. if mc.flags&clientProtocol41 == 0 {
  159. return nil, ErrOldProtocol
  160. }
  161. if mc.flags&clientSSL == 0 && mc.cfg.tls != nil {
  162. return nil, ErrNoTLS
  163. }
  164. pos += 2
  165. if len(data) > pos {
  166. // character set [1 byte]
  167. // status flags [2 bytes]
  168. // capability flags (upper 2 bytes) [2 bytes]
  169. // length of auth-plugin-data [1 byte]
  170. // reserved (all [00]) [10 bytes]
  171. pos += 1 + 2 + 2 + 1 + 10
  172. // second part of the password cipher [mininum 13 bytes],
  173. // where len=MAX(13, length of auth-plugin-data - 8)
  174. //
  175. // The web documentation is ambiguous about the length. However,
  176. // according to mysql-5.7/sql/auth/sql_authentication.cc line 538,
  177. // the 13th byte is "\0 byte, terminating the second part of
  178. // a scramble". So the second part of the password cipher is
  179. // a NULL terminated string that's at least 13 bytes with the
  180. // last byte being NULL.
  181. //
  182. // The official Python library uses the fixed length 12
  183. // which seems to work but technically could have a hidden bug.
  184. cipher = append(cipher, data[pos:pos+12]...)
  185. // TODO: Verify string termination
  186. // EOF if version (>= 5.5.7 and < 5.5.10) or (>= 5.6.0 and < 5.6.2)
  187. // \NUL otherwise
  188. //
  189. //if data[len(data)-1] == 0 {
  190. // return
  191. //}
  192. //return ErrMalformPkt
  193. // make a memory safe copy of the cipher slice
  194. var b [20]byte
  195. copy(b[:], cipher)
  196. return b[:], nil
  197. }
  198. // make a memory safe copy of the cipher slice
  199. var b [8]byte
  200. copy(b[:], cipher)
  201. return b[:], nil
  202. }
  203. // Client Authentication Packet
  204. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
  205. func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
  206. // Adjust client flags based on server support
  207. clientFlags := clientProtocol41 |
  208. clientSecureConn |
  209. clientLongPassword |
  210. clientTransactions |
  211. clientLocalFiles |
  212. clientPluginAuth |
  213. clientMultiResults |
  214. mc.flags&clientLongFlag
  215. if mc.cfg.ClientFoundRows {
  216. clientFlags |= clientFoundRows
  217. }
  218. // To enable TLS / SSL
  219. if mc.cfg.tls != nil {
  220. clientFlags |= clientSSL
  221. }
  222. if mc.cfg.MultiStatements {
  223. clientFlags |= clientMultiStatements
  224. }
  225. // User Password
  226. scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd))
  227. pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1 + 1 + len(scrambleBuff) + 21 + 1
  228. // To specify a db name
  229. if n := len(mc.cfg.DBName); n > 0 {
  230. clientFlags |= clientConnectWithDB
  231. pktLen += n + 1
  232. }
  233. // Calculate packet length and get buffer with that size
  234. data := mc.buf.takeSmallBuffer(pktLen + 4)
  235. if data == nil {
  236. // can not take the buffer. Something must be wrong with the connection
  237. errLog.Print(ErrBusyBuffer)
  238. return driver.ErrBadConn
  239. }
  240. // ClientFlags [32 bit]
  241. data[4] = byte(clientFlags)
  242. data[5] = byte(clientFlags >> 8)
  243. data[6] = byte(clientFlags >> 16)
  244. data[7] = byte(clientFlags >> 24)
  245. // MaxPacketSize [32 bit] (none)
  246. data[8] = 0x00
  247. data[9] = 0x00
  248. data[10] = 0x00
  249. data[11] = 0x00
  250. // Charset [1 byte]
  251. var found bool
  252. data[12], found = collations[mc.cfg.Collation]
  253. if !found {
  254. // Note possibility for false negatives:
  255. // could be triggered although the collation is valid if the
  256. // collations map does not contain entries the server supports.
  257. return errors.New("unknown collation")
  258. }
  259. // SSL Connection Request Packet
  260. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
  261. if mc.cfg.tls != nil {
  262. // Send TLS / SSL request packet
  263. if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil {
  264. return err
  265. }
  266. // Switch to TLS
  267. tlsConn := tls.Client(mc.netConn, mc.cfg.tls)
  268. if err := tlsConn.Handshake(); err != nil {
  269. return err
  270. }
  271. mc.netConn = tlsConn
  272. mc.buf.nc = tlsConn
  273. }
  274. // Filler [23 bytes] (all 0x00)
  275. pos := 13
  276. for ; pos < 13+23; pos++ {
  277. data[pos] = 0
  278. }
  279. // User [null terminated string]
  280. if len(mc.cfg.User) > 0 {
  281. pos += copy(data[pos:], mc.cfg.User)
  282. }
  283. data[pos] = 0x00
  284. pos++
  285. // ScrambleBuffer [length encoded integer]
  286. data[pos] = byte(len(scrambleBuff))
  287. pos += 1 + copy(data[pos+1:], scrambleBuff)
  288. // Databasename [null terminated string]
  289. if len(mc.cfg.DBName) > 0 {
  290. pos += copy(data[pos:], mc.cfg.DBName)
  291. data[pos] = 0x00
  292. pos++
  293. }
  294. // Assume native client during response
  295. pos += copy(data[pos:], "mysql_native_password")
  296. data[pos] = 0x00
  297. // Send Auth packet
  298. return mc.writePacket(data)
  299. }
  300. // Client old authentication packet
  301. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  302. func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
  303. // User password
  304. scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.Passwd))
  305. // Calculate the packet length and add a tailing 0
  306. pktLen := len(scrambleBuff) + 1
  307. data := mc.buf.takeSmallBuffer(4 + pktLen)
  308. if data == nil {
  309. // can not take the buffer. Something must be wrong with the connection
  310. errLog.Print(ErrBusyBuffer)
  311. return driver.ErrBadConn
  312. }
  313. // Add the scrambled password [null terminated string]
  314. copy(data[4:], scrambleBuff)
  315. data[4+pktLen-1] = 0x00
  316. return mc.writePacket(data)
  317. }
  318. // Client clear text authentication packet
  319. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  320. func (mc *mysqlConn) writeClearAuthPacket() error {
  321. // Calculate the packet length and add a tailing 0
  322. pktLen := len(mc.cfg.Passwd) + 1
  323. data := mc.buf.takeSmallBuffer(4 + pktLen)
  324. if data == nil {
  325. // can not take the buffer. Something must be wrong with the connection
  326. errLog.Print(ErrBusyBuffer)
  327. return driver.ErrBadConn
  328. }
  329. // Add the clear password [null terminated string]
  330. copy(data[4:], mc.cfg.Passwd)
  331. data[4+pktLen-1] = 0x00
  332. return mc.writePacket(data)
  333. }
  334. // Native password authentication method
  335. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  336. func (mc *mysqlConn) writeNativeAuthPacket(cipher []byte) error {
  337. scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd))
  338. // Calculate the packet length and add a tailing 0
  339. pktLen := len(scrambleBuff)
  340. data := mc.buf.takeSmallBuffer(4 + pktLen)
  341. if data == nil {
  342. // can not take the buffer. Something must be wrong with the connection
  343. errLog.Print(ErrBusyBuffer)
  344. return driver.ErrBadConn
  345. }
  346. // Add the scramble
  347. copy(data[4:], scrambleBuff)
  348. return mc.writePacket(data)
  349. }
  350. /******************************************************************************
  351. * Command Packets *
  352. ******************************************************************************/
  353. func (mc *mysqlConn) writeCommandPacket(command byte) error {
  354. // Reset Packet Sequence
  355. mc.sequence = 0
  356. data := mc.buf.takeSmallBuffer(4 + 1)
  357. if data == nil {
  358. // can not take the buffer. Something must be wrong with the connection
  359. errLog.Print(ErrBusyBuffer)
  360. return driver.ErrBadConn
  361. }
  362. // Add command byte
  363. data[4] = command
  364. // Send CMD packet
  365. return mc.writePacket(data)
  366. }
  367. func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
  368. // Reset Packet Sequence
  369. mc.sequence = 0
  370. pktLen := 1 + len(arg)
  371. data := mc.buf.takeBuffer(pktLen + 4)
  372. if data == nil {
  373. // can not take the buffer. Something must be wrong with the connection
  374. errLog.Print(ErrBusyBuffer)
  375. return driver.ErrBadConn
  376. }
  377. // Add command byte
  378. data[4] = command
  379. // Add arg
  380. copy(data[5:], arg)
  381. // Send CMD packet
  382. return mc.writePacket(data)
  383. }
  384. func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
  385. // Reset Packet Sequence
  386. mc.sequence = 0
  387. data := mc.buf.takeSmallBuffer(4 + 1 + 4)
  388. if data == nil {
  389. // can not take the buffer. Something must be wrong with the connection
  390. errLog.Print(ErrBusyBuffer)
  391. return driver.ErrBadConn
  392. }
  393. // Add command byte
  394. data[4] = command
  395. // Add arg [32 bit]
  396. data[5] = byte(arg)
  397. data[6] = byte(arg >> 8)
  398. data[7] = byte(arg >> 16)
  399. data[8] = byte(arg >> 24)
  400. // Send CMD packet
  401. return mc.writePacket(data)
  402. }
  403. /******************************************************************************
  404. * Result Packets *
  405. ******************************************************************************/
  406. // Returns error if Packet is not an 'Result OK'-Packet
  407. func (mc *mysqlConn) readResultOK() ([]byte, error) {
  408. data, err := mc.readPacket()
  409. if err == nil {
  410. // packet indicator
  411. switch data[0] {
  412. case iOK:
  413. return nil, mc.handleOkPacket(data)
  414. case iEOF:
  415. if len(data) > 1 {
  416. pluginEndIndex := bytes.IndexByte(data, 0x00)
  417. plugin := string(data[1:pluginEndIndex])
  418. cipher := data[pluginEndIndex+1 : len(data)-1]
  419. switch plugin {
  420. case "mysql_old_password":
  421. // using old_passwords
  422. return cipher, ErrOldPassword
  423. case "mysql_clear_password":
  424. // using clear text password
  425. return cipher, ErrCleartextPassword
  426. case "mysql_native_password":
  427. // using mysql default authentication method
  428. return cipher, ErrNativePassword
  429. default:
  430. return cipher, ErrUnknownPlugin
  431. }
  432. }
  433. // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
  434. return nil, ErrOldPassword
  435. default: // Error otherwise
  436. return nil, mc.handleErrorPacket(data)
  437. }
  438. }
  439. return nil, err
  440. }
  441. // Result Set Header Packet
  442. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
  443. func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {
  444. data, err := mc.readPacket()
  445. if err == nil {
  446. switch data[0] {
  447. case iOK:
  448. return 0, mc.handleOkPacket(data)
  449. case iERR:
  450. return 0, mc.handleErrorPacket(data)
  451. case iLocalInFile:
  452. return 0, mc.handleInFileRequest(string(data[1:]))
  453. }
  454. // column count
  455. num, _, n := readLengthEncodedInteger(data)
  456. if n-len(data) == 0 {
  457. return int(num), nil
  458. }
  459. return 0, ErrMalformPkt
  460. }
  461. return 0, err
  462. }
  463. // Error Packet
  464. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-ERR_Packet
  465. func (mc *mysqlConn) handleErrorPacket(data []byte) error {
  466. if data[0] != iERR {
  467. return ErrMalformPkt
  468. }
  469. // 0xff [1 byte]
  470. // Error Number [16 bit uint]
  471. errno := binary.LittleEndian.Uint16(data[1:3])
  472. // 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION
  473. if errno == 1792 && mc.cfg.RejectReadOnly {
  474. // Oops; we are connected to a read-only connection, and won't be able
  475. // to issue any write statements. Since RejectReadOnly is configured,
  476. // we throw away this connection hoping this one would have write
  477. // permission. This is specifically for a possible race condition
  478. // during failover (e.g. on AWS Aurora). See README.md for more.
  479. //
  480. // We explicitly close the connection before returning
  481. // driver.ErrBadConn to ensure that `database/sql` purges this
  482. // connection and initiates a new one for next statement next time.
  483. mc.Close()
  484. return driver.ErrBadConn
  485. }
  486. pos := 3
  487. // SQL State [optional: # + 5bytes string]
  488. if data[3] == 0x23 {
  489. //sqlstate := string(data[4 : 4+5])
  490. pos = 9
  491. }
  492. // Error Message [string]
  493. return &MySQLError{
  494. Number: errno,
  495. Message: string(data[pos:]),
  496. }
  497. }
  498. func readStatus(b []byte) statusFlag {
  499. return statusFlag(b[0]) | statusFlag(b[1])<<8
  500. }
  501. // Ok Packet
  502. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
  503. func (mc *mysqlConn) handleOkPacket(data []byte) error {
  504. var n, m int
  505. // 0x00 [1 byte]
  506. // Affected rows [Length Coded Binary]
  507. mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
  508. // Insert id [Length Coded Binary]
  509. mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
  510. // server_status [2 bytes]
  511. mc.status = readStatus(data[1+n+m : 1+n+m+2])
  512. if mc.status&statusMoreResultsExists != 0 {
  513. return nil
  514. }
  515. // warning count [2 bytes]
  516. if !mc.strict {
  517. return nil
  518. }
  519. pos := 1 + n + m + 2
  520. if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
  521. return mc.getWarnings()
  522. }
  523. return nil
  524. }
  525. // Read Packets as Field Packets until EOF-Packet or an Error appears
  526. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
  527. func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
  528. columns := make([]mysqlField, count)
  529. for i := 0; ; i++ {
  530. data, err := mc.readPacket()
  531. if err != nil {
  532. return nil, err
  533. }
  534. // EOF Packet
  535. if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
  536. if i == count {
  537. return columns, nil
  538. }
  539. return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
  540. }
  541. // Catalog
  542. pos, err := skipLengthEncodedString(data)
  543. if err != nil {
  544. return nil, err
  545. }
  546. // Database [len coded string]
  547. n, err := skipLengthEncodedString(data[pos:])
  548. if err != nil {
  549. return nil, err
  550. }
  551. pos += n
  552. // Table [len coded string]
  553. if mc.cfg.ColumnsWithAlias {
  554. tableName, _, n, err := readLengthEncodedString(data[pos:])
  555. if err != nil {
  556. return nil, err
  557. }
  558. pos += n
  559. columns[i].tableName = string(tableName)
  560. } else {
  561. n, err = skipLengthEncodedString(data[pos:])
  562. if err != nil {
  563. return nil, err
  564. }
  565. pos += n
  566. }
  567. // Original table [len coded string]
  568. n, err = skipLengthEncodedString(data[pos:])
  569. if err != nil {
  570. return nil, err
  571. }
  572. pos += n
  573. // Name [len coded string]
  574. name, _, n, err := readLengthEncodedString(data[pos:])
  575. if err != nil {
  576. return nil, err
  577. }
  578. columns[i].name = string(name)
  579. pos += n
  580. // Original name [len coded string]
  581. n, err = skipLengthEncodedString(data[pos:])
  582. if err != nil {
  583. return nil, err
  584. }
  585. // Filler [uint8]
  586. // Charset [charset, collation uint8]
  587. // Length [uint32]
  588. pos += n + 1 + 2 + 4
  589. // Field type [uint8]
  590. columns[i].fieldType = data[pos]
  591. pos++
  592. // Flags [uint16]
  593. columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  594. pos += 2
  595. // Decimals [uint8]
  596. columns[i].decimals = data[pos]
  597. //pos++
  598. // Default value [len coded binary]
  599. //if pos < len(data) {
  600. // defaultVal, _, err = bytesToLengthCodedBinary(data[pos:])
  601. //}
  602. }
  603. }
  604. // Read Packets as Field Packets until EOF-Packet or an Error appears
  605. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::ResultsetRow
  606. func (rows *textRows) readRow(dest []driver.Value) error {
  607. mc := rows.mc
  608. if rows.rs.done {
  609. return io.EOF
  610. }
  611. data, err := mc.readPacket()
  612. if err != nil {
  613. return err
  614. }
  615. // EOF Packet
  616. if data[0] == iEOF && len(data) == 5 {
  617. // server_status [2 bytes]
  618. rows.mc.status = readStatus(data[3:])
  619. rows.rs.done = true
  620. if !rows.HasNextResultSet() {
  621. rows.mc = nil
  622. }
  623. return io.EOF
  624. }
  625. if data[0] == iERR {
  626. rows.mc = nil
  627. return mc.handleErrorPacket(data)
  628. }
  629. // RowSet Packet
  630. var n int
  631. var isNull bool
  632. pos := 0
  633. for i := range dest {
  634. // Read bytes and convert to string
  635. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  636. pos += n
  637. if err == nil {
  638. if !isNull {
  639. if !mc.parseTime {
  640. continue
  641. } else {
  642. switch rows.rs.columns[i].fieldType {
  643. case fieldTypeTimestamp, fieldTypeDateTime,
  644. fieldTypeDate, fieldTypeNewDate:
  645. dest[i], err = parseDateTime(
  646. string(dest[i].([]byte)),
  647. mc.cfg.Loc,
  648. )
  649. if err == nil {
  650. continue
  651. }
  652. default:
  653. continue
  654. }
  655. }
  656. } else {
  657. dest[i] = nil
  658. continue
  659. }
  660. }
  661. return err // err != nil
  662. }
  663. return nil
  664. }
  665. // Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
  666. func (mc *mysqlConn) readUntilEOF() error {
  667. for {
  668. data, err := mc.readPacket()
  669. if err != nil {
  670. return err
  671. }
  672. switch data[0] {
  673. case iERR:
  674. return mc.handleErrorPacket(data)
  675. case iEOF:
  676. if len(data) == 5 {
  677. mc.status = readStatus(data[3:])
  678. }
  679. return nil
  680. }
  681. }
  682. }
  683. /******************************************************************************
  684. * Prepared Statements *
  685. ******************************************************************************/
  686. // Prepare Result Packets
  687. // http://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html
  688. func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
  689. data, err := stmt.mc.readPacket()
  690. if err == nil {
  691. // packet indicator [1 byte]
  692. if data[0] != iOK {
  693. return 0, stmt.mc.handleErrorPacket(data)
  694. }
  695. // statement id [4 bytes]
  696. stmt.id = binary.LittleEndian.Uint32(data[1:5])
  697. // Column count [16 bit uint]
  698. columnCount := binary.LittleEndian.Uint16(data[5:7])
  699. // Param count [16 bit uint]
  700. stmt.paramCount = int(binary.LittleEndian.Uint16(data[7:9]))
  701. // Reserved [8 bit]
  702. // Warning count [16 bit uint]
  703. if !stmt.mc.strict {
  704. return columnCount, nil
  705. }
  706. // Check for warnings count > 0, only available in MySQL > 4.1
  707. if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
  708. return columnCount, stmt.mc.getWarnings()
  709. }
  710. return columnCount, nil
  711. }
  712. return 0, err
  713. }
  714. // http://dev.mysql.com/doc/internals/en/com-stmt-send-long-data.html
  715. func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
  716. maxLen := stmt.mc.maxAllowedPacket - 1
  717. pktLen := maxLen
  718. // After the header (bytes 0-3) follows before the data:
  719. // 1 byte command
  720. // 4 bytes stmtID
  721. // 2 bytes paramID
  722. const dataOffset = 1 + 4 + 2
  723. // Can not use the write buffer since
  724. // a) the buffer is too small
  725. // b) it is in use
  726. data := make([]byte, 4+1+4+2+len(arg))
  727. copy(data[4+dataOffset:], arg)
  728. for argLen := len(arg); argLen > 0; argLen -= pktLen - dataOffset {
  729. if dataOffset+argLen < maxLen {
  730. pktLen = dataOffset + argLen
  731. }
  732. stmt.mc.sequence = 0
  733. // Add command byte [1 byte]
  734. data[4] = comStmtSendLongData
  735. // Add stmtID [32 bit]
  736. data[5] = byte(stmt.id)
  737. data[6] = byte(stmt.id >> 8)
  738. data[7] = byte(stmt.id >> 16)
  739. data[8] = byte(stmt.id >> 24)
  740. // Add paramID [16 bit]
  741. data[9] = byte(paramID)
  742. data[10] = byte(paramID >> 8)
  743. // Send CMD packet
  744. err := stmt.mc.writePacket(data[:4+pktLen])
  745. if err == nil {
  746. data = data[pktLen-dataOffset:]
  747. continue
  748. }
  749. return err
  750. }
  751. // Reset Packet Sequence
  752. stmt.mc.sequence = 0
  753. return nil
  754. }
  755. // Execute Prepared Statement
  756. // http://dev.mysql.com/doc/internals/en/com-stmt-execute.html
  757. func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
  758. if len(args) != stmt.paramCount {
  759. return fmt.Errorf(
  760. "argument count mismatch (got: %d; has: %d)",
  761. len(args),
  762. stmt.paramCount,
  763. )
  764. }
  765. const minPktLen = 4 + 1 + 4 + 1 + 4
  766. mc := stmt.mc
  767. // Reset packet-sequence
  768. mc.sequence = 0
  769. var data []byte
  770. if len(args) == 0 {
  771. data = mc.buf.takeBuffer(minPktLen)
  772. } else {
  773. data = mc.buf.takeCompleteBuffer()
  774. }
  775. if data == nil {
  776. // can not take the buffer. Something must be wrong with the connection
  777. errLog.Print(ErrBusyBuffer)
  778. return driver.ErrBadConn
  779. }
  780. // command [1 byte]
  781. data[4] = comStmtExecute
  782. // statement_id [4 bytes]
  783. data[5] = byte(stmt.id)
  784. data[6] = byte(stmt.id >> 8)
  785. data[7] = byte(stmt.id >> 16)
  786. data[8] = byte(stmt.id >> 24)
  787. // flags (0: CURSOR_TYPE_NO_CURSOR) [1 byte]
  788. data[9] = 0x00
  789. // iteration_count (uint32(1)) [4 bytes]
  790. data[10] = 0x01
  791. data[11] = 0x00
  792. data[12] = 0x00
  793. data[13] = 0x00
  794. if len(args) > 0 {
  795. pos := minPktLen
  796. var nullMask []byte
  797. if maskLen, typesLen := (len(args)+7)/8, 1+2*len(args); pos+maskLen+typesLen >= len(data) {
  798. // buffer has to be extended but we don't know by how much so
  799. // we depend on append after all data with known sizes fit.
  800. // We stop at that because we deal with a lot of columns here
  801. // which makes the required allocation size hard to guess.
  802. tmp := make([]byte, pos+maskLen+typesLen)
  803. copy(tmp[:pos], data[:pos])
  804. data = tmp
  805. nullMask = data[pos : pos+maskLen]
  806. pos += maskLen
  807. } else {
  808. nullMask = data[pos : pos+maskLen]
  809. for i := 0; i < maskLen; i++ {
  810. nullMask[i] = 0
  811. }
  812. pos += maskLen
  813. }
  814. // newParameterBoundFlag 1 [1 byte]
  815. data[pos] = 0x01
  816. pos++
  817. // type of each parameter [len(args)*2 bytes]
  818. paramTypes := data[pos:]
  819. pos += len(args) * 2
  820. // value of each parameter [n bytes]
  821. paramValues := data[pos:pos]
  822. valuesCap := cap(paramValues)
  823. for i, arg := range args {
  824. // build NULL-bitmap
  825. if arg == nil {
  826. nullMask[i/8] |= 1 << (uint(i) & 7)
  827. paramTypes[i+i] = fieldTypeNULL
  828. paramTypes[i+i+1] = 0x00
  829. continue
  830. }
  831. // cache types and values
  832. switch v := arg.(type) {
  833. case int64:
  834. paramTypes[i+i] = fieldTypeLongLong
  835. paramTypes[i+i+1] = 0x00
  836. if cap(paramValues)-len(paramValues)-8 >= 0 {
  837. paramValues = paramValues[:len(paramValues)+8]
  838. binary.LittleEndian.PutUint64(
  839. paramValues[len(paramValues)-8:],
  840. uint64(v),
  841. )
  842. } else {
  843. paramValues = append(paramValues,
  844. uint64ToBytes(uint64(v))...,
  845. )
  846. }
  847. case float64:
  848. paramTypes[i+i] = fieldTypeDouble
  849. paramTypes[i+i+1] = 0x00
  850. if cap(paramValues)-len(paramValues)-8 >= 0 {
  851. paramValues = paramValues[:len(paramValues)+8]
  852. binary.LittleEndian.PutUint64(
  853. paramValues[len(paramValues)-8:],
  854. math.Float64bits(v),
  855. )
  856. } else {
  857. paramValues = append(paramValues,
  858. uint64ToBytes(math.Float64bits(v))...,
  859. )
  860. }
  861. case bool:
  862. paramTypes[i+i] = fieldTypeTiny
  863. paramTypes[i+i+1] = 0x00
  864. if v {
  865. paramValues = append(paramValues, 0x01)
  866. } else {
  867. paramValues = append(paramValues, 0x00)
  868. }
  869. case []byte:
  870. // Common case (non-nil value) first
  871. if v != nil {
  872. paramTypes[i+i] = fieldTypeString
  873. paramTypes[i+i+1] = 0x00
  874. if len(v) < mc.maxAllowedPacket-pos-len(paramValues)-(len(args)-(i+1))*64 {
  875. paramValues = appendLengthEncodedInteger(paramValues,
  876. uint64(len(v)),
  877. )
  878. paramValues = append(paramValues, v...)
  879. } else {
  880. if err := stmt.writeCommandLongData(i, v); err != nil {
  881. return err
  882. }
  883. }
  884. continue
  885. }
  886. // Handle []byte(nil) as a NULL value
  887. nullMask[i/8] |= 1 << (uint(i) & 7)
  888. paramTypes[i+i] = fieldTypeNULL
  889. paramTypes[i+i+1] = 0x00
  890. case string:
  891. paramTypes[i+i] = fieldTypeString
  892. paramTypes[i+i+1] = 0x00
  893. if len(v) < mc.maxAllowedPacket-pos-len(paramValues)-(len(args)-(i+1))*64 {
  894. paramValues = appendLengthEncodedInteger(paramValues,
  895. uint64(len(v)),
  896. )
  897. paramValues = append(paramValues, v...)
  898. } else {
  899. if err := stmt.writeCommandLongData(i, []byte(v)); err != nil {
  900. return err
  901. }
  902. }
  903. case time.Time:
  904. paramTypes[i+i] = fieldTypeString
  905. paramTypes[i+i+1] = 0x00
  906. var a [64]byte
  907. var b = a[:0]
  908. if v.IsZero() {
  909. b = append(b, "0000-00-00"...)
  910. } else {
  911. b = v.In(mc.cfg.Loc).AppendFormat(b, timeFormat)
  912. }
  913. paramValues = appendLengthEncodedInteger(paramValues,
  914. uint64(len(b)),
  915. )
  916. paramValues = append(paramValues, b...)
  917. default:
  918. return fmt.Errorf("can not convert type: %T", arg)
  919. }
  920. }
  921. // Check if param values exceeded the available buffer
  922. // In that case we must build the data packet with the new values buffer
  923. if valuesCap != cap(paramValues) {
  924. data = append(data[:pos], paramValues...)
  925. mc.buf.buf = data
  926. }
  927. pos += len(paramValues)
  928. data = data[:pos]
  929. }
  930. return mc.writePacket(data)
  931. }
  932. func (mc *mysqlConn) discardResults() error {
  933. for mc.status&statusMoreResultsExists != 0 {
  934. resLen, err := mc.readResultSetHeaderPacket()
  935. if err != nil {
  936. return err
  937. }
  938. if resLen > 0 {
  939. // columns
  940. if err := mc.readUntilEOF(); err != nil {
  941. return err
  942. }
  943. // rows
  944. if err := mc.readUntilEOF(); err != nil {
  945. return err
  946. }
  947. }
  948. }
  949. return nil
  950. }
  951. // http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
  952. func (rows *binaryRows) readRow(dest []driver.Value) error {
  953. data, err := rows.mc.readPacket()
  954. if err != nil {
  955. return err
  956. }
  957. // packet indicator [1 byte]
  958. if data[0] != iOK {
  959. // EOF Packet
  960. if data[0] == iEOF && len(data) == 5 {
  961. rows.mc.status = readStatus(data[3:])
  962. rows.rs.done = true
  963. if !rows.HasNextResultSet() {
  964. rows.mc = nil
  965. }
  966. return io.EOF
  967. }
  968. rows.mc = nil
  969. // Error otherwise
  970. return rows.mc.handleErrorPacket(data)
  971. }
  972. // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
  973. pos := 1 + (len(dest)+7+2)>>3
  974. nullMask := data[1:pos]
  975. for i := range dest {
  976. // Field is NULL
  977. // (byte >> bit-pos) % 2 == 1
  978. if ((nullMask[(i+2)>>3] >> uint((i+2)&7)) & 1) == 1 {
  979. dest[i] = nil
  980. continue
  981. }
  982. // Convert to byte-coded string
  983. switch rows.rs.columns[i].fieldType {
  984. case fieldTypeNULL:
  985. dest[i] = nil
  986. continue
  987. // Numeric Types
  988. case fieldTypeTiny:
  989. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  990. dest[i] = int64(data[pos])
  991. } else {
  992. dest[i] = int64(int8(data[pos]))
  993. }
  994. pos++
  995. continue
  996. case fieldTypeShort, fieldTypeYear:
  997. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  998. dest[i] = int64(binary.LittleEndian.Uint16(data[pos : pos+2]))
  999. } else {
  1000. dest[i] = int64(int16(binary.LittleEndian.Uint16(data[pos : pos+2])))
  1001. }
  1002. pos += 2
  1003. continue
  1004. case fieldTypeInt24, fieldTypeLong:
  1005. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  1006. dest[i] = int64(binary.LittleEndian.Uint32(data[pos : pos+4]))
  1007. } else {
  1008. dest[i] = int64(int32(binary.LittleEndian.Uint32(data[pos : pos+4])))
  1009. }
  1010. pos += 4
  1011. continue
  1012. case fieldTypeLongLong:
  1013. if rows.rs.columns[i].flags&flagUnsigned != 0 {
  1014. val := binary.LittleEndian.Uint64(data[pos : pos+8])
  1015. if val > math.MaxInt64 {
  1016. dest[i] = uint64ToString(val)
  1017. } else {
  1018. dest[i] = int64(val)
  1019. }
  1020. } else {
  1021. dest[i] = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1022. }
  1023. pos += 8
  1024. continue
  1025. case fieldTypeFloat:
  1026. dest[i] = math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4]))
  1027. pos += 4
  1028. continue
  1029. case fieldTypeDouble:
  1030. dest[i] = math.Float64frombits(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1031. pos += 8
  1032. continue
  1033. // Length coded Binary Strings
  1034. case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
  1035. fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
  1036. fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
  1037. fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON:
  1038. var isNull bool
  1039. var n int
  1040. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  1041. pos += n
  1042. if err == nil {
  1043. if !isNull {
  1044. continue
  1045. } else {
  1046. dest[i] = nil
  1047. continue
  1048. }
  1049. }
  1050. return err
  1051. case
  1052. fieldTypeDate, fieldTypeNewDate, // Date YYYY-MM-DD
  1053. fieldTypeTime, // Time [-][H]HH:MM:SS[.fractal]
  1054. fieldTypeTimestamp, fieldTypeDateTime: // Timestamp YYYY-MM-DD HH:MM:SS[.fractal]
  1055. num, isNull, n := readLengthEncodedInteger(data[pos:])
  1056. pos += n
  1057. switch {
  1058. case isNull:
  1059. dest[i] = nil
  1060. continue
  1061. case rows.rs.columns[i].fieldType == fieldTypeTime:
  1062. // database/sql does not support an equivalent to TIME, return a string
  1063. var dstlen uint8
  1064. switch decimals := rows.rs.columns[i].decimals; decimals {
  1065. case 0x00, 0x1f:
  1066. dstlen = 8
  1067. case 1, 2, 3, 4, 5, 6:
  1068. dstlen = 8 + 1 + decimals
  1069. default:
  1070. return fmt.Errorf(
  1071. "protocol error, illegal decimals value %d",
  1072. rows.rs.columns[i].decimals,
  1073. )
  1074. }
  1075. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, true)
  1076. case rows.mc.parseTime:
  1077. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.Loc)
  1078. default:
  1079. var dstlen uint8
  1080. if rows.rs.columns[i].fieldType == fieldTypeDate {
  1081. dstlen = 10
  1082. } else {
  1083. switch decimals := rows.rs.columns[i].decimals; decimals {
  1084. case 0x00, 0x1f:
  1085. dstlen = 19
  1086. case 1, 2, 3, 4, 5, 6:
  1087. dstlen = 19 + 1 + decimals
  1088. default:
  1089. return fmt.Errorf(
  1090. "protocol error, illegal decimals value %d",
  1091. rows.rs.columns[i].decimals,
  1092. )
  1093. }
  1094. }
  1095. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, false)
  1096. }
  1097. if err == nil {
  1098. pos += int(num)
  1099. continue
  1100. } else {
  1101. return err
  1102. }
  1103. // Please report if this happens!
  1104. default:
  1105. return fmt.Errorf("unknown field type %d", rows.rs.columns[i].fieldType)
  1106. }
  1107. }
  1108. return nil
  1109. }