pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bob McConnell <rmcconne@lightlink.com>
To: Dimitris Sakellarios <dimitris.sakellarios@telesuite.gr>
Cc: pgsql-php@postgresql.org
Subject: Re: How to select the source ip address for a connection to the	database server
Date: Sun, 06 Sep 2009 15:35:32 -0400
Message-ID: <4AA40F04.3020908@lightlink.com> (raw)
In-Reply-To: <AEFA6E9872F74EAC83B4BDAA52FA5676@toshiba>
References: <20090902202318.hp0bplmn4kkc48k0@zeus.netdynamics.gr>
	<4AA30914.7080102@lightlink.com>
	<AEFA6E9872F74EAC83B4BDAA52FA5676@toshiba>

Dimitris Sakellarios wrote:
> Bob hi I would be very interested in giving me some small hint of how you
> did that in PERL so I can start from someplace in PHP.
> 
> BR.

Well, stripping it down to the basics, here is what I used:

-------------8<------------------------------
$port = 23 unless $port;
$them = 'localhost' unless $them;
$AF_INET = 2;
$SOCK_STREAM = 1;

$sockaddr = 'S n a4 x8';

($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp')
     unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) =
	gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
     print "socket ok\n";
}
else {
     die $!;
}

if (bind(S, $this)) {
     print "bind ok\n";
}
else {
     die $!;
}

if (connect(S,$that)) {
     print "connect ok\n";
}
else {
     die $!;
}
-------------8<------------------------------

The key is the variable $thisaddr. Put your server's DNS name or IP 
address as the parameter for the 'gethostbyname($hostname)' call that 
initializes it. That binds the outgoing connection to your IP. In my 
test platform I use "gethostbyname('10.3.1.70')", which binds it 
directly to one of the three network cards in that computer. The rest is 
pretty normal for a socket definition.

But as I said, this is pretty low level. It doesn't use any of the 
socket libraries or classes most people favor for Perl coding these 
days. If you need more ideas, I located this code again today doing a 
Google search on "Perl socket bind connect".

Good luck,

Bob McConnell
N2SPP



view thread (10+ messages)  latest in thread

Message-ID: <4AA40F04.3020908@lightlink.com>
Permalink:  ../4AA40F04.3020908@lightlink.com/
Also on:    postgresql.org/message-id/4AA40F04.3020908@lightlink.com

 · 

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-php@postgresql.org
  Cc: rmcconne@lightlink.com, dimitris.sakellarios@telesuite.gr
  Subject: Re: How to select the source ip address for a connection to the	database server
  In-Reply-To: <4AA40F04.3020908@lightlink.com>
  References: <20090902202318.hp0bplmn4kkc48k0@zeus.netdynamics.gr> <4AA30914.7080102@lightlink.com> <AEFA6E9872F74EAC83B4BDAA52FA5676@toshiba> <4AA40F04.3020908@lightlink.com>

* 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