1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
9 -- Copyright (C) 2002-2004 Ada Core Technologies, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
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
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
94 Addr : System.Address;
95 Addrlen : access C.int) return C.int;
96 pragma Import (C, Syscall_Accept, "accept");
98 function Syscall_Connect
100 Name : System.Address;
101 Namelen : C.int) return C.int;
102 pragma Import (C, Syscall_Connect, "connect");
104 function Syscall_Ioctl
107 Arg : Int_Access) return C.int;
108 pragma Import (C, Syscall_Ioctl, "ioctl");
110 function Syscall_Recv
112 Msg : System.Address;
114 Flags : C.int) return C.int;
115 pragma Import (C, Syscall_Recv, "recv");
117 function Syscall_Recvfrom
119 Msg : System.Address;
122 From : Sockaddr_In_Access;
123 Fromlen : access C.int) return C.int;
124 pragma Import (C, Syscall_Recvfrom, "recvfrom");
126 function Syscall_Send
128 Msg : System.Address;
130 Flags : C.int) return C.int;
131 pragma Import (C, Syscall_Send, "send");
133 function Syscall_Sendto
135 Msg : System.Address;
138 To : Sockaddr_In_Access;
139 Tolen : C.int) return C.int;
140 pragma Import (C, Syscall_Sendto, "sendto");
142 function Syscall_Socket
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);
157 Addr : System.Address;
158 Addrlen : access C.int) return C.int
161 Val : aliased C.int := 1;
164 pragma Unreferenced (Res);
168 R := Syscall_Accept (S, Addr, Addrlen);
169 exit when Thread_Blocking_IO
171 or else Non_Blocking_Socket (S)
172 or else Errno /= Constants.EWOULDBLOCK;
176 if not Thread_Blocking_IO
177 and then R /= Failure
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 ???
197 Name : System.Address;
198 Namelen : C.int) return C.int
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
214 WSet : Fd_Set_Access;
215 Now : aliased Timeval;
218 WSet := New_Socket_Set (No_Socket_Set);
221 Insert_Socket_In_Set (WSet, S);
228 Now'Unchecked_Access);
232 if Res = Failure then
233 Free_Socket_Set (WSet);
240 Free_Socket_Set (WSet);
243 Res := Syscall_Connect (S, Name, Namelen);
246 and then Errno = Constants.EISCONN
254 ---------------------
255 -- C_Gethostbyaddr --
256 ---------------------
258 function C_Gethostbyaddr
259 (Addr : System.Address;
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);
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;
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");
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;
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);
322 ---------------------
323 -- C_Getservbyport --
324 ---------------------
326 function C_Getservbyport
328 Proto : C.char_array) return Servent_Access
330 pragma Warnings (Off, Port);
331 pragma Warnings (Off, Proto);
344 Arg : Int_Access) return C.int
347 if not Thread_Blocking_IO
348 and then Req = Constants.FIONBIO
351 Set_Non_Blocking_Socket (S, True);
355 return Syscall_Ioctl (S, Req, Arg);
364 Msg : System.Address;
366 Flags : C.int) return C.int
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;
389 Msg : System.Address;
392 From : Sockaddr_In_Access;
393 Fromlen : access C.int) return C.int
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;
416 Msg : System.Address;
418 Flags : C.int) return C.int
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;
441 Msg : System.Address;
444 To : Sockaddr_In_Access;
445 Tolen : C.int) return C.int
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;
469 Protocol : C.int) return C.int
472 Val : aliased C.int := 1;
475 pragma Unreferenced (Res);
478 R := Syscall_Socket (Domain, Typ, Protocol);
480 if not Thread_Blocking_IO
481 and then R /= Failure
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);
498 procedure Finalize is
507 procedure Initialize (Process_Blocking_IO : Boolean) is
509 Thread_Blocking_IO := not Process_Blocking_IO;
512 -------------------------
513 -- Non_Blocking_Socket --
514 -------------------------
516 function Non_Blocking_Socket (S : C.int) return Boolean is
521 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
524 end Non_Blocking_Socket;
530 procedure Set_Address
531 (Sin : Sockaddr_In_Access;
535 Sin.Sin_Addr := Address;
543 (Sin : Sockaddr_In_Access;
547 Sin.Sin_Family := C.unsigned_char (Family);
555 (Sin : Sockaddr_In_Access;
559 Sin.Sin_Length := C.unsigned_char (Len);
562 -----------------------------
563 -- Set_Non_Blocking_Socket --
564 -----------------------------
566 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
570 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
572 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
576 end Set_Non_Blocking_Socket;
583 (Sin : Sockaddr_In_Access;
584 Port : C.unsigned_short)
587 Sin.Sin_Port := 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;
602 C_Msg := C_Strerror (C.int (Errno));
604 if C_Msg = C.Strings.Null_Ptr then
605 return Unknown_System_Error;
610 end Socket_Error_Message;
612 -- Package elaboration
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;