1
0
Fork 0

Add UUID4 func and use it instead of hard-coded <iq/> ids.

This commit is contained in:
Matt Goodall 2012-07-12 00:37:46 +01:00
parent 909d0f5fac
commit 20c380ebc6
3 changed files with 20 additions and 4 deletions

View File

@ -52,7 +52,7 @@ func main() {
// Get disco#info for home server. // Get disco#info for home server.
info := &DiscoInfo{} info := &DiscoInfo{}
iq := xmpp.Iq{Id: "abc", Type: "get", To: x.JID.Domain} iq := xmpp.Iq{Id: xmpp.UUID4(), Type: "get", To: x.JID.Domain}
iq.PayloadEncode(info) iq.PayloadEncode(info)
reply, _ := x.SendRecv(&iq) reply, _ := x.SendRecv(&iq)
reply.PayloadDecode(info) reply.PayloadDecode(info)

View File

@ -180,7 +180,7 @@ type saslAuth struct {
func bindResource(stream *Stream, jid JID) (JID, error) { func bindResource(stream *Stream, jid JID) (JID, error) {
req := Iq{Id: "foo", Type: "set"} req := Iq{Id: UUID4(), Type: "set"}
if jid.Resource == "" { if jid.Resource == "" {
req.PayloadEncode(bindIq{}) req.PayloadEncode(bindIq{})
} else { } else {
@ -245,5 +245,3 @@ type saslFailure struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
Reason xml.Name `xml:",any"` Reason xml.Name `xml:",any"`
} }
// BUG(matt): Don't use "foo" as the <iq/> id during resource binding.

18
src/xmpp/uuid.go Normal file
View File

@ -0,0 +1,18 @@
package xmpp
import (
"crypto/rand"
"fmt"
)
// Generate a UUID4.
func UUID4() string {
uuid := make([]byte, 16)
if _, err := rand.Read(uuid); err != nil {
panic(err)
}
uuid[6] = (uuid[6] & 0x0F) | 0x40
uuid[8] = (uuid[8] &^ 0x40) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}