tree checksum vpatch file split hunks
all signers: asciilifeform diana_coman
antecedents:
press order: 
| udp_genesis | asciilifeform diana_coman | 
patch: 
(0 . 0)(1 . 7)
  5 cd echodemo
  6 gprbuild
  7 
  8 cd txdemo
  9 gprbuild
 10 
 11 see demo code for instructions, it is self-explanatory.
-(0 . 0)(1 . 1)
 16 Placeholder.
-(0 . 0)(1 . 1)
 21 Placeholder.
-(0 . 0)(1 . 69)
 26 ------------------------------------------------------------------------------
 27 ------------------------------------------------------------------------------
 28 -- This file is part of 'UDP Echo Demo', accompanies 'UDP' library.         --
 29 --                                                                          --
 30 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
 31 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
 32 --                                                                          --
 33 -- You do not have, nor can you ever acquire the right to use, copy or      --
 34 -- distribute this software ; Should you use this software for any purpose, --
 35 -- or copy and distribute it to anyone or in any manner, you are breaking   --
 36 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
 37 -- continue doing so for the indefinite future. In any case, please         --
 38 -- always : read and understand any software ; verify any PGP signatures    --
 39 -- that you use - for any purpose.                                          --
 40 --                                                                          --
 41 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
 42 ------------------------------------------------------------------------------
 43 ------------------------------------------------------------------------------
 44 
 45 with Ada.Text_IO; use Ada.Text_IO;
 46 
 47 with UDP;
 48 
 49 
 50 procedure UDP_Echo_Demo is
 51    
 52    Socket           : UDP.Socket;
 53    
 54    --  Local_Endpoint   : UDP.Endpoint
 55    --    := (Address => UDP.IP_From_String("127.0.0.1"),
 56    --        Port    => 7000);
 57    
 58    Local_Endpoint   : UDP.Endpoint := (Address => UDP.INADDR_ANY,
 59                                        Port    => 7000);
 60    
 61    Received_Payload : UDP.Payload;
 62    Received_Origin  : UDP.Endpoint;
 63    Received_Valid   : Boolean;
 64    
 65 begin
 66    Put_Line("Opening socket on local endpoint " &
 67               UDP.IP_To_String(Local_Endpoint.Address) &
 68               " :" & UDP.IP_Port'Image(Local_Endpoint.Port) & "...");
 69    
 70    UDP.Open_Socket(Socket, Local_Endpoint);
 71    
 72    Put_Line("Waiting for payload...");
 73    
 74    UDP.Receive(Socket, Received_Origin, Received_Payload, Received_Valid);
 75    
 76    Put_Line("Received payload from " &
 77               UDP.IP_To_String(Received_Origin.Address) &
 78               " :" & UDP.IP_Port'Image(Received_Origin.Port) & "...");
 79    
 80    if Received_Valid then
 81       
 82       Put_Line("Sending received payload back to originator...");
 83       
 84       UDP.Transmit(Socket, Received_Origin, Received_Payload);
 85       
 86    else
 87       
 88       Put_Line("Received short payload, ignored.");
 89       
 90    end if;
 91    
 92    Put_Line("Done.");
 93 
 94 end UDP_Echo_Demo;
-(0 . 0)(1 . 69)
 99 ------------------------------------------------------------------------------
100 ------------------------------------------------------------------------------
101 -- This file is part of 'UDP Echo Demo', accompanies 'UDP' library.         --
102 --                                                                          --
103 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
104 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
105 --                                                                          --
106 -- You do not have, nor can you ever acquire the right to use, copy or      --
107 -- distribute this software ; Should you use this software for any purpose, --
108 -- or copy and distribute it to anyone or in any manner, you are breaking   --
109 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
110 -- continue doing so for the indefinite future. In any case, please         --
111 -- always : read and understand any software ; verify any PGP signatures    --
112 -- that you use - for any purpose.                                          --
113 --                                                                          --
114 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
115 ------------------------------------------------------------------------------
116 ------------------------------------------------------------------------------
117 
118 with "../libudp/udp.gpr";
119 
120 project UDP_Echo_Demo is
121   
122   for Object_Dir use "obj";
123   
124   type Mode_Type is ("debug", "release");
125   Mode : Mode_Type := external ("mode", "release");
126   
127   for Languages   use ("Ada");
128   for Source_Dirs use (".");
129   for Exec_Dir    use "bin";
130   for Main        use ("udp_echo_demo.adb");
131   
132   package Compiler is
133      case Mode is
134         when "debug" =>
135            for Switches ("Ada")
136              use ("-g");
137         when "release" =>
138            for Switches ("Ada")
139              use ("-O2", "-fdump-scos", "-gnata", "-fstack-check",
140                   "-gnatyd", "-gnatym",
141                   "-fdata-sections", "-ffunction-sections");
142      end case;
143   end Compiler;
144   
145   package Binder is
146      case Mode is
147         when "debug" =>
148            for Switches ("Ada")
149              use ();
150         when "release" =>
151            for Switches ("Ada")
152              use ("-static");
153      end case;
154   end Binder;
155 
156   package Linker is
157      case Mode is
158         when "debug" =>
159            for Switches ("Ada")
160              use ();
161         when "release" =>
162            for Switches ("Ada")
163              use ("-Wl,--gc-sections", "-static");
164      end case;
165   end Linker;
166   
167 end UDP_Echo_Demo;
-(0 . 0)(1 . 1)
172 Placeholder.
-(0 . 0)(1 . 1)
177 Placeholder.
-(0 . 0)(1 . 82)
182 ------------------------------------------------------------------------------
183 ------------------------------------------------------------------------------
184 -- This file is part of 'UDP', a datagram sockets library.                  --
185 --                                                                          --
186 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
187 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
188 --                                                                          --
189 -- You do not have, nor can you ever acquire the right to use, copy or      --
190 -- distribute this software ; Should you use this software for any purpose, --
191 -- or copy and distribute it to anyone or in any manner, you are breaking   --
192 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
193 -- continue doing so for the indefinite future. In any case, please         --
194 -- always : read and understand any software ; verify any PGP signatures    --
195 -- that you use - for any purpose.                                          --
196 --                                                                          --
197 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
198 ------------------------------------------------------------------------------
199 ------------------------------------------------------------------------------
200 
201 pragma Restrictions(Immediate_Reclamation);
202 pragma Restrictions(Max_Asynchronous_Select_Nesting => 0);
203 pragma Restrictions(Max_Protected_Entries => 0);
204 pragma Restrictions(Max_Select_Alternatives => 0);
205 pragma Restrictions(Max_Task_Entries => 0);
206 pragma Restrictions(Max_Tasks => 0);
207 pragma Restrictions(No_Abort_Statements);
208 pragma Restrictions(No_Access_Parameter_Allocators);
209 pragma Restrictions(No_Allocators);
210 pragma Restrictions(No_Asynchronous_Control);
211 pragma Restrictions(No_Calendar);
212 pragma Restrictions(No_Coextensions);
213 pragma Restrictions(No_Default_Stream_Attributes);
214 pragma Restrictions(No_Delay);
215 pragma Restrictions(No_Dispatch);
216 pragma Restrictions(No_Dispatching_Calls);
217 pragma Restrictions(No_Dynamic_Attachment);
218 pragma Restrictions(No_Dynamic_Priorities);
219 pragma Restrictions(No_Entry_Calls_In_Elaboration_Code);
220 pragma Restrictions(No_Entry_Queue);
221 pragma Restrictions(No_Enumeration_Maps);
222 pragma Restrictions(No_Exception_Propagation);
223 pragma Restrictions(No_Exception_Registration);
224 pragma Restrictions(No_Finalization);
225 pragma Restrictions(No_Fixed_Io);
226 pragma Restrictions(No_Floating_Point);
227 pragma Restrictions(No_Implementation_Aspect_Specifications);
228 pragma Restrictions(No_Implementation_Units);
229 pragma Restrictions(No_Implicit_Dynamic_Code);
230 pragma Restrictions(No_Implicit_Heap_Allocations);
231 pragma Restrictions(No_Implicit_Protected_Object_Allocations);
232 pragma Restrictions(No_Implicit_Task_Allocations);
233 pragma Restrictions(No_Initialize_Scalars);
234 pragma Restrictions(No_Local_Protected_Objects);
235 pragma Restrictions(No_Local_Timing_Events);
236 pragma Restrictions(No_Multiple_Elaboration);
237 pragma Restrictions(No_Nested_Finalization);
238 pragma Restrictions(No_Protected_Type_Allocators);
239 pragma Restrictions(No_Protected_Types);
240 pragma Restrictions(No_Relative_Delay);
241 pragma Restrictions(No_Requeue_Statements);
242 pragma Restrictions(No_Secondary_Stack);
243 pragma Restrictions(No_Select_Statements);
244 pragma Restrictions(No_Specific_Termination_Handlers);
245 pragma Restrictions(No_Standard_Allocators_After_Elaboration);
246 pragma Restrictions(No_Stream_Optimizations);
247 pragma Restrictions(No_Streams);
248 pragma Restrictions(No_Task_Allocators);
249 pragma Restrictions(No_Task_At_Interrupt_Priority);
250 pragma Restrictions(No_Task_Attributes_Package);
251 pragma Restrictions(No_Task_Hierarchy);
252 pragma Restrictions(No_Tasking);
253 pragma Restrictions(No_Task_Termination);
254 pragma Restrictions(No_Terminate_Alternatives);
255 pragma Restrictions(No_Unchecked_Access);
256 pragma Restrictions(No_Unchecked_Conversion);
257 pragma Restrictions(No_Unchecked_Deallocation);
258 pragma Restrictions(No_Wide_Characters);
259 pragma Restrictions(Pure_Barriers);
260 pragma Restrictions(Simple_Barriers);
261 pragma Restrictions(Static_Priorities);
262 pragma Restrictions(Static_Storage_Size);
263 pragma Validity_Checks(ALL_CHECKS);
-(0 . 0)(1 . 139)
268 ------------------------------------------------------------------------------
269 ------------------------------------------------------------------------------
270 -- This file is part of 'UDP', a datagram sockets library.                  --
271 --                                                                          --
272 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
273 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
274 --                                                                          --
275 -- You do not have, nor can you ever acquire the right to use, copy or      --
276 -- distribute this software ; Should you use this software for any purpose, --
277 -- or copy and distribute it to anyone or in any manner, you are breaking   --
278 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
279 -- continue doing so for the indefinite future. In any case, please         --
280 -- always : read and understand any software ; verify any PGP signatures    --
281 -- that you use - for any purpose.                                          --
282 --                                                                          --
283 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
284 ------------------------------------------------------------------------------
285 ------------------------------------------------------------------------------
286 
287 package body UDP is
288    
289    -- Generate a human representation of a (local-endian) IP Address
290    function IP_To_String(IP : in IP_Address) return IP_Address_Text is
291       Text : IP_Address_Text := (others => ' ');
292    begin
293       Unix_UDP_IP_To_String(IP, Text'Address, Text'Length);
294       return Text;
295    end IP_To_String;
296    
297    
298    -- Generate a (local-endian) IP Address from given human representation
299    function IP_From_String(IP_Text : in String) return IP_Address is
300       Text_With_Null : String(1 .. IP_Text'Length + 1);
301       Result : Interfaces.C.Int := 0;
302       IP     : aliased IP_Address;
303    begin
304       -- We can't use To_C because malicious idiots demanded secondary stack.
305       Text_With_Null(IP_Text'Range) := IP_Text;
306       Text_With_Null(Text_With_Null'Last) := Character'Val(0);
307       
308       -- Let unix do the conversion
309       Result := Unix_UDP_String_To_IP(Text_With_Null'Address,
310                                       IP'Access);
311       case Result is
312          when -1 =>
313             raise UDP_Invalid_Text_IP;
314          when others =>
315             return IP;
316       end case;
317    end IP_From_String;
318    
319    
320    -- Open a UDP socket, with the given local endpoint for both TX and RX
321    procedure Open_Socket(S              : out Socket;
322                          Local_Endpoint : in  Endpoint) is
323       Result : constant Interfaces.C.Int :=
324         Unix_UDP_Socket_Open(Socket     => S'Address,
325                              Local_IP   => Local_Endpoint.Address,
326                              Local_Port => Local_Endpoint.Port);
327    begin
328       case Result is
329          when -1 =>
330             raise UDP_Failed_Open;
331          when -2 =>
332             raise UDP_Failed_SetOpt;
333          when -3 =>
334             raise UDP_Failed_Bind;
335          when others =>
336             null;
337       end case;
338    end Open_Socket;
339    
340    
341    -- Permanently close the given open given socket
342    procedure Close_Socket(S : in out Socket) is
343    begin
344       Unix_UDP_Socket_Close(Socket => S'Address);
345    end Close_Socket;
346    
347    
348    -- Transmit the Payload, via Socket, to given Destination
349    procedure Transmit(S           : in out Socket;
350                       Destination : in     Endpoint;
351                       Payload_Buf : in     Payload) is
352       Result : constant Interfaces.C.Int :=
353         Unix_UDP_Socket_Transmit(Socket      => S'Address,
354                                  Remote_IP   => Destination.Address,
355                                  Remote_Port => Destination.Port,
356                                  Payload_Buf => Payload_Buf'Address,
357                                  Payload_Len => Payload'Length);
358    begin
359       case Result is
360          when -1 =>
361             Close_Socket(S);
362             raise UDP_Failed_Transmit;
363          when others =>
364             -- No eggog
365             null;
366       end case;
367    end Transmit;
368    
369    
370    -- Wait (potentially forever!) for a Payload, via Socket; save its Origin
371    procedure Receive(S           : in out Socket;
372                      Origin      : out    Endpoint;
373                      Payload_Buf : out    Payload;
374                      Valid       : out    Boolean) is
375       
376       -- Scratch pad (if not successful, the call has no outputs)
377       Incoming_Payload : aliased Payload := (others => 0);
378       Incoming_IP      : aliased IP_Address;
379       Incoming_Port    : aliased IP_Port;
380       
381       Result : constant Interfaces.C.Int :=
382         Unix_UDP_Socket_Receive(Socket      => S'Address,
383                                 Origin_IP   => Incoming_IP'Access,
384                                 Origin_Port => Incoming_Port'Access,
385                                 Payload_Buf => Incoming_Payload'Address,
386                                 Payload_Len => Payload'Length);
387    begin
388       Valid := False;
389       case Result is
390          when -1 =>
391             Close_Socket(S);
392             raise UDP_Failed_Receive;
393          when others =>
394             -- No eggog:
395             Origin.Address := Incoming_IP;
396             Origin.Port    := Incoming_Port;
397             Payload_Buf    := Incoming_Payload;
398             
399             -- Was a full-length payload?
400             if (Result = Payload'Length) then
401                Valid := True;
402             end if;
403       end case;
404    end Receive;
405    
406 end UDP;
-(0 . 0)(1 . 149)
411 ------------------------------------------------------------------------------
412 ------------------------------------------------------------------------------
413 -- This file is part of 'UDP', a datagram sockets library.                  --
414 --                                                                          --
415 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
416 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
417 --                                                                          --
418 -- You do not have, nor can you ever acquire the right to use, copy or      --
419 -- distribute this software ; Should you use this software for any purpose, --
420 -- or copy and distribute it to anyone or in any manner, you are breaking   --
421 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
422 -- continue doing so for the indefinite future. In any case, please         --
423 -- always : read and understand any software ; verify any PGP signatures    --
424 -- that you use - for any purpose.                                          --
425 --                                                                          --
426 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
427 ------------------------------------------------------------------------------
428 ------------------------------------------------------------------------------
429 
430 with Interfaces, Interfaces.C; use Interfaces, Interfaces.C;
431 with System; use System;
432 
433 
434 package UDP is
435    
436    pragma Preelaborate;
437    
438    -- This is subject to debate:
439    Payload_Size : constant Positive := 512;
440    
441    type Payload is array(1 .. Payload_Size) of Unsigned_8;
442    
443    --  type IP_Address is array(1 .. 4) of Unsigned_8;
444    subtype IP_Address is Unsigned_32;
445    subtype IP_Port is Unsigned_16;
446    
447    -- Magic that puts emitter on 'any' local interface
448    INADDR_ANY : constant Unsigned_32 := 0;
449    
450    -- An local or remote address:port
451    type Endpoint is
452       record
453          Address : IP_Address;
454          Port    : IP_Port;
455       end record;
456    -- NOTE that both elements are stored in ~local~ endianness.
457    
458    -- Human Representation of any valid IP Address
459    subtype IP_Address_Text is String(1 .. 15);
460    
461    -- Opaque unix turd that stores a socket's state
462    type Socket is private;
463    
464    -- The public API:
465    
466    -- Generate a human representation of a (local-endian) IP Address
467    function IP_To_String(IP : in IP_Address) return IP_Address_Text;
468    
469    -- Generate a (local-endian) IP Address from given human representation
470    function IP_From_String(IP_Text : in String) return IP_Address;
471    
472    -- Open a UDP socket, with the given local endpoint for both TX and RX
473    procedure Open_Socket(S              : out    Socket;
474                          Local_Endpoint : in     Endpoint);
475    
476    -- Permanently close the given open given socket
477    procedure Close_Socket(S             : in out Socket);
478    
479    -- Transmit the Payload, via Socket, to given Destination
480    procedure Transmit(S           : in out Socket;
481                       Destination : in     Endpoint;
482                       Payload_Buf : in     Payload);
483    
484    -- Wait (potentially forever!) for a Payload, via Socket; save its Origin,
485    -- and whether the received Payload was valid (i.e. expected length):
486    procedure Receive(S           : in out Socket;
487                      Origin      : out    Endpoint;
488                      Payload_Buf : out    Payload;
489                      Valid       : out    Boolean);
490    
491    -- Eggogology:
492    UDP_Invalid_Text_IP : exception;
493    UDP_Failed_Open     : exception;
494    UDP_Failed_SetOpt   : exception;
495    UDP_Failed_Bind     : exception;
496    UDP_Failed_Transmit : exception;
497    UDP_Failed_Receive  : exception;
498    
499 private
500    
501    -- 'nicht fuer gefingerpoken und mittengrabben!'
502    
503    -- This record's elements are not accessed from ada:
504    type sockaddr_in is record
505       family   : Unsigned_16;
506       port     : Unsigned_16;
507       sin_addr : Unsigned_32;
508       padding  : Unsigned_64;
509    end record;
510    pragma Convention(C, sockaddr_in);
511    
512    -- Here we also don't care about the elements, only total mass:
513    type Socket is
514       record
515          SA : sockaddr_in;
516          FD : Interfaces.C.int;
517       end record;
518    pragma Convention(C, Socket);
519    
520    -- Everything below -- imports from unix_udp.c:
521    
522    procedure Unix_UDP_IP_To_String
523      (IP                 : Unsigned_32;
524       Output_Buffer      : System.Address;
525       Output_Buffer_Size : Unsigned_32);
526    pragma Import(C, Unix_UDP_IP_To_String, "unix_udp_ip_to_string");
527    
528    function Unix_UDP_String_To_IP
529      (Input_Buffer : System.Address;
530       IP           : not null access Unsigned_32) return Interfaces.C.int;
531    pragma Import(C, Unix_UDP_String_To_IP, "unix_udp_string_to_ip");
532    
533    function Unix_UDP_Socket_Open
534      (Socket     : System.Address;
535       Local_IP   : Unsigned_32;
536       Local_Port : Unsigned_16) return Interfaces.C.int;
537    pragma Import(C, Unix_UDP_Socket_Open, "unix_udp_socket_open");
538    
539    procedure Unix_UDP_Socket_Close
540      (Socket     : System.Address);
541    pragma Import(C, Unix_UDP_Socket_Close, "unix_udp_socket_close");
542    
543    function Unix_UDP_Socket_Transmit
544      (Socket      : System.Address;
545       Remote_IP   : Unsigned_32;
546       Remote_Port : Unsigned_16;
547       Payload_Buf : System.Address;
548       Payload_Len : Unsigned_32) return Interfaces.C.int;
549    pragma Import(C, Unix_UDP_Socket_Transmit, "unix_udp_socket_transmit");
550    
551    function Unix_UDP_Socket_Receive
552      (Socket      : System.Address;
553       Origin_IP   : not null access Unsigned_32;
554       Origin_Port : not null access Unsigned_16;
555       Payload_Buf : System.Address;
556       Payload_Len : Unsigned_32) return Interfaces.C.int;
557    pragma Import(C, Unix_UDP_Socket_Receive, "unix_udp_socket_receive");
558    
559 end UDP;
-(0 . 0)(1 . 74)
564 ------------------------------------------------------------------------------
565 ------------------------------------------------------------------------------
566 -- This file is part of 'UDP', a datagram sockets library.                  --
567 --                                                                          --
568 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
569 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
570 --                                                                          --
571 -- You do not have, nor can you ever acquire the right to use, copy or      --
572 -- distribute this software ; Should you use this software for any purpose, --
573 -- or copy and distribute it to anyone or in any manner, you are breaking   --
574 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
575 -- continue doing so for the indefinite future. In any case, please         --
576 -- always : read and understand any software ; verify any PGP signatures    --
577 -- that you use - for any purpose.                                          --
578 --                                                                          --
579 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
580 ------------------------------------------------------------------------------
581 ------------------------------------------------------------------------------
582 
583 project UDP is
584   
585   for Object_Dir use "obj";
586   
587   type Mode_Type is ("debug", "release");
588   Mode : Mode_Type := external ("mode", "release");
589   
590   for Languages    use ("Ada", "C");
591   for Source_Dirs  use (".");
592   for Library_Dir  use "lib";
593   for Library_Name use "UDP";
594   for Library_Kind use "static";
595   
596   package Compiler is
597     for Leading_Required_Switches ("C") use ("-c");
598     for Object_File_Suffix ("C") use ".o";
599     for Include_Switches ("C") use ("-I");
600 
601      case Mode is
602         when "debug" =>
603            for Switches ("Ada")
604              use ("-g");
605         when "release" =>
606            for Switches ("Ada")
607              use ("-O2", "-fdump-scos", "-gnata", "-fstack-check",
608                   "-gnatyd", "-gnatym",
609                   "-fdata-sections", "-ffunction-sections", "-gnatwr", "-gnatw.d",
610                   "-gnatec=" & UDP'Project_Dir & "restrict.adc");
611            for Switches ("C")
612              use ("-O2", "-Wall", "-fstack-check");
613      end case;
614   end Compiler;
615   
616   package Naming is
617      for Spec_Suffix ("C") use ".h";
618      for Body_Suffix ("C") use ".c";
619   end Naming;
620   
621   package Builder is
622      for Switches ("Ada")
623        use ("-nostdlib");
624   end Builder;
625   
626   package Binder is
627      case Mode is
628         when "debug" =>
629            for Switches ("Ada")
630              use ();
631         when "release" =>
632            for Switches ("Ada")
633              use ("-static");
634      end case;
635   end Binder;
636   
637 end UDP;
-(0 . 0)(1 . 139)
642 /*
643 ------------------------------------------------------------------------------
644 ------------------------------------------------------------------------------
645 -- This file is part of 'UDP', a datagram sockets library.                  --
646 --                                                                          --
647 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
648 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
649 --                                                                          --
650 -- You do not have, nor can you ever acquire the right to use, copy or      --
651 -- distribute this software ; Should you use this software for any purpose, --
652 -- or copy and distribute it to anyone or in any manner, you are breaking   --
653 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
654 -- continue doing so for the indefinite future. In any case, please         --
655 -- always : read and understand any software ; verify any PGP signatures    --
656 -- that you use - for any purpose.                                          --
657 --                                                                          --
658 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
659 ------------------------------------------------------------------------------
660 ------------------------------------------------------------------------------
661 */
662 
663 #include <string.h>
664 #include <unistd.h>
665 #include <arpa/inet.h>
666 #include <netinet/in.h>
667 #include <sys/types.h>
668 #include <sys/socket.h>
669 
670 
671 /* Socket state representation: */
672 typedef struct _UDP_Socket {
673   struct sockaddr_in sa_local;
674   int sock;
675 } UDP_Socket;
676 
677 
678 /* local-endian ip to string conversion */
679 void unix_udp_ip_to_string(uint32_t ip, char *buf, uint32_t buf_size) {
680   struct in_addr addr;
681   addr.s_addr = htonl(ip);
682   char *txt = inet_ntoa(addr);
683   strncpy(buf, txt, buf_size);
684 }
685 
686 
687 /* string to local-endian ip conversion */
688 int unix_udp_string_to_ip(char *buf, uint32_t *ip) {
689   struct in_addr addr;
690   if (inet_aton(buf, &addr) <= 0)
691     return -1;
692   *ip = ntohl(addr.s_addr);
693   return 0;
694 }
695 
696 
697 int unix_udp_socket_open(UDP_Socket *S,
698                          uint32_t local_ip, uint16_t local_port) {
699   /* Open the socket FD: */
700   if ((S->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
701     return -1;
702   }
703   
704   memset(&S->sa_local, 0, sizeof(struct sockaddr_in));
705   
706   /* Set up emitter endpoint, converting from local endianness: */
707   S->sa_local.sin_family = AF_INET;
708   S->sa_local.sin_addr.s_addr = htonl(local_ip);
709   S->sa_local.sin_port = htons(local_port);
710   
711   /* Cure the asinine linuxism where dead sockets interfere with living: */
712   int one = 1;
713   if (setsockopt(S->sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
714     close(S->sock);
715     return -2;
716   }
717   
718   /* Bind the socket */
719   if (bind(S->sock,
720            (struct sockaddr *)&(S->sa_local), sizeof(S->sa_local)) < 0) {
721     close(S->sock);
722     return -3;
723   }
724   
725   /* ok */
726   return 0;
727 }
728 
729 
730 void unix_udp_socket_close(UDP_Socket *S) {
731   close(S->sock);
732 }
733 
734 
735 int unix_udp_socket_transmit(UDP_Socket *S,
736                              uint32_t remote_ip, uint16_t remote_port,
737                              uint8_t *payload, uint32_t payload_len) {
738   int bytes_sent = 0;
739   struct sockaddr_in remote_addr;
740   memset((char *)&remote_addr, 0, sizeof(remote_addr));
741   
742   /* Set up dest endpoint, converting from local endianness: */
743   remote_addr.sin_family = AF_INET;
744   remote_addr.sin_port = htons(remote_port);
745   remote_addr.sin_addr.s_addr = htonl(remote_ip);
746   
747   /* Transmit Datagram */
748   bytes_sent = sendto(S->sock, payload, payload_len,
749                       0, /* no flags */
750                       (struct sockaddr*)&remote_addr,
751                       sizeof(remote_addr));
752   if (bytes_sent <= 0)
753     return -1;
754   
755   return bytes_sent;
756 }
757 
758 
759 int unix_udp_socket_receive(UDP_Socket *S,
760                             uint32_t *origin_ip, uint16_t *origin_port,
761                             uint8_t *payload, uint32_t payload_len) {
762   int bytes_received = 0;
763   struct sockaddr_in orig_addr;
764   socklen_t orig_addr_len = sizeof(orig_addr);
765   memset((char *)&orig_addr, 0, sizeof(orig_addr));
766   
767   /* Receive Datagram (blocking!) */
768   bytes_received = recvfrom(S->sock, payload, payload_len,
769                             0, /* no flags */
770                             (struct sockaddr *)&orig_addr,
771                             &orig_addr_len);
772   
773   if (bytes_received < 0) return -1;
774 
775   /* Save the originator's endpoint in ~local~ endianness */
776   *origin_ip = ntohl(orig_addr.sin_addr.s_addr);
777   *origin_port = ntohs(orig_addr.sin_port);
778   
779   return bytes_received;
780 }
-(0 . 0)(1 . 1)
785 543080 udp_genesis diana_coman Regrind of asciilifeform's UDP lib genesis, to bring it to current format (Keccak hashes, manifest file and standard compliant names for vpatches). A minimal library for UDP communications with fixed-size payloads sent/received over the wire. Uses Ada and C code.
-(0 . 0)(1 . 1)
790 Placeholder.
-(0 . 0)(1 . 1)
795 Placeholder.
-(0 . 0)(1 . 93)
800 ------------------------------------------------------------------------------
801 ------------------------------------------------------------------------------
802 -- This file is part of 'UDP Tx Demo', accompanies 'UDP' library.           --
803 --                                                                          --
804 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
805 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
806 --                                                                          --
807 -- You do not have, nor can you ever acquire the right to use, copy or      --
808 -- distribute this software ; Should you use this software for any purpose, --
809 -- or copy and distribute it to anyone or in any manner, you are breaking   --
810 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
811 -- continue doing so for the indefinite future. In any case, please         --
812 -- always : read and understand any software ; verify any PGP signatures    --
813 -- that you use - for any purpose.                                          --
814 --                                                                          --
815 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
816 ------------------------------------------------------------------------------
817 ------------------------------------------------------------------------------
818 
819 with Ada.Text_IO; use Ada.Text_IO;
820 with Interfaces; use Interfaces;
821 
822 with UDP; use UDP;
823 
824 
825 procedure UDP_Tx_Demo is
826    
827    Socket           : UDP.Socket;
828    
829    
830    Local_Endpoint   : UDP.Endpoint := (Address => UDP.INADDR_ANY,
831                                        Port    => 5000);
832    
833    Remote_Endpoint   : UDP.Endpoint
834      := (Address => UDP.IP_From_String("0.0.0.0"),
835          Port    => 7000);
836    
837    ----- Dulap test, replace with your own  -----
838    --  Remote_Endpoint   : UDP.Endpoint
839    --    := (Address => UDP.IP_From_String("161.0.121.200"),
840    --        Port    => 7000);
841    ----------------------------------------------
842    
843    Sent_Payload     : UDP.Payload;
844    Received_Payload : UDP.Payload;
845    Received_Origin  : UDP.Endpoint;
846    Received_Valid   : Boolean;
847    
848 begin
849    Put_Line("Generating " &
850               UDP.Payload_Size'Image(Sent_Payload'Length) & "-byte turd...");
851    
852    for I in Sent_Payload'Range loop
853       Sent_Payload(I) := Unsigned_8(I mod 256);
854    end loop;
855    
856    Put_Line("Opening socket on local endpoint " &
857               UDP.IP_To_String(Local_Endpoint.Address) &
858               " :" & UDP.IP_Port'Image(Local_Endpoint.Port) & "...");
859    
860    UDP.Open_Socket(Socket, Local_Endpoint);
861    
862    Put_Line("Sending turd to " &
863               UDP.IP_To_String(Remote_Endpoint.Address) &
864               " :" & UDP.IP_Port'Image(Remote_Endpoint.Port) & "...");
865    
866    UDP.Transmit(Socket, Remote_Endpoint, Sent_Payload);
867    
868    Put_Line("Waiting for echo...");
869    
870    UDP.Receive(Socket, Received_Origin, Received_Payload, Received_Valid);
871    
872    Put_Line("Received payload from " &
873               UDP.IP_To_String(Received_Origin.Address) &
874               " :" & UDP.IP_Port'Image(Received_Origin.Port) & "...");
875    
876    if Received_Valid then
877       
878       if Received_Payload = Sent_Payload then
879          Put_Line("Echo came back equal to the send turd!");
880       else
881          Put_Line("Echo came back mutilated!");
882       end if;
883       
884    else
885       
886       Put_Line("Received short payload, ignored.");
887       
888    end if;
889    
890    Put_Line("Done.");
891 
892 end UDP_Tx_Demo;
-(0 . 0)(1 . 69)
897 ------------------------------------------------------------------------------
898 ------------------------------------------------------------------------------
899 -- This file is part of 'UDP Tx Demo', accompanies 'UDP' library.           --
900 --                                                                          --
901 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
902 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
903 --                                                                          --
904 -- You do not have, nor can you ever acquire the right to use, copy or      --
905 -- distribute this software ; Should you use this software for any purpose, --
906 -- or copy and distribute it to anyone or in any manner, you are breaking   --
907 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
908 -- continue doing so for the indefinite future. In any case, please         --
909 -- always : read and understand any software ; verify any PGP signatures    --
910 -- that you use - for any purpose.                                          --
911 --                                                                          --
912 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
913 ------------------------------------------------------------------------------
914 ------------------------------------------------------------------------------
915 
916 with "../libudp/udp.gpr";
917 
918 project UDP_Tx_Demo is
919   
920   for Object_Dir use "obj";
921   
922   type Mode_Type is ("debug", "release");
923   Mode : Mode_Type := external ("mode", "release");
924   
925   for Languages   use ("Ada");
926   for Source_Dirs use (".");
927   for Exec_Dir    use "bin";
928   for Main        use ("udp_tx_demo.adb");
929   
930   package Compiler is
931      case Mode is
932         when "debug" =>
933            for Switches ("Ada")
934              use ("-g");
935         when "release" =>
936            for Switches ("Ada")
937              use ("-O2", "-fdump-scos", "-gnata", "-fstack-check",
938                   "-gnatyd", "-gnatym",
939                   "-fdata-sections", "-ffunction-sections");
940      end case;
941   end Compiler;
942   
943   package Binder is
944      case Mode is
945         when "debug" =>
946            for Switches ("Ada")
947              use ();
948         when "release" =>
949            for Switches ("Ada")
950              use ("-static");
951      end case;
952   end Binder;
953 
954   package Linker is
955      case Mode is
956         when "debug" =>
957            for Switches ("Ada")
958              use ();
959         when "release" =>
960            for Switches ("Ada")
961              use ("-Wl,--gc-sections", "-static");
962      end case;
963   end Linker;
964   
965 end UDP_Tx_Demo;