init.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package native
  2. import (
  3. "github.com/ziutek/mymysql/mysql"
  4. "log"
  5. )
  6. func (my *Conn) init() {
  7. my.seq = 0 // Reset sequence number, mainly for reconnect
  8. if my.Debug {
  9. log.Printf("[%2d ->] Init packet:", my.seq)
  10. }
  11. pr := my.newPktReader()
  12. my.info.prot_ver = pr.readByte()
  13. my.info.serv_ver = pr.readNTB()
  14. my.info.thr_id = pr.readU32()
  15. pr.readFull(my.info.scramble[0:8])
  16. pr.skipN(1)
  17. my.info.caps = pr.readU16()
  18. my.info.lang = pr.readByte()
  19. my.status = mysql.ConnStatus(pr.readU16())
  20. pr.skipN(13)
  21. if my.info.caps&_CLIENT_PROTOCOL_41 != 0 {
  22. pr.readFull(my.info.scramble[8:])
  23. }
  24. pr.skipAll() // Skip other information
  25. if my.Debug {
  26. log.Printf(tab8s+"ProtVer=%d, ServVer=\"%s\" Status=0x%x",
  27. my.info.prot_ver, my.info.serv_ver, my.status,
  28. )
  29. }
  30. if my.info.caps&_CLIENT_PROTOCOL_41 == 0 {
  31. panic(mysql.ErrOldProtocol)
  32. }
  33. }
  34. func (my *Conn) auth() {
  35. if my.Debug {
  36. log.Printf("[%2d <-] Authentication packet", my.seq)
  37. }
  38. flags := uint32(
  39. _CLIENT_PROTOCOL_41 |
  40. _CLIENT_LONG_PASSWORD |
  41. _CLIENT_LONG_FLAG |
  42. _CLIENT_TRANSACTIONS |
  43. _CLIENT_SECURE_CONN |
  44. _CLIENT_LOCAL_FILES |
  45. _CLIENT_MULTI_STATEMENTS |
  46. _CLIENT_MULTI_RESULTS)
  47. // Reset flags not supported by server
  48. flags &= uint32(my.info.caps) | 0xffff0000
  49. scrPasswd := encryptedPasswd(my.passwd, my.info.scramble[:])
  50. pay_len := 4 + 4 + 1 + 23 + len(my.user) + 1 + 1 + len(scrPasswd)
  51. if len(my.dbname) > 0 {
  52. pay_len += len(my.dbname) + 1
  53. flags |= _CLIENT_CONNECT_WITH_DB
  54. }
  55. pw := my.newPktWriter(pay_len)
  56. pw.writeU32(flags)
  57. pw.writeU32(uint32(my.max_pkt_size))
  58. pw.writeByte(my.info.lang) // Charset number
  59. pw.writeZeros(23) // Filler
  60. pw.writeNTB([]byte(my.user)) // Username
  61. pw.writeBin(scrPasswd) // Encrypted password
  62. if len(my.dbname) > 0 {
  63. pw.writeNTB([]byte(my.dbname))
  64. }
  65. if len(my.dbname) > 0 {
  66. pay_len += len(my.dbname) + 1
  67. flags |= _CLIENT_CONNECT_WITH_DB
  68. }
  69. return
  70. }
  71. func (my *Conn) oldPasswd() {
  72. if my.Debug {
  73. log.Printf("[%2d <-] Password packet", my.seq)
  74. }
  75. scrPasswd := encryptedOldPassword(my.passwd, my.info.scramble[:])
  76. pw := my.newPktWriter(len(scrPasswd) + 1)
  77. pw.write(scrPasswd)
  78. pw.writeByte(0)
  79. }