Solved

UDP_socket bind() question

Posted on 2003-11-03
5
937 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do I grep for a specfic DNS record in a directory 3 42
perl to mysql 5 130
Extract multiple value with delimiters from a string 4 168
syslog unix file 20 68
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

15 Experts available now in Live!

Get 1:1 Help Now