#!/usr/bin/perl

# This script is to be run on the web-server, sending out multicast
# beacons containing the IP address of its associated multicast
# interface.  Which you allow a workstation recieving those requests
# to correctly identify where to point the browser.

use strict;
use IO::Interface::Simple;
use IO::Socket::Multicast;

$|++;
my $dest = '226.1.1.2:2000'; 
my $IPs;
my $socks;

($IPs, $socks) = initsocks();

while (1) {
	my $error = 0;
	foreach my $int (keys %{$socks}) {
		my $mess = 'abc' . $IPs->{$int} . 'cba';
		$socks->{$int}->send($mess) || $error++;
	}
	($IPs, $socks) = initsocks() if ($error);
	sleep 10;
}

sub initsocks {
	my %IPs;
	my @ints = IO::Interface::Simple->interfaces;
	foreach my $int (@ints) {
		if ($int =~ /^e/) {
			my $ip = IO::Interface::Simple->new($int);
			$IPs{$int} = $ip->address if ($ip->address);
		}
	}

	my %socks;
	foreach my $int (keys %IPs) {
		$socks{$int} = IO::Socket::Multicast->new(Proto=>'udp',PeerAddr=>$dest);
		$socks{$int}->mcast_if($int);
	}

	print STDERR "webIPbroadcast: (Re)Init called.\n";
	return (\%IPs, \%socks);
}
