4646
4747public class Connection implements AutoCloseable
4848{
49+ /**
50+ * Allow the caller to restrict the IP version of the connection to
51+ * be established.
52+ */
53+ public enum IpVersion {
54+ IPV4_AND_IPV6 , ///< Allow both IPV4 and IPv6, the default.
55+ IPV4_ONLY , ///< Require that the connection be over IPv4 only.
56+ IPV6_ONLY ///< Require that the connection be over IPv6 only.
57+ }
58+
4959 /**
5060 * The identifier presented to the SSH-2 server.
5161 */
@@ -564,30 +574,74 @@ private void close(Throwable t, boolean hard)
564574
565575 /**
566576 * Same as
567- * {@link #connect(ServerHostKeyVerifier, int, int) connect(null, 0, 0)}.
577+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion ) connect(null, 0, 0, IpVersion.IPV4_AND_IPV6 )}.
568578 *
569579 * @return see comments for the
570- * {@link #connect(ServerHostKeyVerifier, int, int) connect(ServerHostKeyVerifier, int, int)}
580+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion ) connect(ServerHostKeyVerifier, int, int, IpVersion )}
571581 * method.
572582 * @throws IOException
573583 */
574584 public synchronized ConnectionInfo connect () throws IOException
575585 {
576- return connect (null , 0 , 0 );
586+ return connect (null , 0 , 0 , IpVersion .IPV4_AND_IPV6 );
587+ }
588+
589+ /**
590+ * Same as
591+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(null, 0, 0, ipVersion)}.
592+ *
593+ * @return see comments for the
594+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(ServerHostKeyVerifier, int, int, IpVersion)}
595+ * method.
596+ * @throws IOException
597+ */
598+ public synchronized ConnectionInfo connect (IpVersion ipVersion ) throws IOException
599+ {
600+ return connect (null , 0 , 0 , ipVersion );
577601 }
578602
603+
579604 /**
580605 * Same as
581- * {@link #connect(ServerHostKeyVerifier, int, int) connect(verifier, 0, 0)}.
606+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion ) connect(verifier, 0, 0, IpVersion.IPV4_AND_IPV6 )}.
582607 *
583608 * @return see comments for the
584- * {@link #connect(ServerHostKeyVerifier, int, int) connect(ServerHostKeyVerifier, int, int)}
609+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion ) connect(ServerHostKeyVerifier, int, int, IpVersion )}
585610 * method.
586611 * @throws IOException
587612 */
588613 public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier ) throws IOException
589614 {
590- return connect (verifier , 0 , 0 );
615+ return connect (verifier , 0 , 0 , IpVersion .IPV4_AND_IPV6 );
616+ }
617+
618+ /**
619+ * Same as
620+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(verifier, 0, 0, ipVersion)}.
621+ *
622+ * @return see comments for the
623+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(ServerHostKeyVerifier, int, int, IpVersion)}
624+ * method.
625+ * @throws IOException
626+ */
627+ public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , IpVersion ipVersion ) throws IOException
628+ {
629+ return connect (verifier , 0 , 0 , ipVersion );
630+ }
631+
632+ /**
633+ * Same as
634+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(verifier, connectTimeout, kexTimeout, IpVersion.IPV4_AND_IPV6)}.
635+ *
636+ * @return see comments for the
637+ * {@link #connect(ServerHostKeyVerifier, int, int, IpVersion) connect(ServerHostKeyVerifier, int, int, IpVersion)}
638+ * method.
639+ * @throws IOException
640+ */
641+ public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , int connectTimeout , int kexTimeout )
642+ throws IOException
643+ {
644+ return connect (verifier , connectTimeout , kexTimeout , IpVersion .IPV4_AND_IPV6 );
591645 }
592646
593647 /**
@@ -649,6 +703,11 @@ public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier) throw
649703 * but it will only have an effect after the
650704 * <code>verifier</code> returns.
651705 *
706+ * @param ipVersion
707+ * Specify whether the connection should be restricted to one of
708+ * IPv4 or IPv6, with a default of allowing both. See
709+ * {@link IpVersion}.
710+ *
652711 * @return A {@link ConnectionInfo} object containing the details of the
653712 * established connection.
654713 *
@@ -672,7 +731,7 @@ public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier) throw
672731 * proxy is buggy and does not return a proper HTTP response,
673732 * then a normal IOException is thrown instead.
674733 */
675- public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , int connectTimeout , int kexTimeout )
734+ public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , int connectTimeout , int kexTimeout , IpVersion ipVersion )
676735 throws IOException
677736 {
678737 final class TimeoutState
@@ -745,9 +804,22 @@ public void run()
745804 token = TimeoutService .addTimeoutHandler (timeoutHorizont , timeoutHandler );
746805 }
747806
807+ TransportManager .IpVersion tmIpVersion ;
808+ if (ipVersion == IpVersion .IPV4_ONLY )
809+ {
810+ tmIpVersion = TransportManager .IpVersion .IPV4_ONLY ;
811+ }
812+ else if (ipVersion == IpVersion .IPV6_ONLY ) {
813+ tmIpVersion = TransportManager .IpVersion .IPV6_ONLY ;
814+ }
815+ else // Assume (ipVersion == IpVersion.IPV4_AND_IPV6), the default.
816+ {
817+ tmIpVersion = TransportManager .IpVersion .IPV4_AND_IPV6 ;
818+ }
819+
748820 try
749821 {
750- tm .initialize (cryptoWishList , verifier , dhgexpara , connectTimeout , getOrCreateSecureRND (), proxyData );
822+ tm .initialize (cryptoWishList , verifier , dhgexpara , connectTimeout , tmIpVersion , getOrCreateSecureRND (), proxyData );
751823 }
752824 catch (SocketTimeoutException se )
753825 {
0 commit comments