UDP_socket bind() question

I'm trying to create UDP server...
When I run my perl script it has one IP address, but when I create UDP_socket and bind() it to INADDR_ANY it get binded to another IP address.
How can I detect to which address it is binded?

socket(Server, PF_INET, SOCK_DGRAM, $proto) or DebugOut("Error in socket(): $!\n");
bind(Server, sockaddr_in($serverPort, INADDR_ANY)) or zDebugOut("error bind");
desktop2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kanduraCommented:
From perlipc manpage:
"We'll leave the address as INADDR_ANY so that the kernel can choose the appropriate interface on multihomed hosts. If you want sit on a particular interface (like the external side of a gateway or firewall machine), you should fill this in with your real address instead."

But why don't you do this:

use IO::Socket::INET;

my $serverAddr = '192.168.0.100';  # socket binds to this address
my $sock = IO::Socket::INET->new(
                                 LocalAddr => $serverAddr,
                                 LocalPort => $serverPort,
                                 Proto     => 'udp') or die $!;

$sock->accept() .... etc

HTH,
Kandura
0
desktop2Author Commented:
It should work... but on one particular host only
I think it should be possible to detect IP address where socket is binded...
0
kanduraCommented:
yes, you can:

# get local socket address
my $host = $sock->sockaddr();

# unpack the address
my $addr = inet_ntoa($host);
# unpack the name
my $name = gethostbyaddr($host,AF_INET);

print "local connection on $name [ $addr ] at port $serverPort";


This also works if you leave LocalAddr undefined (which is the equivalent of INADDR_ANY).

Note that when you bind to INADDR_ANY, then it is accepting connections on all interfaces.
This means that you should get the local address only on an established connection.

Kandura
0
desktop2Author Commented:
I did reply couple days ago but probably something was wrong at this web site so I'm posting reply again.. sorry for delay

This is UDP socket so actually there are no established connection :-(

Can I use it with my code? ... it is somewhat different from code you posted
socket(Server, PF_INET, SOCK_DGRAM, $proto) or DebugOut("Error in socket(): $!\n");
bind(Server, sockaddr_in($serverPort, INADDR_ANY)) or DebugOut("error bind");
my $host = sockaddr(Server);
- I've got an error "Undefined subroutine &main::sockaddr "

0
kanduraCommented:
Hi desktop2,

I may be out of my depth on this, or it's simply not possible with the regular socket functions.
As you already pointed out, udp is a connectionless protocol, so there is no connection information.
Instead, you would have to get the information you want at a lower level (since the destination address
(which is the local address you're after) is not a part of a UDP message). You would have to look at the
header of the IP packets that encapsulate the UDP messages.

I came across a reference today that might be of interest:

"Available on CPAN, the Socket::MsgHdr modules provides advanced socket
messaging -- sendmsg(2), recvmsg(2) and ancillary data manipulation.

This module tries to make the interface familiar but palatable:

  - sendmsg/recvmsg are plain functions or IO::Socket methods
  - msghdr structures become Socket::MsgHdr objects
  - cmsghdr un/packing and alignment are handled by an object method

What good is this?  sendmsg and recvmsg have all the capabilities of
send/sendto and recv/recvfrom, plus ancillary data access.  Depending
on the particulars of your system, ancillary data allows:

  - Passing filehandles between unrelated processes
    (a la I_SENDFD under streams)
  - IPv6 IP option manipulation
  - Enhanced SOCK_RAW, SOCK_DGRAM information
  - Querying socket peer credentials
    (a la getpeereuid())
  - ... even more"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.