udplogger


# udplogger  - log the data coming from the picoweb on UDP/99

require 'subs.pl' ;

$cmd_port = $ENV{"CMDPORT"} || 99 ;
($addr,$func) = @ARGV ;

$AF_INET = 2 ;
$SOCK_STREAM = 1 ;
$SOCK_DGRAM = 2 ;
$INADDR_ANY = pack("N",0) ;
$SOCKADDR = "S n a4 x8" ;
($name,$aliases,$IPPROTO_UDP) = getprotobyname('udp') ;

$| = 1 ;

$listenaddr = pack($SOCKADDR,$AF_INET,$cmd_port,$INADDR_ANY) ;
#
# sockaddr representing the picoweb we're talking to.
#
$sendaddr = pack($SOCKADDR,$AF_INET,$cmd_port,&inet_addr($addr)) ;

socket(L,$AF_INET,$SOCK_DGRAM,$IPPROTO_UDP) || die "socket: $!" ;
bind(L,$listenaddr) || die "bind: $!" ;

for (;;)  {
    undef $rin ;
    for (;;)  {
        vec($rin,fileno(L),1) = 1 ;
        $n = select($rout=$rin,undef,undef,10.0) ;
        if (vec($rout,fileno(L),1))  {
            $n = recv(L,$buf,1024,0) ;
            last if (length($buf) == 0) ;
            $hdr = unpack("S",$buf) ;
            $buffer = substr($buf,2) ;
            #
            # note the header value is not logged - it should be
            # monotonically increasing for each packet - otherwise it
            # means data loss or that the server (this program or an
            # equivalent) was offline.
            #
            print "$buffer" ;
        }
    }
}

Back