#! /usr/bin/perl -w
use strict;
use Socket; # so we can broadcast on udp

# Eventually to be a deamon to broadcast our name/ip info once in awhile.
# Some other daemon will be listening and updating its /etc/hosts as required

#****************************************globals
my $debug = 1; # make zero for daemon so we won't print stuff
my $name; # my name on the ethernet
my $ipaddr; # my ip on the lan
my $msgsock; # the socket
my $portno = 53831; # birthday port
my $dest = sockaddr_in($portno, inet_aton("192.168.1.255"));
my $msg; # what I'm going to broadcast

#****************************************
sub mynetinfo ()
{
# some of this is easier, some harder than for the other machines on the net
 open (MYNAME, "hostname |") or die "Can't run hostname utility: $!";
 $name = <MYNAME>; # note, chomp to get rid of /n
 chomp $name;
 close (MYNAME) or die "Can't close hostname utility: $!";
 
 open (MYIP, "hostname -I |") or die "Can't run hostname -I:$!";
 $ipaddr = <MYIP>;
 chomp $ipaddr;
 close (MYIP) or die "Can't close hostname -I";
 $msg = "$ipaddr $name";
}
#****************************************
sub setupsock()
{
 socket($msgsock,PF_INET,SOCK_DGRAM,getprotobyname("udp")) or die "socket:$!";
 setsockopt($msgsock, SOL_SOCKET, SO_BROADCAST,1) or die "setsockopt:$!";
}
########################### main

setupsock(); # setup socket we'll broadcast from
while (1)
{
 mynetinfo(); # set up/refresh my own net information
 print ("msg:$msg\n") if $debug;
 send($msgsock,"$msg",0,$dest) or die "send:$!";

 sleep (int(rand(31)) + 45); # don't flood network or use much machine either

}
