From: Steffen Heil (Mailinglisten) <lists@steffen-heil.de>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Patch fo select ip and/or port for outgoing connections
Date: Sat, 15 Aug 2015 17:16:11 +0000
Message-ID: <3bdf77beeaa149aeb8972af5b586271b@exchange.sh-solutions.de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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) downloaddiff --git a/org/postgresql/Driver.java.in b/org/postgresql/Driver.java.inindex 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.javaindex 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.javaindex 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: lists@steffen-heil.de
Subject: Re: Patch fo select ip and/or port for outgoing connections
In-Reply-To: <3bdf77beeaa149aeb8972af5b586271b@exchange.sh-solutions.de>
References: <3bdf77beeaa149aeb8972af5b586271b@exchange.sh-solutions.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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