/usr/share/ada/adainclude/aws/aws-net.adb is in libaws3.2.0-dev 3.2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2000-2014, AdaCore --
-- --
-- This library 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 3, or (at your option) any --
-- later version. This library 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. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
------------------------------------------------------------------------------
with Ada.Real_Time;
with Ada.Strings.Fixed;
with Ada.Unchecked_Deallocation;
with AWS.Net.Log;
with AWS.Net.Poll_Events;
with AWS.Net.SSL;
with AWS.Net.Std;
with AWS.OS_Lib;
with Interfaces.C.Strings;
package body AWS.Net is
Timeout_Token : constant String := " timeout.";
---------
-- Add --
---------
procedure Add
(FD_Set : in out FD_Set_Access;
FD : FD_Type;
Event : Wait_Event_Set)
is
Old_Set : FD_Set_Access;
begin
if Length (FD_Set.all) = FD_Set.Size then
Old_Set := FD_Set;
if FD_Set.Size < 256 then
FD_Set := Copy (FD_Set, FD_Set.Size * 2);
else
FD_Set := Copy (FD_Set, FD_Set.Size + 256);
end if;
Free (Old_Set);
end if;
Add (FD_Set.all, FD, Event);
end Add;
------------
-- Adjust --
------------
overriding procedure Adjust (Socket : in out Socket_Type) is
begin
Socket.C.Ref_Count.Increment;
end Adjust;
-----------
-- Check --
-----------
function Check
(Socket : Socket_Type'Class;
Events : Wait_Event_Set) return Event_Set is
begin
return Poll (Socket, Events, 0.0);
end Check;
------------------------
-- Cipher_Description --
------------------------
function Cipher_Description (Socket : Socket_Type) return String is
pragma Unreferenced (Socket);
begin
return "";
end Cipher_Description;
-------------------
-- Error_Message --
-------------------
function Error_Message (Errno : Integer) return String is
use Interfaces.C.Strings;
Ptr : constant chars_ptr := OS_Lib.Socket_StrError (Errno);
Code : constant String := '[' & Utils.Image (Errno) & "] ";
begin
if Ptr = Null_Ptr then
return Code & "Unknown error";
else
return Code & Value (Ptr);
end if;
end Error_Message;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Socket : in out Socket_Type) is
procedure Unchecked_Free is
new Unchecked_Deallocation (RW_Data, RW_Data_Access);
Cache : RW_Data_Access := Socket.C;
Ref_Count : Natural;
begin
-- Ensure call is idempotent
Socket.C := null;
if Cache /= null then
Cache.Ref_Count.Decrement (Value => Ref_Count);
if Ref_Count = 0 then
Free (Socket_Type'Class (Socket));
Unchecked_Free (Cache);
end if;
end if;
end Finalize;
----------
-- Free --
----------
procedure Free (Socket : in out Socket_Access) is
procedure Unchecked_Free is
new Ada.Unchecked_Deallocation (Socket_Type'Class, Socket_Access);
begin
Unchecked_Free (Socket);
end Free;
procedure Free (FD_Set : in out FD_Set_Access) is
procedure Unchecked_Free is
new Ada.Unchecked_Deallocation (Net.FD_Set'Class, FD_Set_Access);
begin
Unchecked_Free (FD_Set);
end Free;
----------------------
-- Get_Socket_Errno --
----------------------
function Get_Socket_Errno (E : Exception_Occurrence) return Natural is
M : constant String := Exception_Message (E);
First, Last : Natural;
Errno : Natural := 0;
begin
if Exception_Identity (E) = Net.Socket_Error'Identity
or else
Strings.Fixed.Index (Exception_Name (E), "CONNECTION_ERROR") /= 0
then
First := M'First;
-- Now check for the start of the message containing the errno
First := Strings.Fixed.Index (M, "[", From => First);
if First /= 0 then
First := First + 1;
Last := First;
while Last < M'Last and then M (Last + 1) in '0' .. '9' loop
Last := Last + 1;
end loop;
Errno := Natural'Value (M (First .. Last));
end if;
end if;
return Errno;
end Get_Socket_Errno;
---------------
-- Host_Name --
---------------
function Host_Name return String is
begin
return Net.Std.Host_Name;
end Host_Name;
----------------
-- Initialize --
----------------
overriding procedure Initialize (Socket : in out Socket_Type) is
begin
if Socket.C = null then
Socket.C := new RW_Data;
end if;
end Initialize;
----------------
-- IO_Control --
----------------
function IO_Control
(Socket : Socket_Type;
Code : Interfaces.C.int) return Stream_Element_Offset
is
use Interfaces;
use type C.int;
S : Socket_Type'Class renames Socket_Type'Class (Socket);
Res : C.int;
Arg : aliased C.int;
begin
Res := OS_Lib.C_Ioctl (C.int (S.Get_FD), Code, Arg'Unchecked_Access);
if Res = -1 then
S.Raise_Socket_Error (Error_Message (OS_Lib.Socket_Errno));
end if;
return Stream_Element_Count (Arg);
end IO_Control;
--------------------
-- IPv6_Available --
--------------------
function IPv6_Available return Boolean is
begin
return Std.IPv6_Available;
end IPv6_Available;
--------------------
-- Is_Any_Address --
--------------------
function Is_Any_Address (Socket : Socket_Type) return Boolean is
pragma Unreferenced (Socket);
begin
return False;
end Is_Any_Address;
-------------
-- Is_IPv6 --
-------------
function Is_IPv6 (Socket : Socket_Type) return Boolean is
pragma Unreferenced (Socket);
begin
return False;
end Is_IPv6;
------------------
-- Is_Listening --
------------------
function Is_Listening (Socket : Socket_Type) return Boolean is
begin
-- Do not use SO_ACCEPCONN socket option because Mac OS/X version 10.8
-- does not support it. Would be better to use SO_ACCEPTCONN instead of
-- boolean flag when possible.
return Socket.C.Listening;
end Is_Listening;
--------------------
-- Is_Peer_Closed --
--------------------
function Is_Peer_Closed
(Socket : Socket_Type;
E : Exception_Occurrence) return Boolean
is
pragma Unreferenced (Socket);
begin
return Exception_Message (E) = Peer_Closed_Message;
end Is_Peer_Closed;
----------------
-- Is_Timeout --
----------------
function Is_Timeout
(Socket : Socket_Type;
E : Exception_Occurrence) return Boolean
is
pragma Unreferenced (Socket);
begin
return Is_Timeout (E);
end Is_Timeout;
function Is_Timeout (E : Exception_Occurrence) return Boolean is
begin
return Strings.Fixed.Index (Exception_Message (E), Timeout_Token) > 0;
end Is_Timeout;
----------------
-- Last_Index --
----------------
function Last_Index
(First : Stream_Element_Offset;
Count : Natural) return Ada.Streams.Stream_Element_Offset is
begin
if First = Stream_Element_Offset'First and then Count = 0 then
raise Constraint_Error with
"last index out of range (no element transferred)";
else
return First + Stream_Element_Offset (Count - 1);
end if;
end Last_Index;
---------------
-- Localhost --
---------------
function Localhost (IPv6 : Boolean) return String is
begin
if IPv6 then
return "::1";
else
return "127.0.0.1";
end if;
end Localhost;
-----------------
-- Output_Busy --
-----------------
function Output_Busy (Socket : Socket_Type) return Stream_Element_Offset is
-- Implementation not in the aws-net-std__ipv6 and aws-net-std__gnat.adb
-- separately because GNAT.Sockets doesn't have appropriate Request_Type
-- anyway, at least in GNAT GPL 2013. So, we avoid code duplication this
-- way. We could easily make this routine abstract and split
-- implementation between aws-net-std__ipv6 and aws-net-std__gnat.adb
-- when GNAT.Sockets.Control_Socket get support it.
begin
pragma Warnings (Off, "*condition is always*");
if OS_Lib.FIONWRITE = -1 then
return -1;
else
return Socket.IO_Control (OS_Lib.FIONWRITE);
end if;
pragma Warnings (On, "*condition is always*");
end Output_Busy;
------------------
-- Output_Space --
------------------
function Output_Space (Socket : Socket_Type) return Stream_Element_Offset is
begin
pragma Warnings (Off, "*condition is always*");
if OS_Lib.FIONSPACE /= -1 then
return Socket.IO_Control (OS_Lib.FIONSPACE);
elsif OS_Lib.FIONWRITE /= -1 then
return Stream_Element_Offset'Max
(Stream_Element_Offset
(Socket_Type'Class (Socket).Get_Send_Buffer_Size)
- Socket.IO_Control (OS_Lib.FIONWRITE), 0);
else
return -1;
end if;
pragma Warnings (On, "*condition is always*");
end Output_Space;
----------
-- Poll --
----------
function Poll
(Socket : Socket_Type'Class;
Events : Wait_Event_Set;
Timeout : Duration) return Event_Set
is
Waiter : FD_Set'Class := To_FD_Set (Socket, Events);
Dummy : Natural;
begin
Wait (Waiter, Timeout, Dummy);
return Status (Waiter, 1);
end Poll;
------------------------
-- Raise_Socket_Error --
------------------------
procedure Raise_Socket_Error
(Socket : Socket_Type'Class; Text : String) is
begin
Log.Error (Socket, Text);
raise Socket_Error with Text;
end Raise_Socket_Error;
-------------
-- Receive --
-------------
function Receive
(Socket : Socket_Type'Class;
Max : Stream_Element_Count := 4096) return Stream_Element_Array
is
Result : Stream_Element_Array (1 .. Max);
Last : Stream_Element_Offset;
begin
Receive (Socket, Result, Last);
return Result (1 .. Last);
end Receive;
----------
-- Send --
----------
procedure Send
(Socket : Socket_Type'Class; Data : Stream_Element_Array)
is
use Ada.Real_Time;
Save : constant Boolean := Socket.C.Can_Wait;
First : Stream_Element_Offset := Data'First;
Last : Stream_Element_Offset;
Stamp : Time;
begin
Socket.C.Can_Wait := True;
if Socket.Timeout = Forever then
Stamp := Time_Last;
elsif Socket.Timeout < 0.0 then
Stamp := Clock;
else
begin
Stamp := Clock + To_Time_Span (Socket.Timeout);
exception
when Constraint_Error =>
-- Too big
Stamp := Time_Last;
end;
end if;
loop
Send (Socket, Data (First .. Data'Last), Last);
exit when Last = Data'Last;
Wait_For (Output, Socket, Timeout => To_Duration (Stamp - Clock));
if Last < Data'Last then
-- Otherwise First should be unchanged, because no data sent
First := Last + 1;
end if;
end loop;
Socket.C.Can_Wait := Save;
exception
when others =>
Socket.C.Can_Wait := Save;
raise;
end Send;
-----------------------
-- Set_Blocking_Mode --
-----------------------
procedure Set_Blocking_Mode
(Socket : in out Socket_Type; Blocking : Boolean) is
begin
if Blocking then
Set_Timeout (Socket_Type'Class (Socket), Forever);
else
Set_Timeout (Socket_Type'Class (Socket), 0.0);
end if;
end Set_Blocking_Mode;
-----------------
-- Set_Timeout --
-----------------
procedure Set_Timeout
(Socket : in out Socket_Type; Timeout : Duration) is
begin
Socket.Timeout := Timeout;
end Set_Timeout;
------------
-- Socket --
------------
function Socket (Security : Boolean) return Socket_Type'Class is
begin
if Security then
declare
Result : SSL.Socket_Type;
begin
return Result;
end;
else
declare
Result : Std.Socket_Type;
begin
return Result;
end;
end if;
end Socket;
function Socket (Security : Boolean) return Socket_Access is
begin
return new Socket_Type'Class'(Socket (Security));
end Socket;
-----------------
-- Socket_Pair --
-----------------
procedure Socket_Pair (S1, S2 : out Socket_Type) is
Server : Std.Socket_Type;
subtype STC is Socket_Type'Class;
Local_Host : constant String := "127.0.0.1";
begin
Std.Bind (Server, Host => Local_Host, Port => 0);
Std.Listen (Server);
Connect (STC (S1), Host => Local_Host, Port => Std.Get_Port (Server));
Std.Set_Timeout (Server, 0.25);
loop
Accept_Socket (Server, New_Socket => STC (S2));
-- to be shure that it is S1 and S2 connected together
exit when Peer_Addr (STC (S2)) = Local_Host
and then Peer_Port (STC (S2)) = Get_Port (STC (S1))
and then Peer_Port (STC (S1)) = Get_Port (STC (S2));
Shutdown (STC (S2));
end loop;
Std.Shutdown (Server);
end Socket_Pair;
---------------
-- To_FD_Set --
---------------
function To_FD_Set
(Socket : Socket_Type;
Events : Wait_Event_Set;
Size : Positive := 1) return FD_Set'Class
is
Result : Poll_Events.Set (Size);
begin
Poll_Events.Add (Result, Get_FD (Socket_Type'Class (Socket)), Events);
return Result;
end To_FD_Set;
----------
-- Wait --
----------
function Wait
(Socket : Socket_Type'Class;
Events : Wait_Event_Set) return Event_Set is
begin
return Poll (Socket, Events, Socket.Timeout);
end Wait;
--------------
-- Wait_For --
--------------
procedure Wait_For
(Mode : Wait_Event_Type; Socket : Socket_Type'Class; Timeout : Duration)
is
Events : Wait_Event_Set := (others => False);
Result : Event_Set;
begin
Events (Mode) := True;
Result := Poll (Socket, Events, Timeout);
if Result = Event_Set'(others => False) then
Raise_Socket_Error
(Socket, Wait_Event_Type'Image (Mode) & Timeout_Token);
elsif Result = Event_Set'(Error => True, others => False) then
Raise_Socket_Error
(Socket, Wait_Event_Type'Image (Mode) & "_Wait error.");
elsif not Result (Mode) then
raise Program_Error;
end if;
end Wait_For;
procedure Wait_For (Mode : Wait_Event_Type; Socket : Socket_Type'Class) is
begin
Wait_For (Mode, Socket, Socket.Timeout);
end Wait_For;
end AWS.Net;
|