* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / ada / g-socthi-vxworks.adb
blob06a60cae2d96427a2f3a7fbd31237de07c2e529e
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-2004 Ada Core Technologies, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This package provides a target dependent thin interface to the sockets
35 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
36 -- should not be directly with'ed by an applications program.
38 -- This version is for VxWorks
40 with GNAT.OS_Lib; use GNAT.OS_Lib;
41 with GNAT.Task_Lock;
43 with Interfaces.C; use Interfaces.C;
44 with Unchecked_Conversion;
46 package body GNAT.Sockets.Thin is
48 Non_Blocking_Sockets : constant Fd_Set_Access :=
49 New_Socket_Set (No_Socket_Set);
50 -- When this package is initialized with Process_Blocking_IO set
51 -- to True, sockets are set in non-blocking mode to avoid blocking
52 -- the whole process when a thread wants to perform a blocking IO
53 -- operation. But the user can also set a socket in non-blocking
54 -- mode by purpose. In order to make a difference between these
55 -- two situations, we track the origin of non-blocking mode in
56 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
57 -- been set in non-blocking mode by the user.
59 Quantum : constant Duration := 0.2;
60 -- When Thread_Blocking_IO is False, we set sockets in
61 -- non-blocking mode and we spend a period of time Quantum between
62 -- two attempts on a blocking operation.
64 Thread_Blocking_IO : Boolean := True;
66 Unknown_System_Error : constant C.Strings.chars_ptr :=
67 C.Strings.New_String ("Unknown system error");
69 -- The following types and variables are required to create a Hostent
70 -- record "by hand".
72 type In_Addr_Access_Array_Access is access In_Addr_Access_Array;
74 Alias_Access : constant Chars_Ptr_Pointers.Pointer :=
75 new C.Strings.chars_ptr'(C.Strings.Null_Ptr);
77 In_Addr_Access_Array_A : constant In_Addr_Access_Array_Access :=
78 new In_Addr_Access_Array'(new In_Addr, null);
80 In_Addr_Access_Ptr : constant In_Addr_Access_Pointers.Pointer :=
81 In_Addr_Access_Array_A
82 (In_Addr_Access_Array_A'First)'Access;
84 Local_Hostent : constant Hostent_Access := new Hostent;
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 -- All these require comments ???
92 function Syscall_Accept
93 (S : C.int;
94 Addr : System.Address;
95 Addrlen : access C.int) return C.int;
96 pragma Import (C, Syscall_Accept, "accept");
98 function Syscall_Connect
99 (S : C.int;
100 Name : System.Address;
101 Namelen : C.int) return C.int;
102 pragma Import (C, Syscall_Connect, "connect");
104 function Syscall_Ioctl
105 (S : C.int;
106 Req : C.int;
107 Arg : Int_Access) return C.int;
108 pragma Import (C, Syscall_Ioctl, "ioctl");
110 function Syscall_Recv
111 (S : C.int;
112 Msg : System.Address;
113 Len : C.int;
114 Flags : C.int) return C.int;
115 pragma Import (C, Syscall_Recv, "recv");
117 function Syscall_Recvfrom
118 (S : C.int;
119 Msg : System.Address;
120 Len : C.int;
121 Flags : C.int;
122 From : Sockaddr_In_Access;
123 Fromlen : access C.int) return C.int;
124 pragma Import (C, Syscall_Recvfrom, "recvfrom");
126 function Syscall_Send
127 (S : C.int;
128 Msg : System.Address;
129 Len : C.int;
130 Flags : C.int) return C.int;
131 pragma Import (C, Syscall_Send, "send");
133 function Syscall_Sendto
134 (S : C.int;
135 Msg : System.Address;
136 Len : C.int;
137 Flags : C.int;
138 To : Sockaddr_In_Access;
139 Tolen : C.int) return C.int;
140 pragma Import (C, Syscall_Sendto, "sendto");
142 function Syscall_Socket
143 (Domain : C.int;
144 Typ : C.int;
145 Protocol : C.int) return C.int;
146 pragma Import (C, Syscall_Socket, "socket");
148 function Non_Blocking_Socket (S : C.int) return Boolean;
149 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
151 --------------
152 -- C_Accept --
153 --------------
155 function C_Accept
156 (S : C.int;
157 Addr : System.Address;
158 Addrlen : access C.int) return C.int
160 R : C.int;
161 Val : aliased C.int := 1;
163 Res : C.int;
164 pragma Unreferenced (Res);
166 begin
167 loop
168 R := Syscall_Accept (S, Addr, Addrlen);
169 exit when Thread_Blocking_IO
170 or else R /= Failure
171 or else Non_Blocking_Socket (S)
172 or else Errno /= Constants.EWOULDBLOCK;
173 delay Quantum;
174 end loop;
176 if not Thread_Blocking_IO
177 and then R /= Failure
178 then
179 -- A socket inherits the properties ot its server especially
180 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
181 -- tracks sockets set in non-blocking mode by user.
183 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
184 Res := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
185 -- Is it OK to ignore result ???
186 end if;
188 return R;
189 end C_Accept;
191 ---------------
192 -- C_Connect --
193 ---------------
195 function C_Connect
196 (S : C.int;
197 Name : System.Address;
198 Namelen : C.int) return C.int
200 Res : C.int;
202 begin
203 Res := Syscall_Connect (S, Name, Namelen);
205 if Thread_Blocking_IO
206 or else Res /= Failure
207 or else Non_Blocking_Socket (S)
208 or else Errno /= Constants.EINPROGRESS
209 then
210 return Res;
211 end if;
213 declare
214 WSet : Fd_Set_Access;
215 Now : aliased Timeval;
217 begin
218 WSet := New_Socket_Set (No_Socket_Set);
220 loop
221 Insert_Socket_In_Set (WSet, S);
222 Now := Immediat;
223 Res := C_Select
224 (S + 1,
225 No_Fd_Set,
226 WSet,
227 No_Fd_Set,
228 Now'Unchecked_Access);
230 exit when Res > 0;
232 if Res = Failure then
233 Free_Socket_Set (WSet);
234 return Res;
235 end if;
237 delay Quantum;
238 end loop;
240 Free_Socket_Set (WSet);
241 end;
243 Res := Syscall_Connect (S, Name, Namelen);
245 if Res = Failure
246 and then Errno = Constants.EISCONN
247 then
248 return Thin.Success;
249 else
250 return Res;
251 end if;
252 end C_Connect;
254 ---------------------
255 -- C_Gethostbyaddr --
256 ---------------------
258 function C_Gethostbyaddr
259 (Addr : System.Address;
260 Len : C.int;
261 Typ : C.int) return Hostent_Access
263 pragma Warnings (Off, Len);
264 pragma Warnings (Off, Typ);
266 type int_Access is access int;
267 function To_Pointer is
268 new Unchecked_Conversion (System.Address, int_Access);
270 procedure VxWorks_Gethostbyaddr
271 (Addr : C.int; Buf : out C.char_array);
272 pragma Import (C, VxWorks_Gethostbyaddr, "hostGetByAddr");
274 Host_Name : C.char_array (1 .. Max_Name_Length);
276 begin
277 VxWorks_Gethostbyaddr (To_Pointer (Addr).all, Host_Name);
279 In_Addr_Access_Ptr.all.all := To_In_Addr (To_Pointer (Addr).all);
280 Local_Hostent.all.H_Name := C.Strings.New_Char_Array (Host_Name);
282 return Local_Hostent;
283 end C_Gethostbyaddr;
285 ---------------------
286 -- C_Gethostbyname --
287 ---------------------
289 function C_Gethostbyname
290 (Name : C.char_array) return Hostent_Access
292 function VxWorks_Gethostbyname
293 (Name : C.char_array) return C.int;
294 pragma Import (C, VxWorks_Gethostbyname, "hostGetByName");
296 Addr : C.int;
298 begin
299 Addr := VxWorks_Gethostbyname (Name);
301 In_Addr_Access_Ptr.all.all := To_In_Addr (Addr);
302 Local_Hostent.all.H_Name := C.Strings.New_Char_Array (To_C (Host_Name));
304 return Local_Hostent;
305 end C_Gethostbyname;
307 ---------------------
308 -- C_Getservbyname --
309 ---------------------
311 function C_Getservbyname
312 (Name : C.char_array;
313 Proto : C.char_array) return Servent_Access
315 pragma Warnings (Off, Name);
316 pragma Warnings (Off, Proto);
318 begin
319 return null;
320 end C_Getservbyname;
322 ---------------------
323 -- C_Getservbyport --
324 ---------------------
326 function C_Getservbyport
327 (Port : C.int;
328 Proto : C.char_array) return Servent_Access
330 pragma Warnings (Off, Port);
331 pragma Warnings (Off, Proto);
333 begin
334 return null;
335 end C_Getservbyport;
337 -------------
338 -- C_Ioctl --
339 -------------
341 function C_Ioctl
342 (S : C.int;
343 Req : C.int;
344 Arg : Int_Access) return C.int
346 begin
347 if not Thread_Blocking_IO
348 and then Req = Constants.FIONBIO
349 then
350 if Arg.all /= 0 then
351 Set_Non_Blocking_Socket (S, True);
352 end if;
353 end if;
355 return Syscall_Ioctl (S, Req, Arg);
356 end C_Ioctl;
358 ------------
359 -- C_Recv --
360 ------------
362 function C_Recv
363 (S : C.int;
364 Msg : System.Address;
365 Len : C.int;
366 Flags : C.int) return C.int
368 Res : C.int;
370 begin
371 loop
372 Res := Syscall_Recv (S, Msg, Len, Flags);
373 exit when Thread_Blocking_IO
374 or else Res /= Failure
375 or else Non_Blocking_Socket (S)
376 or else Errno /= Constants.EWOULDBLOCK;
377 delay Quantum;
378 end loop;
380 return Res;
381 end C_Recv;
383 ----------------
384 -- C_Recvfrom --
385 ----------------
387 function C_Recvfrom
388 (S : C.int;
389 Msg : System.Address;
390 Len : C.int;
391 Flags : C.int;
392 From : Sockaddr_In_Access;
393 Fromlen : access C.int) return C.int
395 Res : C.int;
397 begin
398 loop
399 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
400 exit when Thread_Blocking_IO
401 or else Res /= Failure
402 or else Non_Blocking_Socket (S)
403 or else Errno /= Constants.EWOULDBLOCK;
404 delay Quantum;
405 end loop;
407 return Res;
408 end C_Recvfrom;
410 ------------
411 -- C_Send --
412 ------------
414 function C_Send
415 (S : C.int;
416 Msg : System.Address;
417 Len : C.int;
418 Flags : C.int) return C.int
420 Res : C.int;
422 begin
423 loop
424 Res := Syscall_Send (S, Msg, Len, Flags);
425 exit when Thread_Blocking_IO
426 or else Res /= Failure
427 or else Non_Blocking_Socket (S)
428 or else Errno /= Constants.EWOULDBLOCK;
429 delay Quantum;
430 end loop;
432 return Res;
433 end C_Send;
435 --------------
436 -- C_Sendto --
437 --------------
439 function C_Sendto
440 (S : C.int;
441 Msg : System.Address;
442 Len : C.int;
443 Flags : C.int;
444 To : Sockaddr_In_Access;
445 Tolen : C.int) return C.int
447 Res : C.int;
449 begin
450 loop
451 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
452 exit when Thread_Blocking_IO
453 or else Res /= Failure
454 or else Non_Blocking_Socket (S)
455 or else Errno /= Constants.EWOULDBLOCK;
456 delay Quantum;
457 end loop;
459 return Res;
460 end C_Sendto;
462 --------------
463 -- C_Socket --
464 --------------
466 function C_Socket
467 (Domain : C.int;
468 Typ : C.int;
469 Protocol : C.int) return C.int
471 R : C.int;
472 Val : aliased C.int := 1;
474 Res : C.int;
475 pragma Unreferenced (Res);
477 begin
478 R := Syscall_Socket (Domain, Typ, Protocol);
480 if not Thread_Blocking_IO
481 and then R /= Failure
482 then
483 -- Do not use C_Ioctl as this subprogram tracks sockets set
484 -- in non-blocking mode by user.
486 Res := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
487 -- Is it OK to ignore result ???
488 Set_Non_Blocking_Socket (R, False);
489 end if;
491 return R;
492 end C_Socket;
494 --------------
495 -- Finalize --
496 --------------
498 procedure Finalize is
499 begin
500 null;
501 end Finalize;
503 ----------------
504 -- Initialize --
505 ----------------
507 procedure Initialize (Process_Blocking_IO : Boolean) is
508 begin
509 Thread_Blocking_IO := not Process_Blocking_IO;
510 end Initialize;
512 -------------------------
513 -- Non_Blocking_Socket --
514 -------------------------
516 function Non_Blocking_Socket (S : C.int) return Boolean is
517 R : Boolean;
519 begin
520 Task_Lock.Lock;
521 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
522 Task_Lock.Unlock;
523 return R;
524 end Non_Blocking_Socket;
526 -----------------
527 -- Set_Address --
528 -----------------
530 procedure Set_Address
531 (Sin : Sockaddr_In_Access;
532 Address : In_Addr)
534 begin
535 Sin.Sin_Addr := Address;
536 end Set_Address;
538 ----------------
539 -- Set_Family --
540 ----------------
542 procedure Set_Family
543 (Sin : Sockaddr_In_Access;
544 Family : C.int)
546 begin
547 Sin.Sin_Family := C.unsigned_char (Family);
548 end Set_Family;
550 ----------------
551 -- Set_Length --
552 ----------------
554 procedure Set_Length
555 (Sin : Sockaddr_In_Access;
556 Len : C.int)
558 begin
559 Sin.Sin_Length := C.unsigned_char (Len);
560 end Set_Length;
562 -----------------------------
563 -- Set_Non_Blocking_Socket --
564 -----------------------------
566 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
567 begin
568 Task_Lock.Lock;
569 if V then
570 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
571 else
572 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
573 end if;
575 Task_Lock.Unlock;
576 end Set_Non_Blocking_Socket;
578 --------------
579 -- Set_Port --
580 --------------
582 procedure Set_Port
583 (Sin : Sockaddr_In_Access;
584 Port : C.unsigned_short)
586 begin
587 Sin.Sin_Port := Port;
588 end Set_Port;
590 --------------------------
591 -- Socket_Error_Message --
592 --------------------------
594 function Socket_Error_Message
595 (Errno : Integer) return C.Strings.chars_ptr
597 use type Interfaces.C.Strings.chars_ptr;
599 C_Msg : C.Strings.chars_ptr;
601 begin
602 C_Msg := C_Strerror (C.int (Errno));
604 if C_Msg = C.Strings.Null_Ptr then
605 return Unknown_System_Error;
607 else
608 return C_Msg;
609 end if;
610 end Socket_Error_Message;
612 -- Package elaboration
614 begin
615 Local_Hostent.all.H_Aliases := Alias_Access;
617 -- VxWorks currently only supports AF_INET
619 Local_Hostent.all.H_Addrtype := Constants.AF_INET;
621 Local_Hostent.all.H_Length := 1;
622 Local_Hostent.all.H_Addr_List := In_Addr_Access_Ptr;
624 end GNAT.Sockets.Thin;