pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Defining local IP address when opening connections to database
7+ messages / 6 participants
[nested] [flat]

* Defining local IP address when opening connections to database
@ 2012-03-07 16:20  Michael Bauland <Michael.Bauland@knipp.de>
  0 siblings, 4 replies; 7+ messages in thread

From: Michael Bauland @ 2012-03-07 16:20 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

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?

Cheers,

Michael



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 16:40  Dave Cramer <pg@fastcrypt.com>
  parent: Michael Bauland <Michael.Bauland@knipp.de>
  3 siblings, 0 replies; 7+ messages in thread

From: Dave Cramer @ 2012-03-07 16:40 UTC (permalink / raw)
  To: Michael Bauland <Michael.Bauland@knipp.de>; +Cc: pgsql-jdbc

Michael,

The URL can take an ip address ie jdbc:postgresql://127.0.0.1/dbname

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca



On Wed, Mar 7, 2012 at 11:20 AM, Michael Bauland
<Michael.Bauland@knipp.de> wrote:
> Hi,
>
> 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?
>
> Cheers,
>
> Michael
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 16:41  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Michael Bauland <Michael.Bauland@knipp.de>
  3 siblings, 0 replies; 7+ messages in thread

From: Kevin Grittner @ 2012-03-07 16:41 UTC (permalink / raw)
  To: Michael Bauland <Michael.Bauland@knipp.de>; pgsql-jdbc

Michael Bauland <Michael.Bauland@knipp.de> 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?
 
You probably either need to read this:
 
http://www.postgresql.org/docs/current/interactive/runtime-config-connection.html#RUNTIME-CONFIG-CON...
 
or this:
 
http://jdbc.postgresql.org/documentation/head/connect.html
 
-Kevin



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 16:49  David Johnston <polobo@yahoo.com>
  parent: Michael Bauland <Michael.Bauland@knipp.de>
  3 siblings, 1 reply; 7+ messages in thread

From: David Johnston @ 2012-03-07 16:49 UTC (permalink / raw)
  To: 'Michael Bauland' <Michael.Bauland@knipp.de>; pgsql-jdbc


> -----Original Message-----
> From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-
> owner@postgresql.org] On Behalf Of Michael Bauland
> Sent: Wednesday, March 07, 2012 11:20 AM
> To: pgsql-jdbc@postgresql.org
> Subject: [JDBC] Defining local IP address when opening connections to
> database
> 
> Hi,
> 
> 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?
> 
> Cheers,
> 
> Michael

Are you missing the obvious, in which case read Kevin's and Dave's
responses, or do you mean that on the "client" machine (i.e., the one using
JDBC) you have multiple network adapters and only one of them is allowed to
connect to the database?

If the latter is the case I am not sure about JDBC but I would presume you
could setup some form of routing table on your machine so that any traffic
to the server IP address is forced to be routed through the appropriate
network card.

David J.






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 17:04  Michael Bauland <Michael.Bauland@knipp.de>
  parent: David Johnston <polobo@yahoo.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Bauland @ 2012-03-07 17:04 UTC (permalink / raw)
  To: pgsql-jdbc

Hi David,

thanks for your quick reply.


> Are you missing the obvious, in which case read Kevin's and Dave's
> responses, or do you mean that on the "client" machine (i.e., the one using
> JDBC) you have multiple network adapters and only one of them is allowed to
> connect to the database?
> 
> If the latter is the case I am not sure about JDBC but I would presume you
> could setup some form of routing table on your machine so that any traffic
> to the server IP address is forced to be routed through the appropriate
> network card.

yes, I was referring to the latter. I have a client machine which as
several network adapters, but I would like to always have the connection
to the database using exactly one of the IPs available to my client. I
couldn't find a way to specify this IP.

Yes, the routing table might be one solution. But in my case we're
actually having two services running on the client, and both connect to
the same database. And both services should use a different IP. In which
case the routing table won't help. There doesn't seem to be a way of
doing this with JDBC, is there? Since on
http://jdbc.postgresql.org/documentation/head/connect.html
there are a few connection parameters, but none that lets me set my
local IP for the connection.

Cheers,

Michael



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 19:08  Scott Harrington <scotth01@sns-usa.com>
  parent: Michael Bauland <Michael.Bauland@knipp.de>
  3 siblings, 0 replies; 7+ messages in thread

From: Scott Harrington @ 2012-03-07 19:08 UTC (permalink / raw)
  To: Michael Bauland <Michael.Bauland@knipp.de>; +Cc: pgsql-jdbc

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; }
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Defining local IP address when opening connections to database
@ 2012-03-07 19:55  John R Pierce <pierce@hogranch.com>
  parent: Michael Bauland <Michael.Bauland@knipp.de>
  0 siblings, 0 replies; 7+ messages in thread

From: John R Pierce @ 2012-03-07 19:55 UTC (permalink / raw)
  To: pgsql-jdbc

On 03/07/12 9:04 AM, Michael Bauland wrote:
> yes, I was referring to the latter. I have a client machine which as
> several network adapters, but I would like to always have the connection
> to the database using exactly one of the IPs available to my client. I
> couldn't find a way to specify this IP.
>
> Yes, the routing table might be one solution. But in my case we're
> actually having two services running on the client, and both connect to
> the same database. And both services should use a different IP. In which
> case the routing table won't help. There doesn't seem to be a way of
> doing this with JDBC, is there? Since on
> http://jdbc.postgresql.org/documentation/head/connect.html
> there are a few connection parameters, but none that lets me set my
> local IP for the connection.

what is the point of multiple network adapters connecting to the same 
network other than MPIO style ethernet link bonding (whereupon the 
physical adapters would appear as a single logical ethernet endpoint)

IP networks route by destination address.  if there's multiple paths to 
the same destination that are all the same 'length', the decision as to 
which path to use is completely arbitrary.   putting different firewall 
rules on different interfaces that are all on the same subnet will just 
cause some VERY hard to debug problems.



-- 
john r pierce                            N 37, W 122
santa cruz ca                         mid-left coast




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2012-03-07 19:55 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-03-07 16:20 Defining local IP address when opening connections to database Michael Bauland <Michael.Bauland@knipp.de>
2012-03-07 16:40 ` Dave Cramer <pg@fastcrypt.com>
2012-03-07 16:41 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-03-07 16:49 ` David Johnston <polobo@yahoo.com>
2012-03-07 17:04   ` Michael Bauland <Michael.Bauland@knipp.de>
2012-03-07 19:55     ` John R Pierce <pierce@hogranch.com>
2012-03-07 19:08 ` Scott Harrington <scotth01@sns-usa.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