XmppConnection.java

  1package eu.siacs.conversations.xmpp;
  2
  3import java.io.IOException;
  4import java.io.InputStream;
  5import java.io.OutputStream;
  6import java.math.BigInteger;
  7import java.net.Socket;
  8import java.net.UnknownHostException;
  9import java.security.KeyManagementException;
 10import java.security.KeyStore;
 11import java.security.KeyStoreException;
 12import java.security.MessageDigest;
 13import java.security.NoSuchAlgorithmException;
 14import java.security.SecureRandom;
 15import java.security.cert.CertPathValidatorException;
 16import java.security.cert.CertificateException;
 17import java.security.cert.X509Certificate;
 18import java.util.HashSet;
 19import java.util.Hashtable;
 20import java.util.List;
 21
 22import javax.net.ssl.SSLContext;
 23import javax.net.ssl.SSLSocket;
 24import javax.net.ssl.SSLSocketFactory;
 25import javax.net.ssl.TrustManager;
 26import javax.net.ssl.TrustManagerFactory;
 27import javax.net.ssl.X509TrustManager;
 28
 29import org.json.JSONException;
 30import org.xmlpull.v1.XmlPullParserException;
 31
 32import android.os.Bundle;
 33import android.os.PowerManager;
 34import android.os.SystemClock;
 35import android.util.Log;
 36import eu.siacs.conversations.entities.Account;
 37import eu.siacs.conversations.utils.CryptoHelper;
 38import eu.siacs.conversations.utils.DNSHelper;
 39import eu.siacs.conversations.xml.Element;
 40import eu.siacs.conversations.xml.Tag;
 41import eu.siacs.conversations.xml.TagWriter;
 42import eu.siacs.conversations.xml.XmlReader;
 43import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
 44import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 45import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 46import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
 47import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
 48import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
 49import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
 50import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
 51
 52public class XmppConnection implements Runnable {
 53
 54	protected Account account;
 55	private static final String LOGTAG = "xmppService";
 56
 57	private PowerManager.WakeLock wakeLock;
 58
 59	private SecureRandom random = new SecureRandom();
 60
 61	private Socket socket;
 62	private XmlReader tagReader;
 63	private TagWriter tagWriter;
 64
 65	private boolean shouldBind = true;
 66	private boolean shouldAuthenticate = true;
 67	private Element streamFeatures;
 68	private HashSet<String> discoFeatures = new HashSet<String>();
 69	
 70	private String streamId = null;
 71	
 72	private int stanzasReceived = 0;
 73	private int stanzasSent = 0;
 74	
 75	public long lastPaketReceived = 0;
 76	public long lastPingSent = 0;
 77
 78	private static final int PACKET_IQ = 0;
 79	private static final int PACKET_MESSAGE = 1;
 80	private static final int PACKET_PRESENCE = 2;
 81
 82	private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<String, PacketReceived>();
 83	private OnPresencePacketReceived presenceListener = null;
 84	private OnIqPacketReceived unregisteredIqListener = null;
 85	private OnMessagePacketReceived messageListener = null;
 86	private OnStatusChanged statusListener = null;
 87	private OnTLSExceptionReceived tlsListener;
 88
 89	public XmppConnection(Account account, PowerManager pm) {
 90		this.account = account;
 91		wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
 92				"XmppConnection");
 93		tagReader = new XmlReader(wakeLock);
 94		tagWriter = new TagWriter();
 95	}
 96
 97	protected void changeStatus(int nextStatus) {
 98		if (account.getStatus() != nextStatus) {
 99			account.setStatus(nextStatus);
100			if (statusListener != null) {
101				statusListener.onStatusChanged(account);
102			}
103		}
104	}
105
106	protected void connect() {
107		Log.d(LOGTAG,account.getJid()+ ": connecting");
108		try {
109			tagReader = new XmlReader(wakeLock);
110			tagWriter = new TagWriter();
111			packetCallbacks.clear();
112			this.changeStatus(Account.STATUS_CONNECTING);
113			Bundle namePort = DNSHelper.getSRVRecord(account.getServer());
114			String srvRecordServer = namePort.getString("name");
115			int srvRecordPort = namePort.getInt("port");
116			if (srvRecordServer != null) {
117				Log.d(LOGTAG, account.getJid() + ": using values from dns "
118						+ srvRecordServer + ":" + srvRecordPort);
119				socket = new Socket(srvRecordServer, srvRecordPort);
120			} else {
121				socket = new Socket(account.getServer(), 5222);
122			}
123			OutputStream out = socket.getOutputStream();
124			tagWriter.setOutputStream(out);
125			InputStream in = socket.getInputStream();
126			tagReader.setInputStream(in);
127			tagWriter.beginDocument();
128			sendStartStream();
129			Tag nextTag;
130			while ((nextTag = tagReader.readTag()) != null) {
131				if (nextTag.isStart("stream")) {
132					processStream(nextTag);
133					break;
134				} else {
135					Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName());
136					return;
137				}
138			}
139			if (socket.isConnected()) {
140				socket.close();
141			}
142		} catch (UnknownHostException e) {
143			this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
144			if (wakeLock.isHeld()) {
145				wakeLock.release();
146			}
147			return;
148		} catch (IOException e) {
149			if (account.getStatus() != Account.STATUS_TLS_ERROR) {
150				this.changeStatus(Account.STATUS_OFFLINE);
151			}
152			if (wakeLock.isHeld()) {
153				wakeLock.release();
154			}
155			return;
156		} catch (XmlPullParserException e) {
157			this.changeStatus(Account.STATUS_OFFLINE);
158			Log.d(LOGTAG, "xml exception " + e.getMessage());
159			if (wakeLock.isHeld()) {
160				wakeLock.release();
161			}
162			return;
163		}
164
165	}
166
167	@Override
168	public void run() {
169		connect();
170	}
171
172	private void processStream(Tag currentTag) throws XmlPullParserException,
173			IOException {
174		Tag nextTag = tagReader.readTag();
175		while ((nextTag != null) && (!nextTag.isEnd("stream"))) {
176			if (nextTag.isStart("error")) {
177				processStreamError(nextTag);
178			} else if (nextTag.isStart("features")) {
179				processStreamFeatures(nextTag);
180				if ((streamFeatures.getChildren().size() == 1)
181						&& (streamFeatures.hasChild("starttls"))
182						&& (!account.isOptionSet(Account.OPTION_USETLS))) {
183					changeStatus(Account.STATUS_SERVER_REQUIRES_TLS);
184				}
185			} else if (nextTag.isStart("proceed")) {
186				switchOverToTls(nextTag);
187			} else if (nextTag.isStart("success")) {
188				Log.d(LOGTAG, account.getJid()
189						+ ": logged in");
190				tagReader.readTag();
191				tagReader.reset();
192				sendStartStream();
193				processStream(tagReader.readTag());
194				break;
195			} else if (nextTag.isStart("failure")) {
196				Element failure = tagReader.readElement(nextTag);
197				changeStatus(Account.STATUS_UNAUTHORIZED);
198			} else if (nextTag.isStart("enabled")) {
199				this.stanzasSent = 0;
200				Element enabled = tagReader.readElement(nextTag);
201				if ("true".equals(enabled.getAttribute("resume"))) {
202					this.streamId = enabled.getAttribute("id");
203					Log.d(LOGTAG,account.getJid()+": stream managment enabled (resumable)");
204				} else {
205					Log.d(LOGTAG,account.getJid()+": stream managment enabled");
206				}
207				this.stanzasReceived = 0;
208				RequestPacket r = new RequestPacket();
209				tagWriter.writeStanzaAsync(r);
210			} else if (nextTag.isStart("resumed")) {
211				tagReader.readElement(nextTag);
212				changeStatus(Account.STATUS_ONLINE);
213				Log.d(LOGTAG,account.getJid()+": session resumed");
214			} else if (nextTag.isStart("r")) {
215				tagReader.readElement(nextTag);
216				AckPacket ack = new AckPacket(this.stanzasReceived);
217				//Log.d(LOGTAG,ack.toString());
218				tagWriter.writeStanzaAsync(ack);
219			} else if (nextTag.isStart("a")) {
220				Element ack = tagReader.readElement(nextTag);
221				lastPaketReceived = SystemClock.elapsedRealtime();
222				int serverSequence = Integer.parseInt(ack.getAttribute("h"));
223				if (serverSequence>this.stanzasSent) {
224					this.stanzasSent = serverSequence;
225				}
226				//Log.d(LOGTAG,"server ack"+ack.toString()+" ("+this.stanzasSent+")");
227			} else if (nextTag.isStart("iq")) {
228				processIq(nextTag);
229			} else if (nextTag.isStart("message")) {
230				processMessage(nextTag);
231			} else if (nextTag.isStart("presence")) {
232				processPresence(nextTag);
233			} else {
234				Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName()
235						+ " as child of " + currentTag.getName());
236			}
237			nextTag = tagReader.readTag();
238		}
239		if (account.getStatus() == Account.STATUS_ONLINE) {
240			account.setStatus(Account.STATUS_OFFLINE);
241			if (statusListener != null) {
242				statusListener.onStatusChanged(account);
243			}
244		}
245	}
246
247	private Element processPacket(Tag currentTag, int packetType)
248			throws XmlPullParserException, IOException {
249		Element element;
250		switch (packetType) {
251		case PACKET_IQ:
252			element = new IqPacket();
253			break;
254		case PACKET_MESSAGE:
255			element = new MessagePacket();
256			break;
257		case PACKET_PRESENCE:
258			element = new PresencePacket();
259			break;
260		default:
261			return null;
262		}
263		element.setAttributes(currentTag.getAttributes());
264		Tag nextTag = tagReader.readTag();
265		while (!nextTag.isEnd(element.getName())) {
266			if (!nextTag.isNo()) {
267				Element child = tagReader.readElement(nextTag);
268				element.addChild(child);
269			}
270			nextTag = tagReader.readTag();
271		}
272		++stanzasReceived;
273		lastPaketReceived = SystemClock.elapsedRealtime();
274		return element;
275	}
276
277	private void processIq(Tag currentTag) throws XmlPullParserException,
278			IOException {
279		IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
280		if (packetCallbacks.containsKey(packet.getId())) {
281			if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) {
282				((OnIqPacketReceived) packetCallbacks.get(packet.getId()))
283						.onIqPacketReceived(account, packet);
284			}
285
286			packetCallbacks.remove(packet.getId());
287		} else if (this.unregisteredIqListener != null) {
288			this.unregisteredIqListener.onIqPacketReceived(account, packet);
289		}
290	}
291
292	private void processMessage(Tag currentTag) throws XmlPullParserException,
293			IOException {
294		MessagePacket packet = (MessagePacket) processPacket(currentTag,
295				PACKET_MESSAGE);
296		String id = packet.getAttribute("id");
297		if ((id != null) && (packetCallbacks.containsKey(id))) {
298			if (packetCallbacks.get(id) instanceof OnMessagePacketReceived) {
299				((OnMessagePacketReceived) packetCallbacks.get(id))
300						.onMessagePacketReceived(account, packet);
301			}
302			packetCallbacks.remove(id);
303		} else if (this.messageListener != null) {
304			this.messageListener.onMessagePacketReceived(account, packet);
305		}
306	}
307
308	private void processPresence(Tag currentTag) throws XmlPullParserException,
309			IOException {
310		PresencePacket packet = (PresencePacket) processPacket(currentTag,
311				PACKET_PRESENCE);
312		String id = packet.getAttribute("id");
313		if ((id != null) && (packetCallbacks.containsKey(id))) {
314			if (packetCallbacks.get(id) instanceof OnPresencePacketReceived) {
315				((OnPresencePacketReceived) packetCallbacks.get(id))
316						.onPresencePacketReceived(account, packet);
317			}
318			packetCallbacks.remove(id);
319		} else if (this.presenceListener != null) {
320			this.presenceListener.onPresencePacketReceived(account, packet);
321		}
322	}
323
324	private void sendStartTLS() throws IOException {
325		Tag startTLS = Tag.empty("starttls");
326		startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
327		tagWriter.writeTag(startTLS);
328	}
329
330	private void switchOverToTls(Tag currentTag) throws XmlPullParserException,
331			IOException {
332		Tag nextTag = tagReader.readTag(); // should be proceed end tag
333		try {
334			SSLContext sc = SSLContext.getInstance("TLS");
335			TrustManagerFactory tmf = TrustManagerFactory
336					.getInstance(TrustManagerFactory.getDefaultAlgorithm());
337			// Initialise the TMF as you normally would, for example:
338			// tmf.in
339			try {
340				tmf.init((KeyStore) null);
341			} catch (KeyStoreException e1) {
342				// TODO Auto-generated catch block
343				e1.printStackTrace();
344			}
345
346			TrustManager[] trustManagers = tmf.getTrustManagers();
347			final X509TrustManager origTrustmanager = (X509TrustManager) trustManagers[0];
348
349			TrustManager[] wrappedTrustManagers = new TrustManager[] { new X509TrustManager() {
350
351				@Override
352				public void checkClientTrusted(X509Certificate[] chain,
353						String authType) throws CertificateException {
354					origTrustmanager.checkClientTrusted(chain, authType);
355				}
356
357				@Override
358				public void checkServerTrusted(X509Certificate[] chain,
359						String authType) throws CertificateException {
360					try {
361						origTrustmanager.checkServerTrusted(chain, authType);
362					} catch (CertificateException e) {
363						if (e.getCause() instanceof CertPathValidatorException) {
364							String sha;
365							try {
366								MessageDigest sha1 = MessageDigest.getInstance("SHA1");
367								sha1.update(chain[0].getEncoded());
368								sha = CryptoHelper.bytesToHex(sha1.digest());
369								if (!sha.equals(account.getSSLFingerprint())) {
370									changeStatus(Account.STATUS_TLS_ERROR);
371									if (tlsListener!=null) {
372										tlsListener.onTLSExceptionReceived(sha,account);
373									}
374									throw new CertificateException();
375								}
376							} catch (NoSuchAlgorithmException e1) {
377								// TODO Auto-generated catch block
378								e1.printStackTrace();
379							}
380						} else {
381							throw new CertificateException();
382						}
383					}
384				}
385
386				@Override
387				public X509Certificate[] getAcceptedIssuers() {
388					return origTrustmanager.getAcceptedIssuers();
389				}
390
391			} };
392			sc.init(null, wrappedTrustManagers, null);
393			SSLSocketFactory factory = sc.getSocketFactory();
394			SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
395						socket.getInetAddress().getHostAddress(), socket.getPort(),
396						true);
397			tagReader.setInputStream(sslSocket.getInputStream());
398			tagWriter.setOutputStream(sslSocket.getOutputStream());
399			sendStartStream();
400			Log.d(LOGTAG,account.getJid()+": TLS connection established");
401			processStream(tagReader.readTag());
402			sslSocket.close();
403		} catch (NoSuchAlgorithmException e1) {
404			// TODO Auto-generated catch block
405			e1.printStackTrace();
406		} catch (KeyManagementException e) {
407			// TODO Auto-generated catch block
408			e.printStackTrace();
409		}
410	}
411
412	private void sendSaslAuth() throws IOException, XmlPullParserException {
413		String saslString = CryptoHelper.saslPlain(account.getUsername(),
414				account.getPassword());
415		Element auth = new Element("auth");
416		auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
417		auth.setAttribute("mechanism", "PLAIN");
418		auth.setContent(saslString);
419		tagWriter.writeElement(auth);
420	}
421
422	private void processStreamFeatures(Tag currentTag)
423			throws XmlPullParserException, IOException {
424		this.streamFeatures = tagReader.readElement(currentTag);
425		if (this.streamFeatures.hasChild("starttls")
426				&& account.isOptionSet(Account.OPTION_USETLS)) {
427			sendStartTLS();
428		} else if (this.streamFeatures.hasChild("mechanisms")
429				&& shouldAuthenticate) {
430			sendSaslAuth();
431		} else if (this.streamFeatures.hasChild("sm") && streamId != null) {
432			Log.d(LOGTAG,"found old stream id. trying to remuse");
433			ResumePacket resume = new ResumePacket(this.streamId,stanzasReceived);
434			this.tagWriter.writeStanzaAsync(resume);
435		} else if (this.streamFeatures.hasChild("bind") && shouldBind) {
436			sendBindRequest();
437			if (this.streamFeatures.hasChild("session")) {
438				Log.d(LOGTAG,"sending session");
439				IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
440				Element session = new Element("session");
441				session.setAttribute("xmlns",
442						"urn:ietf:params:xml:ns:xmpp-session");
443				session.setContent("");
444				startSession.addChild(session);
445				this.sendIqPacket(startSession, null);
446			}
447		}
448	}
449
450	private void sendInitialPresence() {
451		PresencePacket packet = new PresencePacket();
452		packet.setAttribute("from", account.getFullJid());
453		if (account.getKeys().has("pgp_signature")) {
454			try {
455				String signature = account.getKeys().getString("pgp_signature");	
456				Element status = new Element("status");
457				status.setContent("online");
458				packet.addChild(status);
459				Element x = new Element("x");
460				x.setAttribute("xmlns", "jabber:x:signed");
461				x.setContent(signature);
462				packet.addChild(x);
463			} catch (JSONException e) {
464				//
465			}
466		}
467		this.sendPresencePacket(packet);
468	}
469
470	private void sendBindRequest() throws IOException {
471		IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
472		Element bind = new Element("bind");
473		bind.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-bind");
474		Element resource = new Element("resource");
475		resource.setContent("Conversations");
476		bind.addChild(resource);
477		iq.addChild(bind);
478		this.sendIqPacket(iq, new OnIqPacketReceived() {
479			@Override
480			public void onIqPacketReceived(Account account, IqPacket packet) {
481				String resource = packet.findChild("bind").findChild("jid")
482						.getContent().split("/")[1];
483				account.setResource(resource);
484				account.setStatus(Account.STATUS_ONLINE);
485				if (streamFeatures.hasChild("sm")) {
486					EnablePacket enable = new EnablePacket();
487					tagWriter.writeStanzaAsync(enable);
488				}
489				sendInitialPresence();
490				sendServiceDiscovery();
491				if (statusListener != null) {
492					statusListener.onStatusChanged(account);
493				}
494			}
495		});
496	}
497
498	private void sendServiceDiscovery() {
499		IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
500		iq.setAttribute("to", account.getServer());
501		Element query = new Element("query");
502		query.setAttribute("xmlns", "http://jabber.org/protocol/disco#info");
503		iq.addChild(query);
504		this.sendIqPacket(iq, new OnIqPacketReceived() {
505
506			@Override
507			public void onIqPacketReceived(Account account, IqPacket packet) {
508				if (packet.hasChild("query")) {
509					List<Element> elements = packet.findChild("query")
510							.getChildren();
511					for (int i = 0; i < elements.size(); ++i) {
512						if (elements.get(i).getName().equals("feature")) {
513							discoFeatures.add(elements.get(i).getAttribute(
514									"var"));
515						}
516					}
517				}
518				if (discoFeatures.contains("urn:xmpp:carbons:2")) {
519					sendEnableCarbons();
520				}
521			}
522		});
523	}
524
525	private void sendEnableCarbons() {
526		IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
527		Element enable = new Element("enable");
528		enable.setAttribute("xmlns", "urn:xmpp:carbons:2");
529		iq.addChild(enable);
530		this.sendIqPacket(iq, new OnIqPacketReceived() {
531
532			@Override
533			public void onIqPacketReceived(Account account, IqPacket packet) {
534				if (!packet.hasChild("error")) {
535					Log.d(LOGTAG, account.getJid()
536							+ ": successfully enabled carbons");
537				} else {
538					Log.d(LOGTAG, account.getJid()
539							+ ": error enableing carbons " + packet.toString());
540				}
541			}
542		});
543	}
544
545	private void processStreamError(Tag currentTag) {
546		Log.d(LOGTAG, "processStreamError");
547	}
548
549	private void sendStartStream() throws IOException {
550		Tag stream = Tag.start("stream:stream");
551		stream.setAttribute("from", account.getJid());
552		stream.setAttribute("to", account.getServer());
553		stream.setAttribute("version", "1.0");
554		stream.setAttribute("xml:lang", "en");
555		stream.setAttribute("xmlns", "jabber:client");
556		stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
557		tagWriter.writeTag(stream);
558	}
559
560	private String nextRandomId() {
561		return new BigInteger(50, random).toString(32);
562	}
563
564	public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
565		String id = nextRandomId();
566		packet.setAttribute("id", id);
567		this.sendPacket(packet, callback);
568	}
569
570	public void sendMessagePacket(MessagePacket packet) {
571		this.sendPacket(packet, null);
572	}
573
574	public void sendMessagePacket(MessagePacket packet,
575			OnMessagePacketReceived callback) {
576		this.sendPacket(packet, callback);
577	}
578
579	public void sendPresencePacket(PresencePacket packet) {
580		this.sendPacket(packet, null);
581	}
582
583	public void sendPresencePacket(PresencePacket packet,
584			OnPresencePacketReceived callback) {
585		this.sendPacket(packet, callback);
586	}
587	
588	private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) {
589		// TODO dont increment stanza count if packet = request packet or ack;
590		++stanzasSent;
591		tagWriter.writeStanzaAsync(packet);
592		if (callback != null) {
593			if (packet.getId()==null) {
594				packet.setId(nextRandomId());
595			}
596			packetCallbacks.put(packet.getId(), callback);
597		}
598	}
599	
600	public void sendPing() {
601		if (streamFeatures.hasChild("sm")) {
602			Log.d(LOGTAG,account.getJid()+": sending r as ping");
603			tagWriter.writeStanzaAsync(new RequestPacket());
604		} else {
605			Log.d(LOGTAG,account.getJid()+": sending iq as ping");
606			IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
607			Element ping = new Element("ping");
608			iq.setAttribute("from",account.getFullJid());
609			ping.setAttribute("xmlns", "urn:xmpp:ping");
610			iq.addChild(ping);
611			this.sendIqPacket(iq, null);
612		}
613	}
614
615	public void setOnMessagePacketReceivedListener(
616			OnMessagePacketReceived listener) {
617		this.messageListener = listener;
618	}
619
620	public void setOnUnregisteredIqPacketReceivedListener(
621			OnIqPacketReceived listener) {
622		this.unregisteredIqListener = listener;
623	}
624
625	public void setOnPresencePacketReceivedListener(
626			OnPresencePacketReceived listener) {
627		this.presenceListener = listener;
628	}
629
630	public void setOnStatusChangedListener(OnStatusChanged listener) {
631		this.statusListener = listener;
632	}
633	
634	public void setOnTLSExceptionReceivedListener(OnTLSExceptionReceived listener) {
635		this.tlsListener = listener;
636	}
637
638	public void disconnect(boolean force) {
639		Log.d(LOGTAG,"disconnecting");
640		try {
641		if (force) {
642				socket.close();
643		}
644		tagWriter.finish();
645		while(!tagWriter.finished()) {
646			Log.d(LOGTAG,"not yet finished");
647			Thread.sleep(100);
648		}
649		tagWriter.writeTag(Tag.end("stream:stream"));
650		} catch (IOException e) {
651			Log.d(LOGTAG,"io exception during disconnect");
652		} catch (InterruptedException e) {
653			Log.d(LOGTAG,"interupted while waiting for disconnect");
654		}
655	}
656	
657	public boolean hasFeatureRosterManagment() {
658		if (this.streamFeatures==null) {
659			return false;
660		} else {
661			return this.streamFeatures.hasChild("ver");
662		}
663	}
664
665	public void r() {
666		this.tagWriter.writeStanzaAsync(new RequestPacket());
667	}
668}