remove semis after statements in one-statement statement lists

R=rsc, r
http://go/go-review/1025029
This commit is contained in:
Robert Griesemer 2009-11-09 12:07:39 -08:00
parent 372b6eb4c5
commit 395b73f134
12 changed files with 80 additions and 80 deletions

View File

@ -92,12 +92,12 @@ type encryptor interface {
func mutualVersion(theirMajor, theirMinor uint8) (major, minor uint8, ok bool) { func mutualVersion(theirMajor, theirMinor uint8) (major, minor uint8, ok bool) {
// We don't deal with peers < TLS 1.0 (aka version 3.1). // We don't deal with peers < TLS 1.0 (aka version 3.1).
if theirMajor < 3 || theirMajor == 3 && theirMinor < 1 { if theirMajor < 3 || theirMajor == 3 && theirMinor < 1 {
return 0, 0, false; return 0, 0, false
} }
major = 3; major = 3;
minor = 2; minor = 2;
if theirMinor < minor { if theirMinor < minor {
minor = theirMinor; minor = theirMinor
} }
ok = true; ok = true;
return; return;

View File

@ -19,7 +19,7 @@ type clientHelloMsg struct {
func (m *clientHelloMsg) marshal() []byte { func (m *clientHelloMsg) marshal() []byte {
if m.raw != nil { if m.raw != nil {
return m.raw; return m.raw
} }
length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods); length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods);
@ -50,7 +50,7 @@ func (m *clientHelloMsg) marshal() []byte {
func (m *clientHelloMsg) unmarshal(data []byte) bool { func (m *clientHelloMsg) unmarshal(data []byte) bool {
if len(data) < 39 { if len(data) < 39 {
return false; return false
} }
m.raw = data; m.raw = data;
m.major = data[4]; m.major = data[4];
@ -58,31 +58,31 @@ func (m *clientHelloMsg) unmarshal(data []byte) bool {
m.random = data[6:38]; m.random = data[6:38];
sessionIdLen := int(data[38]); sessionIdLen := int(data[38]);
if sessionIdLen > 32 || len(data) < 39 + sessionIdLen { if sessionIdLen > 32 || len(data) < 39 + sessionIdLen {
return false; return false
} }
m.sessionId = data[39 : 39 + sessionIdLen]; m.sessionId = data[39 : 39 + sessionIdLen];
data = data[39 + sessionIdLen : len(data)]; data = data[39 + sessionIdLen : len(data)];
if len(data) < 2 { if len(data) < 2 {
return false; return false
} }
// cipherSuiteLen is the number of bytes of cipher suite numbers. Since // cipherSuiteLen is the number of bytes of cipher suite numbers. Since
// they are uint16s, the number must be even. // they are uint16s, the number must be even.
cipherSuiteLen := int(data[0])<<8 | int(data[1]); cipherSuiteLen := int(data[0])<<8 | int(data[1]);
if cipherSuiteLen % 2 == 1 || len(data) < 2 + cipherSuiteLen { if cipherSuiteLen % 2 == 1 || len(data) < 2 + cipherSuiteLen {
return false; return false
} }
numCipherSuites := cipherSuiteLen / 2; numCipherSuites := cipherSuiteLen / 2;
m.cipherSuites = make([]uint16, numCipherSuites); m.cipherSuites = make([]uint16, numCipherSuites);
for i := 0; i < numCipherSuites; i++ { for i := 0; i < numCipherSuites; i++ {
m.cipherSuites[i] = uint16(data[2 + 2*i])<<8 | uint16(data[3 + 2*i]); m.cipherSuites[i] = uint16(data[2 + 2*i])<<8 | uint16(data[3 + 2*i])
} }
data = data[2 + cipherSuiteLen : len(data)]; data = data[2 + cipherSuiteLen : len(data)];
if len(data) < 2 { if len(data) < 2 {
return false; return false
} }
compressionMethodsLen := int(data[0]); compressionMethodsLen := int(data[0]);
if len(data) < 1 + compressionMethodsLen { if len(data) < 1 + compressionMethodsLen {
return false; return false
} }
m.compressionMethods = data[1 : 1 + compressionMethodsLen]; m.compressionMethods = data[1 : 1 + compressionMethodsLen];
@ -101,7 +101,7 @@ type serverHelloMsg struct {
func (m *serverHelloMsg) marshal() []byte { func (m *serverHelloMsg) marshal() []byte {
if m.raw != nil { if m.raw != nil {
return m.raw; return m.raw
} }
length := 38+len(m.sessionId); length := 38+len(m.sessionId);
@ -131,12 +131,12 @@ type certificateMsg struct {
func (m *certificateMsg) marshal() (x []byte) { func (m *certificateMsg) marshal() (x []byte) {
if m.raw != nil { if m.raw != nil {
return m.raw; return m.raw
} }
var i int; var i int;
for _, slice := range m.certificates { for _, slice := range m.certificates {
i += len(slice); i += len(slice)
} }
length := 3 + 3*len(m.certificates) + i; length := 3 + 3*len(m.certificates) + i;
@ -179,7 +179,7 @@ type clientKeyExchangeMsg struct {
func (m *clientKeyExchangeMsg) marshal() []byte { func (m *clientKeyExchangeMsg) marshal() []byte {
if m.raw != nil { if m.raw != nil {
return m.raw; return m.raw
} }
length := len(m.ciphertext)+2; length := len(m.ciphertext)+2;
x := make([]byte, length+4); x := make([]byte, length+4);
@ -198,11 +198,11 @@ func (m *clientKeyExchangeMsg) marshal() []byte {
func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool { func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {
m.raw = data; m.raw = data;
if len(data) < 7 { if len(data) < 7 {
return false; return false
} }
cipherTextLen := int(data[4])<<8 | int(data[5]); cipherTextLen := int(data[4])<<8 | int(data[5]);
if len(data) != 6 + cipherTextLen { if len(data) != 6 + cipherTextLen {
return false; return false
} }
m.ciphertext = data[6:len(data)]; m.ciphertext = data[6:len(data)];
return true; return true;
@ -215,7 +215,7 @@ type finishedMsg struct {
func (m *finishedMsg) marshal() (x []byte) { func (m *finishedMsg) marshal() (x []byte) {
if m.raw != nil { if m.raw != nil {
return m.raw; return m.raw
} }
x = make([]byte, 16); x = make([]byte, 16);
@ -229,7 +229,7 @@ func (m *finishedMsg) marshal() (x []byte) {
func (m *finishedMsg) unmarshal(data []byte) bool { func (m *finishedMsg) unmarshal(data []byte) bool {
m.raw = data; m.raw = data;
if len(data) != 4+12 { if len(data) != 4+12 {
return false; return false
} }
m.verifyData = data[4:len(data)]; m.verifyData = data[4:len(data)];
return true; return true;

View File

@ -62,7 +62,7 @@ func TestMarshalUnmarshal(t *testing.T) {
func randomBytes(n int, rand *rand.Rand) []byte { func randomBytes(n int, rand *rand.Rand) []byte {
r := make([]byte, n); r := make([]byte, n);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
r[i] = byte(rand.Int31()); r[i] = byte(rand.Int31())
} }
return r; return r;
} }
@ -75,7 +75,7 @@ func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
m.sessionId = randomBytes(rand.Intn(32), rand); m.sessionId = randomBytes(rand.Intn(32), rand);
m.cipherSuites = make([]uint16, rand.Intn(63) + 1); m.cipherSuites = make([]uint16, rand.Intn(63) + 1);
for i := 0; i < len(m.cipherSuites); i++ { for i := 0; i < len(m.cipherSuites); i++ {
m.cipherSuites[i] = uint16(rand.Int31()); m.cipherSuites[i] = uint16(rand.Int31())
} }
m.compressionMethods = randomBytes(rand.Intn(63) + 1, rand); m.compressionMethods = randomBytes(rand.Intn(63) + 1, rand);

View File

@ -17,7 +17,7 @@ type zeroSource struct{}
func (zeroSource) Read(b []byte) (n int, err os.Error) { func (zeroSource) Read(b []byte) (n int, err os.Error) {
for i := range b { for i := range b {
b[i] = 0; b[i] = 0
} }
return len(b), nil; return len(b), nil;
@ -56,12 +56,12 @@ func testClientHelloFailure(t *testing.T, clientHello interface{}, expectedAlert
err := script.Perform(0, []*script.Event{send, recvAlert, close1, recvState, close2}); err := script.Perform(0, []*script.Event{send, recvAlert, close1, recvState, close2});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
func TestSimpleError(t *testing.T) { func TestSimpleError(t *testing.T) {
testClientHelloFailure(t, &serverHelloDoneMsg{}, alertUnexpectedMessage); testClientHelloFailure(t, &serverHelloDoneMsg{}, alertUnexpectedMessage)
} }
var badProtocolVersions = []uint8{0, 0, 0, 5, 1, 0, 1, 5, 2, 0, 2, 5, 3, 0} var badProtocolVersions = []uint8{0, 0, 0, 5, 1, 0, 1, 5, 2, 0, 2, 5, 3, 0}
@ -91,7 +91,7 @@ func TestNoCompressionOverlap(t *testing.T) {
func matchServerHello(v interface{}) bool { func matchServerHello(v interface{}) bool {
serverHello, ok := v.(*serverHelloMsg); serverHello, ok := v.(*serverHelloMsg);
if !ok { if !ok {
return false; return false
} }
return serverHello.major == 3 && return serverHello.major == 3 &&
serverHello.minor == 2 && serverHello.minor == 2 &&
@ -111,7 +111,7 @@ func TestAlertForwarding(t *testing.T) {
err := script.Perform(0, []*script.Event{sendAlert, recvAlert, closeWriter, closeControl}); err := script.Perform(0, []*script.Event{sendAlert, recvAlert, closeWriter, closeControl});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -124,14 +124,14 @@ func TestClose(t *testing.T) {
err := script.Perform(0, []*script.Event{close, closed1, closed2}); err := script.Perform(0, []*script.Event{close, closed1, closed2});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
func matchCertificate(v interface{}) bool { func matchCertificate(v interface{}) bool {
cert, ok := v.(*certificateMsg); cert, ok := v.(*certificateMsg);
if !ok { if !ok {
return false; return false
} }
return len(cert.certificates) == 1 && return len(cert.certificates) == 1 &&
bytes.Compare(cert.certificates[0], testCertificate) == 0; bytes.Compare(cert.certificates[0], testCertificate) == 0;
@ -150,7 +150,7 @@ func matchDone(v interface{}) bool {
func matchFinished(v interface{}) bool { func matchFinished(v interface{}) bool {
finished, ok := v.(*finishedMsg); finished, ok := v.(*finishedMsg);
if !ok { if !ok {
return false; return false
} }
return bytes.Compare(finished.verifyData, fromHex("29122ae11453e631487b02ed")) == 0; return bytes.Compare(finished.verifyData, fromHex("29122ae11453e631487b02ed")) == 0;
} }
@ -187,7 +187,7 @@ func TestFullHandshake(t *testing.T) {
err := script.Perform(0, []*script.Event{sendHello, setVersion, recvHello, recvCert, recvDone, sendCKX, sendCCS, recvNCS, sendFinished, setCipher, recvConnectionState, recvFinished}); err := script.Perform(0, []*script.Event{sendHello, setVersion, recvHello, recvCert, recvDone, sendCKX, sendCCS, recvNCS, sendFinished, setCipher, recvConnectionState, recvFinished});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }

6
prf.go
View File

@ -35,7 +35,7 @@ func pHash(result, secret, seed []byte, hash hash.Hash) {
b := h.Sum(); b := h.Sum();
todo := len(b); todo := len(b);
if j+todo > len(result) { if j+todo > len(result) {
todo = len(result)-j; todo = len(result)-j
} }
bytes.Copy(result[j : j+todo], b); bytes.Copy(result[j : j+todo], b);
j += todo; j += todo;
@ -61,7 +61,7 @@ func pRF11(result, secret, label, seed []byte) {
pHash(result2, s2, labelAndSeed, hashSHA1); pHash(result2, s2, labelAndSeed, hashSHA1);
for i, b := range result2 { for i, b := range result2 {
result[i] ^= b; result[i] ^= b
} }
} }
@ -109,7 +109,7 @@ type finishedHash struct {
} }
func newFinishedHash() finishedHash { func newFinishedHash() finishedHash {
return finishedHash{md5.New(), sha1.New(), md5.New(), sha1.New()}; return finishedHash{md5.New(), sha1.New(), md5.New(), sha1.New()}
} }
func (h finishedHash) Write(msg []byte) (n int, err os.Error) { func (h finishedHash) Write(msg []byte) (n int, err os.Error) {

View File

@ -28,7 +28,7 @@ func TestSplitPreMasterSecret(t *testing.T) {
s1 := hex.EncodeToString(out1); s1 := hex.EncodeToString(out1);
s2 := hex.EncodeToString(out2); s2 := hex.EncodeToString(out2);
if s1 != test.out1 || s2 != test.out2 { if s1 != test.out1 || s2 != test.out2 {
t.Errorf("#%d: got: (%s, %s) want: (%s, %s)", i, s1, s2, test.out1, test.out2); t.Errorf("#%d: got: (%s, %s) want: (%s, %s)", i, s1, s2, test.out1, test.out2)
} }
} }
} }
@ -58,7 +58,7 @@ func TestKeysFromPreMasterSecret(t *testing.T) {
serverMACString != test.serverMAC || serverMACString != test.serverMAC ||
clientKeyString != test.clientKey || clientKeyString != test.clientKey ||
serverKeyString != test.serverKey { serverKeyString != test.serverKey {
t.Errorf("#%d: got: (%s, %s, %s, %s, %s) want: (%s, %s, %s, %s %s)", i, masterString, clientMACString, serverMACString, clientKeyString, serverMACString, test.masterSecret, test.clientMAC, test.serverMAC, test.clientKey, test.serverKey); t.Errorf("#%d: got: (%s, %s, %s, %s, %s) want: (%s, %s, %s, %s %s)", i, masterString, clientMACString, serverMACString, clientKeyString, serverMACString, test.masterSecret, test.clientMAC, test.serverMAC, test.clientKey, test.serverKey)
} }
} }
} }

View File

@ -73,13 +73,13 @@ type recordProcessor struct {
func drainRequestChannel(requestChan <-chan interface{}, c ConnectionState) { func drainRequestChannel(requestChan <-chan interface{}, c ConnectionState) {
for v := range requestChan { for v := range requestChan {
if closed(requestChan) { if closed(requestChan) {
return; return
} }
switch r := v.(type) { switch r := v.(type) {
case getConnectionState: case getConnectionState:
r.reply <- c; r.reply <- c
case waitConnectionState: case waitConnectionState:
r.reply <- c; r.reply <- c
} }
} }
} }
@ -104,11 +104,11 @@ func (p *recordProcessor) loop(appDataChan chan<- []byte, requestChan <-chan int
p.appDataSend = nil; p.appDataSend = nil;
p.recordRead = p.recordChan; p.recordRead = p.recordChan;
case c := <-controlChan: case c := <-controlChan:
p.processControlMsg(c); p.processControlMsg(c)
case r := <-requestChan: case r := <-requestChan:
p.processRequestMsg(r); p.processRequestMsg(r)
case r := <-p.recordRead: case r := <-p.recordRead:
p.processRecord(r); p.processRecord(r)
} }
} }
@ -121,7 +121,7 @@ func (p *recordProcessor) loop(appDataChan chan<- []byte, requestChan <-chan int
close(handshakeChan); close(handshakeChan);
if len(p.appData) > 0 { if len(p.appData) > 0 {
appDataChan <- p.appData; appDataChan <- p.appData
} }
close(appDataChan); close(appDataChan);
} }
@ -134,10 +134,10 @@ func (p *recordProcessor) processRequestMsg(requestMsg interface{}) {
switch r := requestMsg.(type) { switch r := requestMsg.(type) {
case getConnectionState: case getConnectionState:
r.reply <- p.connState; r.reply <- p.connState
case waitConnectionState: case waitConnectionState:
if p.connState.HandshakeComplete { if p.connState.HandshakeComplete {
r.reply <- p.connState; r.reply <- p.connState
} }
p.waitQueue.PushBack(r.reply); p.waitQueue.PushBack(r.reply);
} }
@ -156,7 +156,7 @@ func (p *recordProcessor) processControlMsg(msg interface{}) {
func (p *recordProcessor) wakeWaiters() { func (p *recordProcessor) wakeWaiters() {
for i := p.waitQueue.Front(); i != nil; i = i.Next() { for i := p.waitQueue.Front(); i != nil; i = i.Next() {
i.Value.(chan<- ConnectionState) <- p.connState; i.Value.(chan<- ConnectionState) <- p.connState
} }
p.waitQueue.Init(); p.waitQueue.Init();
} }
@ -188,7 +188,7 @@ func (p *recordProcessor) processRecord(r *record) {
switch r.contentType { switch r.contentType {
case recordTypeHandshake: case recordTypeHandshake:
p.processHandshakeRecord(r.payload[0 : len(r.payload) - p.mac.Size()]); p.processHandshakeRecord(r.payload[0 : len(r.payload) - p.mac.Size()])
case recordTypeChangeCipherSpec: case recordTypeChangeCipherSpec:
if len(r.payload) != 1 || r.payload[0] != 1 { if len(r.payload) != 1 || r.payload[0] != 1 {
p.error(alertUnexpectedMessage); p.error(alertUnexpectedMessage);
@ -221,7 +221,7 @@ func (p *recordProcessor) processRecord(r *record) {
func (p *recordProcessor) processHandshakeRecord(data []byte) { func (p *recordProcessor) processHandshakeRecord(data []byte) {
if p.handshakeBuf == nil { if p.handshakeBuf == nil {
p.handshakeBuf = data; p.handshakeBuf = data
} else { } else {
if len(p.handshakeBuf) > maxHandshakeMsg { if len(p.handshakeBuf) > maxHandshakeMsg {
p.error(alertInternalError); p.error(alertInternalError);
@ -238,7 +238,7 @@ func (p *recordProcessor) processHandshakeRecord(data []byte) {
int(p.handshakeBuf[2])<<8 | int(p.handshakeBuf[2])<<8 |
int(p.handshakeBuf[3]); int(p.handshakeBuf[3]);
if handshakeLen + 4 > len(p.handshakeBuf) { if handshakeLen + 4 > len(p.handshakeBuf) {
break; break
} }
bytes := p.handshakeBuf[0 : handshakeLen + 4]; bytes := p.handshakeBuf[0 : handshakeLen + 4];
@ -249,7 +249,7 @@ func (p *recordProcessor) processHandshakeRecord(data []byte) {
// forwarding application data. // forwarding application data.
m := new(finishedMsg); m := new(finishedMsg);
if !m.unmarshal(bytes) { if !m.unmarshal(bytes) {
p.error(alertUnexpectedMessage); p.error(alertUnexpectedMessage)
} }
p.handshakeChan <- m; p.handshakeChan <- m;
var ok bool; var ok bool;
@ -283,11 +283,11 @@ func parseHandshakeMsg(data []byte) (interface{}, bool) {
switch data[0] { switch data[0] {
case typeClientHello: case typeClientHello:
m = new(clientHelloMsg); m = new(clientHelloMsg)
case typeClientKeyExchange: case typeClientKeyExchange:
m = new(clientKeyExchangeMsg); m = new(clientKeyExchangeMsg)
default: default:
return nil, false; return nil, false
} }
ok := m.unmarshal(data); ok := m.unmarshal(data);

View File

@ -40,7 +40,7 @@ func TestNullConnectionState(t *testing.T) {
err := script.Perform(0, []*script.Event{sendReq, getReply}); err := script.Perform(0, []*script.Event{sendReq, getReply});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -61,7 +61,7 @@ func TestWaitConnectionState(t *testing.T) {
err := script.Perform(0, []*script.Event{sendReq, sendReq2, getReply2, sendState, getReply}); err := script.Perform(0, []*script.Event{sendReq, sendReq2, getReply2, sendState, getReply});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -79,7 +79,7 @@ func TestHandshakeAssembly(t *testing.T) {
err := script.Perform(0, []*script.Event{send1, send2, send3, recvMsg}); err := script.Perform(0, []*script.Event{send1, send2, send3, recvMsg});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -95,7 +95,7 @@ func TestEarlyApplicationData(t *testing.T) {
err := script.Perform(0, []*script.Event{send, recv}); err := script.Perform(0, []*script.Event{send, recv});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -114,7 +114,7 @@ func TestApplicationData(t *testing.T) {
err := script.Perform(0, []*script.Event{send1, recv1, send2, send3, recv2}); err := script.Perform(0, []*script.Event{send1, recv1, send2, send3, recv2});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }
@ -132,6 +132,6 @@ func TestInvalidChangeCipherSpec(t *testing.T) {
err := script.Perform(0, []*script.Event{send1, recv1, send2, close, close2}); err := script.Perform(0, []*script.Event{send1, recv1, send2, close, close2});
if err != nil { if err != nil {
t.Errorf("Got error: %s", err); t.Errorf("Got error: %s", err)
} }
} }

View File

@ -23,18 +23,18 @@ func recordReader(c chan<- *record, source io.Reader) {
var header [5]byte; var header [5]byte;
n, _ := buf.Read(header[0:len(header)]); n, _ := buf.Read(header[0:len(header)]);
if n != 5 { if n != 5 {
return; return
} }
recordLength := int(header[3])<<8 | int(header[4]); recordLength := int(header[3])<<8 | int(header[4]);
if recordLength > maxTLSCiphertext { if recordLength > maxTLSCiphertext {
return; return
} }
payload := make([]byte, recordLength); payload := make([]byte, recordLength);
n, _ = buf.Read(payload); n, _ = buf.Read(payload);
if n != recordLength { if n != recordLength {
return; return
} }
c <- &record{recordType(header[0]), header[1], header[2], payload}; c <- &record{recordType(header[0]), header[1], header[2], payload};

View File

@ -12,10 +12,10 @@ import (
func matchRecord(r1, r2 *record) bool { func matchRecord(r1, r2 *record) bool {
if (r1 == nil) != (r2 == nil) { if (r1 == nil) != (r2 == nil) {
return false; return false
} }
if r1 == nil { if r1 == nil {
return true; return true
} }
return r1.contentType == r2.contentType && return r1.contentType == r2.contentType &&
r1.major == r2.major && r1.major == r2.major &&
@ -63,11 +63,11 @@ func matchRecordReaderOutput(t *testing.T, i int, test recordReaderTest, c <-cha
break; break;
} }
if !matchRecord(r1, r2) { if !matchRecord(r1, r2) {
t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1); t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1)
} }
} }
<-c; <-c;
if !closed(c) { if !closed(c) {
t.Errorf("#%d: channel didn't close", i); t.Errorf("#%d: channel didn't close", i)
} }
} }

View File

@ -53,7 +53,7 @@ func (w *recordWriter) loop(writer io.Writer, appChan <-chan []byte, controlChan
for !w.shutdown { for !w.shutdown {
msg := <-controlChan; msg := <-controlChan;
if _, ok := msg.(writerEnableApplicationData); ok { if _, ok := msg.(writerEnableApplicationData); ok {
break; break
} }
w.processControlMessage(msg); w.processControlMessage(msg);
} }
@ -67,9 +67,9 @@ func (w *recordWriter) loop(writer io.Writer, appChan <-chan []byte, controlChan
select { select {
case controlMsg := <-controlChan: case controlMsg := <-controlChan:
w.processControlMessage(controlMsg); w.processControlMessage(controlMsg)
case appMsg := <-appChan: case appMsg := <-appChan:
w.processAppMessage(appMsg); w.processAppMessage(appMsg)
} }
} }
@ -77,13 +77,13 @@ func (w *recordWriter) loop(writer io.Writer, appChan <-chan []byte, controlChan
go func() { go func() {
for _ = range appChan { for _ = range appChan {
} }
}(); }()
} }
if !closed(controlChan) { if !closed(controlChan) {
go func() { go func() {
for _ = range controlChan { for _ = range controlChan {
} }
}(); }()
} }
} }
@ -142,12 +142,12 @@ func (w *recordWriter) processControlMessage(controlMsg interface{}) {
w.major = msg.major; w.major = msg.major;
w.minor = msg.minor; w.minor = msg.minor;
case alert: case alert:
w.writeRecord(&record{recordTypeAlert, w.major, w.minor, []byte{byte(msg.level), byte(msg.error)}}); w.writeRecord(&record{recordTypeAlert, w.major, w.minor, []byte{byte(msg.level), byte(msg.error)}})
case handshakeMessage: case handshakeMessage:
// TODO(agl): marshal may return a slice too large for a single record. // TODO(agl): marshal may return a slice too large for a single record.
w.writeRecord(&record{recordTypeHandshake, w.major, w.minor, msg.marshal()}); w.writeRecord(&record{recordTypeHandshake, w.major, w.minor, msg.marshal()})
default: default:
fmt.Printf("processControlMessage: unknown %#v\n", msg); fmt.Printf("processControlMessage: unknown %#v\n", msg)
} }
} }
@ -162,7 +162,7 @@ func (w *recordWriter) processAppMessage(appMsg []byte) {
for done < len(appMsg) { for done < len(appMsg) {
todo := len(appMsg); todo := len(appMsg);
if todo > maxTLSPlaintext { if todo > maxTLSPlaintext {
todo = maxTLSPlaintext; todo = maxTLSPlaintext
} }
w.writeRecord(&record{recordTypeApplicationData, w.major, w.minor, appMsg[done : done+todo]}); w.writeRecord(&record{recordTypeApplicationData, w.major, w.minor, appMsg[done : done+todo]});
done += todo; done += todo;

14
tls.go
View File

@ -32,7 +32,7 @@ func timeout(c chan<- bool, nsecs int64) {
func (tls *Conn) Read(p []byte) (int, os.Error) { func (tls *Conn) Read(p []byte) (int, os.Error) {
if len(tls.readBuf) == 0 { if len(tls.readBuf) == 0 {
if tls.eof { if tls.eof {
return 0, os.EOF; return 0, os.EOF
} }
var timeoutChan chan bool; var timeoutChan chan bool;
@ -43,15 +43,15 @@ func (tls *Conn) Read(p []byte) (int, os.Error) {
select { select {
case b := <-tls.readChan: case b := <-tls.readChan:
tls.readBuf = b; tls.readBuf = b
case <-timeoutChan: case <-timeoutChan:
return 0, os.EAGAIN; return 0, os.EAGAIN
} }
// TLS distinguishes between orderly closes and truncations. An // TLS distinguishes between orderly closes and truncations. An
// orderly close is represented by a zero length slice. // orderly close is represented by a zero length slice.
if closed(tls.readChan) { if closed(tls.readChan) {
return 0, io.ErrUnexpectedEOF; return 0, io.ErrUnexpectedEOF
} }
if len(tls.readBuf) == 0 { if len(tls.readBuf) == 0 {
tls.eof = true; tls.eof = true;
@ -66,7 +66,7 @@ func (tls *Conn) Read(p []byte) (int, os.Error) {
func (tls *Conn) Write(p []byte) (int, os.Error) { func (tls *Conn) Write(p []byte) (int, os.Error) {
if tls.eof || closed(tls.readChan) { if tls.eof || closed(tls.readChan) {
return 0, os.EOF; return 0, os.EOF
} }
var timeoutChan chan bool; var timeoutChan chan bool;
@ -78,7 +78,7 @@ func (tls *Conn) Write(p []byte) (int, os.Error) {
select { select {
case tls.writeChan <- p: case tls.writeChan <- p:
case <-timeoutChan: case <-timeoutChan:
return 0, os.EAGAIN; return 0, os.EAGAIN
} }
return len(p), nil; return len(p), nil;
@ -148,7 +148,7 @@ type Listener struct {
func (l Listener) Accept() (c net.Conn, err os.Error) { func (l Listener) Accept() (c net.Conn, err os.Error) {
c, err = l.listener.Accept(); c, err = l.listener.Accept();
if err != nil { if err != nil {
return; return
} }
c = Server(c, l.config); c = Server(c, l.config);