! Gateway code for INet process, GDMR, May 1987
! This version uses static tables, inintialised at boot time.

%conststring(31) gateway database = "INET:GATEWAYS.DAT"

%recordformat gateway fm(%integer network, gateway)
%constinteger max gateway = 32
%ownrecord(gateway fm)%array gateway table(1 : max gateway) = 0(*)
%owninteger last gateway = 0

%record(peer info fm)%map find gateway(%integer net)
   %integer i
      !! printstring("Looking for gateway to ")
      !! print INet address(net);  newline
      %result == nil %if last gateway = 0
      %for i = 1, 1, last gateway %cycle
         %if gateway table(i)_network = net %start
            !! printstring("Gated via ")
            !! print INet address(gateway table(i)_gateway);  newline
            %result == find peer info(gateway table(i)_gateway)
         %finish
      %repeat
      %result == nil
%end

%routine read gateway address(%integername a)
   %integer i
      skip symbol %while %not '0' <= next symbol <= '9';  read(a)
      skip symbol %while %not '0' <= next symbol <= '9';  read(i);  a = a << 8 + i
      skip symbol %while %not '0' <= next symbol <= '9';  read(i);  a = a << 8 + i
      skip symbol %while %not '0' <= next symbol <= '9';  read(i);  a = a << 8 + i
%end

%routine read gateway name(%string(*)%name name)
   %integer ch
      skip symbol %while next symbol <= ' '
      name = ""
      %cycle
         read symbol(ch)
         name = name . to string(ch)
      %repeat %until next symbol <= ' '
%end

%routine setup gateways
   %record(peer info fm)%name p
   %record(gateway fm)%name g
   %string(31) name
   %integer which, opened = 0
      %on 3, 9 %start
         %if opened # 0 %start
close:      close input
            select input(0)
         %finish
         event = 0;  ! Suppress spurious exit-handler message
         %return
      %finish
      open input(2, gateway database);  select input(2)
      opened = 1
      %cycle
         which = last gateway + 1
         -> close %if which > max gateway
         g == gateway table(which)
         read gateway address(g_network)
         read gateway name(name)
         g_gateway = INet name to address(name)
         pdate
         print INet address(g_network);  printstring(" gated via ")
         printstring(name);  printstring(" at ")
         print INet address(g_gateway);  newline
         %if g_gateway # 0 %start
            last gateway = which
            ! Ensure that the peer slot exists
            p == find peer info(g_gateway)
            p == new peer info(g_gateway) %if p == nil
         %finish
      %repeat
%end

%end %of %file
