creating socket on specific IP address

A

Al the Pal

I'm sorry if this is a repost. I thought I posted this, but don't see
it on the board, so I'll try again.

Hi Geniuses,

Probably an easy one, but I can't find the answer.

I have a server with several IP addresses, and when I make a socket, I
want control over which IP address I connect from.

Right now I use this:

my $iaddr = Socket::inet_aton($server) || return 0;
my $paddr = Socket::sockaddr_in($port, $iaddr) || return 0;
my $proto = getprotobyname('tcp');
socket(TCP, Socket::pF_INET, Socket::SOCK_STREAM, $proto) || return 0;
connect(TCP, $paddr) || return 0;

How can I make this code force the local IP address to what I want?
Thanks in advance.

- Alex
 
C

chris-usenet

Al the Pal said:
I have a server with several IP addresses, and when I make a socket, I
want control over which IP address I connect from.

perldoc -f bind
How can I make this code force the local IP address to what I want?
Thanks in advance.

perldoc perlipc and look for the server example.

Chris
 
A

Al the Pal

Thanks for the reply, but I wan't clear when I asked the question.

Although this is located on a server, I am asking about a client
connection. The server has several IP addresses, but when I make an
outgoing connection, I want to choose the IP that I make the connection
on. I see nothing in the socket command that let's me choose which IP
the socket connects to locally.
 
A

Al the Pal

I understand now. I always thought of bind as a server only command,
related somehow to listen. I never saw it used in a client, so I just
didn't think it would work for that. I guess it's obvious, now that you
tell me.

Thanks.
 
M

Michele Dondi

I'm sorry if this is a repost. I thought I posted this, but don't see
it on the board, so I'll try again.

This is not a "board".
Hi Geniuses,

This is not likely to increase your chances of getting a helpful
answer.


Michele
 
T

Thomas Kratz

Al said:
Thanks for the reply, but I wan't clear when I asked the question.

Although this is located on a server, I am asking about a client
connection. The server has several IP addresses, but when I make an
outgoing connection, I want to choose the IP that I make the connection
on. I see nothing in the socket command that let's me choose which IP
the socket connects to locally.

Reading this and seeing your code snippet in the original post, I assume
you have a misconception of what opening a client connection over TCP does.
You can specify an destination IP address and a port. Nothing more. How
your system does this is beyond the control of your script.

What network interface is used for the connection is determined by the
routing information the IP stack is working with. You should be able to
have a look at the routing table with a 'netstat -r' (should work on
Unixes and Win32)

Thomas
--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
B

Brian McCauley

Thomas said:
[...] I assume
you have a misconception of what opening a client connection over TCP does.
You can specify an destination IP address and a port. Nothing more. How
your system does this is beyond the control of your script.

No, it would appear that you have the misconception.

When you open a TCP connection you can specify the source address and
port too if you want to. Of course you can only choose an IP address
that belongs to the box you are on.
What network interface is used for the connection is determined by the
routing information the IP stack is working with.

This is true. And the _default_ source IP address will usually be the
primary one assigned to the chosen interface. Although if you've got a
really rich IP stack semantic as say in Linux you can make the default
source address dependant on many factors. For example I have in the
past decided on one box that I want all TCP connections outbound to port
25 to default to a different source IP address.

None of this, of course, is related to Perl.
 
T

Thomas Kratz

Brian said:
Thomas said:
[...] I
assume you have a misconception of what opening a client connection
over TCP does.
You can specify an destination IP address and a port. Nothing more.
How your system does this is beyond the control of your script.


No, it would appear that you have the misconception.

When you open a TCP connection you can specify the source address and
port too if you want to. Of course you can only choose an IP address
that belongs to the box you are on.

You're right. I should have known better.
Wether you can reach the destination via the interface the address is
bound to, is another thing.
This is true. And the _default_ source IP address will usually be the
primary one assigned to the chosen interface. Although if you've got a
really rich IP stack semantic as say in Linux you can make the default
source address dependant on many factors. For example I have in the
past decided on one box that I want all TCP connections outbound to port
25 to default to a different source IP address.

None of this, of course, is related to Perl.

Of course, so EOT.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top