setsockopt IP_MULTICAST_IF

Posted on 2003-12-11
Last Modified: 2008-03-10
How can I set this option on socket?
Question by:desktop2
  • 5
  • 3
LVL 18

Expert Comment

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

              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).

Author Comment

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

LVL 18

Expert Comment

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.

Author Comment

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?
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

LVL 18

Expert Comment

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?
LVL 18

Expert Comment

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

Author Comment

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?
LVL 18

Accepted Solution

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:

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;


Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

21 Experts available now in Live!

Get 1:1 Help Now