-
+ E5225E08AAD2ECDAA42E10638FFF93C70F5756AD293CC7FCA22DFB156583DBEF132EEAF48EB36B6614637DD22EFEFB5CFC43FD724456E9E80E39ACC1742D157Eudp/libudp/udp.ads(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;