#!/usr/bin/perl

#
# poor man's multicast transmission test...
#
# This doesn't try to join the multicast group, so we
# can avoid the need for the IO::Socket::Multicast module.
#
# this sends the following mDNS answer packet:
#   _multicast_test.local TXT "hello, network!"
#

use IO::Socket;
use strict;

my $sock = IO::Socket::INET->new(
	Proto => 'udp',
	PeerPort => 5353,
	PeerAddr => '224.0.0.251',
) || die "cannot create socket: $!\n";

my $header = pack(
	'nnnnnn',
	0,		# message-id
	0,		# flags
	0,		# question count (qdcount)
	1,		# answer count (ancount)
	0,		# nscount
	0		# arcount
);

my $host = "_multicast_test.local";
my $txtdata = "hello, network!";

my $name;
my @labels = split(/\./, $host);
foreach my $label (@labels) {
	$name.=pack('C',length($label)).$label;
}
$name .= pack('C',0);
my $rdata = pack('C',length($txtdata)).$txtdata;

my $answer =
	$name .
	pack('n', 16) . # TXT type
	pack('n', 1)  . # IN class
	pack('N', 0)  . # TTL 0
	pack('n',length($rdata)).$rdata;


$sock->send($header.$answer);

