1package eu.siacs.conversations.xmpp.jingle;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Locale;
9import java.util.Map.Entry;
10
11import android.graphics.BitmapFactory;
12import android.util.Log;
13import eu.siacs.conversations.entities.Account;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.Message;
16import eu.siacs.conversations.services.XmppConnectionService;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xmpp.OnIqPacketReceived;
19import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
20import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
21import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
22import eu.siacs.conversations.xmpp.stanzas.IqPacket;
23
24public class JingleConnection {
25
26 private final String[] extensions = {"webp","jpeg","jpg","png"};
27 private final String[] cryptoExtensions = {"pgp","gpg","otr"};
28
29 private JingleConnectionManager mJingleConnectionManager;
30 private XmppConnectionService mXmppConnectionService;
31
32 public static final int STATUS_INITIATED = 0;
33 public static final int STATUS_ACCEPTED = 1;
34 public static final int STATUS_TERMINATED = 2;
35 public static final int STATUS_CANCELED = 3;
36 public static final int STATUS_FINISHED = 4;
37 public static final int STATUS_TRANSMITTING = 5;
38 public static final int STATUS_FAILED = 99;
39
40 private int ibbBlockSize = 4096;
41
42 private int status = -1;
43 private Message message;
44 private String sessionId;
45 private Account account;
46 private String initiator;
47 private String responder;
48 private List<JingleCandidate> candidates = new ArrayList<JingleCandidate>();
49 private HashMap<String, JingleSocks5Transport> connections = new HashMap<String, JingleSocks5Transport>();
50
51 private String transportId;
52 private Element fileOffer;
53 private JingleFile file = null;
54
55 private String contentName;
56 private String contentCreator;
57
58 private boolean receivedCandidate = false;
59 private boolean sentCandidate = false;
60
61 private boolean acceptedAutomatically = false;
62
63 private JingleTransport transport = null;
64
65 private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
66
67 @Override
68 public void onIqPacketReceived(Account account, IqPacket packet) {
69 if (packet.getType() == IqPacket.TYPE_ERROR) {
70 if (initiator.equals(account.getFullJid())) {
71 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
72 }
73 status = STATUS_FAILED;
74 }
75 }
76 };
77
78 final OnFileTransmissionStatusChanged onFileTransmissionSatusChanged = new OnFileTransmissionStatusChanged() {
79
80 @Override
81 public void onFileTransmitted(JingleFile file) {
82 if (responder.equals(account.getFullJid())) {
83 sendSuccess();
84 if (acceptedAutomatically) {
85 message.markUnread();
86 JingleConnection.this.mXmppConnectionService.updateUi(message.getConversation(), true);
87 }
88 BitmapFactory.Options options = new BitmapFactory.Options();
89 options.inJustDecodeBounds = true;
90 BitmapFactory.decodeFile(file.getAbsolutePath(),options);
91 int imageHeight = options.outHeight;
92 int imageWidth = options.outWidth;
93 message.setBody(""+file.getSize()+","+imageWidth+","+imageHeight);
94 mXmppConnectionService.databaseBackend.createMessage(message);
95 mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVED);
96 }
97 Log.d("xmppService","sucessfully transmitted file:"+file.getAbsolutePath());
98 }
99
100 @Override
101 public void onFileTransferAborted() {
102 JingleConnection.this.sendCancel();
103 JingleConnection.this.cancel();
104 }
105 };
106
107 private OnProxyActivated onProxyActivated = new OnProxyActivated() {
108
109 @Override
110 public void success() {
111 if (initiator.equals(account.getFullJid())) {
112 Log.d("xmppService","we were initiating. sending file");
113 transport.send(file,onFileTransmissionSatusChanged);
114 } else {
115 transport.receive(file,onFileTransmissionSatusChanged);
116 Log.d("xmppService","we were responding. receiving file");
117 }
118 }
119
120 @Override
121 public void failed() {
122 Log.d("xmppService","proxy activation failed");
123 }
124 };
125
126 public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
127 this.mJingleConnectionManager = mJingleConnectionManager;
128 this.mXmppConnectionService = mJingleConnectionManager.getXmppConnectionService();
129 }
130
131 public String getSessionId() {
132 return this.sessionId;
133 }
134
135 public String getAccountJid() {
136 return this.account.getFullJid();
137 }
138
139 public String getCounterPart() {
140 return this.message.getCounterpart();
141 }
142
143 public void deliverPacket(JinglePacket packet) {
144 boolean returnResult = true;
145 if (packet.isAction("session-terminate")) {
146 Reason reason = packet.getReason();
147 if (reason!=null) {
148 if (reason.hasChild("cancel")) {
149 this.cancel();
150 } else if (reason.hasChild("success")) {
151 this.receiveSuccess();
152 } else {
153 this.cancel();
154 }
155 } else {
156 this.cancel();
157 }
158 } else if (packet.isAction("session-accept")) {
159 returnResult = receiveAccept(packet);
160 } else if (packet.isAction("transport-info")) {
161 returnResult = receiveTransportInfo(packet);
162 } else if (packet.isAction("transport-replace")) {
163 if (packet.getJingleContent().hasIbbTransport()) {
164 returnResult = this.receiveFallbackToIbb(packet);
165 } else {
166 returnResult = false;
167 Log.d("xmppService","trying to fallback to something unknown"+packet.toString());
168 }
169 } else if (packet.isAction("transport-accept")) {
170 returnResult = this.receiveTransportAccept(packet);
171 } else {
172 Log.d("xmppService","packet arrived in connection. action was "+packet.getAction());
173 returnResult = false;
174 }
175 IqPacket response;
176 if (returnResult) {
177 response = packet.generateRespone(IqPacket.TYPE_RESULT);
178
179 } else {
180 response = packet.generateRespone(IqPacket.TYPE_ERROR);
181 }
182 account.getXmppConnection().sendIqPacket(response, null);
183 }
184
185 public void init(Message message) {
186 this.contentCreator = "initiator";
187 this.contentName = this.mJingleConnectionManager.nextRandomId();
188 this.message = message;
189 this.account = message.getConversation().getAccount();
190 this.initiator = this.account.getFullJid();
191 this.responder = this.message.getCounterpart();
192 this.sessionId = this.mJingleConnectionManager.nextRandomId();
193 if (this.candidates.size() > 0) {
194 this.sendInitRequest();
195 } else {
196 this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() {
197
198 @Override
199 public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) {
200 if (success) {
201 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(JingleConnection.this, candidate);
202 connections.put(candidate.getCid(), socksConnection);
203 socksConnection.connect(new OnTransportConnected() {
204
205 @Override
206 public void failed() {
207 Log.d("xmppService","connection to our own primary candidete failed");
208 sendInitRequest();
209 }
210
211 @Override
212 public void established() {
213 Log.d("xmppService","succesfully connected to our own primary candidate");
214 mergeCandidate(candidate);
215 sendInitRequest();
216 }
217 });
218 mergeCandidate(candidate);
219 } else {
220 Log.d("xmppService","no primary candidate of our own was found");
221 sendInitRequest();
222 }
223 }
224 });
225 }
226
227 }
228
229 public void init(Account account, JinglePacket packet) {
230 this.status = STATUS_INITIATED;
231 Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().split("/")[0], false);
232 this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
233 this.message.setType(Message.TYPE_IMAGE);
234 this.message.setStatus(Message.STATUS_RECEIVED_OFFER);
235 this.message.setJingleConnection(this);
236 String[] fromParts = packet.getFrom().split("/");
237 this.message.setPresence(fromParts[1]);
238 this.account = account;
239 this.initiator = packet.getFrom();
240 this.responder = this.account.getFullJid();
241 this.sessionId = packet.getSessionId();
242 Content content = packet.getJingleContent();
243 this.contentCreator = content.getAttribute("creator");
244 this.contentName = content.getAttribute("name");
245 this.transportId = content.getTransportId();
246 this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
247 this.fileOffer = packet.getJingleContent().getFileOffer();
248 if (fileOffer!=null) {
249 Element fileSize = fileOffer.findChild("size");
250 Element fileNameElement = fileOffer.findChild("name");
251 if (fileNameElement!=null) {
252 boolean supportedFile = false;
253 String[] filename = fileNameElement.getContent().toLowerCase(Locale.US).split("\\.");
254 if (Arrays.asList(this.extensions).contains(filename[filename.length - 1])) {
255 supportedFile = true;
256 } else if (Arrays.asList(this.cryptoExtensions).contains(filename[filename.length - 1])) {
257 if (filename.length == 3) {
258 if (Arrays.asList(this.extensions).contains(filename[filename.length -2])) {
259 supportedFile = true;
260 if (filename[filename.length - 1].equals("otr")) {
261 Log.d("xmppService","receiving otr file");
262 this.message.setEncryption(Message.ENCRYPTION_OTR);
263 } else {
264 this.message.setEncryption(Message.ENCRYPTION_PGP);
265 }
266 }
267 }
268 }
269 if (supportedFile) {
270 long size = Long.parseLong(fileSize.getContent());
271 message.setBody(""+size);
272 conversation.getMessages().add(message);
273 if (size<=this.mJingleConnectionManager.getAutoAcceptFileSize()) {
274 Log.d("xmppService","auto accepting file from "+packet.getFrom());
275 this.acceptedAutomatically = true;
276 this.sendAccept();
277 } else {
278 message.markUnread();
279 Log.d("xmppService","not auto accepting new file offer with size: "+size+" allowed size:"+this.mJingleConnectionManager.getAutoAcceptFileSize());
280 this.mXmppConnectionService.updateUi(conversation, true);
281 }
282 this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
283 if (message.getEncryption() == Message.ENCRYPTION_OTR) {
284 byte[] key = conversation.getSymmetricKey();
285 if (key==null) {
286 this.sendCancel();
287 this.cancel();
288 return;
289 } else {
290 this.file.setKey(conversation.getSymmetricKey());
291 }
292 }
293 this.file.setExpectedSize(size);
294 } else {
295 this.sendCancel();
296 this.cancel();
297 }
298 } else {
299 this.sendCancel();
300 this.cancel();
301 }
302 } else {
303 this.sendCancel();
304 this.cancel();
305 }
306 }
307
308 private void sendInitRequest() {
309 JinglePacket packet = this.bootstrapPacket("session-initiate");
310 Content content = new Content(this.contentCreator,this.contentName);
311 if (message.getType() == Message.TYPE_IMAGE) {
312 content.setTransportId(this.transportId);
313 this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
314 if (message.getEncryption() == Message.ENCRYPTION_OTR) {
315 Conversation conversation = this.message.getConversation();
316 this.mXmppConnectionService.renewSymmetricKey(conversation);
317 content.setFileOffer(this.file, true);
318 this.file.setKey(conversation.getSymmetricKey());
319 } else {
320 content.setFileOffer(this.file,false);
321 }
322 this.transportId = this.mJingleConnectionManager.nextRandomId();
323 content.setTransportId(this.transportId);
324 content.socks5transport().setChildren(getCandidatesAsElements());
325 packet.setContent(content);
326 this.sendJinglePacket(packet);
327 this.status = STATUS_INITIATED;
328 }
329 }
330
331 private List<Element> getCandidatesAsElements() {
332 List<Element> elements = new ArrayList<Element>();
333 for(JingleCandidate c : this.candidates) {
334 elements.add(c.toElement());
335 }
336 return elements;
337 }
338
339 private void sendAccept() {
340 status = STATUS_ACCEPTED;
341 mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVING);
342 this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() {
343
344 @Override
345 public void onPrimaryCandidateFound(boolean success,final JingleCandidate candidate) {
346 final JinglePacket packet = bootstrapPacket("session-accept");
347 final Content content = new Content(contentCreator,contentName);
348 content.setFileOffer(fileOffer);
349 content.setTransportId(transportId);
350 if ((success)&&(!equalCandidateExists(candidate))) {
351 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(JingleConnection.this, candidate);
352 connections.put(candidate.getCid(), socksConnection);
353 socksConnection.connect(new OnTransportConnected() {
354
355 @Override
356 public void failed() {
357 Log.d("xmppService","connection to our own primary candidate failed");
358 content.socks5transport().setChildren(getCandidatesAsElements());
359 packet.setContent(content);
360 sendJinglePacket(packet);
361 connectNextCandidate();
362 }
363
364 @Override
365 public void established() {
366 Log.d("xmppService","connected to primary candidate");
367 mergeCandidate(candidate);
368 content.socks5transport().setChildren(getCandidatesAsElements());
369 packet.setContent(content);
370 sendJinglePacket(packet);
371 connectNextCandidate();
372 }
373 });
374 } else {
375 Log.d("xmppService","did not find a primary candidate for ourself");
376 content.socks5transport().setChildren(getCandidatesAsElements());
377 packet.setContent(content);
378 sendJinglePacket(packet);
379 connectNextCandidate();
380 }
381 }
382 });
383
384 }
385
386 private JinglePacket bootstrapPacket(String action) {
387 JinglePacket packet = new JinglePacket();
388 packet.setAction(action);
389 packet.setFrom(account.getFullJid());
390 packet.setTo(this.message.getCounterpart());
391 packet.setSessionId(this.sessionId);
392 packet.setInitiator(this.initiator);
393 return packet;
394 }
395
396 private void sendJinglePacket(JinglePacket packet) {
397 //Log.d("xmppService",packet.toString());
398 account.getXmppConnection().sendIqPacket(packet,responseListener);
399 }
400
401 private boolean receiveAccept(JinglePacket packet) {
402 Content content = packet.getJingleContent();
403 mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
404 this.status = STATUS_ACCEPTED;
405 mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
406 this.connectNextCandidate();
407 return true;
408 }
409
410 private boolean receiveTransportInfo(JinglePacket packet) {
411 Content content = packet.getJingleContent();
412 if (content.hasSocks5Transport()) {
413 if (content.socks5transport().hasChild("activated")) {
414 if ((this.transport!=null)&&(this.transport instanceof JingleSocks5Transport)) {
415 onProxyActivated.success();
416 } else {
417 String cid = content.socks5transport().findChild("activated").getAttribute("cid");
418 Log.d("xmppService","received proxy activated ("+cid+")prior to choosing our own transport");
419 JingleSocks5Transport connection = this.connections.get(cid);
420 if (connection!=null) {
421 connection.setActivated(true);
422 } else {
423 Log.d("xmppService","activated connection not found");
424 this.sendCancel();
425 this.cancel();
426 }
427 }
428 return true;
429 } else if (content.socks5transport().hasChild("proxy-error")) {
430 onProxyActivated.failed();
431 return true;
432 } else if (content.socks5transport().hasChild("candidate-error")) {
433 Log.d("xmppService","received candidate error");
434 this.receivedCandidate = true;
435 if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
436 this.connect();
437 }
438 return true;
439 } else if (content.socks5transport().hasChild("candidate-used")){
440 String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
441 if (cid!=null) {
442 Log.d("xmppService","candidate used by counterpart:"+cid);
443 JingleCandidate candidate = getCandidate(cid);
444 candidate.flagAsUsedByCounterpart();
445 this.receivedCandidate = true;
446 if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
447 this.connect();
448 } else {
449 Log.d("xmppService","ignoring because file is already in transmission or we havent sent our candidate yet");
450 }
451 return true;
452 } else {
453 return false;
454 }
455 } else {
456 return false;
457 }
458 } else {
459 return true;
460 }
461 }
462
463 private void connect() {
464 final JingleSocks5Transport connection = chooseConnection();
465 this.transport = connection;
466 if (connection==null) {
467 Log.d("xmppService","could not find suitable candidate");
468 this.disconnect();
469 if (this.initiator.equals(account.getFullJid())) {
470 this.sendFallbackToIbb();
471 }
472 } else {
473 this.status = STATUS_TRANSMITTING;
474 if (connection.needsActivation()) {
475 if (connection.getCandidate().isOurs()) {
476 Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy. going to activate");
477 IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
478 activation.setTo(connection.getCandidate().getJid());
479 activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId());
480 activation.query().addChild("activate").setContent(this.getCounterPart());
481 this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() {
482
483 @Override
484 public void onIqPacketReceived(Account account, IqPacket packet) {
485 if (packet.getType()==IqPacket.TYPE_ERROR) {
486 onProxyActivated.failed();
487 } else {
488 onProxyActivated.success();
489 sendProxyActivated(connection.getCandidate().getCid());
490 }
491 }
492 });
493 } else {
494 Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was a proxy. waiting for other party to activate");
495 }
496 } else {
497 if (initiator.equals(account.getFullJid())) {
498 Log.d("xmppService","we were initiating. sending file");
499 connection.send(file,onFileTransmissionSatusChanged);
500 } else {
501 Log.d("xmppService","we were responding. receiving file");
502 connection.receive(file,onFileTransmissionSatusChanged);
503 }
504 }
505 }
506 }
507
508 private JingleSocks5Transport chooseConnection() {
509 JingleSocks5Transport connection = null;
510 for (Entry<String, JingleSocks5Transport> cursor : connections.entrySet()) {
511 JingleSocks5Transport currentConnection = cursor.getValue();
512 //Log.d("xmppService","comparing candidate: "+currentConnection.getCandidate().toString());
513 if (currentConnection.isEstablished()&&(currentConnection.getCandidate().isUsedByCounterpart()||(!currentConnection.getCandidate().isOurs()))) {
514 //Log.d("xmppService","is usable");
515 if (connection==null) {
516 connection = currentConnection;
517 } else {
518 if (connection.getCandidate().getPriority()<currentConnection.getCandidate().getPriority()) {
519 connection = currentConnection;
520 } else if (connection.getCandidate().getPriority()==currentConnection.getCandidate().getPriority()) {
521 //Log.d("xmppService","found two candidates with same priority");
522 if (initiator.equals(account.getFullJid())) {
523 if (currentConnection.getCandidate().isOurs()) {
524 connection = currentConnection;
525 }
526 } else {
527 if (!currentConnection.getCandidate().isOurs()) {
528 connection = currentConnection;
529 }
530 }
531 }
532 }
533 }
534 }
535 return connection;
536 }
537
538 private void sendSuccess() {
539 JinglePacket packet = bootstrapPacket("session-terminate");
540 Reason reason = new Reason();
541 reason.addChild("success");
542 packet.setReason(reason);
543 this.sendJinglePacket(packet);
544 this.disconnect();
545 this.status = STATUS_FINISHED;
546 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_RECIEVED);
547 this.mJingleConnectionManager.finishConnection(this);
548 }
549
550 private void sendFallbackToIbb() {
551 JinglePacket packet = this.bootstrapPacket("transport-replace");
552 Content content = new Content(this.contentCreator,this.contentName);
553 this.transportId = this.mJingleConnectionManager.nextRandomId();
554 content.setTransportId(this.transportId);
555 content.ibbTransport().setAttribute("block-size",""+this.ibbBlockSize);
556 packet.setContent(content);
557 this.sendJinglePacket(packet);
558 }
559
560 private boolean receiveFallbackToIbb(JinglePacket packet) {
561 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
562 if (receivedBlockSize!=null) {
563 int bs = Integer.parseInt(receivedBlockSize);
564 if (bs>this.ibbBlockSize) {
565 this.ibbBlockSize = bs;
566 }
567 }
568 this.transportId = packet.getJingleContent().getTransportId();
569 this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
570 this.transport.receive(file, onFileTransmissionSatusChanged);
571 JinglePacket answer = bootstrapPacket("transport-accept");
572 Content content = new Content("initiator", "a-file-offer");
573 content.setTransportId(this.transportId);
574 content.ibbTransport().setAttribute("block-size", ""+this.ibbBlockSize);
575 answer.setContent(content);
576 this.sendJinglePacket(answer);
577 return true;
578 }
579
580 private boolean receiveTransportAccept(JinglePacket packet) {
581 if (packet.getJingleContent().hasIbbTransport()) {
582 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
583 if (receivedBlockSize!=null) {
584 int bs = Integer.parseInt(receivedBlockSize);
585 if (bs>this.ibbBlockSize) {
586 this.ibbBlockSize = bs;
587 }
588 }
589 this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
590 this.transport.connect(new OnTransportConnected() {
591
592 @Override
593 public void failed() {
594 Log.d("xmppService","ibb open failed");
595 }
596
597 @Override
598 public void established() {
599 JingleConnection.this.transport.send(file, onFileTransmissionSatusChanged);
600 }
601 });
602 return true;
603 } else {
604 return false;
605 }
606 }
607
608 private void receiveSuccess() {
609 this.status = STATUS_FINISHED;
610 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND);
611 this.disconnect();
612 this.mJingleConnectionManager.finishConnection(this);
613 }
614
615 public void cancel() {
616 this.disconnect();
617 if (this.message!=null) {
618 if (this.responder.equals(account.getFullJid())) {
619 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_RECEPTION_FAILED);
620 } else {
621 if (this.status == STATUS_INITIATED) {
622 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_REJECTED);
623 } else {
624 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED);
625 }
626 }
627 }
628 this.status = STATUS_CANCELED;
629 this.mJingleConnectionManager.finishConnection(this);
630 }
631
632 private void sendCancel() {
633 JinglePacket packet = bootstrapPacket("session-terminate");
634 Reason reason = new Reason();
635 reason.addChild("cancel");
636 packet.setReason(reason);
637 this.sendJinglePacket(packet);
638 }
639
640 private void connectNextCandidate() {
641 for(JingleCandidate candidate : this.candidates) {
642 if ((!connections.containsKey(candidate.getCid())&&(!candidate.isOurs()))) {
643 this.connectWithCandidate(candidate);
644 return;
645 }
646 }
647 this.sendCandidateError();
648 }
649
650 private void connectWithCandidate(final JingleCandidate candidate) {
651 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this,candidate);
652 connections.put(candidate.getCid(), socksConnection);
653 socksConnection.connect(new OnTransportConnected() {
654
655 @Override
656 public void failed() {
657 Log.d("xmppService", "connection failed with "+candidate.getHost()+":"+candidate.getPort());
658 connectNextCandidate();
659 }
660
661 @Override
662 public void established() {
663 Log.d("xmppService", "established connection with "+candidate.getHost()+":"+candidate.getPort());
664 sendCandidateUsed(candidate.getCid());
665 }
666 });
667 }
668
669 private void disconnect() {
670 Iterator<Entry<String, JingleSocks5Transport>> it = this.connections.entrySet().iterator();
671 while (it.hasNext()) {
672 Entry<String, JingleSocks5Transport> pairs = it.next();
673 pairs.getValue().disconnect();
674 it.remove();
675 }
676 }
677
678 private void sendProxyActivated(String cid) {
679 JinglePacket packet = bootstrapPacket("transport-info");
680 Content content = new Content(this.contentCreator,this.contentName);
681 content.setTransportId(this.transportId);
682 content.socks5transport().addChild("activated").setAttribute("cid", cid);
683 packet.setContent(content);
684 this.sendJinglePacket(packet);
685 }
686
687 private void sendCandidateUsed(final String cid) {
688 JinglePacket packet = bootstrapPacket("transport-info");
689 Content content = new Content(this.contentCreator,this.contentName);
690 content.setTransportId(this.transportId);
691 content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
692 packet.setContent(content);
693 this.sentCandidate = true;
694 if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
695 connect();
696 }
697 this.sendJinglePacket(packet);
698 }
699
700 private void sendCandidateError() {
701 JinglePacket packet = bootstrapPacket("transport-info");
702 Content content = new Content(this.contentCreator,this.contentName);
703 content.setTransportId(this.transportId);
704 content.socks5transport().addChild("candidate-error");
705 packet.setContent(content);
706 this.sentCandidate = true;
707 if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
708 connect();
709 }
710 this.sendJinglePacket(packet);
711 }
712
713 public String getInitiator() {
714 return this.initiator;
715 }
716
717 public String getResponder() {
718 return this.responder;
719 }
720
721 public int getStatus() {
722 return this.status;
723 }
724
725 private boolean equalCandidateExists(JingleCandidate candidate) {
726 for(JingleCandidate c : this.candidates) {
727 if (c.equalValues(candidate)) {
728 return true;
729 }
730 }
731 return false;
732 }
733
734 private void mergeCandidate(JingleCandidate candidate) {
735 for(JingleCandidate c : this.candidates) {
736 if (c.equals(candidate)) {
737 return;
738 }
739 }
740 this.candidates.add(candidate);
741 }
742
743 private void mergeCandidates(List<JingleCandidate> candidates) {
744 for(JingleCandidate c : candidates) {
745 mergeCandidate(c);
746 }
747 }
748
749 private JingleCandidate getCandidate(String cid) {
750 for(JingleCandidate c : this.candidates) {
751 if (c.getCid().equals(cid)) {
752 return c;
753 }
754 }
755 return null;
756 }
757
758 interface OnProxyActivated {
759 public void success();
760 public void failed();
761 }
762
763 public boolean hasTransportId(String sid) {
764 return sid.equals(this.transportId);
765 }
766
767 public JingleTransport getTransport() {
768 return this.transport;
769 }
770
771 public void accept() {
772 if (status==STATUS_INITIATED) {
773 new Thread(new Runnable() {
774
775 @Override
776 public void run() {
777 sendAccept();
778 }
779 }).start();
780 } else {
781 Log.d("xmppService","status ("+status+") was not ok");
782 }
783 }
784}