Skip to content

Commit 4fd4bff

Browse files
committed
Add support for connection attributes.
This sets attribute _client_name with the value "Go MySQL Driver" Also sets _os, _platform, _pid and program_name by default.
1 parent 0cc29e9 commit 4fd4bff

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Aaron Hopkins <go-sql-driver at die.net>
1515
Arne Hormann <arnehormann at gmail.com>
1616
Carlos Nieto <jose.carlos at menteslibres.net>
1717
Chris Moos <chris at tech9computers.com>
18+
Daniël van Eeden <daniel.van.eeden at myname.nl>
1819
DisposaBoy <disposaboy at dby.me>
1920
Frederick Mayle <frederickmayle at gmail.com>
2021
Gustavo Kristic <gkristic at gmail.com>

packets.go

+33
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import (
1616
"fmt"
1717
"io"
1818
"math"
19+
"os"
20+
"path"
21+
"runtime"
22+
"strconv"
1923
"time"
2024
)
2125

@@ -214,6 +218,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
214218
clientLongPassword |
215219
clientTransactions |
216220
clientLocalFiles |
221+
clientConnectAttrs |
217222
mc.flags&clientLongFlag
218223

219224
if mc.cfg.clientFoundRows {
@@ -228,7 +233,22 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
228233
// User Password
229234
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.passwd))
230235

236+
attrs := make(map[string]string)
237+
attrs["_os"] = runtime.GOOS
238+
attrs["_client_name"] = "Go MySQL Driver"
239+
attrs["_pid"] = strconv.Itoa(os.Getpid())
240+
attrs["_platform"] = runtime.GOARCH
241+
attrs["program_name"] = path.Base(os.Args[0])
242+
243+
attrlen := 0
244+
for attrname, attrvalue := range attrs {
245+
attrlen += len(attrname) + len(attrvalue)
246+
// one byte to store attrname length and one byte to store attrvalue length
247+
attrlen += 2
248+
}
249+
231250
pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff)
251+
pktLen += attrlen + 1 // one byte to store the total length of attrs
232252

233253
// To specify a db name
234254
if n := len(mc.cfg.dbname); n > 0 {
@@ -295,6 +315,19 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
295315
pos += copy(data[pos:], mc.cfg.dbname)
296316
data[pos] = 0x00
297317
}
318+
pos++
319+
320+
// Connection attributes
321+
data[pos] = byte(attrlen)
322+
pos++
323+
324+
for attrname, attrvalue := range attrs {
325+
data[pos] = byte(len(attrname))
326+
pos += 1 + copy(data[pos+1:], attrname)
327+
328+
data[pos] = byte(len(attrvalue))
329+
pos += 1 + copy(data[pos+1:], attrvalue)
330+
}
298331

299332
// Send Auth packet
300333
return mc.writePacket(data)

0 commit comments

Comments
 (0)