pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Harrington <scotth01@sns-usa.com>
To: Michael Bauland <Michael.Bauland@knipp.de>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Defining local IP address when opening connections to database
Date: Wed, 7 Mar 2012 14:08:33 -0500 (Eastern Standard Time)
Message-ID: <alpine.WNT.2.00.1203071356590.6596@turks> (raw)
In-Reply-To: <4F578AB8.2050002@knipp.de>
References: <4F578AB8.2050002@knipp.de>

On Wed, 7 Mar 2012, Michael Bauland wrote:

> I've got a problem when using the PostgreSQL JDBC driver to open a
> connection to a database (not using SSL). The server on which I'm using
> the driver has several IPs configured, but I would like to use a certain
> IP (since only for that IP the firewalls have been set-up to allow the
> connection to the database). Looking at the documentation I couldn't
> find a way to define this local IP.
> Have I missed something? If not, would it be possible to add this
> feature of defining the local IP when setting up a connection?

You haven't missed anything. I had the same requirement a while ago and 
needed to patch (see attached) my local build to bind to a localAddress 
specified in the JDBC properties.

I find it useful to bind to a particular local address not only for 
routing but also simply to improve the connection logs.

Hope this helps.

Attachments:

pgjdbc-localAddress.patch (application/octet-stream, 5.4K) download Index: org/postgresql/core/v2/ProtocolConnectionImpl.java =================================================================== --- org/postgresql/core/v2/ProtocolConnectionImpl.java (revision 15188) +++ org/postgresql/core/v2/ProtocolConnectionImpl.java (revision 15200) @@ -90,7 +90,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); + cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); Index: org/postgresql/core/v2/ConnectionFactoryImpl.java =================================================================== --- org/postgresql/core/v2/ConnectionFactoryImpl.java (revision 15188) +++ org/postgresql/core/v2/ConnectionFactoryImpl.java (revision 15200) @@ -59,7 +59,7 @@ PGStream newStream = null; try { - newStream = new PGStream(host, port); + newStream = new PGStream(host, port, info.getProperty("localAddress")); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -160,7 +160,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getHost(), pgStream.getPort()); + return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress()); case 'N': if (logger.logDebug()) Index: org/postgresql/core/v3/ProtocolConnectionImpl.java =================================================================== --- org/postgresql/core/v3/ProtocolConnectionImpl.java (revision 15188) +++ org/postgresql/core/v3/ProtocolConnectionImpl.java (revision 15200) @@ -13,6 +13,7 @@ import org.postgresql.PGNotification; import org.postgresql.core.*; +import java.net.InetAddress; import java.sql.*; import java.io.IOException; import java.util.ArrayList; @@ -42,6 +43,10 @@ return pgStream.getPort(); } + public String getLocalAddress() { + return pgStream.getLocalAddress(); + } + public String getUser() { return user; } @@ -90,7 +95,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort()); + cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== --- org/postgresql/core/v3/ConnectionFactoryImpl.java (revision 15188) +++ org/postgresql/core/v3/ConnectionFactoryImpl.java (revision 15200) @@ -73,7 +73,7 @@ PGStream newStream = null; try { - newStream = new PGStream(host, port); + newStream = new PGStream(host, port, info.getProperty("localAddress")); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -189,7 +189,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getHost(), pgStream.getPort()); + return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress()); case 'N': if (logger.logDebug()) Index: org/postgresql/core/PGStream.java =================================================================== --- org/postgresql/core/PGStream.java (revision 15188) +++ org/postgresql/core/PGStream.java (revision 15200) @@ -16,6 +16,8 @@ import java.io.IOException; import java.io.EOFException; import java.io.Writer; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.Socket; import java.sql.*; @@ -34,6 +36,7 @@ { private final String host; private final int port; + private final String localAddress; private final byte[] _int4buf; private final byte[] _int2buf; @@ -52,14 +55,22 @@ * * @param host the hostname to connect to * @param port the port number that the postmaster is sitting on + * @param localAddress the local address to bind to (or null for "any local address") * @exception IOException if an IOException occurs below it. */ - public PGStream(String host, int port) throws IOException + public PGStream(String host, int port, String localAddress) throws IOException { this.host = host; this.port = port; + this.localAddress = localAddress; - changeSocket(new Socket(host, port)); + if (localAddress == null) { + changeSocket(new Socket(host, port)); + } + else { + InetAddress addr = InetAddress.getByName(localAddress); + changeSocket(new Socket(host, port, addr, 0)); + } setEncoding(Encoding.getJVMEncoding("US-ASCII")); _int2buf = new byte[2]; @@ -74,6 +85,10 @@ return port; } + public String getLocalAddress() { + return localAddress; + } + public Socket getSocket() { return connection; }

view thread (7+ messages)  latest in thread

Message-ID: <alpine.WNT.2.00.1203071356590.6596@turks>
Permalink:  ../alpine.WNT.2.00.1203071356590.6596@turks/
Also on:    postgresql.org/message-id/alpine.WNT.2.00.1203071356590.6596@turks

 ·  · 

reply

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: scotth01@sns-usa.com, Michael.Bauland@knipp.de
  Subject: Re: Defining local IP address when opening connections to database
  In-Reply-To: <alpine.WNT.2.00.1203071356590.6596@turks>
  References: <4F578AB8.2050002@knipp.de> <alpine.WNT.2.00.1203071356590.6596@turks>

* 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