-- Echo_String - Provide a trivial UDP echo server from a fixed port.
-- Copyright (C) 2022,2023 Prince Trippy <programmer@verisimilitudes.net>.

-- This program is free software: you can redistribute it and/or modify it under the terms of the
-- GNU Affero General Public License version 3 as published by the Free Software Foundation.

-- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
-- even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- See the GNU Affero General Public License for more details.

-- You should have received a copy of the GNU Affero General Public License along with this program.
-- If not, see <http://www.gnu.org/licenses/>.

with Interfaces.C, Posix_UDP_Garbage; use Interfaces.C, Posix_UDP_Garbage;

procedure Echo_String is
   A : String(1 .. 512) := (others => ' ');
   S : Sockaddr := Make_Sockaddr(Address => 0, Port => 7);
   F : Int;
   I : Size_T;
begin
   begin
      Socket(F);
      Bind(Socket => F, Socket_Address => S);
      loop
         begin -- Notice that, by starting it from one, a length of zero results in that null array.
            Recvfrom(Socket => F, Buffer => A, Socket_Address => S, Length => I);
            Sendto(Socket => F, Buffer => A(1 .. Integer(I)), Socket_Address => S);
         exception
            when others => null;
         end;
      end loop;
   exception
      when others => null;
   end;
   Close(F);
end Echo_String;