|
| 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 | + |
| 22 | +-- GNAT Library packages. |
| 23 | +with GNAT.Sockets; |
| 24 | + |
| 25 | +-- The WolfSSL package. |
| 26 | +with WolfSSL; |
| 27 | + |
| 28 | +-- This is a wrapper package around the GNAT.Sockets package. |
| 29 | +-- GNAT.Sockets raises exceptions to signal errors but exceptions |
| 30 | +-- are not supported by SPARK. This package converts raised exceptions |
| 31 | +-- into returned enumeration values by functions indicating success |
| 32 | +-- or failure. |
| 33 | +-- |
| 34 | +-- The intended use of this package is to demonstrate the usage |
| 35 | +-- of the WolfSSL Ada binding in Ada/SPARK code. |
| 36 | +package SPARK_Sockets with SPARK_Mode is |
| 37 | + |
| 38 | + subtype Byte_Array is WolfSSL.Byte_Array; |
| 39 | + subtype Byte_Index is WolfSSL.Byte_Index; use type Byte_Index; |
| 40 | + |
| 41 | + subtype Port_Type is GNAT.Sockets.Port_Type; |
| 42 | + |
| 43 | + subtype Level_Type is GNAT.Sockets.Level_Type; |
| 44 | + |
| 45 | + subtype Socket_Type is GNAT.Sockets.Socket_Type; |
| 46 | + subtype Option_Name is GNAT.Sockets.Option_Name; |
| 47 | + subtype Option_Type is GNAT.Sockets.Option_Type; |
| 48 | + subtype Family_Type is GNAT.Sockets.Family_Type; |
| 49 | + |
| 50 | + subtype Sock_Addr_Type is GNAT.Sockets.Sock_Addr_Type; |
| 51 | + subtype Inet_Addr_Type is GNAT.Sockets.Inet_Addr_Type; |
| 52 | + |
| 53 | + Socket_Error : exception renames GNAT.Sockets.Socket_Error; |
| 54 | + |
| 55 | + Reuse_Address : Option_Name renames GNAT.Sockets.Reuse_Address; |
| 56 | + |
| 57 | + Socket_Level : Level_Type renames GNAT.Sockets.Socket_Level; |
| 58 | + |
| 59 | + Family_Inet : Family_Type renames GNAT.Sockets.Family_Inet; |
| 60 | + use type GNAT.Sockets.Family_Type; |
| 61 | + |
| 62 | + Any_Inet_Addr : Inet_Addr_Type renames GNAT.Sockets.Any_Inet_Addr; |
| 63 | + |
| 64 | + subtype Subprogram_Result is WolfSSL.Subprogram_Result; |
| 65 | + use type Subprogram_Result; |
| 66 | + |
| 67 | + Success : Subprogram_Result renames WolfSSL.Success; |
| 68 | + Failure : Subprogram_Result renames WolfSSL.Failure; |
| 69 | + |
| 70 | + type Optional_Inet_Addr (Exists : Boolean := False) is record |
| 71 | + case Exists is |
| 72 | + when True => Addr : Inet_Addr_Type; |
| 73 | + when False => null; |
| 74 | + end case; |
| 75 | + end record; |
| 76 | + |
| 77 | + function Inet_Addr (Image : String) return Optional_Inet_Addr; |
| 78 | + |
| 79 | + type Optional_Socket (Exists : Boolean := False) is record |
| 80 | + case Exists is |
| 81 | + when True => Socket : Socket_Type; |
| 82 | + when False => null; |
| 83 | + end case; |
| 84 | + end record; |
| 85 | + |
| 86 | + procedure Create_Socket (Socket : in out Optional_Socket) with |
| 87 | + Pre => not Socket.Exists; |
| 88 | + |
| 89 | + function Connect_Socket (Socket : Socket_Type; |
| 90 | + Server : Sock_Addr_Type) |
| 91 | + return Subprogram_Result; |
| 92 | + |
| 93 | + function To_C (Socket : Socket_Type) return Integer with Inline; |
| 94 | + |
| 95 | + -- Close a socket and more specifically a non-connected socket. |
| 96 | + procedure Close_Socket (Socket : in out Optional_Socket) with |
| 97 | + Pre => Socket.Exists, |
| 98 | + Post => not Socket.Exists; |
| 99 | + |
| 100 | + function Set_Socket_Option (Socket : Socket_Type; |
| 101 | + Level : Level_Type; |
| 102 | + Option : Option_Type) |
| 103 | + return Subprogram_Result; |
| 104 | + -- Manipulate socket options. |
| 105 | + |
| 106 | + function Bind_Socket (Socket : Socket_Type; |
| 107 | + Address : Sock_Addr_Type) |
| 108 | + return Subprogram_Result; |
| 109 | + |
| 110 | + function Listen_Socket (Socket : Socket_Type; |
| 111 | + Length : Natural) return Subprogram_Result; |
| 112 | + -- To accept connections, a socket is first created with |
| 113 | + -- Create_Socket, a willingness to accept incoming connections and |
| 114 | + -- a queue Length for incoming connections are specified. |
| 115 | + -- The queue length of 15 is an example value that should be |
| 116 | + -- appropriate in usual cases. It can be adjusted according to each |
| 117 | + -- application's particular requirements. |
| 118 | + |
| 119 | + procedure Accept_Socket (Server : Socket_Type; |
| 120 | + Socket : out Optional_Socket; |
| 121 | + Address : out Sock_Addr_Type; |
| 122 | + Result : out Subprogram_Result) with |
| 123 | + Post => (if Result = Success then Socket.Exists else not Socket.Exists); |
| 124 | + |
| 125 | + procedure To_C (Item : String; |
| 126 | + Target : out Byte_Array; |
| 127 | + Count : out Byte_Index) with |
| 128 | + Pre => Item'Length <= Target'Length, |
| 129 | + Post => Count <= Target'Last; |
| 130 | + |
| 131 | + procedure To_Ada (Item : Byte_Array; |
| 132 | + Target : out String; |
| 133 | + Count : out Natural) with |
| 134 | + Pre => Item'Length <= Target'Length, |
| 135 | + Post => Count <= Target'Last; |
| 136 | + |
| 137 | +end SPARK_Sockets; |
0 commit comments