2016-12-24 01:37:35 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2019-06-30 03:28:17 +00:00
|
|
|
"io"
|
2016-12-24 01:37:35 +00:00
|
|
|
)
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
type packetType uint8
|
|
|
|
|
2016-12-24 01:37:35 +00:00
|
|
|
type header struct {
|
2019-06-30 03:28:17 +00:00
|
|
|
PacketType packetType
|
2016-12-24 01:37:35 +00:00
|
|
|
Status uint8
|
|
|
|
Size uint16
|
|
|
|
Spid uint16
|
|
|
|
PacketNo uint8
|
|
|
|
Pad uint8
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
// tdsBuffer reads and writes TDS packets of data to the transport.
|
|
|
|
// The write and read buffers are separate to make sending attn signals
|
|
|
|
// possible without locks. Currently attn signals are only sent during
|
|
|
|
// reads, not writes.
|
2016-12-24 01:37:35 +00:00
|
|
|
type tdsBuffer struct {
|
2019-06-30 03:28:17 +00:00
|
|
|
transport io.ReadWriteCloser
|
|
|
|
|
|
|
|
packetSize int
|
|
|
|
|
|
|
|
// Write fields.
|
|
|
|
wbuf []byte
|
|
|
|
wpos int
|
|
|
|
wPacketSeq byte
|
|
|
|
wPacketType packetType
|
|
|
|
|
|
|
|
// Read fields.
|
|
|
|
rbuf []byte
|
|
|
|
rpos int
|
|
|
|
rsize int
|
2016-12-24 01:37:35 +00:00
|
|
|
final bool
|
2019-06-30 03:28:17 +00:00
|
|
|
rPacketType packetType
|
|
|
|
|
|
|
|
// afterFirst is assigned to right after tdsBuffer is created and
|
|
|
|
// before the first use. It is executed after the first packet is
|
|
|
|
// written and then removed.
|
|
|
|
afterFirst func()
|
2016-12-24 01:37:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
func newTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *tdsBuffer {
|
|
|
|
return &tdsBuffer{
|
|
|
|
packetSize: int(bufsize),
|
|
|
|
wbuf: make([]byte, 1<<16),
|
|
|
|
rbuf: make([]byte, 1<<16),
|
|
|
|
rpos: 8,
|
|
|
|
transport: transport,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *tdsBuffer) ResizeBuffer(packetSize int) {
|
|
|
|
rw.packetSize = packetSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tdsBuffer) PackageSize() int {
|
|
|
|
return w.packetSize
|
2016-12-24 01:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tdsBuffer) flush() (err error) {
|
2019-06-30 03:28:17 +00:00
|
|
|
// Write packet size.
|
|
|
|
w.wbuf[0] = byte(w.wPacketType)
|
|
|
|
binary.BigEndian.PutUint16(w.wbuf[2:], uint16(w.wpos))
|
|
|
|
w.wbuf[6] = w.wPacketSeq
|
2016-12-24 01:37:35 +00:00
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
// Write packet into underlying transport.
|
|
|
|
if _, err = w.transport.Write(w.wbuf[:w.wpos]); err != nil {
|
2016-12-24 01:37:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
// It is possible to create a whole new buffer after a flush.
|
|
|
|
// Useful for debugging. Normally reuse the buffer.
|
|
|
|
// w.wbuf = make([]byte, 1<<16)
|
2016-12-24 01:37:35 +00:00
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
// Execute afterFirst hook if it is set.
|
2016-12-24 01:37:35 +00:00
|
|
|
if w.afterFirst != nil {
|
|
|
|
w.afterFirst()
|
|
|
|
w.afterFirst = nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
w.wpos = 8
|
|
|
|
w.wPacketSeq++
|
2016-12-24 01:37:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tdsBuffer) Write(p []byte) (total int, err error) {
|
|
|
|
for {
|
2019-06-30 03:28:17 +00:00
|
|
|
copied := copy(w.wbuf[w.wpos:w.packetSize], p)
|
|
|
|
w.wpos += copied
|
2016-12-24 01:37:35 +00:00
|
|
|
total += copied
|
|
|
|
if copied == len(p) {
|
2019-06-30 03:28:17 +00:00
|
|
|
return
|
2016-12-24 01:37:35 +00:00
|
|
|
}
|
|
|
|
if err = w.flush(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p = p[copied:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tdsBuffer) WriteByte(b byte) error {
|
2019-07-26 04:10:20 +00:00
|
|
|
if int(w.wpos) == len(w.wbuf) || w.wpos == w.packetSize {
|
2016-12-24 01:37:35 +00:00
|
|
|
if err := w.flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
w.wbuf[w.wpos] = b
|
|
|
|
w.wpos += 1
|
2016-12-24 01:37:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:10:20 +00:00
|
|
|
func (w *tdsBuffer) BeginPacket(packetType packetType, resetSession bool) {
|
|
|
|
status := byte(0)
|
|
|
|
if resetSession {
|
|
|
|
switch packetType {
|
|
|
|
// Reset session can only be set on the following packet types.
|
|
|
|
case packSQLBatch, packRPCRequest, packTransMgrReq:
|
|
|
|
status = 0x8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.wbuf[1] = status // Packet is incomplete. This byte is set again in FinishPacket.
|
2019-06-30 03:28:17 +00:00
|
|
|
w.wpos = 8
|
|
|
|
w.wPacketSeq = 1
|
|
|
|
w.wPacketType = packetType
|
2016-12-24 01:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tdsBuffer) FinishPacket() error {
|
2019-07-26 04:10:20 +00:00
|
|
|
w.wbuf[1] |= 1 // Mark this as the last packet in the message.
|
2016-12-24 01:37:35 +00:00
|
|
|
return w.flush()
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
var headerSize = binary.Size(header{})
|
|
|
|
|
2016-12-24 01:37:35 +00:00
|
|
|
func (r *tdsBuffer) readNextPacket() error {
|
2019-06-30 03:28:17 +00:00
|
|
|
h := header{}
|
2016-12-24 01:37:35 +00:00
|
|
|
var err error
|
2019-06-30 03:28:17 +00:00
|
|
|
err = binary.Read(r.transport, binary.BigEndian, &h)
|
2016-12-24 01:37:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 04:10:20 +00:00
|
|
|
if int(h.Size) > r.packetSize {
|
2016-12-24 01:37:35 +00:00
|
|
|
return errors.New("Invalid packet size, it is longer than buffer size")
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
if headerSize > int(h.Size) {
|
2016-12-24 01:37:35 +00:00
|
|
|
return errors.New("Invalid packet size, it is shorter than header size")
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
_, err = io.ReadFull(r.transport, r.rbuf[headerSize:h.Size])
|
2016-12-24 01:37:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
r.rpos = headerSize
|
|
|
|
r.rsize = int(h.Size)
|
|
|
|
r.final = h.Status != 0
|
|
|
|
r.rPacketType = h.PacketType
|
2016-12-24 01:37:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:28:17 +00:00
|
|
|
func (r *tdsBuffer) BeginRead() (packetType, error) {
|
2016-12-24 01:37:35 +00:00
|
|
|
err := r.readNextPacket()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
return r.rPacketType, nil
|
2016-12-24 01:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) ReadByte() (res byte, err error) {
|
2019-06-30 03:28:17 +00:00
|
|
|
if r.rpos == r.rsize {
|
2016-12-24 01:37:35 +00:00
|
|
|
if r.final {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
err = r.readNextPacket()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
res = r.rbuf[r.rpos]
|
|
|
|
r.rpos++
|
2016-12-24 01:37:35 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) byte() byte {
|
|
|
|
b, err := r.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
badStreamPanic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) ReadFull(buf []byte) {
|
|
|
|
_, err := io.ReadFull(r, buf[:])
|
|
|
|
if err != nil {
|
|
|
|
badStreamPanic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) uint64() uint64 {
|
|
|
|
var buf [8]byte
|
|
|
|
r.ReadFull(buf[:])
|
|
|
|
return binary.LittleEndian.Uint64(buf[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) int32() int32 {
|
|
|
|
return int32(r.uint32())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) uint32() uint32 {
|
|
|
|
var buf [4]byte
|
|
|
|
r.ReadFull(buf[:])
|
|
|
|
return binary.LittleEndian.Uint32(buf[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) uint16() uint16 {
|
|
|
|
var buf [2]byte
|
|
|
|
r.ReadFull(buf[:])
|
|
|
|
return binary.LittleEndian.Uint16(buf[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) BVarChar() string {
|
|
|
|
l := int(r.byte())
|
|
|
|
return r.readUcs2(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) UsVarChar() string {
|
|
|
|
l := int(r.uint16())
|
|
|
|
return r.readUcs2(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) readUcs2(numchars int) string {
|
|
|
|
b := make([]byte, numchars*2)
|
|
|
|
r.ReadFull(b)
|
|
|
|
res, err := ucs22str(b)
|
|
|
|
if err != nil {
|
|
|
|
badStreamPanic(err)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tdsBuffer) Read(buf []byte) (copied int, err error) {
|
|
|
|
copied = 0
|
|
|
|
err = nil
|
2019-06-30 03:28:17 +00:00
|
|
|
if r.rpos == r.rsize {
|
2016-12-24 01:37:35 +00:00
|
|
|
if r.final {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
err = r.readNextPacket()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:28:17 +00:00
|
|
|
copied = copy(buf, r.rbuf[r.rpos:r.rsize])
|
|
|
|
r.rpos += copied
|
2016-12-24 01:37:35 +00:00
|
|
|
return
|
|
|
|
}
|