Solved

UDP_socket bind() question

Posted on 2003-11-03
5
936 Views
Last Modified: 2008-03-03
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");
0
Comment
Question by:desktop2
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
 

Author Comment

by:desktop2
Comment Utility
It should work... but on one particular host only
I think it should be possible to detect IP address where socket is binded...
0
 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
 

Author Comment

by:desktop2
Comment Utility
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
 
LVL 18

Accepted Solution

by:
kandura earned 125 total points
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now