Solved

setsockopt IP_MULTICAST_IF

Posted on 2003-12-11
8
2,645 Views
Last Modified: 2008-03-10
How can I set this option on socket?
IP_MULTICAST_IF
0
Comment
Question by:desktop2
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 9923978
To make your life a whole lot easier, I'd suggest you install IO::Socket::Multicast as it contains all the things you need nicely wrapped into object methods.

The low level details are:
   The level for this option is SOL_IP
   Value is a ip_mreqn structure

In other words, you would call it as
   setsockopt $socket. SOL_IP, IP_MULTICAST_IF, $ip_mreqn
where $socket is your socket, and $ip_mreqn is the structure mentioned above (packed of course).

Quoting 'man ip':

 IP_MULTICAST_IF
              Set the local device for a multicast socket.  Argu­ment is an ip_mreqn
              or ip_mreq structure similar to IP_ADD_MEMBERSHIP.

Over there I read:
       IP_ADD_MEMBERSHIP
              Join  a  multicast  group.  Argument  is  a  struct
              ip_mreqn structure.

              struct ip_mreqn {
                  struct in_addr imr_multiaddr; /* IP multicast group address */
                  struct in_addr imr_address;   /* IP address of local interface */
                  int            imr_ifindex;   /* interface index */
              };

              imr_multiaddr contains the address of the multicast
              group the application wants to join or  leave.   It
              must  be a valid multicast address.  imr_address is
              the address of the local interface with  which  the
              system  should  join  the multicast group; if it is
              equal to INADDR_ANY  an  appropriate  interface  is
              chosen by the system.  imr_ifindex is the interface
              index of the interface that should  join/leave  the
              imr_multiaddr   group,   or   0   to  indicate  any
              interface.

              For compatibility, the  old  ip_mreq  structure  is
              still  supported.  It differs from ip_mreqn only by
              not including the imr_ifindex field. Only valid  as
              a setsockopt(2).
0
 

Author Comment

by:desktop2
ID: 9925487
Could you please show how should I pack "ip_mreqn  $ip_mreqn" before calling setsockopt
Still having problem with it :-(

0
 
LVL 18

Expert Comment

by:kandura
ID: 9927515
assuming you have obtained the in_addr parts of the structure through sockaddr_in or something like that, you would do

$packed = pack "NNI", $imr_multiaddr, $imr_address, $imr_ifindex;

N   An unsigned long in "network" (big-endian) order.
I   An unsigned integer value.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:desktop2
ID: 9928385
socket(Sock, PF_INET, SOCK_DGRAM, 0);
bind(Sock, sockaddr_in(0, INADDR_ANY)); # use first free port
setsockopt(Sock, SOL_IP, IP_MULTICAST_IF, $IpPacked) or DebugOut("Error setsockopt $!\n");

It returns error "Protocol not available"

Does it mean that network does not support multicast?
0
 
LVL 18

Expert Comment

by:kandura
ID: 9928603
No, it probably means that the constants SOL_IP and IP_MULTICAST_IF are not exported from Socket.

I dug a little deeper (with the help of the source of IO::Socket::Multicast), and the standard include files on my system.
SOL_IP is defined as 0, and IP_MULTICAST_IF is defined as 32.

Either try the setsockopt call with those values, or follow my original recommendation to use IO::Socket::Multicast.

Does your operating system support multicast?
0
 
LVL 18

Expert Comment

by:kandura
ID: 9928794
By the way, is there a specific reason why you're writing all this code yourself instead of using IO::Socket::Multicast?
0
 

Author Comment

by:desktop2
ID: 9929030
It worked OK, thanks.
but I still have problem with
setsockopt(Sock, 0, 3, pack("C", 80)) or DebugOut("Error setsockopt(IP_MULTICAST_TTL) $!\n");

same error :-(

I'm not sure if Apache where I'm hosting my web site support multicast or not...
Where can I download this module IO:Socket::Multicast?
0
 
LVL 18

Accepted Solution

by:
kandura earned 125 total points
ID: 9929137
I hope it's just a typo, because on my system IP_MULTICAST_TTL is defined as 33 (not the 3 you have in that call).

If your website is hosted on a linux machine, then you most likely have multicast support. It's not a property of Apache though, it's an OS feature.

You can find IO::Socket::Multicast on cpan:
http://search.cpan.org/author/LDS/IO-Socket-Multicast-0.21/Multicast.pm

If you have shell access to the web server, then you should be able to install it with

    perl -MCPAN -e shell

and then typing
    install IO::Socket::Multicast

If you don't have root access (most likely if this is a shared account), then you may be able to install modules into a directory you own, by setting makepl_arg to an appropriate value (within that CPAN session) by doing

o conf makepl_arg  PREFIX=~/cgi-bin LIB=~/cgi-bin/lib INSTALLMAN1DIR=~/cgi-bin/man/man1 INSTALLMAN3DIR=~/cgi-bin/man/man3

(that's one line, and it means that new modules get installed into your ~/cgi-bin/lib directory. Do it before you install anything.)

Provided that that all works, you would then be able to use those modules by first adding this line to your scripts:

use lib '/path/to/cgi-bin/lib';

and then
use IO::Socket::Multicast;

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…

831 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