1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
------------------------------------------------------------------------------
-- --
-- GNATSOCKS --
-- --
-- S o c k e t s --
-- --
-- B o d y --
-- --
-- --
-- Copyright (c) 1997-1998 Florida State University (FSU), --
-- All Rights Reserved. --
-- --
-- This file is a component of GNATSOCKS, an Ada interfaces to socket I/O --
-- services, for use with the GNAT Ada compiler. --
-- --
-- GNATSOCKS is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 2, or (at your option) any --
-- later version. FLORIST is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty --
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have received a --
-- copy of the GNU General Public License distributed with GNARL; see --
-- file COPYING. If not, write to the Free Software Foundation, 59 --
-- Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
------------------------------------------------------------------------------
-- [$Revision: 110555 $]
with Ada.Text_IO,
Ada.Streams,
POSIX,
POSIX.C,
POSIX.Implementation,
System,
Unchecked_Conversion;
package body Sockets is
use Ada.Text_IO,
POSIX,
POSIX.C,
POSIX.C.Sockets,
POSIX.Implementation;
function close (fildes : int) return int;
pragma Import (C, close, close_LINKNAME);
procedure Close (Sock : in out Socket'Class) is
begin
if Sock.fd /= 0 then Check (close (Sock.fd)); end if;
Sock.fd := 0;
end Close;
procedure Finalize (Sock : in out Socket) is
begin
Close (Sock);
end Finalize;
--------------------------------------
function connect
(socket_fd : int;
name : sockaddr_ptr;
namelen : int)
return int;
pragma Import (C, connect, connect_LINKNAME);
-- attempts to make a connection to a named socket.
function make_socket
(domain : int;
socket_type : int;
protocol : int)
return int;
pragma Import (C, make_socket, socket_LINKNAME);
procedure Open
(Sock : in out Stream_Socket;
Addr : Socket_Address'Class) is
begin
if not Valid (Addr) then raise Constraint_Error; end if;
if Sock.fd /= 0 then raise Constraint_Error; end if;
Sock.fd := Check (make_socket (PF_INET, SOCK_STREAM, 0));
Check (connect (Sock.fd, Address (Addr), Length (Addr)));
Sock.in_stream.sock := Sock'Unchecked_Access;
Sock.in_ptr := Sock.in_stream'Unchecked_Access;
Sock.out_stream.sock := Sock'Unchecked_Access;
Sock.out_ptr := Sock.out_stream'Unchecked_Access;
Sock.tag := Addr'Tag;
end Open;
function Get_Input_Stream (Sock : Stream_Socket) return Input_Stream_Ptr is
begin
if Sock.fd = 0 then raise Constraint_Error; end if;
return Sock.in_ptr;
end Get_Input_Stream;
function Get_Output_Stream (Sock : Stream_Socket) return Output_Stream_Ptr is
begin
if Sock.fd = 0 then raise Constraint_Error; end if;
return Sock.out_ptr;
end Get_Output_Stream;
-----------------------------------------
function listen
(socket_fd : int;
backlog : int)
return int;
pragma Import (C, listen, listen_LINKNAME);
-- specifies that we want to listen on the argument socket ID.
-- backlog is the number of backlogged connections that are allowed.
-- To accept connections, a socket is first crated with socked(),
-- a backlog for incoming connections is specified with listen(),
-- and then the connections are accepted with accept().
function bind
(socket_fd : int;
name : sockaddr_ptr;
namelen : int)
return int;
pragma Import (C, bind, bind_LINKNAME);
-- assigns a name to an unnamed socket.
-- A socket is first created with socket().
-- Binding a socket in the UNIX (not Internet) domain
-- causes creation of a socket in the file system, that must
-- be deleted later when it is no longer needed, using unlink()
procedure Open
(Sock : in out Server_Socket;
Addr : Socket_Address'Class;
Count : Natural := 0) is
begin
if not Valid (Addr) then raise Constraint_Error; end if;
if Sock.fd /= 0 then raise Constraint_Error; end if;
Sock.fd := Check (make_socket (Protocol_Family (Addr), SOCK_STREAM, 0));
Check (bind (Sock.fd, Address (Addr), Length (Addr)));
Check (listen (Sock.fd, int (Count)));
Sock.tag := Addr'Tag;
end Open;
function accept_connection
(socket_fd : int;
addr : sockaddr_ptr;
addrlen : access int)
return int;
pragma Import (C, accept_connection, accept_LINKNAME);
-- returns a socket ID for a new connection that has been
-- requested via the socket whose ID is given as argument.
-- The argument must be an existing socket that is bound to an address
-- and is listeninf for connections.
-- addr received the address of the connecting entity.
procedure Accept_Connection
(Server : Server_Socket;
Stream : in out Stream_Socket'Class;
Peer : in out Socket_Address'Class) is
addrlen : aliased int := Length (Peer);
use type Ada.Tags.Tag;
begin
if Server.fd = 0 then raise Constraint_Error; end if;
if Stream.fd /= 0 then raise Constraint_Error; end if;
if Server.tag /= Peer'Tag then raise Constraint_Error; end if;
-- require that the Peer and Server object be of the same type as the
-- socket address used to create the Server
Stream.fd := Check (accept_connection (Server.fd,
Address (Peer), addrlen'Unchecked_Access));
Stream.in_stream.sock := Stream'Unchecked_Access;
Stream.in_ptr := Stream.in_stream'Unchecked_Access;
Stream.out_stream.sock := Stream'Unchecked_Access;
Stream.out_ptr := Stream.out_stream'Unchecked_Access;
Stream.tag := Server.tag;
if addrlen /= Length (Peer) then raise
Constraint_Error;
end if;
end Accept_Connection;
-----------------------------------------
procedure Open
(Sock : in out Datagram_Socket;
Addr : Socket_Address'Class) is
begin
if not Valid (Addr) then raise Constraint_Error; end if;
if Sock.fd /= 0 then raise Constraint_Error; end if;
Sock.fd := Check (make_socket (Protocol_Family (Addr), SOCK_DGRAM, 0));
Check (bind (Sock.fd, Address (Addr), Length (Addr)));
Sock.tag := Addr'Tag;
end Open;
-----------------------------------------
function read
(fildes : int;
buf : System.Address;
nbyte : size_t)
return ssize_t;
pragma Import (C, read, read_LINKNAME);
function write
(fildes : int;
buf : char_ptr;
nbyte : size_t)
return ssize_t;
pragma Import (C, write, write_LINKNAME);
procedure Read
(Stream : in out Input_Stream;
Item : out Ada.Streams.Stream_Element_Array;
Last : out Ada.Streams.Stream_Element_Offset) is
use type Ada.Streams.Stream_Element_Offset;
Tmp : ssize_t;
function "+" is new
Unchecked_Conversion (System.Address, char_ptr);
begin
Tmp := read (Stream.sock.fd, Item'Address, size_t (Item'Length));
Check (int (Tmp));
Last := Item'First + Ada.Streams.Stream_Element_Offset (Tmp);
end Read;
procedure Write
(Stream : in out Input_Stream;
Item : in Ada.Streams.Stream_Element_Array) is
begin
Raise_POSIX_Error (Operation_Not_Implemented);
end Write;
procedure Read
(Stream : in out Output_Stream;
Item : out Ada.Streams.Stream_Element_Array;
Last : out Ada.Streams.Stream_Element_Offset) is
begin
Raise_POSIX_Error (Operation_Not_Implemented);
end Read;
procedure Write
(Stream : in out Output_Stream;
Item : in Ada.Streams.Stream_Element_Array) is
function "+" is new
Unchecked_Conversion (System.Address, char_ptr);
begin
Check (int (write (Stream.sock.fd, +Item'Address,
size_t (Item'Length))));
end Write;
----------------------------------------------------
function sendto
(socket_fd : int;
buf : char_ptr;
len : int;
flags : int;
from : sockaddr_ptr;
fromlen : int_ptr)
return int;
pragma Import (C, sendto, sendto_LINKNAME);
procedure Send
(Sock : in Datagram_Socket;
Addr : in Socket_Address'Class;
Data : in String) is
Data_With_NUL : POSIX_String;
begin
if not Valid (Addr) then raise Constraint_Error; end if;
if Sock.fd /= 0 then raise Constraint_Error; end if;
if Sock.tag /= Addr'Tag then raise Constraint_Error; end if;
Check (sendto (socket_fd => Sock.fd,
buf => +Data (Data'First)'Address,
len => Data'Length,
flags => 0,
from => Address (Addr),
fromlen => Length (Addr)));
end Send;
function recvfrom
(socket_fd : int;
buf : char_ptr;
len : int;
flags : int;
from : sockaddr_ptr; -- may be null
fromlen : int_ptr)
return int;
pragma Import (C, recvfrom, recvfrom_LINKNAME);
procedure Receive
(Sock : in Datagram_Socket;
Addr : out Socket_Address'Class;
Buff : in out String;
Last : out Natural) is
begin
if not Valid (Addr) then raise Constraint_Error; end if;
if Sock.fd /= 0 then raise Constraint_Error; end if;
if Sock.tag /= Addr'Tag then raise Constraint_Error; end if;
Check (recvfrom (socket_fd => Sock.fd,
buf => +Buff (Buff'First)'Address,
len => Buff'Length,
flags => 0,
from => Address (Buff),
fromlen => Length (Addr)));
end Receive;
----------------------------------------------------
-- Stuff that may be bound-to in future extensions?
-- function getsockopt
-- (socket_fd : int;
-- level : int;
-- optname : int;
-- optval : char_ptr;
-- optlen : int_ptr)
-- return int;
-- pragma Import (C, getsockopt, getsockopt_LINKNAME);
-- -- get options associated with a socket
-- function setsockopt
-- (socket_fd : int;
-- level : int;
-- optname : int;
-- optval : char_ptr;
-- optlen : int)
-- return int;
-- pragma Import (C, setsockopt, setsockopt_LINKNAME);
-- -- set options associated with a socket
-- function recv
-- (socket_fd : int;
-- buf : char_ptr;
-- len : int;
-- flags : int)
-- return int;
-- pragma Import (C, recv, recv_LINKNAME);
-- -- requires socket must be connected
-- function send
-- (socket_fd : int;
-- buf : char_ptr;
-- len : int;
-- flags : int)
-- return int;
-- pragma Import (C, send, send_LINKNAME);
-- -- requires socket must be connected
-- function sendmsg
-- (socket_fd : int;
-- msg : struct_msghdr_ptr;
-- flags : int)
-- return int;
-- pragma Import (C, recvmsg, recvmsg_LINKNAME);
-- function recvmsg
-- (socket_fd : int;
-- msg : struct_msghdr_ptr;
-- flags : int)
-- return int;
-- pragma Import (C, recvmsg, recvmsg_LINKNAME);
-- other functions we may need?
-- ioctl
-- fcntl
-- socketpair
-- shutdown
end Sockets;
|