Skip to content

Commit 71b28ca

Browse files
Added Initialize and Finalize functions to initialize and cleanup resources of the WolfSSL library. Removed definitions of exceptions.
1 parent f49ffc0 commit 71b28ca

4 files changed

Lines changed: 41 additions & 21 deletions

File tree

wrapper/Ada/tls_client.adb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ package body Tls_Client with SPARK_Mode is
131131

132132
Result : WolfSSL.Subprogram_Result;
133133
begin
134+
Result := WolfSSL.Initialize;
135+
if Result = Failure then
136+
Put_Line ("ERROR: Failed to initialize the WolfSSL library.");
137+
return;
138+
end if;
139+
134140
if Argument_Count < 1 then
135141
Put_Line ("usage: tcl_client <IPv4 address>");
136142
return;
@@ -297,7 +303,10 @@ package body Tls_Client with SPARK_Mode is
297303
SPARK_Sockets.Close_Socket (C);
298304
WolfSSL.Free (Ssl);
299305
WolfSSL.Free (Context => Ctx);
300-
WolfSSL.Finalize;
306+
Result := WolfSSL.Finalize;
307+
if Result = Failure then
308+
Put_Line ("ERROR: Failed to finalize the WolfSSL library.");
309+
end if;
301310
end Run;
302311

303312
end Tls_Client;

wrapper/Ada/tls_server.adb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ package body Tls_Server with SPARK_Mode is
111111
Input : WolfSSL.Read_Result;
112112
Option : Option_Type;
113113
begin
114+
Result := WolfSSL.Initialize;
115+
if Result = Failure then
116+
Put_Line ("ERROR: Failed to initialize the WolfSSL library.");
117+
return;
118+
end if;
119+
114120
SPARK_Sockets.Create_Socket (Socket => L);
115121
if not L.Exists then
116122
Put_Line ("ERROR: Failed to create socket.");
@@ -308,7 +314,11 @@ package body Tls_Server with SPARK_Mode is
308314
end loop;
309315
SPARK_Sockets.Close_Socket (L);
310316
WolfSSL.Free (Context => Ctx);
311-
WolfSSL.Finalize;
317+
Result := WolfSSL.Finalize;
318+
if Result = Failure then
319+
Put_Line ("ERROR: Failed to finalize the WolfSSL library.");
320+
return;
321+
end if;
312322
end Run;
313323

314324
end Tls_Server;

wrapper/Ada/wolfssl.adb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,23 @@ package body WolfSSL is
4444
External_Name => "wolfSSL_Cleanup",
4545
Import => True;
4646

47-
procedure Finalize is
47+
function Initialize return Subprogram_Result is
48+
Result : constant int := Initialize_WolfSSL;
49+
begin
50+
if Result = WOLFSSL_SUCCESS then
51+
return Success;
52+
else
53+
return Failure;
54+
end if;
55+
end Initialize;
56+
57+
function Finalize return Subprogram_Result is
4858
Result : constant int := Finalize_WolfSSL;
4959
begin
50-
if Result /= WOLFSSL_SUCCESS then
51-
raise Cleanup_Error;
60+
if Result = WOLFSSL_SUCCESS then
61+
return Success;
62+
else
63+
return Failure;
5264
end if;
5365
end Finalize;
5466

@@ -728,9 +740,4 @@ package body WolfSSL is
728740
Ssl := null;
729741
end Free;
730742

731-
Result : constant int := Initialize_WolfSSL;
732-
begin
733-
if Result /= WOLFSSL_SUCCESS then
734-
raise Initialization_Error;
735-
end if;
736743
end WolfSSL;

wrapper/Ada/wolfssl.ads

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,20 @@ with Interfaces.C;
2525
-- the API of this package is used correctly.
2626
package WolfSSL with SPARK_Mode is
2727

28-
procedure Finalize;
29-
-- Must be called before application exit.
28+
type Subprogram_Result is (Success, Failure);
3029

31-
Initialization_Error : exception;
32-
-- Raised if error was encountered during initialization of the
33-
-- WolfSSL library. The WolfSSL libray is initialized during
34-
-- elaboration time.
30+
function Initialize return Subprogram_Result;
31+
-- Must be called before usage of the WolfSSL library.
3532

36-
Cleanup_Error : exception;
37-
-- Raised if error was encountered during application shutdown
38-
-- and cleanup of resources allocated by WolfSSL has failed.
33+
function Finalize return Subprogram_Result;
34+
-- Must be called before application exit to cleanup resources.
3935

4036
subtype char_array is Interfaces.C.char_array; -- Remove?
4137

4238
subtype Byte_Type is Interfaces.C.char;
4339
subtype Byte_Index is Interfaces.C.size_t range 0 .. 16_000;
4440
subtype Byte_Array is Interfaces.C.char_array;
4541

46-
type Subprogram_Result is (Success, Failure);
47-
4842
type Context_Type is limited private;
4943

5044
function Is_Valid (Context : Context_Type) return Boolean;

0 commit comments

Comments
 (0)