author | Chris Jones <christian.jones@sri.com> |
Sun, 22 Sep 2013 17:43:34 -0500 | |
changeset 152 | 69c5b4382e39 |
parent 148 | b1b4900eee5b |
child 153 | bbd4166df95d |
permissions | -rw-r--r-- |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
1 |
// The lowest level of XMPP protocol, where TLS is applied after the |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
2 |
// initial handshake. |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
3 |
|
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
4 |
package xmpp |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
5 |
|
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
6 |
import ( |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
7 |
"crypto/tls" |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
8 |
"io" |
145 | 9 |
"net" |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
10 |
"time" |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
11 |
) |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
12 |
|
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
13 |
var l1interval = time.Second |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
14 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
15 |
type layer1 struct { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
16 |
sock net.Conn |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
17 |
recvSocks chan<- net.Conn |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
18 |
sendSocks chan net.Conn |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
19 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
20 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
21 |
func startLayer1(sock net.Conn, recvWriter io.WriteCloser, |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
22 |
sendReader io.ReadCloser) *layer1 { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
23 |
l1 := layer1{sock: sock} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
24 |
recvSocks := make(chan net.Conn) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
25 |
l1.recvSocks = recvSocks |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
26 |
sendSocks := make(chan net.Conn, 1) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
27 |
l1.sendSocks = sendSocks |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
28 |
go recvTransport(recvSocks, recvWriter) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
29 |
go sendTransport(sendSocks, sendReader) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
30 |
recvSocks <- sock |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
31 |
sendSocks <- sock |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
32 |
return &l1 |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
33 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
34 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
35 |
func (l1 *layer1) startTls(conf *tls.Config) { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
36 |
sendSockToSender := func(sock net.Conn) { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
37 |
for { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
38 |
select { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
39 |
case <-l1.sendSocks: |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
40 |
case l1.sendSocks <- sock: |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
41 |
return |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
42 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
43 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
44 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
45 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
46 |
sendSockToSender(nil) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
47 |
l1.recvSocks <- nil |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
48 |
l1.sock = tls.Client(l1.sock, conf) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
49 |
sendSockToSender(l1.sock) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
50 |
l1.recvSocks <- l1.sock |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
51 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
52 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
53 |
func recvTransport(socks <-chan net.Conn, w io.WriteCloser) { |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
54 |
defer w.Close() |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
55 |
var sock net.Conn |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
56 |
p := make([]byte, 1024) |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
57 |
for { |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
58 |
select { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
59 |
case sock = <-socks: |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
60 |
default: |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
61 |
} |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
62 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
63 |
if sock == nil { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
64 |
time.Sleep(l1interval) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
65 |
} else { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
66 |
sock.SetReadDeadline(time.Now().Add(l1interval)) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
67 |
nr, err := sock.Read(p) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
68 |
if nr == 0 { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
69 |
if errno, ok := err.(*net.OpError); ok { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
70 |
if errno.Timeout() { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
71 |
continue |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
72 |
} |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
73 |
} |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
74 |
Warn.Logf("recvTransport: %s", err) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
75 |
break |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
76 |
} |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
77 |
nw, err := w.Write(p[:nr]) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
78 |
if nw < nr { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
79 |
Warn.Logf("recvTransport: %s", err) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
80 |
break |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
81 |
} |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
82 |
} |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
83 |
} |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
84 |
} |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
85 |
|
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
86 |
func sendTransport(socks <-chan net.Conn, r io.Reader) { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
87 |
var sock net.Conn |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
88 |
p := make([]byte, 1024) |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
89 |
for { |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
90 |
nr, err := r.Read(p) |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
91 |
if nr == 0 { |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
92 |
Warn.Logf("sendTransport: %s", err) |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
93 |
break |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
94 |
} |
148
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
95 |
for nr > 0 { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
96 |
select { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
97 |
case sock = <-socks: |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
98 |
if sock != nil { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
99 |
defer sock.Close() |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
100 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
101 |
default: |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
102 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
103 |
|
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
104 |
if sock == nil { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
105 |
time.Sleep(l1interval) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
106 |
} else { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
107 |
nw, err := sock.Write(p[:nr]) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
108 |
nr -= nw |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
109 |
if nr != 0 { |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
110 |
Warn.Logf("write: %s", err) |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
111 |
break |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
112 |
} |
b1b4900eee5b
Made layers 1 and 3 more modular, shrinking the surface area of the coupling between them.
Chris Jones <christian.jones@sri.com>
parents:
147
diff
changeset
|
113 |
} |
143
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
114 |
} |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
115 |
} |
62166e57800e
Split stream.go into layer1, layer2, layer3, and sasl.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
116 |
} |