setsockopt IP_MULTICAST_IF

How can I set this option on socket?
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.

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':

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

              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).
desktop2Author Commented:
Could you please show how should I pack "ip_mreqn  $ip_mreqn" before calling setsockopt
Still having problem with it :-(

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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

desktop2Author Commented:
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?
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?
By the way, is there a specific reason why you're writing all this code yourself instead of using IO::Socket::Multicast?
desktop2Author Commented:
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?
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:

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;

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

From novice to tech pro — start learning today.