Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 429AD634323 for ; Sun, 6 Sep 2009 16:35:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 67104-05 for ; Sun, 6 Sep 2009 19:35:43 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from emerald.lightlink.com (emerald.lightlink.com [64.57.176.19]) by mail.postgresql.org (Postfix) with ESMTP id 6027B634271 for ; Sun, 6 Sep 2009 16:35:43 -0300 (ADT) Received: from [172.16.10.72] (cpe-24-58-18-216.twcny.res.rr.com [24.58.18.216]) by emerald.lightlink.com (8.9.3p2/8.8.8) with ESMTP id PAA01730; Sun, 6 Sep 2009 15:35:32 -0400 Message-ID: <4AA40F04.3020908@lightlink.com> Date: Sun, 06 Sep 2009 15:35:32 -0400 From: Bob McConnell User-Agent: Thunderbird 2.0.0.23 (X11/20090812) MIME-Version: 1.0 To: Dimitris Sakellarios CC: pgsql-php@postgresql.org Subject: Re: How to select the source ip address for a connection to the database server References: <20090902202318.hp0bplmn4kkc48k0@zeus.netdynamics.gr> <4AA30914.7080102@lightlink.com> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/8 X-Sequence-Number: 4066 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