Solved

UDP_socket bind() question

Posted on 2003-11-03
5
938 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
ID: 9675310
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
ID: 9675747
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
ID: 9677208
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
ID: 9694381
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
ID: 9736089
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 151
Perl Sort Question 4 136
Extract data from span tag 1 92
Perl Untar File 1 45
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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