MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / readInitialHandshake

Method readInitialHandshake

IceFireDB-SQLProxy/pkg/mysql/client/auth.go:23–97  ·  view source on GitHub ↗

See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake

()

Source from the content-addressed store, hash-verified

21
22// See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
23func (c *Conn) readInitialHandshake() error {
24 data, err := c.ReadPacket()
25 if err != nil {
26 return errors.Trace(err)
27 }
28
29 if data[0] == ERR_HEADER {
30 return errors.Annotate(c.handleErrorPacket(data), "read initial handshake error")
31 }
32
33 if data[0] < MinProtocolVersion {
34 return errors.Errorf("invalid protocol version %d, must >= 10", data[0])
35 }
36
37 // skip mysql version
38 // mysql version end with 0x00
39 pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1
40
41 // connection id length is 4
42 c.connectionID = binary.LittleEndian.Uint32(data[pos : pos+4])
43 pos += 4
44
45 c.salt = []byte{}
46 c.salt = append(c.salt, data[pos:pos+8]...)
47
48 // skip filter
49 pos += 8 + 1
50
51 // capability lower 2 bytes
52 c.capability = uint32(binary.LittleEndian.Uint16(data[pos : pos+2]))
53 // check protocol
54 if c.capability&CLIENT_PROTOCOL_41 == 0 {
55 return errors.New("the MySQL server can not support protocol 41 and above required by the client")
56 }
57 if c.capability&CLIENT_SSL == 0 && c.tlsConfig != nil {
58 return errors.New("the MySQL Server does not support TLS required by the client")
59 }
60 pos += 2
61
62 if len(data) > pos {
63 // skip server charset
64 // c.charset = data[pos]
65 pos += 1
66
67 c.status = binary.LittleEndian.Uint16(data[pos : pos+2])
68 pos += 2
69 // capability flags (upper 2 bytes)
70 c.capability = uint32(binary.LittleEndian.Uint16(data[pos:pos+2]))<<16 | c.capability
71 pos += 2
72
73 // skip auth data len or [00]
74 // skip reserved (all [00])
75 pos += 10 + 1
76
77 // The documentation is ambiguous about the length.
78 // The official Python library uses the fixed length 12
79 // mysql-proxy also use 12
80 // which is not documented but seems to work.

Callers 1

handshakeMethod · 0.95

Calls 2

ReadPacketMethod · 0.95
handleErrorPacketMethod · 0.95

Tested by

no test coverage detected