1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . S O C K E T S . T H I N --
9 -- Copyright (C) 2001 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 with Interfaces.C.Pointers;
35 with Interfaces.C.Strings;
36 with GNAT.Sockets.Constants;
41 package GNAT.Sockets.Thin is
43 -- ??? more comments needed ???
45 -- This package is intended for hosts implementing BSD sockets with a
46 -- standard interface. It will be used as a default for all the platforms
47 -- that do not have a specific version of this file.
49 package C renames Interfaces.C;
52 -- This is so we can declare the Failure constant below
54 Success : constant C.int := 0;
55 Failure : constant C.int := -1;
57 function Socket_Errno return Integer renames GNAT.OS_Lib.Errno;
58 -- Returns last socket error number.
60 function Socket_Error_Message (Errno : Integer) return String;
61 -- Returns the error message string for the error number Errno. If
62 -- Errno is not known it returns "Unknown system error".
64 type Fd_Set is mod 2 ** 32;
65 pragma Convention (C, Fd_Set);
67 Null_Fd_Set : constant Fd_Set := 0;
69 type Fd_Set_Access is access all Fd_Set;
70 pragma Convention (C, Fd_Set_Access);
72 type Timeval_Unit is new C.int;
73 pragma Convention (C, Timeval_Unit);
75 type Timeval is record
76 Tv_Sec : Timeval_Unit;
77 Tv_Usec : Timeval_Unit;
79 pragma Convention (C, Timeval);
81 type Timeval_Access is access all Timeval;
82 pragma Convention (C, Timeval_Access);
84 Immediat : constant Timeval := (0, 0);
86 type Int_Access is access all C.int;
87 pragma Convention (C, Int_Access);
88 -- Access to C integers
90 type Chars_Ptr_Array is array (C.size_t range <>) of
91 aliased C.Strings.chars_ptr;
93 package Chars_Ptr_Pointers is
94 new C.Pointers (C.size_t, C.Strings.chars_ptr, Chars_Ptr_Array,
96 -- Arrays of C (char *)
98 type In_Addr is record
99 S_B1, S_B2, S_B3, S_B4 : C.unsigned_char;
101 pragma Convention (C, In_Addr);
104 type In_Addr_Access is access all In_Addr;
105 pragma Convention (C, In_Addr_Access);
106 -- Access to internet address
108 Inaddr_Any : aliased constant In_Addr := (others => 0);
109 -- Any internet address (all the interfaces)
111 type In_Addr_Access_Array is array (C.size_t range <>)
112 of aliased In_Addr_Access;
113 pragma Convention (C, In_Addr_Access_Array);
115 package In_Addr_Access_Pointers is
116 new C.Pointers (C.size_t, In_Addr_Access, In_Addr_Access_Array, null);
117 -- Array of internet addresses
119 type Sockaddr is record
120 Sa_Family : C.unsigned_short;
121 Sa_Data : C.char_array (1 .. 14);
123 pragma Convention (C, Sockaddr);
126 type Sockaddr_Access is access all Sockaddr;
127 pragma Convention (C, Sockaddr_Access);
128 -- Access to socket address
130 type Sockaddr_In is record
131 Sin_Family : C.unsigned_short := Constants.AF_INET;
132 Sin_Port : C.unsigned_short := 0;
133 Sin_Addr : In_Addr := Inaddr_Any;
134 Sin_Zero : C.char_array (1 .. 8) := (others => C.char'Val (0));
136 pragma Convention (C, Sockaddr_In);
137 -- Internet socket address
139 type Sockaddr_In_Access is access all Sockaddr_In;
140 pragma Convention (C, Sockaddr_In_Access);
141 -- Access to internet socket address
143 type Hostent is record
144 H_Name : C.Strings.chars_ptr;
145 H_Aliases : Chars_Ptr_Pointers.Pointer;
148 H_Addr_List : In_Addr_Access_Pointers.Pointer;
150 pragma Convention (C, Hostent);
153 type Hostent_Access is access all Hostent;
154 pragma Convention (C, Hostent_Access);
155 -- Access to host entry
157 type Two_Int is array (0 .. 1) of C.int;
158 pragma Convention (C, Two_Int);
163 Addr : System.Address;
164 Addrlen : access C.int)
169 Name : System.Address;
179 Name : System.Address;
183 function C_Gethostbyaddr
184 (Addr : System.Address;
187 return Hostent_Access;
189 function C_Gethostbyname
190 (Name : C.char_array)
191 return Hostent_Access;
193 function C_Gethostname
194 (Name : System.Address;
198 function C_Getpeername
200 Name : System.Address;
201 Namelen : access C.int)
204 function C_Getsockname
206 Name : System.Address;
207 Namelen : access C.int)
210 function C_Getsockopt
214 Optval : System.Address;
215 Optlen : access C.int)
219 (Cp : C.Strings.chars_ptr)
228 function C_Listen (S, Backlog : C.int) return C.int;
232 Buf : System.Address;
238 Msg : System.Address;
245 Msg : System.Address;
248 From : Sockaddr_In_Access;
249 Fromlen : access C.int)
254 Readfds : Fd_Set_Access;
255 Writefds : Fd_Set_Access;
256 Exceptfds : Fd_Set_Access;
257 Timeout : Timeval_Access)
262 Msg : System.Address;
269 Msg : System.Address;
272 To : Sockaddr_In_Access;
276 function C_Setsockopt
280 Optval : System.Address;
297 return C.Strings.chars_ptr;
300 (Command : System.Address)
305 Buf : System.Address;
309 -- Return highest numbered socket (what does this refer to???)
311 procedure Clear (Item : in out Fd_Set; Socket : in C.int);
312 procedure Empty (Item : in out Fd_Set);
313 function Is_Empty (Item : Fd_Set) return Boolean;
314 function Is_Set (Item : Fd_Set; Socket : C.int) return Boolean;
315 function Max (Item : Fd_Set) return C.int;
316 procedure Set (Item : in out Fd_Set; Socket : in C.int);
319 procedure Initialize (Process_Blocking_IO : Boolean);
323 pragma Import (C, C_Bind, "bind");
324 pragma Import (C, C_Close, "close");
325 pragma Import (C, C_Gethostbyaddr, "gethostbyaddr");
326 pragma Import (C, C_Gethostbyname, "gethostbyname");
327 pragma Import (C, C_Gethostname, "gethostname");
328 pragma Import (C, C_Getpeername, "getpeername");
329 pragma Import (C, C_Getsockname, "getsockname");
330 pragma Import (C, C_Getsockopt, "getsockopt");
331 pragma Import (C, C_Inet_Addr, "inet_addr");
332 pragma Import (C, C_Listen, "listen");
333 pragma Import (C, C_Read, "read");
334 pragma Import (C, C_Select, "select");
335 pragma Import (C, C_Setsockopt, "setsockopt");
336 pragma Import (C, C_Shutdown, "shutdown");
337 pragma Import (C, C_Strerror, "strerror");
338 pragma Import (C, C_System, "system");
339 pragma Import (C, C_Write, "write");
341 end GNAT.Sockets.Thin;