Debian Codegraph @

gcc/ada/libgnat/g-socthi.adb

raw
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) 2001-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 is the default version
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 -- Comments required for following functions ???
61
62 function Syscall_Accept
63 (S : C.int;
64 Addr : System.Address;
65 Addrlen : not null access C.unsigned) return C.int;
66 pragma Import (C, Syscall_Accept, "accept");
67
68 function Syscall_Connect
69 (S : C.int;
70 Name : System.Address;
71 Namelen : C.unsigned) return C.int;
72 pragma Import (C, Syscall_Connect, "connect");
73
74 function Syscall_Recv
75 (S : C.int;
76 Msg : System.Address;
77 Len : C.size_t;
78 Flags : C.int) return System.CRTL.ssize_t;
79 pragma Import (C, Syscall_Recv, "recv");
80
81 function Syscall_Recvfrom
82 (S : C.int;
83 Msg : System.Address;
84 Len : C.size_t;
85 Flags : C.int;
86 From : System.Address;
87 Fromlen : not null access C.unsigned) return System.CRTL.ssize_t;
88 pragma Import (C, Syscall_Recvfrom, "recvfrom");
89
90 function Syscall_Recvmsg
91 (S : C.int;
92 Msg : System.Address;
93 Flags : C.int) return System.CRTL.ssize_t;
94 pragma Import (C, Syscall_Recvmsg, "recvmsg");
95
96 function Syscall_Sendmsg
97 (S : C.int;
98 Msg : System.Address;
99 Flags : C.int) return System.CRTL.ssize_t;
100 pragma Import (C, Syscall_Sendmsg, "sendmsg");
101
102 function Syscall_Sendto
103 (S : C.int;
104 Msg : System.Address;
105 Len : C.size_t;
106 Flags : C.int;
107 To : System.Address;
108 Tolen : C.unsigned) return System.CRTL.ssize_t;
109 pragma Import (C, Syscall_Sendto, "sendto");
110
111 function Syscall_Socket
112 (Domain : C.int;
113 Typ : C.int;
114 Protocol : C.int) return C.int;
115 pragma Import (C, Syscall_Socket, "socket");
116
117 procedure Disable_SIGPIPE (S : C.int);
118 pragma Import (C, Disable_SIGPIPE, "__gnat_disable_sigpipe");
119
120 procedure Disable_All_SIGPIPEs;
121 pragma Import (C, Disable_All_SIGPIPEs, "__gnat_disable_all_sigpipes");
122 -- Sets the process to ignore all SIGPIPE signals on platforms that
123 -- don't support Disable_SIGPIPE for particular streams.
124
125 function Non_Blocking_Socket (S : C.int) return Boolean;
126 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
127
128 ------------
129 -- C_Bind --
130 ------------
131
132 function C_Bind
133 (S : C.int;
134 Name : System.Address;
135 Namelen : C.int) return C.int
136 is
137 function Bind
138 (S : C.int;
139 Name : System.Address;
140 Namelen : C.unsigned) return C.int
141 with Import, Convention => C, External_Name => "bind";
142
143 begin
144 return Bind (S, Name, C.unsigned (Namelen));
145 end C_Bind;
146
147 --------------
148 -- C_Accept --
149 --------------
150
151 function C_Accept
152 (S : C.int;
153 Addr : System.Address;
154 Addrlen : not null access C.int) return C.int
155 is
156 R : C.int;
157 U_Addrlen : aliased C.unsigned;
158 Val : aliased C.int := 1;
159
160 Discard : C.int;
161 pragma Warnings (Off, Discard);
162
163 begin
164 U_Addrlen := C.unsigned (Addrlen.all);
165
166 loop
167 R := Syscall_Accept (S, Addr, U_Addrlen'Unchecked_Access);
168 exit when SOSC.Thread_Blocking_IO
169 or else R /= Failure
170 or else Non_Blocking_Socket (S)
171 or else Errno /= SOSC.EWOULDBLOCK;
172 delay Quantum;
173 end loop;
174
175 Addrlen.all := C.int (U_Addrlen);
176
177 if not SOSC.Thread_Blocking_IO
178 and then R /= Failure
179 then
180 -- A socket inherits the properties ot its server especially
181 -- the FIONBIO flag. Do not use Socket_Ioctl as this subprogram
182 -- tracks sockets set in non-blocking mode by user.
183
184 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
185 Discard := C_Ioctl (R, SOSC.FIONBIO, Val'Access);
186 end if;
187
188 Disable_SIGPIPE (R);
189 return R;
190 end C_Accept;
191
192 ---------------
193 -- C_Connect --
194 ---------------
195
196 function C_Connect
197 (S : C.int;
198 Name : System.Address;
199 Namelen : C.int) return C.int
200 is
201 Res : C.int;
202
203 begin
204 Res := Syscall_Connect (S, Name, C.unsigned (Namelen));
205
206 if SOSC.Thread_Blocking_IO
207 or else Res /= Failure
208 or else Non_Blocking_Socket (S)
209 or else Errno /= SOSC.EINPROGRESS
210 then
211 return Res;
212 end if;
213
214 pragma Warnings (Off, "unreachable code");
215 declare -- unreachable if Thread_Blocking_IO is statically True
216 pragma Warnings (On, "unreachable code");
217 WSet : aliased Fd_Set;
218 Now : aliased Timeval;
219
220 begin
221 Reset_Socket_Set (WSet'Access);
222 loop
223 Insert_Socket_In_Set (WSet'Access, S);
224 Now := Immediat;
225 Res := C_Select
226 (S + 1,
227 No_Fd_Set_Access,
228 WSet'Access,
229 No_Fd_Set_Access,
230 Now'Unchecked_Access);
231
232 exit when Res > 0;
233
234 if Res = Failure then
235 return Res;
236 end if;
237
238 delay Quantum;
239 end loop;
240 end;
241
242 Res := Syscall_Connect (S, Name, C.unsigned (Namelen));
243
244 if Res = Failure
245 and then Errno = SOSC.EISCONN
246 then
247 return Thin_Common.Success;
248 else
249 return Res;
250 end if;
251 end C_Connect;
252
253 -------------------
254 -- C_Getpeername --
255 -------------------
256
257 function C_Getpeername
258 (S : C.int;
259 Name : System.Address;
260 Namelen : not null access C.int) return C.int
261 is
262 function Getpeername
263 (S : C.int;
264 Name : System.Address;
265 Namelen : not null access C.unsigned) return C.int
266 with Import, Convention => C, External_Name => "getpeername";
267
268 U_Namelen : aliased C.unsigned;
269 Val : C.int;
270
271 begin
272 U_Namelen := C.unsigned (Namelen.all);
273 Val := Getpeername (S, Name, U_Namelen'Unchecked_Access);
274 Namelen.all := C.int (U_Namelen);
275 return Val;
276 end C_Getpeername;
277
278 -------------------
279 -- C_Getsockname --
280 -------------------
281
282 function C_Getsockname
283 (S : C.int;
284 Name : System.Address;
285 Namelen : not null access C.int) return C.int
286 is
287 function Getsockname
288 (S : C.int;
289 Name : System.Address;
290 Namelen : not null access C.unsigned) return C.int
291 with Import, Convention => C, External_Name => "getsockname";
292
293 U_Namelen : aliased C.unsigned;
294 Val : C.int;
295
296 begin
297 U_Namelen := C.unsigned (Namelen.all);
298 Val := Getsockname (S, Name, U_Namelen'Unchecked_Access);
299 Namelen.all := C.int (U_Namelen);
300 return Val;
301 end C_Getsockname;
302
303 -------------------
304 -- C_Getsockopt --
305 -------------------
306
307 function C_Getsockopt
308 (S : C.int;
309 Level : C.int;
310 Optname : C.int;
311 Optval : System.Address;
312 Optlen : not null access C.int) return C.int
313 is
314 function Getsockopt
315 (S : C.int;
316 Level : C.int;
317 Optname : C.int;
318 Optval : System.Address;
319 Optlen : not null access C.unsigned) return C.int
320 with Import, Convention => C, External_Name => "getsockopt";
321
322 U_Optlen : aliased C.unsigned;
323 Val : C.int;
324
325 begin
326 U_Optlen := C.unsigned (Optlen.all);
327 Val := Getsockopt (S, Level, Optname, Optval, U_Optlen'Unchecked_Access);
328 Optlen.all := C.int (U_Optlen);
329 return Val;
330 end C_Getsockopt;
331
332 ------------------
333 -- Socket_Ioctl --
334 ------------------
335
336 function Socket_Ioctl
337 (S : C.int;
338 Req : SOSC.IOCTL_Req_T;
339 Arg : access C.int) return C.int
340 is
341 begin
342 if not SOSC.Thread_Blocking_IO and then Req = SOSC.FIONBIO then
343 if Arg.all /= 0 then
344 Set_Non_Blocking_Socket (S, True);
345 end if;
346 end if;
347
348 return C_Ioctl (S, Req, Arg);
349 end Socket_Ioctl;
350
351 ------------
352 -- C_Recv --
353 ------------
354
355 function C_Recv
356 (S : C.int;
357 Msg : System.Address;
358 Len : C.size_t;
359 Flags : C.int) return C.int
360 is
361 Res : C.int;
362
363 begin
364 loop
365 Res := C.int (Syscall_Recv (S, Msg, Len, Flags));
366 exit when SOSC.Thread_Blocking_IO
367 or else Res /= Failure
368 or else Non_Blocking_Socket (S)
369 or else Errno /= SOSC.EWOULDBLOCK;
370 delay Quantum;
371 end loop;
372
373 return Res;
374 end C_Recv;
375
376 ----------------
377 -- C_Recvfrom --
378 ----------------
379
380 function C_Recvfrom
381 (S : C.int;
382 Msg : System.Address;
383 Len : C.size_t;
384 Flags : C.int;
385 From : System.Address;
386 Fromlen : not null access C.int) return C.int
387 is
388 Res : C.int;
389 U_Fromlen : aliased C.unsigned;
390
391 begin
392 U_Fromlen := C.unsigned (Fromlen.all);
393
394 loop
395 Res := C.int (Syscall_Recvfrom (S, Msg, Len, Flags, From,
396 U_Fromlen'Unchecked_Access));
397 exit when SOSC.Thread_Blocking_IO
398 or else Res /= Failure
399 or else Non_Blocking_Socket (S)
400 or else Errno /= SOSC.EWOULDBLOCK;
401 delay Quantum;
402 end loop;
403
404 Fromlen.all := C.int (U_Fromlen);
405
406 return Res;
407 end C_Recvfrom;
408
409 ---------------
410 -- C_Recvmsg --
411 ---------------
412
413 function C_Recvmsg
414 (S : C.int;
415 Msg : System.Address;
416 Flags : C.int) return System.CRTL.ssize_t
417 is
418 Res : System.CRTL.ssize_t;
419
420 begin
421 loop
422 Res := Syscall_Recvmsg (S, Msg, Flags);
423 exit when SOSC.Thread_Blocking_IO
424 or else Res /= System.CRTL.ssize_t (Failure)
425 or else Non_Blocking_Socket (S)
426 or else Errno /= SOSC.EWOULDBLOCK;
427 delay Quantum;
428 end loop;
429
430 return Res;
431 end C_Recvmsg;
432
433 ---------------
434 -- C_Sendmsg --
435 ---------------
436
437 function C_Sendmsg
438 (S : C.int;
439 Msg : System.Address;
440 Flags : C.int) return System.CRTL.ssize_t
441 is
442 Res : System.CRTL.ssize_t;
443
444 begin
445 loop
446 Res := Syscall_Sendmsg (S, Msg, Flags);
447 exit when SOSC.Thread_Blocking_IO
448 or else Res /= System.CRTL.ssize_t (Failure)
449 or else Non_Blocking_Socket (S)
450 or else Errno /= SOSC.EWOULDBLOCK;
451 delay Quantum;
452 end loop;
453
454 return Res;
455 end C_Sendmsg;
456
457 --------------
458 -- C_Sendto --
459 --------------
460
461 function C_Sendto
462 (S : C.int;
463 Msg : System.Address;
464 Len : C.size_t;
465 Flags : C.int;
466 To : System.Address;
467 Tolen : C.int) return C.int
468 is
469 Res : C.int;
470
471 begin
472 loop
473 Res := C.int (Syscall_Sendto (S, Msg, Len, Flags, To,
474 C.unsigned (Tolen)));
475 exit when SOSC.Thread_Blocking_IO
476 or else Res /= Failure
477 or else Non_Blocking_Socket (S)
478 or else Errno /= SOSC.EWOULDBLOCK;
479 delay Quantum;
480 end loop;
481
482 return Res;
483 end C_Sendto;
484
485 ------------------
486 -- C_Setsockopt --
487 ------------------
488
489 function C_Setsockopt
490 (S : C.int;
491 Level : C.int;
492 Optname : C.int;
493 Optval : System.Address;
494 Optlen : C.int) return C.int
495 is
496 function Setsockopt
497 (S : C.int;
498 Level : C.int;
499 Optname : C.int;
500 Optval : System.Address;
501 Optlen : C.unsigned) return C.int
502 with Import, Convention => C, External_Name => "setsockopt";
503
504 begin
505 return Setsockopt (S, Level, Optname, Optval, C.unsigned (Optlen));
506 end C_Setsockopt;
507
508 --------------
509 -- C_Socket --
510 --------------
511
512 function C_Socket
513 (Domain : C.int;
514 Typ : C.int;
515 Protocol : C.int) return C.int
516 is
517 R : C.int;
518 Val : aliased C.int := 1;
519
520 Discard : C.int;
521
522 begin
523 R := Syscall_Socket (Domain, Typ, Protocol);
524
525 if not SOSC.Thread_Blocking_IO
526 and then R /= Failure
527 then
528 -- Do not use Socket_Ioctl as this subprogram tracks sockets set
529 -- in non-blocking mode by user.
530
531 Discard := C_Ioctl (R, SOSC.FIONBIO, Val'Access);
532 Set_Non_Blocking_Socket (R, False);
533 end if;
534 Disable_SIGPIPE (R);
535 return R;
536 end C_Socket;
537
538 --------------
539 -- Finalize --
540 --------------
541
542 procedure Finalize is
543 begin
544 null;
545 end Finalize;
546
547 -------------------------
548 -- Host_Error_Messages --
549 -------------------------
550
551 package body Host_Error_Messages is separate;
552
553 ----------------
554 -- Initialize --
555 ----------------
556
557 procedure Initialize is
558 begin
559 Disable_All_SIGPIPEs;
560 Reset_Socket_Set (Non_Blocking_Sockets'Access);
561 end Initialize;
562
563 -------------------------
564 -- Non_Blocking_Socket --
565 -------------------------
566
567 function Non_Blocking_Socket (S : C.int) return Boolean is
568 R : Boolean;
569 begin
570 Task_Lock.Lock;
571 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
572 Task_Lock.Unlock;
573 return R;
574 end Non_Blocking_Socket;
575
576 -----------------------------
577 -- Set_Non_Blocking_Socket --
578 -----------------------------
579
580 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
581 begin
582 Task_Lock.Lock;
583
584 if V then
585 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
586 else
587 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
588 end if;
589
590 Task_Lock.Unlock;
591 end Set_Non_Blocking_Socket;
592
593 --------------------------------
594 -- Nonreentrant_Gethostbyaddr --
595 --------------------------------
596
597 function Nonreentrant_Gethostbyaddr
598 (Addr : System.Address;
599 Addr_Len : C.int;
600 Addr_Type : C.int) return Hostent_Access
601 is
602 function Gethostbyaddr
603 (Addr : System.Address;
604 Addr_Len : C.unsigned;
605 Addr_Type : C.int) return Hostent_Access
606 with Import, Convention => C, External_Name => "gethostbyaddr";
607
608 begin
609 return Gethostbyaddr (Addr, C.unsigned (Addr_Len), Addr_Type);
610 end Nonreentrant_Gethostbyaddr;
611
612 --------------------
613 -- Signalling_Fds --
614 --------------------
615
616 package body Signalling_Fds is
617
618 -- In this default implementation, we use a C version of these
619 -- subprograms provided by socket.c.
620
621 function C_Create (Fds : not null access Fd_Pair) return C.int;
622 function C_Read (Rsig : C.int) return C.int;
623 function C_Write (Wsig : C.int) return C.int;
624 procedure C_Close (Sig : C.int);
625
626 pragma Import (C, C_Create, "__gnat_create_signalling_fds");
627 pragma Import (C, C_Read, "__gnat_read_signalling_fd");
628 pragma Import (C, C_Write, "__gnat_write_signalling_fd");
629 pragma Import (C, C_Close, "__gnat_close_signalling_fd");
630
631 function Create
632 (Fds : not null access Fd_Pair) return C.int renames C_Create;
633 function Read (Rsig : C.int) return C.int renames C_Read;
634 function Write (Wsig : C.int) return C.int renames C_Write;
635 procedure Close (Sig : C.int) renames C_Close;
636
637 end Signalling_Fds;
638
639 --------------------------
640 -- Socket_Error_Message --
641 --------------------------
642
643 function Socket_Error_Message (Errno : Integer) return String is separate;
644
645 end GNAT.Sockets.Thin;