2
0

spark_sockets.adb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Interfaces.C;
  22. package body SPARK_Sockets is
  23. function Inet_Addr (Image : String) return Optional_Inet_Addr is
  24. A : Inet_Addr_Type;
  25. begin
  26. A := GNAT.Sockets.Inet_Addr (Image);
  27. return (Exists => True, Addr => A);
  28. exception
  29. when others =>
  30. return (Exists => False);
  31. end Inet_Addr;
  32. procedure Create_Socket (Socket : in out Optional_Socket) is
  33. S : Socket_Type;
  34. begin
  35. GNAT.Sockets.Create_Socket (S);
  36. Socket := (Exists => True, Socket => S);
  37. exception
  38. when others =>
  39. Socket := (Exists => False);
  40. end Create_Socket;
  41. function Connect_Socket (Socket : Socket_Type;
  42. Server : Sock_Addr_Type)
  43. return Subprogram_Result is
  44. begin
  45. GNAT.Sockets.Connect_Socket (Socket, Server);
  46. return Success;
  47. exception
  48. when others =>
  49. return Failure;
  50. end Connect_Socket;
  51. function To_C (Socket : Socket_Type) return Integer is
  52. begin
  53. -- The call to GNAT.Sockets.To_C can never raise an exception.
  54. return GNAT.Sockets.To_C (Socket);
  55. end To_C;
  56. procedure Close_Socket (Socket : in out Optional_Socket) is
  57. begin
  58. GNAT.Sockets.Close_Socket (Socket.Socket);
  59. Socket := (Exists => False);
  60. end Close_Socket;
  61. function Set_Socket_Option (Socket : Socket_Type;
  62. Level : Level_Type;
  63. Option : Option_Type)
  64. return Subprogram_Result is
  65. begin
  66. GNAT.Sockets.Set_Socket_Option (Socket, Level, Option);
  67. return Success;
  68. exception
  69. when others =>
  70. return Failure;
  71. end Set_Socket_Option;
  72. function Bind_Socket (Socket : Socket_Type;
  73. Address : Sock_Addr_Type)
  74. return Subprogram_Result is
  75. begin
  76. GNAT.Sockets.Bind_Socket (Socket, Address);
  77. return Success;
  78. exception
  79. when others =>
  80. return Failure;
  81. end Bind_Socket;
  82. function Listen_Socket (Socket : Socket_Type;
  83. Length : Natural) return Subprogram_Result is
  84. begin
  85. GNAT.Sockets.Listen_Socket (Socket, Length);
  86. return Success;
  87. exception
  88. when others =>
  89. return Failure;
  90. end Listen_Socket;
  91. procedure Accept_Socket (Server : Socket_Type;
  92. Socket : out Optional_Socket;
  93. Address : out Sock_Addr_Type;
  94. Result : out Subprogram_Result) is
  95. C : Socket_Type;
  96. begin
  97. GNAT.Sockets.Accept_Socket (Server, C, Address);
  98. Socket := (Exists => True, Socket => C);
  99. Result := Success;
  100. exception
  101. when others =>
  102. Socket := (Exists => False);
  103. Address := (Family => GNAT.Sockets.Family_Unspec);
  104. Result := Failure;
  105. end Accept_Socket;
  106. procedure To_C (Item : String;
  107. Target : out Byte_Array;
  108. Count : out Byte_Index) is
  109. begin
  110. Interfaces.C.To_C (Item => Item,
  111. Target => Target,
  112. Count => Count,
  113. Append_Nul => False);
  114. end To_C;
  115. procedure To_Ada (Item : Byte_Array;
  116. Target : out String;
  117. Count : out Natural) is
  118. begin
  119. Interfaces.C.To_Ada (Item => Item,
  120. Target => Target,
  121. Count => Count,
  122. Trim_Nul => False);
  123. end To_Ada;
  124. end SPARK_Sockets;