2
0

spark_sockets.ads 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. -- spark_sockets.ads
  2. --
  3. -- Copyright (C) 2006-2023 wolfSSL Inc.
  4. --
  5. -- This file is part of wolfSSL.
  6. --
  7. -- wolfSSL is free software; you can redistribute it and/or modify
  8. -- it under the terms of the GNU General Public License as published by
  9. -- the Free Software Foundation; either version 2 of the License, or
  10. -- (at your option) any later version.
  11. --
  12. -- wolfSSL is distributed in the hope that it will be useful,
  13. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. -- GNU General Public License for more details.
  16. --
  17. -- You should have received a copy of the GNU General Public License
  18. -- along with this program; if not, write to the Free Software
  19. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. --
  21. -- GNAT Library packages.
  22. with GNAT.Sockets;
  23. -- The WolfSSL package.
  24. with WolfSSL;
  25. -- This is a wrapper package around the GNAT.Sockets package.
  26. -- GNAT.Sockets raises exceptions to signal errors but exceptions
  27. -- are not supported by SPARK. This package converts raised exceptions
  28. -- into returned enumeration values by functions indicating success
  29. -- or failure.
  30. --
  31. -- The intended use of this package is to demonstrate the usage
  32. -- of the WolfSSL Ada binding in Ada/SPARK code.
  33. package SPARK_Sockets with SPARK_Mode is
  34. subtype Byte_Array is WolfSSL.Byte_Array;
  35. subtype Byte_Index is WolfSSL.Byte_Index; use type Byte_Index;
  36. subtype Port_Type is GNAT.Sockets.Port_Type;
  37. subtype Level_Type is GNAT.Sockets.Level_Type;
  38. subtype Socket_Type is GNAT.Sockets.Socket_Type;
  39. subtype Option_Name is GNAT.Sockets.Option_Name;
  40. subtype Option_Type is GNAT.Sockets.Option_Type;
  41. subtype Family_Type is GNAT.Sockets.Family_Type;
  42. subtype Sock_Addr_Type is GNAT.Sockets.Sock_Addr_Type;
  43. subtype Inet_Addr_Type is GNAT.Sockets.Inet_Addr_Type;
  44. Socket_Error : exception renames GNAT.Sockets.Socket_Error;
  45. Reuse_Address : Option_Name renames GNAT.Sockets.Reuse_Address;
  46. Socket_Level : Level_Type renames GNAT.Sockets.Socket_Level;
  47. Family_Inet : Family_Type renames GNAT.Sockets.Family_Inet;
  48. use type GNAT.Sockets.Family_Type;
  49. Any_Inet_Addr : Inet_Addr_Type renames GNAT.Sockets.Any_Inet_Addr;
  50. subtype Subprogram_Result is WolfSSL.Subprogram_Result;
  51. use type Subprogram_Result;
  52. Success : Subprogram_Result renames WolfSSL.Success;
  53. Failure : Subprogram_Result renames WolfSSL.Failure;
  54. type Optional_Inet_Addr (Exists : Boolean := False) is record
  55. case Exists is
  56. when True => Addr : Inet_Addr_Type;
  57. when False => null;
  58. end case;
  59. end record;
  60. function Inet_Addr (Image : String) return Optional_Inet_Addr;
  61. type Optional_Socket (Exists : Boolean := False) is record
  62. case Exists is
  63. when True => Socket : Socket_Type;
  64. when False => null;
  65. end case;
  66. end record;
  67. procedure Create_Stream_Socket (Socket : in out Optional_Socket) with
  68. Pre => not Socket.Exists;
  69. procedure Create_Datagram_Socket (Socket : in out Optional_Socket) with
  70. Pre => not Socket.Exists;
  71. function Connect_Socket (Socket : Socket_Type;
  72. Server : Sock_Addr_Type)
  73. return Subprogram_Result;
  74. function To_C (Socket : Socket_Type) return Integer with Inline;
  75. -- Close a socket and more specifically a non-connected socket.
  76. procedure Close_Socket (Socket : in out Optional_Socket) with
  77. Pre => Socket.Exists,
  78. Post => not Socket.Exists;
  79. function Set_Socket_Option (Socket : Socket_Type;
  80. Level : Level_Type;
  81. Option : Option_Type)
  82. return Subprogram_Result;
  83. -- Manipulate socket options.
  84. function Bind_Socket (Socket : Socket_Type;
  85. Address : Sock_Addr_Type)
  86. return Subprogram_Result;
  87. function Listen_Socket (Socket : Socket_Type;
  88. Length : Natural) return Subprogram_Result;
  89. -- To accept connections, a socket is first created with
  90. -- Create_Socket, a willingness to accept incoming connections and
  91. -- a queue Length for incoming connections are specified.
  92. -- The queue length of 15 is an example value that should be
  93. -- appropriate in usual cases. It can be adjusted according to each
  94. -- application's particular requirements.
  95. function Receive_Socket (Socket : Socket_Type) return Subprogram_Result;
  96. procedure Accept_Socket (Server : Socket_Type;
  97. Socket : out Optional_Socket;
  98. Address : out Sock_Addr_Type;
  99. Result : out Subprogram_Result) with
  100. Post => (if Result = Success then Socket.Exists else not Socket.Exists);
  101. procedure To_C (Item : String;
  102. Target : out Byte_Array;
  103. Count : out Byte_Index) with
  104. Pre => Item'Length <= Target'Length,
  105. Post => Count <= Target'Last;
  106. procedure To_Ada (Item : Byte_Array;
  107. Target : out String;
  108. Count : out Natural) with
  109. Pre => Item'Length <= Target'Length,
  110. Post => Count <= Target'Last;
  111. end SPARK_Sockets;