From 20c380ebc6b648534743e01e230a8499a6edd6fb Mon Sep 17 00:00:00 2001 From: Matt Goodall Date: Thu, 12 Jul 2012 00:37:46 +0100 Subject: [PATCH] Add UUID4 func and use it instead of hard-coded ids. --- client.go | 2 +- src/xmpp/client.go | 4 +--- src/xmpp/uuid.go | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/xmpp/uuid.go diff --git a/client.go b/client.go index 93f51c7..7daca71 100644 --- a/client.go +++ b/client.go @@ -52,7 +52,7 @@ func main() { // Get disco#info for home server. 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) reply, _ := x.SendRecv(&iq) reply.PayloadDecode(info) diff --git a/src/xmpp/client.go b/src/xmpp/client.go index b08aca7..44089dc 100644 --- a/src/xmpp/client.go +++ b/src/xmpp/client.go @@ -180,7 +180,7 @@ type saslAuth struct { func bindResource(stream *Stream, jid JID) (JID, error) { - req := Iq{Id: "foo", Type: "set"} + req := Iq{Id: UUID4(), Type: "set"} if jid.Resource == "" { req.PayloadEncode(bindIq{}) } else { @@ -245,5 +245,3 @@ type saslFailure struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"` Reason xml.Name `xml:",any"` } - -// BUG(matt): Don't use "foo" as the id during resource binding. diff --git a/src/xmpp/uuid.go b/src/xmpp/uuid.go new file mode 100644 index 0000000..aafa40c --- /dev/null +++ b/src/xmpp/uuid.go @@ -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:]) +} +