File: sockets-unix.adb

package info (click to toggle)
libflorist 2006-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,696 kB
  • ctags: 314
  • sloc: ada: 10,587; ansic: 7,869; makefile: 199
file content (132 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download | duplicates (2)
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
-----------------------------------------------------------------------------
--                                                                          --
--                                 GNATSOCKS                                --
--                                                                          --
--                          S o c k e t s . U n i x                         --
--                                                                          --
--                                  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: 1.1.1.1 $]

with Ada.Finalization,
     Ada.Streams,
     POSIX.C,
     POSIX.Implementation,
     System,
     Unchecked_Conversion;
package body Sockets.Unix is

   use POSIX,
       POSIX.C,
       POSIX.C.Sockets,
       POSIX.Implementation;

   function getsockname
     (socket_fd : int;
      name : sockaddr_var_ptr;
      namelen : access int)
     return int;
      pragma Import (C, getsockname, getsockname_LINKNAME);
   --  returns the current name for a socket

   function "+" is new Unchecked_Conversion
     (sockaddr_un_ptr, sockaddr_var_ptr);
   function "+" is new Unchecked_Conversion
     (sockaddr_un_ptr, sockaddr_ptr);

   function Get_Address
     (Sock : in Socket'Class)
     return Unix_Socket_Address is
      addr : aliased struct_sockaddr_un;
      addrlen : aliased int := addr'Size / char'Size;
   begin
      Check (getsockname (Sock.fd, +addr'Unchecked_Access,
        addrlen'Unchecked_Access));
      if addrlen /= addr'Size / char'Size then raise Constraint_Error; end if;
      return (Socket_Address with un_addr => addr);
   end Get_Address;

   type String_Ptr is access all String;
   function New_Address (Path : String) return Unix_Socket_Address is
      addr : struct_sockaddr_un;
      function sptr_to_psptr is new Unchecked_Conversion
         (String_Ptr, POSIX_String_Ptr);
   begin
      if Path'Length > addr.sun_path'Length - 1 then
         raise Constraint_Error;
      end if;
      addr.sun_family := AF_UNIX;
      Nulterminate (addr.sun_path, Path);
      return (Socket_Address with un_addr => addr);
   end New_Address;

   function Get_Path (Addr : Unix_Socket_Address) return String is
   begin
      if Valid  (Addr) then raise Constraint_Error; end if;
      return Form_String (Addr.un_addr.sun_path (1)'Unchecked_Access);
   end Get_Path;

   ----------------------------------------

   function Address
     (Addr : Unix_Socket_Address)
     return sockaddr_ptr is
   begin
      return +Addr.un_addr'Unchecked_Access;
   end Address;

   function Length (Addr : Unix_Socket_Address) return POSIX.C.int is
   begin
      return Addr.un_addr'Size / char'Size;
   end Length;

   function Valid (Addr : Unix_Socket_Address) return Boolean is
   --  only the most basic validity check
   --  ... consider doing more?
   begin
      for I in Addr.un_addr.sun_path'Range loop
         if Addr.un_addr.sun_path (I) = NUL then
            return Addr.un_addr.sun_family = AF_UNIX;
         end if;
      end loop;
      return False;
   end Valid;

   function Protocol_Family
     (Addr : Unix_Socket_Address) return POSIX.C.int is
   begin
      return PF_UNIX;
   end Protocol_Family;

end Sockets.Unix;