Skip to content

Pack multiple handshake messages into a single record #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

type pipeConn struct {
label string
closed bool
r *bytes.Buffer
w *bytes.Buffer
Expand All @@ -30,6 +31,9 @@ func pipe() (client *pipeConn, server *pipeConn) {
client = new(pipeConn)
server = new(pipeConn)

client.label = "client"
server.label = "server"

c2s := bytes.NewBuffer(nil)
server.r = c2s
client.w = c2s
Expand Down Expand Up @@ -64,6 +68,8 @@ func (p *pipeConn) Read(data []byte) (n int, err error) {
}

func (p *pipeConn) Write(data []byte) (n int, err error) {
logf(logTypePipe, "[%s] write: %d %x\n", p.label, len(data), data)

p.wLock.Lock()
defer p.wLock.Unlock()
if p.closed {
Expand Down
64 changes: 56 additions & 8 deletions handshake-layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,70 @@ func (h *HandshakeLayer) ReadMessage() (*HandshakeMessage, error) {
}

func (h *HandshakeLayer) QueueMessage(hm *HandshakeMessage) error {
if h.datagram {
hm.cipher = h.conn.(*DefaultRecordLayer).cipher
h.queued = append(h.queued, hm)
return nil
hm.cipher = h.conn.(*DefaultRecordLayer).cipher
h.queued = append(h.queued, hm)
return nil
}

func (h *HandshakeLayer) packAndWrite(hms []*HandshakeMessage) (int, error) {
buffer := []byte{}
for _, hm := range hms {
buffer = append(buffer, hm.Marshal()...)
}

record := &TLSPlaintext{
contentType: RecordTypeHandshake,
fragment: buffer,
}
cipher := hms[0].cipher
err := h.conn.(*DefaultRecordLayer).writeRecordWithPadding(record, cipher, 0)
if err != nil {
return 0, err
}
_, err := h.WriteMessages([]*HandshakeMessage{hm})
return err

return len(hms), nil
}

func (h *HandshakeLayer) packAndWriteQueue() (int, error) {
if len(h.queued) == 0 {
return 0, nil
}

buffer := []*HandshakeMessage{}
written := 0
for i, hm := range h.queued {
buffer = append(buffer, hm)

if i < len(h.queued)-1 && h.queued[i+1].cipher == hm.cipher {
continue
}

count, err := h.packAndWrite(buffer)
if err != nil {
return 0, err
}

written += count
buffer = []*HandshakeMessage{}
}

return written, nil
}

func (h *HandshakeLayer) SendQueuedMessages() (int, error) {
logf(logTypeHandshake, "Sending outgoing messages")
count, err := h.WriteMessages(h.queued)

if !h.datagram {
count, err := h.packAndWriteQueue()
if err != nil {
return count, err
}

h.ClearQueuedMessages()
return count, nil
}
return count, err

return h.WriteMessages(h.queued)
}

func (h *HandshakeLayer) ClearQueuedMessages() {
Expand Down
1 change: 1 addition & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
logTypeNegotiation = "negotiation"
logTypeIO = "io"
logTypeFrameReader = "frame"
logTypePipe = "pipe"
logTypeVerbose = "verbose"
)

Expand Down