socks5 initialisation works. doesn't get activate though

Daniel Gultsch created

Change summary

src/eu/siacs/conversations/xmpp/XmppConnection.java                 |  6 
src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java        | 62 
src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java | 38 
src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCandidateFound.java |  7 
src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCanditateFound.java |  7 
src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java         | 70 
src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java         | 13 
src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java    |  7 
src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java               |  8 
9 files changed, 180 insertions(+), 38 deletions(-)

Detailed changes

src/eu/siacs/conversations/xmpp/XmppConnection.java 🔗

@@ -748,8 +748,10 @@ public class XmppConnection implements Runnable {
 	}
 
 	public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
-		String id = nextRandomId();
-		packet.setAttribute("id", id);
+		if (packet.getId()==null) {
+			String id = nextRandomId();
+			packet.setAttribute("id", id);
+		}
 		packet.setFrom(account.getFullJid());
 		this.sendPacket(packet, callback);
 	}

src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java 🔗

@@ -1,6 +1,7 @@
 package eu.siacs.conversations.xmpp.jingle;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 import android.util.Log;
@@ -21,6 +22,7 @@ public class JingleConnection {
 	
 	public static final int STATUS_INITIATED = 0;
 	public static final int STATUS_ACCEPTED = 1;
+	public static final int STATUS_TERMINATED = 2;
 	public static final int STATUS_FAILED = 99;
 	
 	private int status = -1;
@@ -29,13 +31,15 @@ public class JingleConnection {
 	private Account account;
 	private String initiator;
 	private String responder;
-	private List<Element> canditates = new ArrayList<Element>();
+	private List<Element> candidates = new ArrayList<Element>();
+	private HashMap<String, SocksConnection> connections = new HashMap<String, SocksConnection>();
 	
 	private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
 		
 		@Override
 		public void onIqPacketReceived(Account account, IqPacket packet) {
 			if (packet.getType() == IqPacket.TYPE_ERROR) {
+				mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
 				status = STATUS_FAILED;
 			}
 		}
@@ -60,9 +64,16 @@ public class JingleConnection {
 	}
 	
 	public void deliverPacket(JinglePacket packet) {
-		Log.d("xmppService","packet arrived in connection");
-		if (packet.isAction("")) {
-			
+		
+		if (packet.isAction("session-terminate")) {
+			if (status == STATUS_INITIATED) {
+				mXmppConnectionService.markMessage(message, Message.STATUS_SEND_REJECTED);
+			}
+			status = STATUS_TERMINATED;
+		} else if (packet.isAction("session-accept")) {
+			accept(packet);
+		} else {
+			Log.d("xmppService","packet arrived in connection. action was "+packet.getAction());
 		}
 	}
 	
@@ -70,15 +81,16 @@ public class JingleConnection {
 		this.message = message;
 		this.account = message.getConversation().getAccount();
 		this.initiator = this.account.getFullJid();
-		if (this.canditates.size() > 0) {
+		this.responder = this.message.getCounterpart();
+		if (this.candidates.size() > 0) {
 			this.sendInitRequest();
 		} else {
-			this.mJingleConnectionManager.getPrimaryCanditate(account, new OnPrimaryCanditateFound() {
+			this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() {
 				
 				@Override
-				public void onPrimaryCanditateFound(boolean success, Element canditate) {
+				public void onPrimaryCandidateFound(boolean success, Element canditate) {
 					if (success) {
-						canditates.add(canditate);
+						candidates.add(canditate);
 					}
 					sendInitRequest();
 				}
@@ -96,7 +108,7 @@ public class JingleConnection {
 			content.setAttribute("creator", "initiator");
 			content.setAttribute("name", "a-file-offer");
 			content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message));
-			content.setCanditates(this.canditates);
+			content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates);
 			packet.setContent(content);
 			Log.d("xmppService",packet.toString());
 			account.getXmppConnection().sendIqPacket(packet, this.responseListener);
@@ -111,5 +123,37 @@ public class JingleConnection {
 		packet.setSessionId(this.sessionId);
 		return packet;
 	}
+	
+	private void accept(JinglePacket packet) {
+		Log.d("xmppService","session-accept: "+packet.toString());
+		Content content = packet.getJingleContent();
+		this.candidates.addAll(content.getCanditates());
+		this.status = STATUS_ACCEPTED;
+		this.connectWithCandidates();
+		IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
+		Log.d("xmppService","response "+response.toString());
+		account.getXmppConnection().sendIqPacket(response, null);
+	}
+	
+	private void connectWithCandidates() {
+		for(Element canditate : this.candidates) {
+			String host = canditate.getAttribute("host");
+			int port = Integer.parseInt(canditate.getAttribute("port"));
+			SocksConnection socksConnection = new SocksConnection(this, host, port);
+			socksConnection.connect();
+			this.connections.put(canditate.getAttribute("cid"), socksConnection);
+		}
+	}
+	
+	private void sendCandidateUsed(String cid) {
+		
+	}
 
+	public String getInitiator() {
+		return this.initiator;
+	}
+	
+	public String getResponder() {
+		return this.responder;
+	}
 }

src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java 🔗

@@ -23,7 +23,7 @@ public class JingleConnectionManager {
 	private List<JingleConnection> connections = new ArrayList<JingleConnection>(); // make
 																					// concurrent
 
-	private ConcurrentHashMap<String, Element> primaryCanditates = new ConcurrentHashMap<String, Element>();
+	private ConcurrentHashMap<String, Element> primaryCandidates = new ConcurrentHashMap<String, Element>();
 
 	private SecureRandom random = new SecureRandom();
 
@@ -67,9 +67,9 @@ public class JingleConnectionManager {
 		return this.xmppConnectionService;
 	}
 
-	public void getPrimaryCanditate(Account account,
-			final OnPrimaryCanditateFound listener) {
-		if (!this.primaryCanditates.containsKey(account.getJid())) {
+	public void getPrimaryCandidate(Account account,
+			final OnPrimaryCandidateFound listener) {
+		if (!this.primaryCandidates.containsKey(account.getJid())) {
 			String xmlns = "http://jabber.org/protocol/bytestreams";
 			final String proxy = account.getXmppConnection()
 					.findDiscoItemByFeature(xmlns);
@@ -90,34 +90,34 @@ public class JingleConnectionManager {
 								if (streamhost != null) {
 									Log.d("xmppService", "streamhost found "
 											+ streamhost.toString());
-									Element canditate = new Element("canditate");
-									canditate.setAttribute("cid",
+									Element candidate = new Element("candidate");
+									candidate.setAttribute("cid",
 											nextRandomId());
-									canditate.setAttribute("host",
+									candidate.setAttribute("host",
 											streamhost.getAttribute("host"));
-									canditate.setAttribute("port",
+									candidate.setAttribute("port",
 											streamhost.getAttribute("port"));
-									canditate.setAttribute("type", "proxy");
-									canditate.setAttribute("jid", proxy);
-									canditate
+									candidate.setAttribute("type", "proxy");
+									candidate.setAttribute("jid", proxy);
+									candidate
 											.setAttribute("priority", "655360");
-									primaryCanditates.put(account.getJid(),
-											canditate);
-									listener.onPrimaryCanditateFound(true,
-											canditate);
+									primaryCandidates.put(account.getJid(),
+											candidate);
+									listener.onPrimaryCandidateFound(true,
+											candidate);
 								} else {
-									listener.onPrimaryCanditateFound(false,
+									listener.onPrimaryCandidateFound(false,
 											null);
 								}
 							}
 						});
 			} else {
-				listener.onPrimaryCanditateFound(false, null);
+				listener.onPrimaryCandidateFound(false, null);
 			}
 
 		} else {
-			listener.onPrimaryCanditateFound(true,
-					this.primaryCanditates.get(account.getJid()));
+			listener.onPrimaryCandidateFound(true,
+					this.primaryCandidates.get(account.getJid()));
 		}
 	}
 

src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java 🔗

@@ -0,0 +1,70 @@
+package eu.siacs.conversations.xmpp.jingle;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+import eu.siacs.conversations.utils.CryptoHelper;
+
+import android.util.Log;
+
+public class SocksConnection {
+	
+	private JingleConnection jingleConnection;
+	private Socket socket;
+	private String host;
+	private int port;
+	private String destination;
+	
+	public SocksConnection(JingleConnection jingleConnection, String host, int port) {
+		this.jingleConnection = jingleConnection;
+		this.host = host;
+		this.port = port;
+		try {
+			MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
+			StringBuilder destBuilder = new StringBuilder();
+			destBuilder.append(jingleConnection.getSessionId());
+			destBuilder.append(jingleConnection.getInitiator());
+			destBuilder.append(jingleConnection.getResponder());
+			mDigest.reset();
+			Log.d("xmppService","plain destination: "+destBuilder.toString());
+			this.destination = CryptoHelper.bytesToHex(mDigest.digest(destBuilder.toString().getBytes()));
+			Log.d("xmppService","host="+host+", port="+port+", destination: "+destination);
+		} catch (NoSuchAlgorithmException e) {
+			
+		}
+	}
+	
+	public boolean connect() {
+		try {
+			this.socket = new Socket(this.host, this.port);
+			InputStream is = socket.getInputStream();
+			OutputStream os = socket.getOutputStream();
+			byte[] login = {0x05, 0x01, 0x00};
+			byte[] expectedReply = {0x05,0x00};
+			byte[] reply = new byte[2];
+			os.write(login);
+			is.read(reply);
+			if (Arrays.equals(reply, expectedReply)) {
+				String connect = ""+'\u0005'+'\u0001'+'\u0000'+'\u0003'+'\u0028'+this.destination+'\u0000'+'\u0000';
+				os.write(connect.getBytes());
+				byte[] result = new byte[2];
+				is.read(result);
+				int status = result[0];
+				return (status==0);
+			} else {
+				socket.close();
+				return false;
+			}
+		} catch (UnknownHostException e) {
+			return false;
+		} catch (IOException e) {
+			return false;
+		}
+	}
+}

src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java 🔗

@@ -1,6 +1,7 @@
 package eu.siacs.conversations.xmpp.jingle.stanzas;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 import eu.siacs.conversations.xml.Element;
@@ -22,14 +23,24 @@ public class Content extends Element {
 		file.addChild("name").setContent(actualFile.getName());
 	}
 
-	public void setCanditates(List<Element> canditates) {
+	public void setCandidates(String transportId, List<Element> canditates) {
 		Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
 		if (transport==null) {
 			transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1");
 		}
+		transport.setAttribute("sid", transportId);
 		transport.clearChildren();
 		for(Element canditate : canditates) {
 			transport.addChild(canditate);
 		}
 	}
+	
+	public List<Element> getCanditates() {
+		Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
+		if (transport==null) {
+			return new ArrayList<Element>();
+		} else {
+			return transport.getChildren();
+		}
+	}
 }

src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java 🔗

@@ -33,6 +33,13 @@ public class JinglePacket extends IqPacket {
 		return this;
 	}
 	
+	public Content getJingleContent() {
+		if (this.content==null) {
+			this.content = new Content();
+		}
+		return this.content;
+	}
+	
 	public JinglePacket setReason(Reason reason) {
 		this.reason = reason;
 		return this;

src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java 🔗

@@ -63,5 +63,13 @@ public class IqPacket extends AbstractStanza {
 			return 1000;
 		}
 	}
+	
+	public IqPacket generateRespone(int type) {
+		IqPacket packet = new IqPacket(type);
+		packet.setFrom(this.getTo());
+		packet.setTo(this.getFrom());
+		packet.setId(this.getId());
+		return packet;
+	}
 
 }