pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPatch fo select ip and/or port for outgoing connections
2+ messages / 2 participants
[nested] [flat]
* Patch fo select ip and/or port for outgoing connections
@ 2015-08-15 17:16 Steffen Heil (Mailinglisten) <lists@steffen-heil.de>
2015-08-15 19:22 ` Re: Patch fo select ip and/or port for outgoing connections Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 2+ messages in thread
From: Steffen Heil (Mailinglisten) @ 2015-08-15 17:16 UTC (permalink / raw)
To: pgsql-jdbc
Hi
In our environment we had the requirement to enforce that (on a multihomed server) the database driver uses a certain local ip to connect to the database server.
In java this can easily be archived calling "bind" on the tcp socket before calling connect.
I patched the source of the jdbc driver to make this available through the connection properties "BIND_IP" and "BIND_PORT".
Both are optional, we are using "BIND_IP" in production code.
I attached the patch for everyone to use in any way.
(I am unsure about the contribution process and license requirements but you can use this code however you see fit).
I would be grateful, if this patch or another way to archive the same goal could be included in the next release...
Best regards,
Steffen
Attachments:
postgresql-9.4-1201-bind.patch (application/octet-stream, 2.5K) download
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;
}
=
smime.p7s (application/pkcs7-signature, 6.2K) download
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Patch fo select ip and/or port for outgoing connections
2015-08-15 17:16 Patch fo select ip and/or port for outgoing connections Steffen Heil (Mailinglisten) <lists@steffen-heil.de>
@ 2015-08-15 19:22 ` Dave Cramer <pg@fastcrypt.com>
0 siblings, 0 replies; 2+ messages in thread
From: Dave Cramer @ 2015-08-15 19:22 UTC (permalink / raw)
To: Steffen Heil (Mailinglisten) <lists@steffen-heil.de>; +Cc: pgsql-jdbc
Interesting, thanks for the patch.
Cheers,
Dave
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On 15 August 2015 at 13:16, Steffen Heil (Mailinglisten) <
lists@steffen-heil.de> wrote:
> Hi
>
>
> In our environment we had the requirement to enforce that (on a multihomed
> server) the database driver uses a certain local ip to connect to the
> database server.
> In java this can easily be archived calling "bind" on the tcp socket
> before calling connect.
>
> I patched the source of the jdbc driver to make this available through the
> connection properties "BIND_IP" and "BIND_PORT".
> Both are optional, we are using "BIND_IP" in production code.
>
> I attached the patch for everyone to use in any way.
> (I am unsure about the contribution process and license requirements but
> you can use this code however you see fit).
>
> I would be grateful, if this patch or another way to archive the same goal
> could be included in the next release...
>
>
> Best regards,
> Steffen
>
>
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2015-08-15 19:22 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-08-15 17:16 Patch fo select ip and/or port for outgoing connections Steffen Heil (Mailinglisten) <lists@steffen-heil.de>
2015-08-15 19:22 ` Dave Cramer <pg@fastcrypt.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox