46
46
47
47
public class Connection implements AutoCloseable
48
48
{
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
+
49
59
/**
50
60
* The identifier presented to the SSH-2 server.
51
61
*/
@@ -564,30 +574,74 @@ private void close(Throwable t, boolean hard)
564
574
565
575
/**
566
576
* 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 )}.
568
578
*
569
579
* @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 )}
571
581
* method.
572
582
* @throws IOException
573
583
*/
574
584
public synchronized ConnectionInfo connect () throws IOException
575
585
{
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 );
577
601
}
578
602
603
+
579
604
/**
580
605
* 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 )}.
582
607
*
583
608
* @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 )}
585
610
* method.
586
611
* @throws IOException
587
612
*/
588
613
public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier ) throws IOException
589
614
{
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 );
591
645
}
592
646
593
647
/**
@@ -649,6 +703,11 @@ public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier) throw
649
703
* but it will only have an effect after the
650
704
* <code>verifier</code> returns.
651
705
*
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
+ *
652
711
* @return A {@link ConnectionInfo} object containing the details of the
653
712
* established connection.
654
713
*
@@ -672,7 +731,7 @@ public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier) throw
672
731
* proxy is buggy and does not return a proper HTTP response,
673
732
* then a normal IOException is thrown instead.
674
733
*/
675
- public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , int connectTimeout , int kexTimeout )
734
+ public synchronized ConnectionInfo connect (ServerHostKeyVerifier verifier , int connectTimeout , int kexTimeout , IpVersion ipVersion )
676
735
throws IOException
677
736
{
678
737
final class TimeoutState
@@ -745,9 +804,22 @@ public void run()
745
804
token = TimeoutService .addTimeoutHandler (timeoutHorizont , timeoutHandler );
746
805
}
747
806
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
+
748
820
try
749
821
{
750
- tm .initialize (cryptoWishList , verifier , dhgexpara , connectTimeout , getOrCreateSecureRND (), proxyData );
822
+ tm .initialize (cryptoWishList , verifier , dhgexpara , connectTimeout , tmIpVersion , getOrCreateSecureRND (), proxyData );
751
823
}
752
824
catch (SocketTimeoutException se )
753
825
{
0 commit comments