| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . S O C K E T S . T H I N -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2002-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This package provides a target dependent thin interface to the sockets |
| 33 |
-- layer for use by the GNAT.Sockets package (g-socket.ads). This package |
| 34 |
-- should not be directly with'ed by an applications program. |
| 35 |
|
| 36 |
-- This version is for VxWorks |
| 37 |
|
| 38 |
with GNAT.OS_Lib; use GNAT.OS_Lib; |
| 39 |
with GNAT.Task_Lock; |
| 40 |
|
| 41 |
with Interfaces.C; use Interfaces.C; |
| 42 |
|
| 43 |
package body GNAT.Sockets.Thin is |
| 44 |
|
| 45 |
Non_Blocking_Sockets : aliased Fd_Set; |
| 46 |
-- When this package is initialized with Process_Blocking_IO set |
| 47 |
-- to True, sockets are set in non-blocking mode to avoid blocking |
| 48 |
-- the whole process when a thread wants to perform a blocking IO |
| 49 |
-- operation. But the user can also set a socket in non-blocking |
| 50 |
-- mode by purpose. In order to make a difference between these |
| 51 |
-- two situations, we track the origin of non-blocking mode in |
| 52 |
-- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has |
| 53 |
-- been set in non-blocking mode by the user. |
| 54 |
|
| 55 |
Quantum : constant Duration := 0.2; |
| 56 |
-- When SOSC.Thread_Blocking_IO is False, we set sockets in |
| 57 |
-- non-blocking mode and we spend a period of time Quantum between |
| 58 |
-- two attempts on a blocking operation. |
| 59 |
|
| 60 |
----------------------- |
| 61 |
-- Local Subprograms -- |
| 62 |
----------------------- |
| 63 |
|
| 64 |
-- All these require comments ??? |
| 65 |
|
| 66 |
function Syscall_Accept |
| 67 |
(S : C.int; |
| 68 |
Addr : System.Address; |
| 69 |
Addrlen : not null access C.int) return C.int; |
| 70 |
pragma Import (C, Syscall_Accept, "accept"); |
| 71 |
|
| 72 |
function Syscall_Connect |
| 73 |
(S : C.int; |
| 74 |
Name : System.Address; |
| 75 |
Namelen : C.int) return C.int; |
| 76 |
pragma Import (C, Syscall_Connect, "connect"); |
| 77 |
|
| 78 |
function Syscall_Recv |
| 79 |
(S : C.int; |
| 80 |
Msg : System.Address; |
| 81 |
Len : C.size_t; |
| 82 |
Flags : C.int) return C.int; |
| 83 |
pragma Import (C, Syscall_Recv, "recv"); |
| 84 |
|
| 85 |
function Syscall_Recvfrom |
| 86 |
(S : C.int; |
| 87 |
Msg : System.Address; |
| 88 |
Len : C.size_t; |
| 89 |
Flags : C.int; |
| 90 |
From : System.Address; |
| 91 |
Fromlen : not null access C.int) return C.int; |
| 92 |
pragma Import (C, Syscall_Recvfrom, "recvfrom"); |
| 93 |
|
| 94 |
function Syscall_Recvmsg |
| 95 |
(S : C.int; |
| 96 |
Msg : System.Address; |
| 97 |
Flags : C.int) return C.int; |
| 98 |
pragma Import (C, Syscall_Recvmsg, "recvmsg"); |
| 99 |
|
| 100 |
function Syscall_Sendmsg |
| 101 |
(S : C.int; |
| 102 |
Msg : System.Address; |
| 103 |
Flags : C.int) return C.int; |
| 104 |
pragma Import (C, Syscall_Sendmsg, "sendmsg"); |
| 105 |
|
| 106 |
function Syscall_Send |
| 107 |
(S : C.int; |
| 108 |
Msg : System.Address; |
| 109 |
Len : C.size_t; |
| 110 |
Flags : C.int) return System.CRTL.ssize_t; |
| 111 |
pragma Import (C, Syscall_Send, "send"); |
| 112 |
|
| 113 |
function Syscall_Sendto |
| 114 |
(S : C.int; |
| 115 |
Msg : System.Address; |
| 116 |
Len : C.size_t; |
| 117 |
Flags : C.int; |
| 118 |
To : System.Address; |
| 119 |
Tolen : C.int) return System.CRTL.ssize_t; |
| 120 |
pragma Import (C, Syscall_Sendto, "sendto"); |
| 121 |
|
| 122 |
function Syscall_Socket |
| 123 |
(Domain : C.int; |
| 124 |
Typ : C.int; |
| 125 |
Protocol : C.int) return C.int; |
| 126 |
pragma Import (C, Syscall_Socket, "socket"); |
| 127 |
|
| 128 |
function Non_Blocking_Socket (S : C.int) return Boolean; |
| 129 |
procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean); |
| 130 |
|
| 131 |
-------------- |
| 132 |
-- C_Accept -- |
| 133 |
-------------- |
| 134 |
|
| 135 |
function C_Accept |
| 136 |
(S : C.int; |
| 137 |
Addr : System.Address; |
| 138 |
Addrlen : not null access C.int) return C.int |
| 139 |
is |
| 140 |
R : C.int; |
| 141 |
Val : aliased C.int := 1; |
| 142 |
|
| 143 |
Res : C.int; |
| 144 |
pragma Unreferenced (Res); |
| 145 |
|
| 146 |
begin |
| 147 |
loop |
| 148 |
R := Syscall_Accept (S, Addr, Addrlen); |
| 149 |
exit when SOSC.Thread_Blocking_IO |
| 150 |
or else R /= Failure |
| 151 |
or else Non_Blocking_Socket (S) |
| 152 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 153 |
delay Quantum; |
| 154 |
end loop; |
| 155 |
|
| 156 |
if not SOSC.Thread_Blocking_IO |
| 157 |
and then R /= Failure |
| 158 |
then |
| 159 |
-- A socket inherits the properties of its server especially |
| 160 |
-- the FIONBIO flag. Do not use Socket_Ioctl as this subprogram |
| 161 |
-- tracks sockets set in non-blocking mode by user. |
| 162 |
|
| 163 |
Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S)); |
| 164 |
Res := C_Ioctl (R, SOSC.FIONBIO, Val'Access); |
| 165 |
-- Is it OK to ignore result ??? |
| 166 |
end if; |
| 167 |
|
| 168 |
return R; |
| 169 |
end C_Accept; |
| 170 |
|
| 171 |
--------------- |
| 172 |
-- C_Connect -- |
| 173 |
--------------- |
| 174 |
|
| 175 |
function C_Connect |
| 176 |
(S : C.int; |
| 177 |
Name : System.Address; |
| 178 |
Namelen : C.int) return C.int |
| 179 |
is |
| 180 |
Res : C.int; |
| 181 |
|
| 182 |
begin |
| 183 |
Res := Syscall_Connect (S, Name, Namelen); |
| 184 |
|
| 185 |
if SOSC.Thread_Blocking_IO |
| 186 |
or else Res /= Failure |
| 187 |
or else Non_Blocking_Socket (S) |
| 188 |
or else Errno /= SOSC.EINPROGRESS |
| 189 |
then |
| 190 |
return Res; |
| 191 |
end if; |
| 192 |
|
| 193 |
pragma Warnings (Off, "unreachable code"); |
| 194 |
declare -- unreachable if Thread_Blocking_IO is statically True |
| 195 |
pragma Warnings (On, "unreachable code"); |
| 196 |
WSet : aliased Fd_Set; |
| 197 |
Now : aliased Timeval; |
| 198 |
begin |
| 199 |
Reset_Socket_Set (WSet'Access); |
| 200 |
loop |
| 201 |
Insert_Socket_In_Set (WSet'Access, S); |
| 202 |
Now := Immediat; |
| 203 |
Res := C_Select |
| 204 |
(S + 1, |
| 205 |
No_Fd_Set_Access, |
| 206 |
WSet'Access, |
| 207 |
No_Fd_Set_Access, |
| 208 |
Now'Unchecked_Access); |
| 209 |
|
| 210 |
exit when Res > 0; |
| 211 |
|
| 212 |
if Res = Failure then |
| 213 |
return Res; |
| 214 |
end if; |
| 215 |
|
| 216 |
delay Quantum; |
| 217 |
end loop; |
| 218 |
end; |
| 219 |
|
| 220 |
Res := Syscall_Connect (S, Name, Namelen); |
| 221 |
|
| 222 |
if Res = Failure |
| 223 |
and then Errno = SOSC.EISCONN |
| 224 |
then |
| 225 |
return Thin_Common.Success; |
| 226 |
else |
| 227 |
return Res; |
| 228 |
end if; |
| 229 |
end C_Connect; |
| 230 |
|
| 231 |
------------------ |
| 232 |
-- Socket_Ioctl -- |
| 233 |
------------------ |
| 234 |
|
| 235 |
function Socket_Ioctl |
| 236 |
(S : C.int; |
| 237 |
Req : SOSC.IOCTL_Req_T; |
| 238 |
Arg : access C.int) return C.int |
| 239 |
is |
| 240 |
begin |
| 241 |
if not SOSC.Thread_Blocking_IO and then Req = SOSC.FIONBIO then |
| 242 |
if Arg.all /= 0 then |
| 243 |
Set_Non_Blocking_Socket (S, True); |
| 244 |
end if; |
| 245 |
end if; |
| 246 |
|
| 247 |
return C_Ioctl (S, Req, Arg); |
| 248 |
end Socket_Ioctl; |
| 249 |
|
| 250 |
------------ |
| 251 |
-- C_Recv -- |
| 252 |
------------ |
| 253 |
|
| 254 |
function C_Recv |
| 255 |
(S : C.int; |
| 256 |
Msg : System.Address; |
| 257 |
Len : C.size_t; |
| 258 |
Flags : C.int) return C.int |
| 259 |
is |
| 260 |
Res : C.int; |
| 261 |
|
| 262 |
begin |
| 263 |
loop |
| 264 |
Res := Syscall_Recv (S, Msg, Len, Flags); |
| 265 |
exit when SOSC.Thread_Blocking_IO |
| 266 |
or else Res /= Failure |
| 267 |
or else Non_Blocking_Socket (S) |
| 268 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 269 |
delay Quantum; |
| 270 |
end loop; |
| 271 |
|
| 272 |
return Res; |
| 273 |
end C_Recv; |
| 274 |
|
| 275 |
---------------- |
| 276 |
-- C_Recvfrom -- |
| 277 |
---------------- |
| 278 |
|
| 279 |
function C_Recvfrom |
| 280 |
(S : C.int; |
| 281 |
Msg : System.Address; |
| 282 |
Len : C.size_t; |
| 283 |
Flags : C.int; |
| 284 |
From : System.Address; |
| 285 |
Fromlen : not null access C.int) return C.int |
| 286 |
is |
| 287 |
Res : C.int; |
| 288 |
|
| 289 |
begin |
| 290 |
loop |
| 291 |
Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen); |
| 292 |
exit when SOSC.Thread_Blocking_IO |
| 293 |
or else Res /= Failure |
| 294 |
or else Non_Blocking_Socket (S) |
| 295 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 296 |
delay Quantum; |
| 297 |
end loop; |
| 298 |
|
| 299 |
return Res; |
| 300 |
end C_Recvfrom; |
| 301 |
|
| 302 |
--------------- |
| 303 |
-- C_Recvmsg -- |
| 304 |
--------------- |
| 305 |
|
| 306 |
function C_Recvmsg |
| 307 |
(S : C.int; |
| 308 |
Msg : System.Address; |
| 309 |
Flags : C.int) return System.CRTL.ssize_t |
| 310 |
is |
| 311 |
Res : C.int; |
| 312 |
|
| 313 |
begin |
| 314 |
loop |
| 315 |
Res := Syscall_Recvmsg (S, Msg, Flags); |
| 316 |
exit when SOSC.Thread_Blocking_IO |
| 317 |
or else Res /= Failure |
| 318 |
or else Non_Blocking_Socket (S) |
| 319 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 320 |
delay Quantum; |
| 321 |
end loop; |
| 322 |
|
| 323 |
return System.CRTL.ssize_t (Res); |
| 324 |
end C_Recvmsg; |
| 325 |
|
| 326 |
--------------- |
| 327 |
-- C_Sendmsg -- |
| 328 |
--------------- |
| 329 |
|
| 330 |
function C_Sendmsg |
| 331 |
(S : C.int; |
| 332 |
Msg : System.Address; |
| 333 |
Flags : C.int) return System.CRTL.ssize_t |
| 334 |
is |
| 335 |
Res : C.int; |
| 336 |
|
| 337 |
begin |
| 338 |
loop |
| 339 |
Res := Syscall_Sendmsg (S, Msg, Flags); |
| 340 |
exit when SOSC.Thread_Blocking_IO |
| 341 |
or else Res /= Failure |
| 342 |
or else Non_Blocking_Socket (S) |
| 343 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 344 |
delay Quantum; |
| 345 |
end loop; |
| 346 |
|
| 347 |
return System.CRTL.ssize_t (Res); |
| 348 |
end C_Sendmsg; |
| 349 |
|
| 350 |
-------------- |
| 351 |
-- C_Sendto -- |
| 352 |
-------------- |
| 353 |
|
| 354 |
function C_Sendto |
| 355 |
(S : C.int; |
| 356 |
Msg : System.Address; |
| 357 |
Len : C.size_t; |
| 358 |
Flags : C.int; |
| 359 |
To : System.Address; |
| 360 |
Tolen : C.int) return C.int |
| 361 |
is |
| 362 |
use System; |
| 363 |
|
| 364 |
Res : C.int; |
| 365 |
|
| 366 |
begin |
| 367 |
loop |
| 368 |
if To = Null_Address then |
| 369 |
|
| 370 |
-- In violation of the standard sockets API, VxWorks does not |
| 371 |
-- support sendto(2) calls on connected sockets with a null |
| 372 |
-- destination address, so use send(2) instead in that case. |
| 373 |
|
| 374 |
Res := C.int (Syscall_Send (S, Msg, Len, Flags)); |
| 375 |
|
| 376 |
-- Normal case where destination address is non-null |
| 377 |
|
| 378 |
else |
| 379 |
Res := C.int (Syscall_Sendto (S, Msg, Len, Flags, To, Tolen)); |
| 380 |
end if; |
| 381 |
|
| 382 |
exit when SOSC.Thread_Blocking_IO |
| 383 |
or else Res /= Failure |
| 384 |
or else Non_Blocking_Socket (S) |
| 385 |
or else Errno /= SOSC.EWOULDBLOCK; |
| 386 |
delay Quantum; |
| 387 |
end loop; |
| 388 |
|
| 389 |
return Res; |
| 390 |
end C_Sendto; |
| 391 |
|
| 392 |
-------------- |
| 393 |
-- C_Socket -- |
| 394 |
-------------- |
| 395 |
|
| 396 |
function C_Socket |
| 397 |
(Domain : C.int; |
| 398 |
Typ : C.int; |
| 399 |
Protocol : C.int) return C.int |
| 400 |
is |
| 401 |
R : C.int; |
| 402 |
Val : aliased C.int := 1; |
| 403 |
|
| 404 |
Res : C.int; |
| 405 |
pragma Unreferenced (Res); |
| 406 |
|
| 407 |
begin |
| 408 |
R := Syscall_Socket (Domain, Typ, Protocol); |
| 409 |
|
| 410 |
if not SOSC.Thread_Blocking_IO |
| 411 |
and then R /= Failure |
| 412 |
then |
| 413 |
-- Do not use Socket_Ioctl as this subprogram tracks sockets set |
| 414 |
-- in non-blocking mode by user. |
| 415 |
|
| 416 |
Res := C_Ioctl (R, SOSC.FIONBIO, Val'Access); |
| 417 |
-- Is it OK to ignore result ??? |
| 418 |
Set_Non_Blocking_Socket (R, False); |
| 419 |
end if; |
| 420 |
|
| 421 |
return R; |
| 422 |
end C_Socket; |
| 423 |
|
| 424 |
------------------ |
| 425 |
-- C_Socketpair -- |
| 426 |
------------------ |
| 427 |
|
| 428 |
function C_Socketpair |
| 429 |
(Domain : C.int; |
| 430 |
Typ : C.int; |
| 431 |
Protocol : C.int; |
| 432 |
Fds : not null access Fd_Pair) return C.int is separate; |
| 433 |
|
| 434 |
-------------- |
| 435 |
-- Finalize -- |
| 436 |
-------------- |
| 437 |
|
| 438 |
procedure Finalize is |
| 439 |
begin |
| 440 |
null; |
| 441 |
end Finalize; |
| 442 |
|
| 443 |
------------------------- |
| 444 |
-- Host_Error_Messages -- |
| 445 |
------------------------- |
| 446 |
|
| 447 |
package body Host_Error_Messages is separate; |
| 448 |
|
| 449 |
---------------- |
| 450 |
-- Initialize -- |
| 451 |
---------------- |
| 452 |
|
| 453 |
procedure Initialize is |
| 454 |
begin |
| 455 |
Reset_Socket_Set (Non_Blocking_Sockets'Access); |
| 456 |
end Initialize; |
| 457 |
|
| 458 |
------------------------- |
| 459 |
-- Non_Blocking_Socket -- |
| 460 |
------------------------- |
| 461 |
|
| 462 |
function Non_Blocking_Socket (S : C.int) return Boolean is |
| 463 |
R : Boolean; |
| 464 |
begin |
| 465 |
Task_Lock.Lock; |
| 466 |
R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0); |
| 467 |
Task_Lock.Unlock; |
| 468 |
return R; |
| 469 |
end Non_Blocking_Socket; |
| 470 |
|
| 471 |
----------------------------- |
| 472 |
-- Set_Non_Blocking_Socket -- |
| 473 |
----------------------------- |
| 474 |
|
| 475 |
procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is |
| 476 |
begin |
| 477 |
Task_Lock.Lock; |
| 478 |
if V then |
| 479 |
Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S); |
| 480 |
else |
| 481 |
Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S); |
| 482 |
end if; |
| 483 |
|
| 484 |
Task_Lock.Unlock; |
| 485 |
end Set_Non_Blocking_Socket; |
| 486 |
|
| 487 |
-------------------- |
| 488 |
-- Signalling_Fds -- |
| 489 |
-------------------- |
| 490 |
|
| 491 |
package body Signalling_Fds is separate; |
| 492 |
|
| 493 |
-------------------------- |
| 494 |
-- Socket_Error_Message -- |
| 495 |
-------------------------- |
| 496 |
|
| 497 |
function Socket_Error_Message (Errno : Integer) return String is separate; |
| 498 |
|
| 499 |
end GNAT.Sockets.Thin; |