forked from thda/tds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
1242 lines (1124 loc) · 26.3 KB
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tds
import (
"fmt"
"os"
"reflect"
"strconv"
bin "github.com/thda/tds/binary"
"errors"
)
// messagerReader is the interface which describes
// the messages sent by the netlib buffer.
//
// A message should provide serialization functions
// as well as a token and clues on its size.
type messageReader interface {
Token() token
Size() uint8
SizeLen() uint8
LimitRead() bool
Read(*bin.Encoder) error
}
// messageReaderWriter is implemented by messages
// which can be read an written
type messageReaderWriter interface {
messageReader
Write(*bin.Encoder) error
}
//go:generate stringer -type=token
type token byte
// message Tokens
const (
noneToken token = 0x00
capabilityReqToken token = 0x01
capabilityResToken token = 0x02
paramFmt2Token token = 0x20 // 32
languageToken token = 0x21 // 33
orderBy2Token token = 0x22 // 34
wideColumnFmtToken token = 0x61 // 97
dynamic2Token token = 0x62 // 98
msgToken token = 0x65 // 101
returnStatusToken token = 0x79 // 121
curCloseToken token = 0x80 // 128
curDeleteToken token = 0x81 // 129
curFetchToken token = 0x82 // 130
curFmtToken token = 0x83 // 131
curOpenToken token = 0x84 // 132
curDeclareToken token = 0x86 // 134
logoutToken token = 0x71 // 113
tableNameToken token = 0xa4 // 164
columnInfoToken token = 0xa5 // 165
optionCmdToken token = 0xa6 // 166
cmpRowNameToken token = 0xa7 // 167
cmpRowFmtToken token = 0xa8 // 168
orderByToken token = 0xa9 // 169
infoToken token = 0xab // 171
loginAckToken token = 0xad // 173
controlToken token = 0xae // 174
rowToken token = 0xd1 // 209
cmpRowToken token = 0xd3 // 211
paramToken token = 0xd7 // 215
capabilitiesToken token = 0xe2 // 226
envChangeToken token = 0xe3 // 227
sqlMessageToken token = 0xe5 // 229
dbRPCToken token = 0xe6 // 230
dynamicToken token = 0xe7 // 231
paramFmtToken token = 0xec // 236
authToken token = 0xed // 237
columnFmtToken token = 0xee // 238
doneToken token = 0xfd // 253
doneProcToken token = 0xfe // 254
doneInProcToken token = 0xff // 255
)
// Message attribute options
const (
noFlag = 0
fixedSize uint8 = 1 << iota // fixed length message ?
// shall we use a limited reader for this messages ?
//
// This is used by some messages
// when there is now way to tell we are at the end of the messages.
// See for example columnInfo, tabName.
limitRead
long // 32 bits length field
short // 8 bits length field
ignoreSize
)
// msgs is a table which contains the attributes for each message type
var msgs = map[token]msg{
noneToken: {noneToken, noFlag, 0},
capabilityReqToken: {capabilityReqToken, noFlag, 0},
capabilityResToken: {capabilityResToken, noFlag, 0},
paramFmt2Token: {paramFmt2Token, long, 0},
languageToken: {languageToken, ignoreSize, 0},
orderBy2Token: {orderBy2Token, long, 0},
cmpRowFmtToken: {cmpRowFmtToken, noFlag, 0},
cmpRowNameToken: {cmpRowNameToken, noFlag, 0},
wideColumnFmtToken: {wideColumnFmtToken, long, 0},
dynamic2Token: {dynamic2Token, long, 0},
returnStatusToken: {returnStatusToken, fixedSize, 4},
curCloseToken: {curCloseToken, noFlag, 0},
curDeleteToken: {curDeleteToken, noFlag, 0},
curFetchToken: {curFetchToken, noFlag, 0},
curFmtToken: {curFmtToken, noFlag, 0},
curOpenToken: {curOpenToken, noFlag, 0},
curDeclareToken: {curDeclareToken, noFlag, 0},
logoutToken: {logoutToken, fixedSize, 1},
tableNameToken: {tableNameToken, limitRead, 0},
columnInfoToken: {columnInfoToken, limitRead, 0},
optionCmdToken: {optionCmdToken, noFlag, 0},
controlToken: {controlToken, noFlag, 0},
orderByToken: {orderByToken, noFlag, 0},
loginAckToken: {loginAckToken, noFlag, 0},
rowToken: {rowToken, ignoreSize, 0},
paramToken: {paramToken, ignoreSize, 0},
cmpRowToken: {cmpRowToken, ignoreSize, 0},
capabilitiesToken: {capabilitiesToken, noFlag, 0},
envChangeToken: {envChangeToken, noFlag, 0},
sqlMessageToken: {sqlMessageToken, noFlag, 0},
infoToken: {infoToken, noFlag, 0},
dbRPCToken: {dbRPCToken, noFlag, 0},
dynamicToken: {dynamicToken, noFlag, 0},
paramFmtToken: {paramFmtToken, noFlag, 0},
columnFmtToken: {columnFmtToken, noFlag, 0},
doneToken: {doneToken, fixedSize, 8},
msgToken: {msgToken, short, 0},
doneProcToken: {doneProcToken, fixedSize, 8},
doneInProcToken: {doneInProcToken, fixedSize, 8}}
// msg is the struct containing all the attributes of a TDS message.
// It is embeded in all message structs and implements the Message interface.
type msg struct {
token token
flags uint8
size uint8 // Size for fixed length messages
}
func newMsg(t token) msg {
// check for existence
attr, _ := safeGetMsg(t)
return attr
}
func safeGetMsg(t token) (msg, error) {
// check for existence
attr, ok := msgs[t]
if !ok {
return msg{}, fmt.Errorf("tds: unknown token %s", t)
}
return attr, nil
}
func (attr msg) Token() token {
return attr.token
}
func (attr msg) Size() uint8 {
if attr.flags&fixedSize != 0 {
return attr.size
}
return 0
}
func (attr msg) SizeLen() uint8 {
if attr.flags&short != 0 {
return 8
}
if attr.flags&long != 0 {
return 32
}
if attr.flags&fixedSize != 0 || attr.flags&ignoreSize != 0 {
return 0
}
return 16
}
func (attr msg) LimitRead() bool {
return attr.flags&limitRead != 0
}
type emptyMsg struct {
msg
}
func (e emptyMsg) Write(*bin.Encoder) error {
return nil
}
func (e emptyMsg) Read(*bin.Encoder) error {
return nil
}
//
// capabilities
//
const defaultcapabilitiesLength = 14
// capabilities request bit
const (
_ = iota
reqLang
reqRPC
reqEvt
reqMstmt
reqBcp
reqCursor
reqDynf
reqMsg
reqParam
dataInt1
dataInt2
dataInt4
dataBit
dataChar
dataVchar
dataBin
dataVbin
dataMny8
dataMny4
dataDate8
dataDate4
dataFlt4
dataFlt8
dataNum
dataText
dataImage
dataDec
dataLchar
dataLbin
dataIntn
dataDatetimen
dataMoneyn
csrPrev
csrFirst
csrLast
csrAbs
csrRel
csrMulti
conOob
conInband
conLogical
protoText
protoBulk
reqUrgevt
dataSensitivity
dataBoundary
protoDynamic
protoDynproc
dataFltn
dataBitn
dataInt8
dataVoid
dolBulk
objectJava1
objectChar
reqReserved1
objectBinary
dataColumnstatus
widetable
reqReserved2
dataUint2
dataUint4
dataUint8
dataUintn
curImplicit
dataNlbin
imageNchar
blobNchar16
blobNchar8
blobNcharScsu
dataDate
dataTime
dataInterval
csrScroll
csrSensitive
csrInsensitive
csrSemisensitive
csrKeysetdriven
reqSrvpktsize
dataUnitext
capClusterfailover
dataSint1
reqLargeident
reqBlobNchar16
dataXML
reqCurinfo3
reqDbrpc2
_
reqMigrate
multiRequests
_
_
dataBigdatetime
dataUsecs
rpcparamLob
reqInstid
reqGrid
reqDynBatch
reqLangBatch
reqRPCBatch
dataLoblocator
reqRowCountSelect
reqLogParams
reqDynNoParamFmt
reqRO
)
// capabilities response bits
const (
_ = iota
resNomsg
resNoeed
resNoparam
dataNoint1
dataNoint2
dataNoint4
dataNobit
dataNochar
dataNovchar
dataNobin
dataNovbin
dataNomny8
dataNomny4
dataNodate8
dataNodate4
dataNoflt4
dataNoflt8
dataNonum
dataNotext
dataNoimage
dataNodec
dataNolchar
dataNolbin
dataNointn
dataNodatetimen
dataNomoneyn
conNooob
conNoinband
protoNotext
protoNobulk
dataNosensitivity
dataNoboundary
resNotdsdebug
resNostripblanks
dataNoint8
objectNojava1
objectNochar
dataNocolumnstatus
objectNobinary
resReserved1
dataNouint2
dataNouint4
dataNouint8
dataNouintn
noWidetables
dataNonlbin
imageNonchar
blobNonchar16
blobNonchar8
blobNoncharScsu
dataNodate
dataNotime
dataNointerval
dataNounitext
dataNosint1
resNolargeident
resNoblobNchar16
noSrvpktsize
resNodataXML
nonintReturnValue
resNoxnldata
resSuppressFmt
resSuppressDoneinproc
resForceRowfmt2
dataNobigdatetime
dataNousecs
resNoTdscontrol
rpcparamNolob
_
dataNoloblocator
)
var defaultReqcapabilities = [...]int{dataLoblocator, reqLangBatch, reqDynBatch,
rpcparamLob, dataUsecs, dataBigdatetime,
reqDbrpc2, reqCurinfo3, dataXML, reqLargeident, dataUnitext,
reqSrvpktsize, csrSemisensitive, csrInsensitive, csrScroll, dataTime,
dataDate, dataNlbin, curImplicit, dataUintn,
dataUint8, dataUint4, dataUint2, widetable,
dolBulk, dataVoid, dataInt8, dataFltn, protoDynproc,
dataBoundary, dataSensitivity, conInband,
csrMulti, csrRel, csrAbs, csrLast, csrFirst, csrPrev, dataMoneyn,
dataDatetimen, dataIntn, dataLbin, dataLchar, dataDec, dataImage, dataText, dataNum,
dataFlt8, dataFlt4, dataDate4, dataDate8, dataMny4, dataMny8, dataVbin, dataBin,
dataVchar, dataChar, dataBit, dataInt4, dataInt2, dataInt1, reqParam, reqMsg,
reqDynf, reqCursor, reqBcp, reqRPC, reqLang}
var defaultRescapabilities = [...]int{resNoTdscontrol, resSuppressFmt,
resNoxnldata, resNotdsdebug, objectNojava1}
// capabilities token. Tricky one. Stores the connection's capability
// in an array if bits.
type capabilities struct {
msg
reqToken byte
e int8
req []byte
resToken byte
resFieldLen int8
res []byte
}
func newCapabilities() *capabilities {
c := &capabilities{reqToken: byte(capabilityReqToken), resToken: byte(capabilityResToken)}
c.req = make([]byte, defaultcapabilitiesLength)
c.res = make([]byte, defaultcapabilitiesLength)
// set capabilities from default
for _, capability := range &defaultReqcapabilities {
c.setcapabilities(capabilityReqToken, capability)
}
for _, capability := range &defaultRescapabilities {
c.setcapabilities(capabilityResToken, capability)
}
return c
}
// Setcapabilities sets the capabilities of a capability structs by playing with the bitmaps
func (c *capabilities) setcapabilities(capabilityType token, capabilities ...int) error {
var target []byte
var length int
// determine the target array
switch capabilityType {
case capabilityReqToken:
target = c.req[:]
case capabilityResToken:
target = c.res[:]
default:
return errors.New("tds: invalid capability type. Should be capabilityReqToken or capabilityResToken")
}
length = len(target)
for _, capability := range capabilities[:] {
// get the byte to modify and the bit position in this byte
capIndex := length - 1 - capability/8
pos := uint(capability) % 8
if capIndex >= length {
return fmt.Errorf("tds: trying to write above the capacity array length, %d > %d", length, capIndex)
}
// bit shifting at the correct offset
target[capIndex] |= (1 << pos)
}
return nil
}
// IsSet check if a capability is set
func (c *capabilities) isSet(capabilityType token, capability int) bool {
var target []byte
var length int
switch capabilityType {
case capabilityReqToken:
target = c.req[:]
case capabilityResToken:
target = c.res[:]
default:
return false
}
length = len(target)
// get the byte to access and the bit position in this byte
capIndex := length - 1 - capability/8
pos := capability % 8
// bit shifting at the correct offset
return capIndex < length && target[capIndex]&(1<<uint(pos)) != 0
}
// Write serializes a capabilities struct
func (c capabilities) Write(e *bin.Encoder) error {
e.WriteByte(c.reqToken)
e.WriteInt8(int8(len(c.req)))
e.Write(c.req[:])
e.WriteByte(c.resToken)
e.WriteInt8(int8(len(c.res)))
e.Write(c.res[:])
err := e.Err()
return err
}
// Reads a capabilities struct
func (c *capabilities) Read(e *bin.Encoder) error {
c.reqToken = e.ReadByte()
c.e = e.Int8()
c.req = make([]byte, c.e)
e.Read(c.req[:])
c.resToken = e.ReadByte()
c.resFieldLen = e.Int8()
c.res = make([]byte, c.resFieldLen)
e.Read(c.res[:])
err := e.Err()
return err
}
//
// done
//
// Transaction states
const (
doneNoTran = iota // No transaction in effect
doneTranSucceed // Transaction completed successfully
doneTranProgress // Transaction in progress
doneTranAbort // Transaction aborted
)
// Done packets status
const (
doneFinal = 0x0000
doneMoreResults = 0x0001
doneError = 0x0002
doneProc = 0x0008
doneCount = 0x0010
doneProcCount = 0x0040
doneCancel = 0x0020
)
// done token
type done struct {
msg
status int16
tranState int16
count int32
}
func (d done) Write(e *bin.Encoder) error {
e.WriteInt16(d.status)
e.WriteInt16(d.tranState)
e.WriteInt32(d.count)
err := e.Err()
return err
}
func (d *done) Read(e *bin.Encoder) error {
d.status = e.Int16()
d.tranState = e.Int16()
d.count = e.Int32()
err := e.Err()
return err
}
// dynamic type
const (
dynamicPrepare = 0x01 << iota
dynamicExec
dynamicDealloc
dynamicExecImmediate
dynamicProcname
dynamicAck
dynamicDescIn
dynamicDescOut
)
// dynamic status
const (
dynamicHasArgs = 0x01 << iota
dynamicSuppressParamFmt
dynamicBatchParams
)
//
// dynamic
//
// dynamic is a dynamic statement prepare / ack token
type dynamic struct {
msg
operation byte
status byte
name string // 1 byte length
statement string // 4 bytes length
}
// Read unserializes a Dynamic2 struct
func (d *dynamic) Read(e *bin.Encoder) error {
d.operation = e.ReadByte()
d.status = e.ReadByte()
d.name, _ = e.ReadString(8)
// dynamicPrepare messages contain the statement
if d.operation&dynamicPrepare > 0 {
d.statement, _ = e.ReadString(32)
}
err := e.Err()
return err
}
// Write writes the dynamic token to the wire
func (d dynamic) Write(e *bin.Encoder) error {
e.WriteByte(d.operation)
e.WriteByte(d.status)
e.WriteStringWithLen(8, d.name)
// DynamicPrepare messages contain the statement
if d.operation&dynamicPrepare > 0 {
e.WriteStringWithLen(32, d.statement)
}
err := e.Err()
return err
}
//
// envChange
//
//go:generate stringer -type=changeType
type changeType byte
// login change tokens
const (
dbChange changeType = 0x01
langChange changeType = 0x02
charsetChange changeType = 0x03
packetSizeChange changeType = 0x04
)
// EnvChange token used to change packet size, db, and some other options
type envChange struct {
msg
changeType changeType
newValue string // 8 bit length
oldValue string // 8 bit length
}
func (env envChange) Write(e *bin.Encoder) error {
e.WriteByte(byte(env.changeType))
e.WriteStringWithLen(8, env.newValue)
e.WriteStringWithLen(8, env.oldValue)
err := e.Err()
return err
}
func (env *envChange) Read(e *bin.Encoder) error {
env.changeType = changeType(e.ReadByte())
env.newValue, _ = e.ReadString(8)
env.oldValue, _ = e.ReadString(8)
err := e.Err()
return err
}
func (env envChange) String() string {
return "env change: " + fmt.Sprint(env.changeType) +
"\nold value: " + env.oldValue + "\nnew value: " + env.newValue
}
//
// language
//
// language token
type language struct {
msg
messageLen uint32
status byte
query string
}
// Read unserializes a TdsLanguage struct
func (l *language) Read(e *bin.Encoder) error {
l.messageLen = e.Uint32()
l.status = e.ReadByte()
l.query = e.ReadStringWithLen(int(l.messageLen - 1))
err := e.Err()
return err
}
// Read unserializes a TdsLanguage struct
func (l language) Write(e *bin.Encoder) error {
e.WriteUint32(uint32(len(l.query)) + 1)
e.WriteByte(l.status)
e.WriteString(l.query)
err := e.Err()
return err
}
// Implement the string interface for debugging purposes
func (l language) String() string {
return fmt.Sprintf("status: %#x\n", l.status) +
fmt.Sprintf("query: %s\n", l.query)
}
//
// login
//
const defaultLibrary = "gtds"
var defaultProtocolVersion = [4]byte{5, 0, 0, 0}
var defaultLibraryVersion = [4]byte{1, 0, 0, 0}
var (
loginSecEncrypt1 = uint8(1)
loginSecEncrypt2 = uint8(32)
loginSecNonce = uint8(128)
)
// login is the tds v5 login packet
type login struct {
msg
clientHost string // 30 bytes
user string // 30 bytes
password string // 30 bytes
pid string // 30 bytes
int2BE int8
int4BE int8
char int8
flt int8
dateBE int8
notifyDBChange int8
bulkCopy int8
_ [9]byte
app string // 30 bytes
server string // 30 bytes
password2Length int
password2 string // 253 bytes, length prefix and suffix
protocolVersion [4]byte
library string // 30 bytes
libraryVersion [4]byte
convertShort int8
float4BE int8
dateTime4BE int8
language string // 30 bytes
_ byte
oldSecure int16
encrypted uint8
_ byte
secSpare [9]byte
charset string // 30 bytes
_ int8
packetSize int // 6 bytes string
capabilities capabilities
}
// set the login capabilities and trim them
func (l *login) setCapabilities(c capabilities) {
l.capabilities = c
}
func writeFixedSizeString(e *bin.Encoder, s string, size int, putSize bool) int {
if len(s) > size {
s = s[:size]
}
written := e.WriteString(s)
e.Pad(0x00, size-written)
if putSize {
e.WriteInt8(int8(written))
}
return written
}
func readFixedSizeString(e *bin.Encoder, size int) string {
out := make([]byte, size)
e.Read(out)
realSize := e.Int8()
return string(out[:realSize])
}
func (l login) Write(e *bin.Encoder) error {
writeFixedSizeString(e, l.clientHost, 30, true)
writeFixedSizeString(e, l.user, 30, true)
writeFixedSizeString(e, l.password, 30, true)
writeFixedSizeString(e, fmt.Sprintf("%d", os.Getpid()), 30, true)
e.WriteInt8(3) // type of int2
e.WriteInt8(1) // type of int4
e.WriteInt8(6) // type of char
e.WriteInt8(10) // type of flt
e.WriteInt8(9) // type of date
e.WriteInt8(1) // notify of use db
e.WriteInt8(0) // disallow dump/load and bulk insert
e.Pad(0x00, 9) // magic
writeFixedSizeString(e, l.app, 30, true)
writeFixedSizeString(e, l.server, 30, true)
e.WriteByte(0x00)
e.WriteInt8(int8(0))
writeFixedSizeString(e, "", 254, false)
e.Write(l.protocolVersion[:])
writeFixedSizeString(e, l.library, 10, true)
e.Write(l.libraryVersion[:])
e.WriteInt8(0) // convert short date
e.WriteInt8(13) // format of flt32
e.WriteInt8(17) // format of smalldate
writeFixedSizeString(e, l.language, 30, true)
e.WriteInt8(1)
e.Pad(0x00, 2)
e.WriteUint8(l.encrypted)
e.Pad(0x00, 10)
writeFixedSizeString(e, l.charset, 30, true)
e.WriteInt8(1) // notify language change
writeFixedSizeString(e, fmt.Sprintf("%d", l.packetSize), 6, true)
// if no packet size was given, ask the server
if l.packetSize == 0 {
l.capabilities.setcapabilities(capabilityReqToken, reqSrvpktsize)
}
e.Pad(0x00, 4) // magic
err := e.Err()
return err
}
func (l *login) Read(e *bin.Encoder) error {
l.clientHost = readFixedSizeString(e, 30)
l.user = readFixedSizeString(e, 30)
l.password = readFixedSizeString(e, 30)
l.pid = readFixedSizeString(e, 30)
l.int2BE = e.Int8()
e.Skip(15)
l.app = readFixedSizeString(e, 30)
l.server = readFixedSizeString(e, 30)
e.Skip(256)
e.Read(l.protocolVersion[:])
l.library = readFixedSizeString(e, 10)
e.Read(l.libraryVersion[:])
e.Skip(3)
l.language = readFixedSizeString(e, 30)
e.Skip(14)
l.charset = readFixedSizeString(e, 30)
e.Skip(1)
pktStr := readFixedSizeString(e, 6)
l.packetSize, _ = strconv.Atoi(pktStr)
e.Skip(4)
l.capabilities.Read(e)
err := e.Err()
return err
}
//
// loginAck
//
// login status
const (
loginSuccessToken = 0x05
loginFailedToken = 0x06
)
// LoginAck is the login ack packet
type loginAck struct {
msg
ack int8
tdsVersion [4]byte
serverFieldLen int8
server string
serverVersion [4]byte
}
// Write serializes a TdsLoginAck struct
func (l loginAck) Write(e *bin.Encoder) error {
e.WriteByte(loginSuccessToken)
e.Write(l.tdsVersion[:])
e.WriteStringWithLen(8, l.server)
e.Write(l.serverVersion[:])
err := e.Err()
return err
}
// Read reads the LoginAck from the connection
func (l *loginAck) Read(e *bin.Encoder) error {
l.ack = e.Int8()
e.Read(l.tdsVersion[:])
l.server, _ = e.ReadString(8)
e.Read(l.serverVersion[:])
err := e.Err()
return err
}
//
// logout
//
type logout struct {
msg
option byte
}
func (l logout) Write(e *bin.Encoder) error {
e.WriteByte(l.option)
err := e.Err()
return err
}
func (l logout) Read(e *bin.Encoder) error {
l.option = e.ReadByte()
err := e.Err()
return err
}
//
// dbRPC
//
type dbRPC struct {
msg
Name string
Flags uint16
HasParams bool
}
func (r *dbRPC) Read(e *bin.Encoder) error {
r.Name, _ = e.ReadString(8)
r.Flags = e.Uint16()
// check for parameters
r.HasParams = r.Flags&0x02 != 0
err := e.Err()
return err
}
//
// sqlMessage
//
// SybError is the struct containing sybase error information
type SybError struct {
MsgNumber int32
State int8
Severity int8
SQLState string // 1 byte size
HasEed uint8
TranState uint16
Message string // 2 bytes size
Server string // 1 byte size
Procedure string // 1 byte size
LineNumber int16
}
// implement the error interface
func (e SybError) Error() string {
if e.Procedure != "" {
return fmt.Sprintf("Msg: %d, Level: %d, State: %d\nServer: %s, Procedure: %s, Line: %d:\n%s",
e.MsgNumber, e.Severity, e.State, e.Server, e.Procedure, e.LineNumber, e.Message)
}
return fmt.Sprintf("Msg: %d, Level: %d, State: %d\nServer: %s, Line: %d:\n%s",
e.MsgNumber, e.Severity, e.State, e.Server, e.LineNumber, e.Message)
}
// Max message size
const maxEedSize = 1024
// sqlMessage message structure.
type sqlMessage struct {
msg
SybError
}
func (m sqlMessage) Write(e *bin.Encoder) error {
e.WriteInt32(m.MsgNumber)
e.WriteInt8(m.State)
e.WriteInt8(m.Severity)
e.WriteStringWithLen(8, m.SQLState)
e.WriteUint8(m.HasEed)
e.WriteUint16(m.TranState)
e.WriteStringWithLen(16, m.Message)
e.WriteStringWithLen(8, m.Server)
e.WriteStringWithLen(8, m.Procedure)
e.WriteInt16(m.LineNumber)
err := e.Err()
return err
}
func (m *sqlMessage) Read(e *bin.Encoder) error {
m.MsgNumber = e.Int32()
m.State = e.Int8()
m.Severity = e.Int8()
m.SQLState, _ = e.ReadString(8)
m.HasEed = e.Uint8()
m.TranState = e.Uint16()
m.Message, _ = e.ReadString(16)
m.Server, _ = e.ReadString(8)
m.Procedure, _ = e.ReadString(8)
m.LineNumber = e.Int16()
err := e.Err()
return err
}
//
// option
//