%include "INet:Formats.Inc"
%include "INet:INetAddrs"

%externalpredicatespec FS lookup(%string(31) what, %integername value)

%routine print INet address(%integer addr)
   write(addr >> 24 & 255, 0);  print symbol('.')
   write(addr >> 16 & 255, 0);  print symbol('.')
   write(addr >>  8 & 255, 0);  print symbol('.')
   write(addr       & 255, 0)
%end

%routine print peer(%string(*)%name name, %record(peer info fm)%name p)
   printstring(name);  printstring(" is at ")
   print INet address(p_address)
   newlines(2)
write(p_IP packets in,               -8);  printstring(" IP in     ")
write(p_IP bytes in,                 -8);  printstring(" bytes in  ")
write(p_IP old broadcasts,           -8);  printstring(" old broad ")
write(p_IP new broadcasts,           -8);  printstring(" new broad" );  newline
write(p_IP fragmented packets,       -8);  printstring(" fragmented")
write(p_IP dud lengths,              -8);  printstring(" dud length")
write(p_IP dud protocols,            -8);  printstring(" dud prot  ")
write(p_IP other errors,             -8);  printstring(" other errs");  newline
write(p_IP packets out,              -8);  printstring(" pckts out ")
write(p_IP bytes out,                -8);  printstring(" bytes out" );  newline
newline

write(p_ICMP in,                     -8);  printstring(" ICMP in   ")
write(p_ICMP errors in,              -8);  printstring(" errors in ")
write(p_ICMP broadcasts,             -8);  printstring(" broadcasts");  newline
write(p_ICMP out,                    -8);  printstring(" ICMP out  ")
write(p_ICMP errors out,             -8);  printstring(" errors out");  newline
newline

write(p_TCP packets in,              -8);  printstring(" TCP in    ")
write(p_TCP bytes in,                -8);  printstring(" bytes in  ")
write(p_TCP packets out,             -8);  printstring(" pckts out ")
write(p_TCP bytes out,               -8);  printstring(" bytes out" );  newline
write(p_TCP bogus addresses,         -8);  printstring(" bogus adrs")
write(p_TCP checksum errors,         -8);  printstring(" chck errs ")
write(p_TCP for closed,              -8);  printstring(" for closed")
write(p_TCP resets received,         -8);  printstring(" resets"    );  newline
write(p_TCP junk received,           -8);  printstring(" junk      ")
write(p_TCP SYNs received,           -8);  printstring(" SYNs      ")
write(p_TCP FINs received,           -8);  printstring(" FINs"      );  newline
write(p_TCP good ACKs received,      -8);  printstring(" good ACKs ")
write(p_TCP old ACKs received,       -8);  printstring(" old ACKs  ")
write(p_TCP dud ACKs received,       -8);  printstring(" dud ACKs  ")
write(p_TCP window updates,          -8);  printstring(" window ups");  newline
write(p_TCP connections established, -8);  printstring(" estblished")
write(p_TCP connections reset,       -8);  printstring(" reset     ")
write(p_TCP connections closed,      -8);  printstring(" closed"    );  newline
write(p_TCP acceptable segments,     -8);  printstring(" acceptable")
write(p_TCP unacceptable segments,   -8);  printstring(" unaccptble")
write(p_TCP ahead segments,          -8);  printstring(" ahead     ")
write(p_TCP duplicate bytes,         -8);  printstring(" duplicate" );  newline
write(p_TCP data bytes received,     -8);  printstring(" data bytes")
write(p_TCP data bytes sent,         -8);  printstring(" data sent ")
write(p_TCP retransmits,             -8);  printstring(" reTXs     ")
write(p_TCP retransmit timeouts,     -8);  printstring(" reTX tmos" );  newline
newline

write(p_UDP packets in,              -8);  printstring(" UDP in    ")
write(p_UDP bytes in,                -8);  printstring(" bytes in  ");  newline
write(p_UDP checksum errors,         -8);  printstring(" check errs")
write(p_UDP no checksums,            -8);  printstring(" no check  ")
write(p_UDP bogus checksums,         -8);  printstring(" bogus chck")
write(p_UDP other errors,            -8);  printstring(" other errs");  newline
write(p_UDP packets out,             -8);  printstring(" pckts out ")
write(p_UDP bytes out,               -8);  printstring(" bytes out" );  newline
newline
%end

%begin
   %record(peer table fm)%name peer table
   %record(peer info fm)%name peer
   %string(127) host
   %integer x, i
      %on 9 %start;  %stop;  %finish
      %if FS lookup(peer table name, x) %start
         peer table == record(x)
      %else
         printstring("No peer table?");  newline
         %stop
      %finish
      host = CLI param
      %if host = "" %start
         prompt("Peer: ");  read(host) %until host # ""
      %finish
      x = INet name to address(host)
      %if x = 0 %start
         printstring(host);  printstring(" unknown");  newline
         %stop
      %finish
      %for i = 1, 1, peer table_slots used %cycle
         peer == peer table_slot(i)
         %if peer_address = x %start
            print peer(host, peer)
            %exit
         %finish
      %repeat
%end %of %program
