author | Chris Jones <christian.jones@sri.com> |
Thu, 19 Jan 2012 12:25:36 -0600 | |
changeset 74 | e619e18dcec3 |
parent 72 | 53f15893a1a7 |
child 75 | 03a923eb5c01 |
permissions | -rw-r--r-- |
10 | 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style |
|
3 |
// license that can be found in the LICENSE file. |
|
4 |
||
5 |
// This file contains the three layers of processing for the |
|
6 |
// communication with the server: transport (where TLS happens), XML |
|
7 |
// (where strings are converted to go structures), and Stream (where |
|
17 | 8 |
// we respond to XMPP events on behalf of the library client), or send |
9 |
// those events to the client. |
|
10 | 10 |
|
11 |
package xmpp |
|
12 |
||
13 |
import ( |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
14 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"crypto/rand" |
10 | 16 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
17 |
"encoding/base64" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
18 |
"encoding/xml" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
19 |
"fmt" |
10 | 20 |
"io" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
21 |
"log/syslog" |
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
22 |
"math/big" |
10 | 23 |
"net" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
24 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
25 |
"strings" |
10 | 26 |
"time" |
27 |
) |
|
28 |
||
17 | 29 |
// Callback to handle a stanza with a particular id. |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
30 |
type stanzaHandler struct { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
31 |
id string |
17 | 32 |
// Return true means pass this to the application |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
33 |
f func(Stanza) bool |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
34 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
35 |
|
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
36 |
// BUG(cjyar) Review all these *Client receiver methods. They should |
17 | 37 |
// probably either all be receivers, or none. |
38 |
||
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
39 |
func (cl *Client) readTransport(w io.WriteCloser) { |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
40 |
defer w.Close() |
10 | 41 |
cl.socket.SetReadTimeout(1e8) |
42 |
p := make([]byte, 1024) |
|
43 |
for { |
|
44 |
if cl.socket == nil { |
|
45 |
cl.waitForSocket() |
|
46 |
} |
|
47 |
nr, err := cl.socket.Read(p) |
|
48 |
if nr == 0 { |
|
72 | 49 |
if errno, ok := err.(*net.OpError); ok { |
10 | 50 |
if errno.Timeout() { |
51 |
continue |
|
52 |
} |
|
53 |
} |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
54 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
55 |
Log.Err("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
56 |
} |
10 | 57 |
break |
58 |
} |
|
59 |
nw, err := w.Write(p[:nr]) |
|
60 |
if nw < nr { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
61 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
62 |
Log.Err("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
63 |
} |
10 | 64 |
break |
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
func (cl *Client) writeTransport(r io.Reader) { |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
70 |
defer cl.socket.Close() |
10 | 71 |
p := make([]byte, 1024) |
72 |
for { |
|
73 |
nr, err := r.Read(p) |
|
74 |
if nr == 0 { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
75 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
76 |
Log.Err("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
77 |
} |
10 | 78 |
break |
79 |
} |
|
80 |
nw, err := cl.socket.Write(p[:nr]) |
|
81 |
if nw < nr { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
82 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
83 |
Log.Err("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
84 |
} |
10 | 85 |
break |
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
||
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
90 |
func readXml(r io.Reader, ch chan<- interface{}, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
91 |
extStanza map[string]func(*xml.Name) interface{}) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
92 |
if Loglevel >= syslog.LOG_DEBUG { |
10 | 93 |
pr, pw := io.Pipe() |
94 |
go tee(r, pw, "S: ") |
|
95 |
r = pr |
|
96 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
97 |
defer close(ch) |
10 | 98 |
|
99 |
p := xml.NewParser(r) |
|
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
100 |
Loop: |
10 | 101 |
for { |
102 |
// Sniff the next token on the stream. |
|
103 |
t, err := p.Token() |
|
104 |
if t == nil { |
|
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
105 |
if err != io.EOF { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
106 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
107 |
Log.Err("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
108 |
} |
10 | 109 |
} |
110 |
break |
|
111 |
} |
|
112 |
var se xml.StartElement |
|
113 |
var ok bool |
|
72 | 114 |
if se, ok = t.(xml.StartElement); !ok { |
10 | 115 |
continue |
116 |
} |
|
117 |
||
118 |
// Allocate the appropriate structure for this token. |
|
119 |
var obj interface{} |
|
120 |
switch se.Name.Space + " " + se.Name.Local { |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
121 |
case NsStream + " stream": |
10 | 122 |
st, err := parseStream(se) |
123 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
124 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
125 |
Log.Err("unmarshal stream: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
126 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
127 |
} |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
128 |
break Loop |
10 | 129 |
} |
130 |
ch <- st |
|
131 |
continue |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
132 |
case "stream error", NsStream + " error": |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
133 |
obj = &streamError{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
134 |
case NsStream + " features": |
10 | 135 |
obj = &Features{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
136 |
case NsTLS + " proceed", NsTLS + " failure": |
10 | 137 |
obj = &starttls{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
138 |
case NsSASL + " challenge", NsSASL + " failure", |
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
139 |
NsSASL + " success": |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
140 |
obj = &auth{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
141 |
case "jabber:client iq": |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
142 |
obj = &Iq{} |
16
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
143 |
case "jabber:client message": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
144 |
obj = &Message{} |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
145 |
case "jabber:client presence": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
146 |
obj = &Presence{} |
10 | 147 |
default: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
148 |
obj = &Generic{} |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
149 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
150 |
Log.Notice("Ignoring unrecognized: " + |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
151 |
se.Name.Space + " " + se.Name.Local) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
152 |
} |
10 | 153 |
} |
154 |
||
155 |
// Read the complete XML stanza. |
|
156 |
err = p.Unmarshal(obj, &se) |
|
157 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
158 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
159 |
Log.Err("unmarshal: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
160 |
} |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
161 |
break Loop |
10 | 162 |
} |
163 |
||
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
164 |
// If it's a Stanza, we try to unmarshal its innerxml |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
165 |
// into objects of the appropriate respective |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
166 |
// types. This is specified by our extensions. |
72 | 167 |
if st, ok := obj.(Stanza); ok { |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
168 |
err = parseExtended(st, extStanza) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
169 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
170 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
171 |
Log.Err("ext unmarshal: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
172 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
173 |
} |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
174 |
break Loop |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
175 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
176 |
} |
17 | 177 |
|
10 | 178 |
// Put it on the channel. |
179 |
ch <- obj |
|
180 |
} |
|
181 |
} |
|
182 |
||
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
183 |
func parseExtended(st Stanza, extStanza map[string]func(*xml.Name) interface{}) error { |
38 | 184 |
// Now parse the stanza's innerxml to find the string that we |
185 |
// can unmarshal this nested element from. |
|
186 |
reader := strings.NewReader(st.innerxml()) |
|
187 |
p := xml.NewParser(reader) |
|
188 |
for { |
|
189 |
t, err := p.Token() |
|
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
190 |
if err == io.EOF { |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
191 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
192 |
} |
38 | 193 |
if err != nil { |
194 |
return err |
|
195 |
} |
|
72 | 196 |
if se, ok := t.(xml.StartElement); ok { |
197 |
if con, ok := extStanza[se.Name.Space]; ok { |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
198 |
// Call the indicated constructor. |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
199 |
nested := con(&se.Name) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
200 |
|
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
201 |
// Unmarshal the nested element and |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
202 |
// stuff it back into the stanza. |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
203 |
err := p.Unmarshal(nested, &se) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
204 |
if err != nil { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
205 |
return err |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
206 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
207 |
st.addNested(nested) |
38 | 208 |
} |
209 |
} |
|
210 |
} |
|
211 |
||
212 |
return nil |
|
213 |
} |
|
214 |
||
10 | 215 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
216 |
if Loglevel >= syslog.LOG_DEBUG { |
10 | 217 |
pr, pw := io.Pipe() |
218 |
go tee(pr, w, "C: ") |
|
219 |
w = pw |
|
220 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
221 |
defer func(w io.Writer) { |
72 | 222 |
if c, ok := w.(io.Closer); ok { |
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
223 |
c.Close() |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
224 |
} |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
225 |
}(w) |
10 | 226 |
|
227 |
for obj := range ch { |
|
228 |
err := xml.Marshal(w, obj) |
|
229 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
230 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
231 |
Log.Err("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
232 |
} |
10 | 233 |
break |
234 |
} |
|
235 |
} |
|
236 |
} |
|
237 |
||
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
238 |
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- Stanza) { |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
239 |
defer close(cliOut) |
10 | 240 |
|
72 | 241 |
handlers := make(map[string]func(Stanza) bool) |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
242 |
Loop: |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
243 |
for { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
244 |
select { |
72 | 245 |
case h := <-cl.handlers: |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
246 |
handlers[h.id] = h.f |
72 | 247 |
case x, ok := <-srvIn: |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
248 |
if !ok { |
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
249 |
break Loop |
26 | 250 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
251 |
send := false |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
252 |
switch obj := x.(type) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
253 |
case *stream: |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
254 |
handleStream(obj) |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
255 |
case *streamError: |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
256 |
cl.handleStreamError(obj) |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
257 |
case *Features: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
258 |
cl.handleFeatures(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
259 |
case *starttls: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
260 |
cl.handleTls(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
261 |
case *auth: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
262 |
cl.handleSasl(obj) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
263 |
default: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
264 |
send = true |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
265 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
266 |
if !send { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
267 |
continue |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
268 |
} |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
269 |
st, ok := x.(Stanza) |
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
270 |
if !ok { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
271 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
272 |
Log.Warning(fmt.Sprintf( |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
273 |
"Unhandled non-stanza: %v", x)) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
274 |
} |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
275 |
continue |
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
276 |
} |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
277 |
if handlers[st.GetId()] != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
278 |
f := handlers[st.GetId()] |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
279 |
handlers[st.GetId()] = nil |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
280 |
send = f(st) |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
281 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
282 |
if send { |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
283 |
cliOut <- st |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
284 |
} |
10 | 285 |
} |
286 |
} |
|
287 |
} |
|
288 |
||
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
289 |
// This loop is paused until resource binding is complete. Otherwise |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
290 |
// the app might inject something inappropriate into our negotiations |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
291 |
// with the server. The control channel controls this loop's |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
292 |
// activity. |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
293 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
294 |
control <-chan int) { |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
295 |
defer close(srvOut) |
10 | 296 |
|
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
297 |
var input <-chan Stanza |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
298 |
Loop: |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
299 |
for { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
300 |
select { |
72 | 301 |
case status := <-control: |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
302 |
switch status { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
303 |
case 0: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
304 |
input = nil |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
305 |
case 1: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
306 |
input = cliIn |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
307 |
case -1: |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
308 |
break Loop |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
309 |
} |
72 | 310 |
case x, ok := <-input: |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
311 |
if !ok { |
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
312 |
break Loop |
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
313 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
314 |
if x == nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
315 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
316 |
Log.Notice("Refusing to send" + |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
317 |
" nil stanza") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
318 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
319 |
continue |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
320 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
321 |
srvOut <- x |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
322 |
} |
10 | 323 |
} |
324 |
} |
|
325 |
||
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
326 |
// Stanzas from the remote go up through a stack of filters to the |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
327 |
// app. This function manages the filters. |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
328 |
func filterTop(filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
329 |
topFilter <-chan Stanza, app chan<- Stanza) { |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
330 |
defer close(app) |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
331 |
Loop: |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
332 |
for { |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
333 |
select { |
72 | 334 |
case newFilterOut := <-filterOut: |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
335 |
if newFilterOut == nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
336 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
337 |
Log.Warning("Received nil filter") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
338 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
339 |
filterIn <- nil |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
340 |
continue |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
341 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
342 |
filterIn <- topFilter |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
51
diff
changeset
|
343 |
topFilter = newFilterOut |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
344 |
|
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
345 |
case data, ok := <-topFilter: |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
346 |
if !ok { |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
347 |
break Loop |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
348 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
349 |
app <- data |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
350 |
} |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
351 |
} |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
352 |
} |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
353 |
|
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
354 |
func filterBottom(from <-chan Stanza, to chan<- Stanza) { |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
355 |
defer close(to) |
72 | 356 |
for data := range from { |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
357 |
to <- data |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
358 |
} |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
359 |
} |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
360 |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
361 |
func handleStream(ss *stream) { |
10 | 362 |
} |
363 |
||
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
364 |
func (cl *Client) handleStreamError(se *streamError) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
365 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
366 |
Log.Notice(fmt.Sprintf("Received stream error: %v", se)) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
367 |
} |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
62
diff
changeset
|
368 |
close(cl.Out) |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
369 |
} |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
370 |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
371 |
func (cl *Client) handleFeatures(fe *Features) { |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
372 |
cl.Features = fe |
10 | 373 |
if fe.Starttls != nil { |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
374 |
start := &starttls{XMLName: xml.Name{Space: NsTLS, |
10 | 375 |
Local: "starttls"}} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
376 |
cl.xmlOut <- start |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
377 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
378 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
379 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
380 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
381 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
382 |
return |
10 | 383 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
384 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
385 |
if fe.Bind != nil { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
386 |
cl.bind(fe.Bind) |
17 | 387 |
return |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
388 |
} |
10 | 389 |
} |
390 |
||
391 |
// readTransport() is running concurrently. We need to stop it, |
|
392 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
393 |
// its inner loop; see below. |
|
394 |
func (cl *Client) handleTls(t *starttls) { |
|
395 |
tcp := cl.socket |
|
396 |
||
397 |
// Set the socket to nil, and wait for the reader routine to |
|
398 |
// signal that it's paused. |
|
399 |
cl.socket = nil |
|
400 |
cl.socketSync.Add(1) |
|
401 |
cl.socketSync.Wait() |
|
402 |
||
403 |
// Negotiate TLS with the server. |
|
404 |
tls := tls.Client(tcp, nil) |
|
405 |
||
406 |
// Make the TLS connection available to the reader, and wait |
|
407 |
// for it to signal that it's working again. |
|
408 |
cl.socketSync.Add(1) |
|
409 |
cl.socket = tls |
|
410 |
cl.socketSync.Wait() |
|
411 |
||
66
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
412 |
// Reset the read timeout on the (underlying) socket so the |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
413 |
// reader doesn't get woken up unnecessarily. |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
414 |
tcp.SetReadTimeout(0) |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
415 |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
416 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
417 |
Log.Info("TLS negotiation succeeded.") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
418 |
} |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
419 |
cl.Features = nil |
10 | 420 |
|
421 |
// Now re-send the initial handshake message to start the new |
|
422 |
// session. |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
423 |
hsOut := &stream{To: cl.Jid.Domain, Version: Version} |
10 | 424 |
cl.xmlOut <- hsOut |
425 |
} |
|
426 |
||
427 |
// Synchronize with handleTls(). Called from readTransport() when |
|
428 |
// cl.socket is nil. |
|
429 |
func (cl *Client) waitForSocket() { |
|
430 |
// Signal that we've stopped reading from the socket. |
|
431 |
cl.socketSync.Done() |
|
432 |
||
433 |
// Wait until the socket is available again. |
|
434 |
for cl.socket == nil { |
|
435 |
time.Sleep(1e8) |
|
436 |
} |
|
437 |
||
438 |
// Signal that we're going back to the read loop. |
|
439 |
cl.socketSync.Done() |
|
440 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
441 |
|
59 | 442 |
// BUG(cjyar): Doesn't implement TLS/SASL EXTERNAL. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
443 |
func (cl *Client) chooseSasl(fe *Features) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
444 |
var digestMd5 bool |
72 | 445 |
for _, m := range fe.Mechanisms.Mechanism { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
446 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
447 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
448 |
digestMd5 = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
449 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
450 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
451 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
452 |
if digestMd5 { |
72 | 453 |
auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, Mechanism: "DIGEST-MD5"} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
454 |
cl.xmlOut <- auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
455 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
456 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
457 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
458 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
459 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
460 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
461 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
462 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
463 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
464 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
465 |
Log.Err("SASL challenge decode: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
466 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
467 |
} |
72 | 468 |
return |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
469 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
470 |
srvMap := parseSasl(string(str)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
471 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
472 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
473 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
474 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
475 |
cl.saslDigest2(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
476 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
477 |
case "failure": |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
478 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
479 |
Log.Notice("SASL authentication failed") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
480 |
} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
481 |
case "success": |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
482 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
483 |
Log.Info("Sasl authentication succeeded") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
484 |
} |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
485 |
cl.Features = nil |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
486 |
ss := &stream{To: cl.Jid.Domain, Version: Version} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
487 |
cl.xmlOut <- ss |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
488 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
489 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
490 |
|
72 | 491 |
func (cl *Client) saslDigest1(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
492 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
493 |
var hasAuth bool |
72 | 494 |
for _, qop := range strings.Fields(srvMap["qop"]) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
495 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
496 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
497 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
498 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
499 |
if !hasAuth { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
500 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
501 |
Log.Err("Server doesn't support SASL auth") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
502 |
} |
72 | 503 |
return |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
504 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
505 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
506 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
507 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
508 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
509 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
510 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
511 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
512 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
513 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
514 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
515 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
516 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
517 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
518 |
// Begin building the response. Username is |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
519 |
// user@domain or just domain. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
520 |
var username string |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
23
diff
changeset
|
521 |
if cl.Jid.Node == "" { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
522 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
523 |
} else { |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
23
diff
changeset
|
524 |
username = cl.Jid.Node |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
525 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
526 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
527 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
528 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
529 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
530 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
531 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
532 |
if Log != nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
533 |
Log.Err("SASL rand: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
534 |
} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
535 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
536 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
537 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
538 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
539 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
540 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
541 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
542 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
543 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
544 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
545 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
546 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
547 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
548 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
549 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
550 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
551 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
552 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
72 | 553 |
clMap["nc"] = nonceCountStr |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
554 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
555 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
556 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
557 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
558 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
559 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
560 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
561 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
562 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
563 |
b64 := base64.StdEncoding |
72 | 564 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, Chardata: b64.EncodeToString([]byte(clStr))} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
565 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
566 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
567 |
|
72 | 568 |
func (cl *Client) saslDigest2(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
569 |
if cl.saslExpected == srvMap["rspauth"] { |
72 | 570 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
571 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
572 |
} else { |
72 | 573 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "failure"}, Any: &Generic{XMLName: xml.Name{Space: NsSASL, |
574 |
Local: "abort"}}} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
575 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
576 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
577 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
578 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
579 |
// Takes a string like `key1=value1,key2="value2"...` and returns a |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
580 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
581 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
582 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
583 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
584 |
m := make(map[string]string) |
72 | 585 |
for _, pair := range strs { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
586 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
587 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
588 |
m[key] = value |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
589 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
590 |
return m |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
591 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
592 |
|
17 | 593 |
// Inverse of parseSasl(). |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
594 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
595 |
var terms []string |
72 | 596 |
for key, value := range m { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
597 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
598 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
599 |
} |
72 | 600 |
terms = append(terms, key+"="+value) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
601 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
602 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
603 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
604 |
|
17 | 605 |
// Computes the response string for digest authentication. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
606 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
607 |
authenticate, digestUri, nonceCountStr string) string { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
608 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
609 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
610 |
h.Write([]byte(text)) |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
611 |
return h.Sum(nil) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
612 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
613 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
614 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
615 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
616 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
617 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
618 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
619 |
|
72 | 620 |
a1 := string(h(username+":"+realm+":"+passwd)) + ":" + |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
621 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
622 |
a2 := authenticate + ":" + digestUri |
72 | 623 |
response := hex(kd(hex(h(a1)), nonce+":"+ |
624 |
nonceCountStr+":"+cnonceStr+":auth:"+ |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
625 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
626 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
627 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
628 |
|
17 | 629 |
// Send a request to bind a resource. RFC 3920, section 7. |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
630 |
func (cl *Client) bind(bindAdv *bindIq) { |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
631 |
res := cl.Jid.Resource |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
632 |
bindReq := &bindIq{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
633 |
if res != "" { |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
634 |
bindReq.Resource = &res |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
635 |
} |
72 | 636 |
msg := &Iq{Type: "set", Id: <-Id, Nested: []interface{}{bindReq}} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
637 |
f := func(st Stanza) bool { |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
638 |
if st.GetType() == "error" { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
639 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
640 |
Log.Err("Resource binding failed") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
641 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
642 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
643 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
644 |
var bindRepl *bindIq |
72 | 645 |
for _, ele := range st.GetNested() { |
646 |
if b, ok := ele.(*bindIq); ok { |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
647 |
bindRepl = b |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
648 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
649 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
650 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
651 |
if bindRepl == nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
652 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
653 |
Log.Err(fmt.Sprintf("Bad bind reply: %v", |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
654 |
st)) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
655 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
656 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
657 |
} |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
658 |
jidStr := bindRepl.Jid |
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
659 |
if jidStr == nil || *jidStr == "" { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
660 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
661 |
Log.Err("Can't bind empty resource") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
662 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
663 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
664 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
665 |
jid := new(JID) |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
666 |
if !jid.Set(*jidStr) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
667 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
668 |
Log.Err("Can't parse JID " + *jidStr) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
669 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
670 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
671 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
672 |
cl.Jid = *jid |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
673 |
if Log != nil { |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
674 |
Log.Info("Bound resource: " + cl.Jid.String()) |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
675 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
676 |
cl.bindDone() |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
677 |
return false |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
678 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
679 |
cl.HandleStanza(msg.Id, f) |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
680 |
cl.xmlOut <- msg |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
681 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
682 |
|
17 | 683 |
// Register a callback to handle the next XMPP stanza (iq, message, or |
684 |
// presence) with a given id. The provided function will not be called |
|
685 |
// more than once. If it returns false, the stanza will not be made |
|
33
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
32
diff
changeset
|
686 |
// available on the normal Client.In channel. The stanza handler |
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
32
diff
changeset
|
687 |
// must not read from that channel, as deliveries on it cannot proceed |
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
32
diff
changeset
|
688 |
// until the handler returns true or false. |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
689 |
func (cl *Client) HandleStanza(id string, f func(Stanza) bool) { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
690 |
h := &stanzaHandler{id: id, f: f} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
691 |
cl.handlers <- h |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
692 |
} |