spark_sockets.ads 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_Socket (Socket : in out Optional_Socket) with
  68. Pre => not Socket.Exists;
  69. function Connect_Socket (Socket : Socket_Type;
  70. Server : Sock_Addr_Type)
  71. return Subprogram_Result;
  72. function To_C (Socket : Socket_Type) return Integer with Inline;
  73. -- Close a socket and more specifically a non-connected socket.
  74. procedure Close_Socket (Socket : in out Optional_Socket) with
  75. Pre => Socket.Exists,
  76. Post => not Socket.Exists;
  77. function Set_Socket_Option (Socket : Socket_Type;
  78. Level : Level_Type;
  79. Option : Option_Type)
  80. return Subprogram_Result;
  81. -- Manipulate socket options.
  82. function Bind_Socket (Socket : Socket_Type;
  83. Address : Sock_Addr_Type)
  84. return Subprogram_Result;
  85. function Listen_Socket (Socket : Socket_Type;
  86. Length : Natural) return Subprogram_Result;
  87. -- To accept connections, a socket is first created with
  88. -- Create_Socket, a willingness to accept incoming connections and
  89. -- a queue Length for incoming connections are specified.
  90. -- The queue length of 15 is an example value that should be
  91. -- appropriate in usual cases. It can be adjusted according to each
  92. -- application's particular requirements.
  93. procedure Accept_Socket (Server : Socket_Type;
  94. Socket : out Optional_Socket;
  95. Address : out Sock_Addr_Type;
  96. Result : out Subprogram_Result) with
  97. Post => (if Result = Success then Socket.Exists else not Socket.Exists);
  98. procedure To_C (Item : String;
  99. Target : out Byte_Array;
  100. Count : out Byte_Index) with
  101. Pre => Item'Length <= Target'Length,
  102. Post => Count <= Target'Last;
  103. procedure To_Ada (Item : Byte_Array;
  104. Target : out String;
  105. Count : out Natural) with
  106. Pre => Item'Length <= Target'Length,
  107. Post => Count <= Target'Last;
  108. end SPARK_Sockets;