stream.go
changeset 105 aa895dfae3f6
parent 104 99e03b33b20d
child 110 7696e6a01709
equal deleted inserted replaced
104:99e03b33b20d 105:aa895dfae3f6
   297 		case x, ok := <-input:
   297 		case x, ok := <-input:
   298 			if !ok {
   298 			if !ok {
   299 				break Loop
   299 				break Loop
   300 			}
   300 			}
   301 			if x == nil {
   301 			if x == nil {
   302 				Info.Logf("Refusing to send nil stanza")
   302 				Info.Log("Refusing to send nil stanza")
   303 				continue
   303 				continue
   304 			}
   304 			}
   305 			srvOut <- x
   305 			srvOut <- x
   306 		}
   306 		}
   307 	}
   307 	}
   315 Loop:
   315 Loop:
   316 	for {
   316 	for {
   317 		select {
   317 		select {
   318 		case newFilterOut := <-filterOut:
   318 		case newFilterOut := <-filterOut:
   319 			if newFilterOut == nil {
   319 			if newFilterOut == nil {
   320 				Warn.Logf("Received nil filter")
   320 				Warn.Log("Received nil filter")
   321 				filterIn <- nil
   321 				filterIn <- nil
   322 				continue
   322 				continue
   323 			}
   323 			}
   324 			filterIn <- topFilter
   324 			filterIn <- topFilter
   325 			topFilter = newFilterOut
   325 			topFilter = newFilterOut
   379 	cl.socket = nil
   379 	cl.socket = nil
   380 	cl.socketSync.Add(1)
   380 	cl.socketSync.Add(1)
   381 	cl.socketSync.Wait()
   381 	cl.socketSync.Wait()
   382 
   382 
   383 	// Negotiate TLS with the server.
   383 	// Negotiate TLS with the server.
   384 	tls := tls.Client(tcp, nil)
   384 	tls := tls.Client(tcp, &TlsConfig)
   385 
   385 
   386 	// Make the TLS connection available to the reader, and wait
   386 	// Make the TLS connection available to the reader, and wait
   387 	// for it to signal that it's working again.
   387 	// for it to signal that it's working again.
   388 	cl.socketSync.Add(1)
   388 	cl.socketSync.Add(1)
   389 	cl.socket = tls
   389 	cl.socket = tls
   390 	cl.socketSync.Wait()
   390 	cl.socketSync.Wait()
   391 
   391 
   392 	Info.Logf("TLS negotiation succeeded.")
   392 	Info.Log("TLS negotiation succeeded.")
   393 	cl.Features = nil
   393 	cl.Features = nil
   394 
   394 
   395 	// Now re-send the initial handshake message to start the new
   395 	// Now re-send the initial handshake message to start the new
   396 	// session.
   396 	// session.
   397 	hsOut := &stream{To: cl.Jid.Domain, Version: Version}
   397 	hsOut := &stream{To: cl.Jid.Domain, Version: Version}
   444 			cl.saslDigest1(srvMap)
   444 			cl.saslDigest1(srvMap)
   445 		} else {
   445 		} else {
   446 			cl.saslDigest2(srvMap)
   446 			cl.saslDigest2(srvMap)
   447 		}
   447 		}
   448 	case "failure":
   448 	case "failure":
   449 		Info.Logf("SASL authentication failed")
   449 		Info.Log("SASL authentication failed")
   450 	case "success":
   450 	case "success":
   451 		Info.Logf("Sasl authentication succeeded")
   451 		Info.Log("Sasl authentication succeeded")
   452 		cl.Features = nil
   452 		cl.Features = nil
   453 		ss := &stream{To: cl.Jid.Domain, Version: Version}
   453 		ss := &stream{To: cl.Jid.Domain, Version: Version}
   454 		cl.xmlOut <- ss
   454 		cl.xmlOut <- ss
   455 	}
   455 	}
   456 }
   456 }
   462 		if qop == "auth" {
   462 		if qop == "auth" {
   463 			hasAuth = true
   463 			hasAuth = true
   464 		}
   464 		}
   465 	}
   465 	}
   466 	if !hasAuth {
   466 	if !hasAuth {
   467 		Warn.Logf("Server doesn't support SASL auth")
   467 		Warn.Log("Server doesn't support SASL auth")
   468 		return
   468 		return
   469 	}
   469 	}
   470 
   470 
   471 	// Pick a realm.
   471 	// Pick a realm.
   472 	var realm string
   472 	var realm string
   598 	}
   598 	}
   599 	msg := &Iq{Type: "set", Id: <-Id, Nested: []interface{}{bindReq}}
   599 	msg := &Iq{Type: "set", Id: <-Id, Nested: []interface{}{bindReq}}
   600 	f := func(st Stanza) bool {
   600 	f := func(st Stanza) bool {
   601 		iq, ok := st.(*Iq)
   601 		iq, ok := st.(*Iq)
   602 		if !ok {
   602 		if !ok {
   603 			Warn.Logf("non-iq response")
   603 			Warn.Log("non-iq response")
   604 		}
   604 		}
   605 		if iq.Type == "error" {
   605 		if iq.Type == "error" {
   606 			Warn.Logf("Resource binding failed")
   606 			Warn.Log("Resource binding failed")
   607 			return false
   607 			return false
   608 		}
   608 		}
   609 		var bindRepl *bindIq
   609 		var bindRepl *bindIq
   610 		for _, ele := range iq.Nested {
   610 		for _, ele := range iq.Nested {
   611 			if b, ok := ele.(*bindIq); ok {
   611 			if b, ok := ele.(*bindIq); ok {
   617 			Warn.Logf("Bad bind reply: %v", iq)
   617 			Warn.Logf("Bad bind reply: %v", iq)
   618 			return false
   618 			return false
   619 		}
   619 		}
   620 		jidStr := bindRepl.Jid
   620 		jidStr := bindRepl.Jid
   621 		if jidStr == nil || *jidStr == "" {
   621 		if jidStr == nil || *jidStr == "" {
   622 			Warn.Logf("Can't bind empty resource")
   622 			Warn.Log("Can't bind empty resource")
   623 			return false
   623 			return false
   624 		}
   624 		}
   625 		jid := new(JID)
   625 		jid := new(JID)
   626 		if err := jid.Set(*jidStr); err != nil {
   626 		if err := jid.Set(*jidStr); err != nil {
   627 			Warn.Logf("Can't parse JID %s: %s", *jidStr, err)
   627 			Warn.Logf("Can't parse JID %s: %s", *jidStr, err)