Browse Source

Add support for binding to IPv6 addresses.

Change-Id: Iaf88955e20a8797c0f20924ef343626a435af91f
Reviewed-on: http://gerrit.dmdirc.com/2970
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8
Shane Mc Cormack 10 years ago
parent
commit
b02cb4ddef

+ 15
- 0
src/com/dmdirc/parser/common/BaseParser.java View File

@@ -61,6 +61,9 @@ public abstract class BaseParser extends ThreadedParser {
61 61
     /** The IP that this parser should bind to. */
62 62
     private String bindIp;
63 63
 
64
+    /** The IPv6 IP that this parser should bind to. */
65
+    private String bindIpv6;
66
+
64 67
     /** The URI of the proxy to use when connecting, if any. */
65 68
     private URI proxy;
66 69
 
@@ -228,4 +231,16 @@ public abstract class BaseParser extends ThreadedParser {
228 231
     public void setBindIP(final String ip) {
229 232
         this.bindIp = ip;
230 233
     }
234
+
235
+    /** {@inheritDoc} */
236
+    @Override
237
+    public String getBindIPv6() {
238
+        return bindIpv6;
239
+    }
240
+
241
+    /** {@inheritDoc} */
242
+    @Override
243
+    public void setBindIPv6(final String ip) {
244
+        this.bindIpv6 = ip;
245
+    }
231 246
 }

+ 14
- 0
src/com/dmdirc/parser/interfaces/Parser.java View File

@@ -116,6 +116,20 @@ public interface Parser {
116 116
      */
117 117
     void setBindIP(String ip);
118 118
 
119
+    /**
120
+     * Get the IPv6 address that this parser will bind to.
121
+     *
122
+     * @return IPv6 that this parser is bound to ("" for default IP)
123
+     */
124
+    String getBindIPv6();
125
+
126
+    /**
127
+     * Set the IPv6 address that this parser will bind to.
128
+     *
129
+     * @param ip IPv6 IP to bind to
130
+     */
131
+    void setBindIPv6(String ip);
132
+
119 133
     /**
120 134
      * Gets the proxy URI that this parser will use.
121 135
      *

+ 30
- 8
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -775,6 +775,15 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
775 775
         disconnectOnFatal = newValue;
776 776
     }
777 777
 
778
+    /**
779
+     * Create a new Socket object for the given target, using the given proxy
780
+     * if appropriate.
781
+     *
782
+     * @param target Target URI to connect to.
783
+     * @param proxy Proxy URI to use
784
+     * @return Socket, with IP Binding or Proxy as appropriate,
785
+     * @throws IOException If there was an issue creating the socket.
786
+     */
778 787
     private Socket newSocket(final URI target, final URI proxy) throws IOException {
779 788
         if (target.getPort() > 65535 || target.getPort() <= 0) {
780 789
             throw new IOException("Server port (" + target.getPort() + ") is invalid.");
@@ -785,20 +794,33 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
785 794
             callDebugInfo(DEBUG_SOCKET, "Not using Proxy");
786 795
             mySocket = new Socket();
787 796
 
788
-            if (getBindIP() != null && !getBindIP().isEmpty()) {
789
-                callDebugInfo(DEBUG_SOCKET, "Binding to IP: " + getBindIP());
790
-                try {
791
-                    mySocket.bind(new InetSocketAddress(InetAddress.getByName(getBindIP()), 0));
792
-                } catch (IOException e) {
793
-                    callDebugInfo(DEBUG_SOCKET, "Binding failed: " + e.getMessage());
797
+            final InetSocketAddress sockAddr = new InetSocketAddress(target.getHost(), target.getPort());
798
+
799
+            if (sockAddr.getAddress() instanceof Inet6Address) {
800
+                if (getBindIPv6() != null && !getBindIPv6().isEmpty()) {
801
+                    callDebugInfo(DEBUG_SOCKET, "Binding to IPv6: " + getBindIP());
802
+                    try {
803
+                        mySocket.bind(new InetSocketAddress(InetAddress.getByName(getBindIPv6()), 0));
804
+                    } catch (IOException e) {
805
+                        callDebugInfo(DEBUG_SOCKET, "Binding failed: " + e.getMessage());
806
+                    }
807
+                }
808
+            } else {
809
+                if (getBindIP() != null && !getBindIP().isEmpty()) {
810
+                    callDebugInfo(DEBUG_SOCKET, "Binding to IPv4: " + getBindIP());
811
+                    try {
812
+                        mySocket.bind(new InetSocketAddress(InetAddress.getByName(getBindIP()), 0));
813
+                    } catch (IOException e) {
814
+                        callDebugInfo(DEBUG_SOCKET, "Binding failed: " + e.getMessage());
815
+                    }
794 816
                 }
795 817
             }
796 818
 
797
-            mySocket.connect(new InetSocketAddress(target.getHost(), target.getPort()), connectTimeout);
819
+            mySocket.connect(sockAddr, connectTimeout);
798 820
         } else {
799 821
             callDebugInfo(DEBUG_SOCKET, "Using Proxy");
800 822
 
801
-            if (getBindIP() != null && !getBindIP().isEmpty()) {
823
+            if ((getBindIP() != null && !getBindIP().isEmpty()) || (getBindIPv6() != null && !getBindIPv6().isEmpty())) {
802 824
                 callDebugInfo(DEBUG_SOCKET, "IP Binding is not possible when using a proxy.");
803 825
             }
804 826
 

Loading…
Cancel
Save