diff --git a/org/postgresql/Driver.java.in b/org/postgresql/Driver.java.in index f6847f0..bb8a7ef 100644 --- a/org/postgresql/Driver.java.in +++ b/org/postgresql/Driver.java.in @@ -611,9 +611,11 @@ public class Driver implements java.sql.Driver { String[] hosts = props.getProperty("PGHOST").split(","); String[] ports = props.getProperty("PGPORT").split(","); + String bindIp = props.getProperty("BIND_IP"); + int bindPort = Integer.parseInt(props.getProperty("BIND_PORT","0")); HostSpec[] hostSpecs = new HostSpec[hosts.length]; for (int i = 0; i < hostSpecs.length; ++i) { - hostSpecs[i] = new HostSpec(hosts[i], Integer.parseInt(ports[i])); + hostSpecs[i] = new HostSpec(bindIp, bindPort, hosts[i], Integer.parseInt(ports[i])); } return hostSpecs; } diff --git a/org/postgresql/core/PGStream.java b/org/postgresql/core/PGStream.java index 530caf2..5243ced 100644 --- a/org/postgresql/core/PGStream.java +++ b/org/postgresql/core/PGStream.java @@ -58,6 +58,8 @@ public class PGStream this.hostSpec = hostSpec; Socket socket = new Socket(); + if ( hostSpec.getBindIp() != null || hostSpec.getBindPort() != 0 ) + socket.bind(new InetSocketAddress(hostSpec.getBindIp(), hostSpec.getBindPort())); socket.connect(new InetSocketAddress(hostSpec.getHost(), hostSpec.getPort()), timeout); changeSocket(socket); setEncoding(Encoding.getJVMEncoding("US-ASCII")); diff --git a/org/postgresql/util/HostSpec.java b/org/postgresql/util/HostSpec.java index d3780cf..50a5dfb 100644 --- a/org/postgresql/util/HostSpec.java +++ b/org/postgresql/util/HostSpec.java @@ -11,15 +11,35 @@ package org.postgresql.util; * Simple container for host and port. */ public class HostSpec { + protected final String bindIp; + protected final int bindPort; protected final String host; protected final int port; public HostSpec(String host, int port) { + this.bindIp = null; + this.bindPort = 0; this.host = host; this.port = port; } + public HostSpec(String bindIp, int bindPort, String host, int port) + { + this.bindIp = bindIp; + this.bindPort = bindPort; + this.host = host; + this.port = port; + } + + public String getBindIp() { + return bindIp; + } + + public int getBindPort() { + return bindPort; + } + public String getHost() { return host; }