iphony has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I came across this "IO::Socket::Multicast" in CPAN and my goal is to "tunnel" multicast packets across unicast network and then finally converting them back to multicast. For the multicast to unicast portion, I managed to use the above module to convert multicast packets to unicast address.
#!/usr/bin/perl use IO::Socket::Multicast; use strict; my $multicast_ip = '232.0.64.4'; my $multicast_port = '9100'; my $if = 'eth1'; my $in_sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>$mult +icast_port); $in_sock->mcast_add($multicast_ip, $if) || die "\nCouldn't set group: +$!\n"; my $dest_ip = '10.10.20.20:9100'; my $out_sock = IO::Socket::Multicast->new(Proto=>'udp',PeerAddr=>$dest +_ip); while (1) { my $data; next unless $in_sock->recv($data,1024); $out_sock->send($data) }
However, I need to listen and forward packets on 30 different ports. Question is how do I efficiently create 30 instance of the below code without typing them out line by line? Second question is, are there better ways to achieve this multicast tunneling in Perl without using any 3rd party tunneling applications?
my $in_sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>$mult +icast_port);
Thanks.

Replies are listed 'Best First'.
Re: Multicast to Unicast to Multicast
by zwon (Abbot) on Jan 09, 2009 at 11:18 UTC

    Use loop:

    my @m_ports = (1111,2222,3333,4444); my @in_sockets; for (@m_ports) { my $in_sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>$ +_); push @in_sockets, $in_sock if $in_sock; }