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