Skip to content

Commit 97d1741

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 66b7d5c commit 97d1741

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

@@ -219,6 +223,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
219223
clientTransactions |
220224
clientLocalFiles |
221225
clientPluginAuth |
226+
clientConnectAttrs |
222227
mc.flags&clientLongFlag
223228

224229
if mc.cfg.clientFoundRows {
@@ -233,7 +238,22 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
233238
// User Password
234239
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.passwd))
235240

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

238258
// To specify a db name
239259
if n := len(mc.cfg.dbname); n > 0 {
@@ -301,6 +321,19 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
301321
data[pos] = 0x00
302322
pos++
303323
}
324+
pos++
325+
326+
// Connection attributes
327+
data[pos] = byte(attrlen)
328+
pos++
329+
330+
for attrname, attrvalue := range attrs {
331+
data[pos] = byte(len(attrname))
332+
pos += 1 + copy(data[pos+1:], attrname)
333+
334+
data[pos] = byte(len(attrvalue))
335+
pos += 1 + copy(data[pos+1:], attrvalue)
336+
}
304337

305338
// Assume native client during response
306339
pos += copy(data[pos:], "mysql_native_password")

0 commit comments

Comments
 (0)