1
0

spark_sockets.adb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. -- spark_sockets.adb
  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. with Ada.Streams;
  22. with Interfaces.C;
  23. package body SPARK_Sockets is
  24. function Inet_Addr (Image : String) return Optional_Inet_Addr is
  25. A : Inet_Addr_Type;
  26. begin
  27. A := GNAT.Sockets.Inet_Addr (Image);
  28. return (Exists => True, Addr => A);
  29. exception
  30. when others =>
  31. return (Exists => False);
  32. end Inet_Addr;
  33. procedure Create_Socket
  34. (Socket : in out Optional_Socket;
  35. Family : GNAT.Sockets.Family_Type;
  36. Mode : GNAT.Sockets.Mode_Type) is
  37. S : Socket_Type;
  38. begin
  39. GNAT.Sockets.Create_Socket (S, Family, Mode);
  40. Socket := (Exists => True, Socket => S);
  41. exception
  42. when others =>
  43. Socket := (Exists => False);
  44. end Create_Socket;
  45. procedure Create_Stream_Socket (Socket : in out Optional_Socket) is
  46. begin
  47. Create_Socket
  48. (Socket => Socket,
  49. Family => GNAT.Sockets.Family_Inet,
  50. Mode => GNAT.Sockets.Socket_Stream);
  51. end Create_Stream_Socket;
  52. procedure Create_Datagram_Socket (Socket : in out Optional_Socket) is
  53. begin
  54. Create_Socket
  55. (Socket => Socket,
  56. Family => GNAT.Sockets.Family_Inet,
  57. Mode => GNAT.Sockets.Socket_Datagram);
  58. end Create_Datagram_Socket;
  59. function Connect_Socket (Socket : Socket_Type;
  60. Server : Sock_Addr_Type)
  61. return Subprogram_Result is
  62. begin
  63. GNAT.Sockets.Connect_Socket (Socket, Server);
  64. return Success;
  65. exception
  66. when others =>
  67. return Failure;
  68. end Connect_Socket;
  69. function To_C (Socket : Socket_Type) return Integer is
  70. begin
  71. -- The call to GNAT.Sockets.To_C can never raise an exception.
  72. return GNAT.Sockets.To_C (Socket);
  73. end To_C;
  74. procedure Close_Socket (Socket : in out Optional_Socket) is
  75. begin
  76. GNAT.Sockets.Close_Socket (Socket.Socket);
  77. Socket := (Exists => False);
  78. end Close_Socket;
  79. function Set_Socket_Option (Socket : Socket_Type;
  80. Level : Level_Type;
  81. Option : Option_Type)
  82. return Subprogram_Result is
  83. begin
  84. GNAT.Sockets.Set_Socket_Option (Socket, Level, Option);
  85. return Success;
  86. exception
  87. when others =>
  88. return Failure;
  89. end Set_Socket_Option;
  90. function Bind_Socket (Socket : Socket_Type;
  91. Address : Sock_Addr_Type)
  92. return Subprogram_Result is
  93. begin
  94. GNAT.Sockets.Bind_Socket (Socket, Address);
  95. return Success;
  96. exception
  97. when others =>
  98. return Failure;
  99. end Bind_Socket;
  100. function Listen_Socket (Socket : Socket_Type;
  101. Length : Natural) return Subprogram_Result is
  102. begin
  103. GNAT.Sockets.Listen_Socket (Socket, Length);
  104. return Success;
  105. exception
  106. when others =>
  107. return Failure;
  108. end Listen_Socket;
  109. function Receive_Socket
  110. (Socket : Socket_Type)
  111. return Subprogram_Result is
  112. Item : Ada.Streams.Stream_Element_Array (1 .. 4096);
  113. Last : Ada.Streams.Stream_Element_Offset;
  114. From : GNAT.Sockets.Sock_Addr_Type;
  115. begin
  116. GNAT.Sockets.Receive_Socket (Socket, Item, Last, From);
  117. return Success;
  118. exception
  119. when others =>
  120. return Failure;
  121. end Receive_Socket;
  122. procedure Accept_Socket (Server : Socket_Type;
  123. Socket : out Optional_Socket;
  124. Address : out Sock_Addr_Type;
  125. Result : out Subprogram_Result) is
  126. C : Socket_Type;
  127. begin
  128. GNAT.Sockets.Accept_Socket (Server, C, Address);
  129. Socket := (Exists => True, Socket => C);
  130. Result := Success;
  131. exception
  132. when others =>
  133. Socket := (Exists => False);
  134. Address := (Family => GNAT.Sockets.Family_Unspec);
  135. Result := Failure;
  136. end Accept_Socket;
  137. procedure To_C (Item : String;
  138. Target : out Byte_Array;
  139. Count : out Byte_Index) is
  140. begin
  141. Interfaces.C.To_C (Item => Item,
  142. Target => Target,
  143. Count => Count,
  144. Append_Nul => False);
  145. end To_C;
  146. procedure To_Ada (Item : Byte_Array;
  147. Target : out String;
  148. Count : out Natural) is
  149. begin
  150. Interfaces.C.To_Ada (Item => Item,
  151. Target => Target,
  152. Count => Count,
  153. Trim_Nul => False);
  154. end To_Ada;
  155. end SPARK_Sockets;