You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

packets.go 31KB

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