author | Chris Jones <christian.jones@sri.com> |
Sun, 15 Sep 2013 11:34:35 -0600 | |
changeset 142 | 0ff033eed887 |
parent 140 | f94b958f8df0 |
permissions | -rw-r--r-- |
10 | 1 |
// This file contains the three layers of processing for the |
2 |
// communication with the server: transport (where TLS happens), XML |
|
3 |
// (where strings are converted to go structures), and Stream (where |
|
17 | 4 |
// we respond to XMPP events on behalf of the library client), or send |
5 |
// those events to the client. |
|
10 | 6 |
|
7 |
package xmpp |
|
8 |
||
9 |
import ( |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
10 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
11 |
"crypto/rand" |
10 | 12 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
13 |
"encoding/base64" |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
14 |
"encoding/xml" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"fmt" |
10 | 16 |
"io" |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
17 |
"math/big" |
10 | 18 |
"net" |
128
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
127
diff
changeset
|
19 |
"reflect" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
20 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
21 |
"strings" |
10 | 22 |
"time" |
23 |
) |
|
24 |
||
17 | 25 |
// 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
|
26 |
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
|
27 |
id string |
17 | 28 |
// Return true means pass this to the application |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
29 |
f func(Stanza) bool |
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 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
31 |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
32 |
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
|
33 |
defer w.Close() |
10 | 34 |
p := make([]byte, 1024) |
35 |
for { |
|
36 |
if cl.socket == nil { |
|
37 |
cl.waitForSocket() |
|
38 |
} |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
39 |
cl.socket.SetReadDeadline(time.Now().Add(time.Second)) |
10 | 40 |
nr, err := cl.socket.Read(p) |
41 |
if nr == 0 { |
|
72 | 42 |
if errno, ok := err.(*net.OpError); ok { |
10 | 43 |
if errno.Timeout() { |
44 |
continue |
|
45 |
} |
|
46 |
} |
|
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
47 |
Warn.Logf("read: %s", err) |
10 | 48 |
break |
49 |
} |
|
50 |
nw, err := w.Write(p[:nr]) |
|
51 |
if nw < nr { |
|
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
52 |
Warn.Logf("read: %s", err) |
10 | 53 |
break |
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
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
|
59 |
defer cl.socket.Close() |
10 | 60 |
p := make([]byte, 1024) |
61 |
for { |
|
62 |
nr, err := r.Read(p) |
|
63 |
if nr == 0 { |
|
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
64 |
Warn.Logf("write: %s", err) |
10 | 65 |
break |
66 |
} |
|
67 |
nw, err := cl.socket.Write(p[:nr]) |
|
68 |
if nw < nr { |
|
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
69 |
Warn.Logf("write: %s", err) |
10 | 70 |
break |
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
75 |
func readXml(r io.Reader, ch chan<- interface{}, |
128
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
127
diff
changeset
|
76 |
extStanza map[xml.Name]reflect.Type) { |
116 | 77 |
if _, ok := Debug.(*noLog); !ok { |
10 | 78 |
pr, pw := io.Pipe() |
79 |
go tee(r, pw, "S: ") |
|
80 |
r = pr |
|
81 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
82 |
defer close(ch) |
10 | 83 |
|
114
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
84 |
// This trick loads our namespaces into the parser. |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
85 |
nsstr := fmt.Sprintf(`<a xmlns="%s" xmlns:stream="%s">`, |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
86 |
NsClient, NsStream) |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
87 |
nsrdr := strings.NewReader(nsstr) |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
88 |
p := xml.NewDecoder(io.MultiReader(nsrdr, r)) |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
89 |
p.Token() |
a058e33c1666
Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents:
113
diff
changeset
|
90 |
|
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
|
91 |
Loop: |
10 | 92 |
for { |
93 |
// Sniff the next token on the stream. |
|
94 |
t, err := p.Token() |
|
95 |
if t == nil { |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
96 |
if err != io.EOF { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
97 |
Warn.Logf("read: %s", err) |
10 | 98 |
} |
99 |
break |
|
100 |
} |
|
101 |
var se xml.StartElement |
|
102 |
var ok bool |
|
72 | 103 |
if se, ok = t.(xml.StartElement); !ok { |
10 | 104 |
continue |
105 |
} |
|
106 |
||
107 |
// Allocate the appropriate structure for this token. |
|
108 |
var obj interface{} |
|
109 |
switch se.Name.Space + " " + se.Name.Local { |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
110 |
case NsStream + " stream": |
10 | 111 |
st, err := parseStream(se) |
112 |
if err != nil { |
|
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
113 |
Warn.Logf("unmarshal stream: %s", err) |
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
|
114 |
break Loop |
10 | 115 |
} |
116 |
ch <- st |
|
117 |
continue |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
118 |
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
|
119 |
obj = &streamError{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
120 |
case NsStream + " features": |
10 | 121 |
obj = &Features{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
122 |
case NsTLS + " proceed", NsTLS + " failure": |
10 | 123 |
obj = &starttls{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
124 |
case NsSASL + " challenge", NsSASL + " failure", |
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
125 |
NsSASL + " success": |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
126 |
obj = &auth{} |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
127 |
case NsClient + " iq": |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
128 |
obj = &Iq{} |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
129 |
case NsClient + " message": |
16
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
130 |
obj = &Message{} |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
131 |
case NsClient + " presence": |
16
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
132 |
obj = &Presence{} |
10 | 133 |
default: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
134 |
obj = &Generic{} |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
135 |
Info.Logf("Ignoring unrecognized: %s %s", se.Name.Space, |
100 | 136 |
se.Name.Local) |
10 | 137 |
} |
138 |
||
139 |
// Read the complete XML stanza. |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
140 |
err = p.DecodeElement(obj, &se) |
10 | 141 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
142 |
Warn.Logf("unmarshal: %s", err) |
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
|
143 |
break Loop |
10 | 144 |
} |
145 |
||
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
146 |
// 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
|
147 |
// into objects of the appropriate respective |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
148 |
// types. This is specified by our extensions. |
116 | 149 |
if st, ok := obj.(Stanza); ok { |
111
36287f2cf06e
Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
110
diff
changeset
|
150 |
err = parseExtended(st.GetHeader(), extStanza) |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
151 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
152 |
Warn.Logf("ext unmarshal: %s", err) |
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
|
153 |
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
|
154 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
155 |
} |
17 | 156 |
|
10 | 157 |
// Put it on the channel. |
158 |
ch <- obj |
|
159 |
} |
|
160 |
} |
|
161 |
||
128
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
127
diff
changeset
|
162 |
func parseExtended(st *Header, extStanza map[xml.Name]reflect.Type) error { |
38 | 163 |
// Now parse the stanza's innerxml to find the string that we |
164 |
// can unmarshal this nested element from. |
|
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
105
diff
changeset
|
165 |
reader := strings.NewReader(st.Innerxml) |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
166 |
p := xml.NewDecoder(reader) |
38 | 167 |
for { |
168 |
t, err := p.Token() |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
169 |
if err == io.EOF { |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
170 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
171 |
} |
38 | 172 |
if err != nil { |
173 |
return err |
|
174 |
} |
|
72 | 175 |
if se, ok := t.(xml.StartElement); ok { |
128
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
127
diff
changeset
|
176 |
if typ, ok := extStanza[se.Name]; ok { |
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
127
diff
changeset
|
177 |
nested := reflect.New(typ).Interface() |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
178 |
|
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
179 |
// Unmarshal the nested element and |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
180 |
// stuff it back into the stanza. |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
181 |
err := p.DecodeElement(nested, &se) |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
182 |
if err != nil { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
183 |
return err |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
184 |
} |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
105
diff
changeset
|
185 |
st.Nested = append(st.Nested, nested) |
38 | 186 |
} |
187 |
} |
|
188 |
} |
|
189 |
||
190 |
return nil |
|
191 |
} |
|
192 |
||
10 | 193 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
116 | 194 |
if _, ok := Debug.(*noLog); !ok { |
10 | 195 |
pr, pw := io.Pipe() |
196 |
go tee(pr, w, "C: ") |
|
197 |
w = pw |
|
198 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
199 |
defer func(w io.Writer) { |
72 | 200 |
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
|
201 |
c.Close() |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
202 |
} |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
203 |
}(w) |
10 | 204 |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
205 |
enc := xml.NewEncoder(w) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
206 |
|
10 | 207 |
for obj := range ch { |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
208 |
if st, ok := obj.(*stream); ok { |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
209 |
_, err := w.Write([]byte(st.String())) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
210 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
211 |
Warn.Logf("write: %s", err) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
212 |
} |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
213 |
} else { |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
214 |
err := enc.Encode(obj) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
215 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
216 |
Warn.Logf("marshal: %s", err) |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
217 |
break |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
218 |
} |
10 | 219 |
} |
220 |
} |
|
221 |
} |
|
222 |
||
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
223 |
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
|
224 |
defer close(cliOut) |
10 | 225 |
|
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
226 |
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
|
227 |
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
|
228 |
for { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
229 |
select { |
72 | 230 |
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
|
231 |
handlers[h.id] = h.f |
72 | 232 |
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
|
233 |
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
|
234 |
break Loop |
26 | 235 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
236 |
switch obj := x.(type) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
case *streamError: |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
cl.handleSasl(obj) |
112
bd56fb741f69
Step 2 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
111
diff
changeset
|
247 |
case Stanza: |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
248 |
send := true |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
249 |
id := obj.GetHeader().Id |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
250 |
if handlers[id] != nil { |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
251 |
f := handlers[id] |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
252 |
delete(handlers, id) |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
253 |
send = f(obj) |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
254 |
} |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
255 |
if send { |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
256 |
cliOut <- obj |
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
257 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
258 |
default: |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
105
diff
changeset
|
259 |
Warn.Logf("Unhandled non-stanza: %T %#v", x, x) |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
260 |
} |
10 | 261 |
} |
262 |
} |
|
263 |
} |
|
264 |
||
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
265 |
// 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
|
266 |
// 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
|
267 |
// 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
|
268 |
// activity. |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
269 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza, |
116 | 270 |
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
|
271 |
defer close(srvOut) |
10 | 272 |
|
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
273 |
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
|
274 |
Loop: |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
275 |
for { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
276 |
select { |
72 | 277 |
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
|
278 |
switch status { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
279 |
case 0: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
280 |
input = nil |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
281 |
case 1: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
282 |
input = cliIn |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
283 |
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
|
284 |
break Loop |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
285 |
} |
72 | 286 |
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
|
287 |
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
|
288 |
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
|
289 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
290 |
if x == nil { |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
291 |
Info.Log("Refusing to send nil stanza") |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
292 |
continue |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
293 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
294 |
srvOut <- x |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
295 |
} |
10 | 296 |
} |
297 |
} |
|
298 |
||
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
299 |
func handleStream(ss *stream) { |
10 | 300 |
} |
301 |
||
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
302 |
func (cl *Client) handleStreamError(se *streamError) { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
303 |
Info.Logf("Received stream error: %v", se) |
140
f94b958f8df0
Don't close the wrong end of a channel. But if we get an error, we can close the underlying socket.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
304 |
cl.socket.Close() |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
305 |
} |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
306 |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
307 |
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
|
308 |
cl.Features = fe |
10 | 309 |
if fe.Starttls != nil { |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
310 |
start := &starttls{XMLName: xml.Name{Space: NsTLS, |
10 | 311 |
Local: "starttls"}} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
312 |
cl.sendXml <- start |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
313 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
314 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
315 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
316 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
317 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
318 |
return |
10 | 319 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
320 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
321 |
if fe.Bind != nil { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
322 |
cl.bind(fe.Bind) |
17 | 323 |
return |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
324 |
} |
10 | 325 |
} |
326 |
||
327 |
// readTransport() is running concurrently. We need to stop it, |
|
328 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
329 |
// its inner loop; see below. |
|
330 |
func (cl *Client) handleTls(t *starttls) { |
|
331 |
tcp := cl.socket |
|
332 |
||
333 |
// Set the socket to nil, and wait for the reader routine to |
|
334 |
// signal that it's paused. |
|
335 |
cl.socket = nil |
|
336 |
cl.socketSync.Add(1) |
|
337 |
cl.socketSync.Wait() |
|
338 |
||
339 |
// Negotiate TLS with the server. |
|
130
da6f37ae3ffe
Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents:
129
diff
changeset
|
340 |
tls := tls.Client(tcp, &cl.tlsConfig) |
10 | 341 |
|
342 |
// Make the TLS connection available to the reader, and wait |
|
343 |
// for it to signal that it's working again. |
|
344 |
cl.socketSync.Add(1) |
|
345 |
cl.socket = tls |
|
346 |
cl.socketSync.Wait() |
|
347 |
||
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
348 |
Info.Log("TLS negotiation succeeded.") |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
349 |
cl.Features = nil |
10 | 350 |
|
351 |
// Now re-send the initial handshake message to start the new |
|
352 |
// session. |
|
118
fb9bb98a8d70
Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
116
diff
changeset
|
353 |
hsOut := &stream{To: cl.Jid.Domain, Version: XMPPVersion} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
354 |
cl.sendXml <- hsOut |
10 | 355 |
} |
356 |
||
357 |
// Synchronize with handleTls(). Called from readTransport() when |
|
358 |
// cl.socket is nil. |
|
359 |
func (cl *Client) waitForSocket() { |
|
360 |
// Signal that we've stopped reading from the socket. |
|
361 |
cl.socketSync.Done() |
|
362 |
||
363 |
// Wait until the socket is available again. |
|
364 |
for cl.socket == nil { |
|
365 |
time.Sleep(1e8) |
|
366 |
} |
|
367 |
||
368 |
// Signal that we're going back to the read loop. |
|
369 |
cl.socketSync.Done() |
|
370 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
371 |
|
59 | 372 |
// BUG(cjyar): Doesn't implement TLS/SASL EXTERNAL. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
373 |
func (cl *Client) chooseSasl(fe *Features) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
374 |
var digestMd5 bool |
72 | 375 |
for _, m := range fe.Mechanisms.Mechanism { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
376 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
377 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
378 |
digestMd5 = true |
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 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
381 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
382 |
if digestMd5 { |
72 | 383 |
auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, Mechanism: "DIGEST-MD5"} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
384 |
cl.sendXml <- auth |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
385 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
386 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
387 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
388 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
389 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
390 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
391 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
392 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
393 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
394 |
Warn.Logf("SASL challenge decode: %s", err) |
72 | 395 |
return |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
396 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
397 |
srvMap := parseSasl(string(str)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
398 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
399 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
400 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
401 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
402 |
cl.saslDigest2(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
403 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
404 |
case "failure": |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
405 |
Info.Log("SASL authentication failed") |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
406 |
case "success": |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
407 |
Info.Log("Sasl authentication succeeded") |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
408 |
cl.Features = nil |
118
fb9bb98a8d70
Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
116
diff
changeset
|
409 |
ss := &stream{To: cl.Jid.Domain, Version: XMPPVersion} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
410 |
cl.sendXml <- ss |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
411 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
412 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
413 |
|
72 | 414 |
func (cl *Client) saslDigest1(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
415 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
416 |
var hasAuth bool |
72 | 417 |
for _, qop := range strings.Fields(srvMap["qop"]) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
418 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
419 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
420 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
421 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
422 |
if !hasAuth { |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
423 |
Warn.Log("Server doesn't support SASL auth") |
72 | 424 |
return |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
425 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
426 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
427 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
428 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
429 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
430 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
431 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
432 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
433 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
434 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
435 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
436 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
437 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
438 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
439 |
// Begin building the response. Username is |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
440 |
// user@domain or just domain. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
441 |
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
|
442 |
if cl.Jid.Node == "" { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
443 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
444 |
} else { |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
23
diff
changeset
|
445 |
username = cl.Jid.Node |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
446 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
447 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
448 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
449 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
450 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
451 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
452 |
if err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
453 |
Warn.Logf("SASL rand: %s", err) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
454 |
return |
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 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
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 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
459 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
460 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
461 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
462 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
463 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
464 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
465 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
466 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
467 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
468 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
469 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
470 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
471 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
72 | 472 |
clMap["nc"] = nonceCountStr |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
473 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
474 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
475 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
476 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
477 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
478 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
479 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
480 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
481 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
482 |
b64 := base64.StdEncoding |
72 | 483 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, Chardata: b64.EncodeToString([]byte(clStr))} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
484 |
cl.sendXml <- clObj |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
485 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
486 |
|
72 | 487 |
func (cl *Client) saslDigest2(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
488 |
if cl.saslExpected == srvMap["rspauth"] { |
72 | 489 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}} |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
490 |
cl.sendXml <- clObj |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
491 |
} else { |
72 | 492 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "failure"}, Any: &Generic{XMLName: xml.Name{Space: NsSASL, |
493 |
Local: "abort"}}} |
|
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
494 |
cl.sendXml <- clObj |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
495 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
496 |
} |
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 |
// 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
|
499 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
500 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
501 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
502 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
503 |
m := make(map[string]string) |
72 | 504 |
for _, pair := range strs { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
505 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
506 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
507 |
m[key] = value |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
508 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
509 |
return m |
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 |
|
17 | 512 |
// Inverse of parseSasl(). |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
513 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
514 |
var terms []string |
72 | 515 |
for key, value := range m { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
516 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
517 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
518 |
} |
72 | 519 |
terms = append(terms, key+"="+value) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
520 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
521 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
522 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
523 |
|
17 | 524 |
// Computes the response string for digest authentication. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
525 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
116 | 526 |
authenticate, digestUri, nonceCountStr string) string { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
527 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
528 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
529 |
h.Write([]byte(text)) |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
530 |
return h.Sum(nil) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
531 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
532 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
533 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
534 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
535 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
536 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
537 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
538 |
|
72 | 539 |
a1 := string(h(username+":"+realm+":"+passwd)) + ":" + |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
540 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
541 |
a2 := authenticate + ":" + digestUri |
72 | 542 |
response := hex(kd(hex(h(a1)), nonce+":"+ |
543 |
nonceCountStr+":"+cnonceStr+":auth:"+ |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
544 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
545 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
546 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
547 |
|
17 | 548 |
// 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
|
549 |
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
|
550 |
res := cl.Jid.Resource |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
551 |
bindReq := &bindIq{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
552 |
if res != "" { |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
553 |
bindReq.Resource = &res |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
554 |
} |
118
fb9bb98a8d70
Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
116
diff
changeset
|
555 |
msg := &Iq{Header: Header{Type: "set", Id: NextId(), |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
105
diff
changeset
|
556 |
Nested: []interface{}{bindReq}}} |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
557 |
f := func(st Stanza) bool { |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
558 |
iq, ok := st.(*Iq) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
559 |
if !ok { |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
560 |
Warn.Log("non-iq response") |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
561 |
} |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
562 |
if iq.Type == "error" { |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
563 |
Warn.Log("Resource binding failed") |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
564 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
565 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
566 |
var bindRepl *bindIq |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
567 |
for _, ele := range iq.Nested { |
72 | 568 |
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
|
569 |
bindRepl = b |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
570 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
571 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
572 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
573 |
if bindRepl == nil { |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
105
diff
changeset
|
574 |
Warn.Logf("Bad bind reply: %#v", iq) |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
575 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
576 |
} |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
577 |
jidStr := bindRepl.Jid |
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
578 |
if jidStr == nil || *jidStr == "" { |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
104
diff
changeset
|
579 |
Warn.Log("Can't bind empty resource") |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
580 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
581 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
582 |
jid := new(JID) |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
583 |
if err := jid.Set(*jidStr); err != nil { |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
584 |
Warn.Logf("Can't parse JID %s: %s", *jidStr, err) |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
585 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
586 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
587 |
cl.Jid = *jid |
102
872e936f9f3f
Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
588 |
Info.Logf("Bound resource: %s", cl.Jid.String()) |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
589 |
cl.bindDone() |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
590 |
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
|
591 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
592 |
cl.HandleStanza(msg.Id, f) |
127
a8f9a0c07fc8
Renamed client.In and client.Out to Recv and Send, respectively.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
593 |
cl.sendXml <- msg |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
594 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
595 |
|
17 | 596 |
// Register a callback to handle the next XMPP stanza (iq, message, or |
597 |
// presence) with a given id. The provided function will not be called |
|
598 |
// 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
|
599 |
// 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
|
600 |
// 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
|
601 |
// until the handler returns true or false. |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
112
diff
changeset
|
602 |
func (cl *Client) HandleStanza(id string, f func(Stanza) bool) { |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
603 |
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
|
604 |
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
|
605 |
} |