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-2006, AdaCore --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 function VxWorks_hostGetByAddr
271 (Addr : C.int; Buf : System.Address) return C.int;
272 pragma Import (C, VxWorks_hostGetByAddr, "hostGetByAddr");
274 Host_Name : aliased C.char_array (1 .. Max_Name_Length);
277 if VxWorks_hostGetByAddr (To_Pointer (Addr).all,
278 Host_Name (Host_Name'First)'Address)
284 In_Addr_Access_Ptr.all.all := To_In_Addr (To_Pointer (Addr).all);
285 Local_Hostent.all.H_Name := C.Strings.New_Char_Array (Host_Name);
287 return Local_Hostent;
290 ---------------------
291 -- C_Gethostbyname --
292 ---------------------
294 function C_Gethostbyname
295 (Name : C.char_array) return Hostent_Access
297 function VxWorks_hostGetByName
298 (Name : C.char_array) return C.int;
299 pragma Import (C, VxWorks_hostGetByName, "hostGetByName");
304 Addr := VxWorks_hostGetByName (Name);
305 if Addr = Constants.ERROR then
309 In_Addr_Access_Ptr.all.all := To_In_Addr (Addr);
310 Local_Hostent.all.H_Name := C.Strings.New_Char_Array (To_C (Host_Name));
312 return Local_Hostent;
315 ---------------------
316 -- C_Getservbyname --
317 ---------------------
319 function C_Getservbyname
320 (Name : C.char_array;
321 Proto : C.char_array) return Servent_Access
323 pragma Warnings (Off, Name);
324 pragma Warnings (Off, Proto);
330 ---------------------
331 -- C_Getservbyport --
332 ---------------------
334 function C_Getservbyport
336 Proto : C.char_array) return Servent_Access
338 pragma Warnings (Off, Port);
339 pragma Warnings (Off, Proto);
352 Arg : Int_Access) return C.int
355 if not Thread_Blocking_IO
356 and then Req = Constants.FIONBIO
359 Set_Non_Blocking_Socket (S, True);
363 return Syscall_Ioctl (S, Req, Arg);
372 Msg : System.Address;
374 Flags : C.int) return C.int
380 Res := Syscall_Recv (S, Msg, Len, Flags);
381 exit when Thread_Blocking_IO
382 or else Res /= Failure
383 or else Non_Blocking_Socket (S)
384 or else Errno /= Constants.EWOULDBLOCK;
397 Msg : System.Address;
400 From : Sockaddr_In_Access;
401 Fromlen : access C.int) return C.int
407 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
408 exit when Thread_Blocking_IO
409 or else Res /= Failure
410 or else Non_Blocking_Socket (S)
411 or else Errno /= Constants.EWOULDBLOCK;
424 Msg : System.Address;
426 Flags : C.int) return C.int
432 Res := Syscall_Send (S, Msg, Len, Flags);
433 exit when Thread_Blocking_IO
434 or else Res /= Failure
435 or else Non_Blocking_Socket (S)
436 or else Errno /= Constants.EWOULDBLOCK;
449 Msg : System.Address;
452 To : Sockaddr_In_Access;
453 Tolen : C.int) return C.int
459 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
460 exit when Thread_Blocking_IO
461 or else Res /= Failure
462 or else Non_Blocking_Socket (S)
463 or else Errno /= Constants.EWOULDBLOCK;
477 Protocol : C.int) return C.int
480 Val : aliased C.int := 1;
483 pragma Unreferenced (Res);
486 R := Syscall_Socket (Domain, Typ, Protocol);
488 if not Thread_Blocking_IO
489 and then R /= Failure
491 -- Do not use C_Ioctl as this subprogram tracks sockets set
492 -- in non-blocking mode by user.
494 Res := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
495 -- Is it OK to ignore result ???
496 Set_Non_Blocking_Socket (R, False);
506 procedure Finalize is
515 procedure Initialize (Process_Blocking_IO : Boolean) is
517 Thread_Blocking_IO := not Process_Blocking_IO;
520 -------------------------
521 -- Non_Blocking_Socket --
522 -------------------------
524 function Non_Blocking_Socket (S : C.int) return Boolean is
528 R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
531 end Non_Blocking_Socket;
537 procedure Set_Address
538 (Sin : Sockaddr_In_Access;
542 Sin.Sin_Addr := Address;
550 (Sin : Sockaddr_In_Access;
554 Sin.Sin_Family := C.unsigned_char (Family);
562 (Sin : Sockaddr_In_Access;
566 Sin.Sin_Length := C.unsigned_char (Len);
569 -----------------------------
570 -- Set_Non_Blocking_Socket --
571 -----------------------------
573 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
577 Insert_Socket_In_Set (Non_Blocking_Sockets, S);
579 Remove_Socket_From_Set (Non_Blocking_Sockets, S);
583 end Set_Non_Blocking_Socket;
590 (Sin : Sockaddr_In_Access;
591 Port : C.unsigned_short)
594 Sin.Sin_Port := Port;
597 --------------------------
598 -- Socket_Error_Message --
599 --------------------------
601 function Socket_Error_Message
602 (Errno : Integer) return C.Strings.chars_ptr
604 use type Interfaces.C.Strings.chars_ptr;
606 C_Msg : C.Strings.chars_ptr;
609 C_Msg := C_Strerror (C.int (Errno));
611 if C_Msg = C.Strings.Null_Ptr then
612 return Unknown_System_Error;
617 end Socket_Error_Message;
619 -- Package elaboration
622 Local_Hostent.all.H_Aliases := Alias_Access;
624 -- VxWorks currently only supports AF_INET
626 Local_Hostent.all.H_Addrtype := Constants.AF_INET;
628 Local_Hostent.all.H_Length := 1;
629 Local_Hostent.all.H_Addr_List := In_Addr_Access_Ptr;
631 end GNAT.Sockets.Thin;