From e497af18500c01f4cb51e7da47ae10c5413336a2 Mon Sep 17 00:00:00 2001 From: boris Date: Thu, 4 Sep 2014 13:28:04 +0000 Subject: [PATCH] Adds support for google's ssltcp. --- src/org/ice4j/ice/Component.java | 4 +- src/org/ice4j/ice/LocalCandidate.java | 23 +++++ src/org/ice4j/ice/TcpHostCandidate.java | 3 +- .../harvest/GoogleTurnCandidateHarvest.java | 2 +- .../GoogleTurnSSLCandidateHarvester.java | 8 +- .../harvest/MultiplexingTcpHostHarvester.java | 88 +++++++++++++++++-- .../ice/harvest/StunCandidateHarvester.java | 2 +- 7 files changed, 112 insertions(+), 18 deletions(-) diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 16209ec..3e98c7c 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -880,9 +880,9 @@ public class Component */ public LocalCandidate findLocalCandidate(TransportAddress localAddress) { - for( LocalCandidate localCnd : localCandidates) + for (LocalCandidate localCnd : localCandidates) { - if(localCnd.getTransportAddress().equals(localAddress)) + if (localCnd.getTransportAddress().equals(localAddress)) return localCnd; } diff --git a/src/org/ice4j/ice/LocalCandidate.java b/src/org/ice4j/ice/LocalCandidate.java index a91a169..6352636 100644 --- a/src/org/ice4j/ice/LocalCandidate.java +++ b/src/org/ice4j/ice/LocalCandidate.java @@ -37,6 +37,11 @@ public abstract class LocalCandidate */ private String ufrag = null; + /** + * Whether this LocalCandidate uses SSL. + */ + private boolean isSSL = false; + /** * The Logger used by the LocalCandidate class and its * instances for logging output. @@ -395,4 +400,22 @@ public abstract class LocalCandidate { return getParentComponent().findLocalCandidate(relatedAddress); } + + /** + * Gets the value of the 'ssl' flag. + * @return the value of the 'ssl' flag. + */ + public boolean isSSL() + { + return isSSL; + } + + /** + * Sets the value of the 'ssl' flag. + * @param isSSL the value to set. + */ + public void setSSL(boolean isSSL) + { + this.isSSL = isSSL; + } } diff --git a/src/org/ice4j/ice/TcpHostCandidate.java b/src/org/ice4j/ice/TcpHostCandidate.java index 6e554a6..0e67146 100644 --- a/src/org/ice4j/ice/TcpHostCandidate.java +++ b/src/org/ice4j/ice/TcpHostCandidate.java @@ -37,7 +37,8 @@ public class TcpHostCandidate * @param parentComponent the Component that this candidate * belongs to. */ - public TcpHostCandidate(TransportAddress transportAddress, Component parentComponent) + public TcpHostCandidate(TransportAddress transportAddress, + Component parentComponent) { super(transportAddress, parentComponent); diff --git a/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java b/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java index b9fc788..b65d0a7 100644 --- a/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java +++ b/src/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java @@ -100,7 +100,7 @@ public class GoogleTurnCandidateHarvest TransportAddress relayedAddress = ((MappedAddressAttribute) attribute).getAddress(); - if(harvester.stunServer.getTransport() == Transport.TCP) + if (harvester.stunServer.getTransport() == Transport.TCP) { relayedAddress = new TransportAddress( relayedAddress.getAddress(), diff --git a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java index 0c5d524..f081a63 100644 --- a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java @@ -25,9 +25,9 @@ public class GoogleTurnSSLCandidateHarvester extends GoogleTurnCandidateHarvester { /** - * Data for the SSL server handshake. + * Data for the SSL message sent by the server. */ - private static final byte SSL_SERVER_HANDSHAKE[] = + static final byte SSL_SERVER_HANDSHAKE[] = { 0x16, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x46, 0x03, 0x01, 0x42, (byte)0x85, 0x45, (byte)0xa7, 0x27, @@ -43,9 +43,9 @@ public class GoogleTurnSSLCandidateHarvester }; /** - * Data for the SSL client handshake. + * Data for the SSL message sent by the client. */ - private static final byte SSL_CLIENT_HANDSHAKE[] = + static final byte SSL_CLIENT_HANDSHAKE[] = { (byte)0x80, 0x46, 0x01, 0x03, 0x01, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, (byte)0x80, 0x03, 0x00, diff --git a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java index 9786101..e613cba 100644 --- a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java +++ b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java @@ -79,6 +79,11 @@ public class MultiplexingTcpHostHarvester */ private boolean close = false; + /** + * Whether or not to use ssltcp. + */ + private boolean ssltcp = false; + /** * Channels pending to be added to the list that {@link #readThread} reads * from. @@ -112,7 +117,42 @@ public class MultiplexingTcpHostHarvester public MultiplexingTcpHostHarvester(int port) throws IOException { - this(port, Collections.list(NetworkInterface.getNetworkInterfaces())); + this(port, + Collections.list(NetworkInterface.getNetworkInterfaces()), + false); + } + + /** + * Initializes a new MultiplexingTcpHostHarvester, which is to + * listen on port number port on all IP addresses on all + * available interfaces. + * + * @param port the port to listen on. + * @param ssltcp whether to use ssltcp or not. + */ + public MultiplexingTcpHostHarvester(int port, boolean ssltcp) + throws IOException + { + this(port, + Collections.list(NetworkInterface.getNetworkInterfaces()), + ssltcp); + } + + /** + * Initializes a new MultiplexingTcpHostHarvester, which is to + * listen on the specified list of TransportAddresses. + * + * @param transportAddresses the transport addresses to listen on. + * @param ssltcp whether to use ssltcp or not. + */ + public MultiplexingTcpHostHarvester( + List transportAddresses, + boolean ssltcp) + throws IOException + { + this.ssltcp = ssltcp; + this.localAddresses.addAll(transportAddresses); + init(); } /** @@ -123,10 +163,9 @@ public class MultiplexingTcpHostHarvester */ public MultiplexingTcpHostHarvester( List transportAddresses) - throws IOException + throws IOException { - this.localAddresses.addAll(transportAddresses); - init(); + this(transportAddresses, false); } /** @@ -138,9 +177,11 @@ public class MultiplexingTcpHostHarvester * @param interfaces the interfaces to listen on. */ public MultiplexingTcpHostHarvester(int port, - List interfaces) + List interfaces, + boolean ssltcp) throws IOException { + this.ssltcp = ssltcp; initializeLocalAddresses(port, interfaces); init(); } @@ -212,7 +253,9 @@ public class MultiplexingTcpHostHarvester if((addr instanceof Inet4Address) || useIPv6) { TransportAddress transportAddress - = new TransportAddress(addr, port, Transport.TCP); + = new TransportAddress(addr, + port, + Transport.TCP); localAddresses.add(transportAddress); } } @@ -292,8 +335,10 @@ public class MultiplexingTcpHostHarvester TcpHostCandidate candidate = new TcpHostCandidate(transportAddress, component); candidate.setTcpType(CandidateTcpType.PASSIVE); + if (ssltcp) + candidate.setSSL(true); - candidates.add(new TcpHostCandidate(transportAddress, component)); + candidates.add(candidate); } return candidates; } @@ -675,11 +720,36 @@ public class MultiplexingTcpHostHarvester // socket's input stream channel.configureBlocking(true); - // read an RFC4571 frame into datagramPacket Socket socket = channel.socket(); + InputStream inputStream = socket.getInputStream(); + + // If we use ssltcp we wait for a pseudo-ssl handshake from the + // client. + if (ssltcp) + { + byte[] buf + = new byte[GoogleTurnSSLCandidateHarvester + .SSL_CLIENT_HANDSHAKE.length]; + + socket.getInputStream().read(buf); + if (Arrays.equals(buf, + GoogleTurnSSLCandidateHarvester + .SSL_CLIENT_HANDSHAKE)) + { + socket.getOutputStream().write( + GoogleTurnSSLCandidateHarvester + .SSL_SERVER_HANDSHAKE); + } + else + { + throw new ReadThreadException( + "Expected a pseudo ssl handshake, didn't get one."); + } + } + // read an RFC4571 frame into datagramPacket DelegatingSocket.receiveFromNetwork( datagramPacket, - socket.getInputStream(), + inputStream, socket.getLocalAddress(), socket.getLocalPort()); diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 32471e6..3b887da 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -416,7 +416,7 @@ public class StunCandidateHarvester /** * Returns the host candidate. - * For UDP it simply returns the candidate passed as paramter + * For UDP it simply returns the candidate passed as a parameter. * * However for TCP, we cannot return the same hostCandidate because in Java * a "server" socket cannot connect to a destination with the same local