This file is indexed.

/usr/share/ada/adainclude/anet/anet-sockets-thin.adb is in libanet0.1-dev 0.1-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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
--
--  Copyright (C) 2011, 2012 secunet Security Networks AG
--  Copyright (C) 2011, 2012 Reto Buerki <reet@codelabs.ch>
--  Copyright (C) 2011, 2012 Adrian-Ken Rueegsegger <ken@codelabs.ch>
--
--  This program is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; either version 2 of the License, or (at your
--  option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
--
--  This program 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.
--
--  As a special exception, if other files instantiate generics from this
--  unit,  or  you  link  this  unit  with  other  files  to  produce  an
--  executable   this  unit  does  not  by  itself  cause  the  resulting
--  executable to  be  covered by the  GNU General  Public License.  This
--  exception does  not  however  invalidate  any  other reasons why  the
--  executable file might be covered by the GNU Public License.
--

with Anet.Byte_Swapping;
with Anet.OS;

package body Anet.Sockets.Thin is

   package C renames Interfaces.C;

   Families : constant array (Family_Type) of C.int
     := (Family_Inet   => Constants.Sys.AF_INET,
         Family_Inet6  => Constants.Sys.AF_INET6,
         Family_Packet => Constants.AF_PACKET,
         Family_Unix   => Constants.AF_UNIX);
   --  Address family mapping.

   Levels : constant array (Level_Type) of C.int
     := (Socket_Level => Constants.Sys.SOL_SOCKET);
   --  Protocol level mapping.

   Options_Bool : constant array (Option_Name_Bool) of C.int
     := (Reuse_Address => Constants.Sys.SO_REUSEADDR,
         Broadcast     => Constants.Sys.SO_BROADCAST);
   --  Mapping for option names with boolean value.

   Options_Str : constant array (Option_Name_Str) of C.int
     := (Bind_To_Device => Constants.SO_BINDTODEVICE);
   --  Mapping for option names with string value.

   Modes : constant array (Mode_Type) of C.int
     := (Stream_Socket   => Constants.Sys.SOCK_STREAM,
         Datagram_Socket => Constants.Sys.SOCK_DGRAM);
   --  Socket mode mapping.

   Get_Requests : constant array (Netdev_Request_Name) of C.int
     := (If_Addr   => Constants.SIOCGIFADDR,
         If_Flags  => Constants.SIOCGIFFLAGS,
         If_Hwaddr => Constants.SIOCGIFHWADDR,
         If_Index  => Constants.SIOCGIFINDEX);
   --  Currently supported netdevice ioctl get requests.

   Set_Requests : constant array (Netdev_Request_Name) of C.int
     := (If_Flags => Constants.SIOCSIFFLAGS,
         others   => C.int (-1));
   --  Currently supported netdevice ioctl set requests.

   type Sockaddr_Type is record
      Sa_Family : C.unsigned_short;
      --  Address family
      Sa_Data   : C.char_array (1 .. 14) := (others => C.nul);
      --  Family-specific data
   end record;
   pragma Convention (C, Sockaddr_Type);
   --  Generic socket address.

   type Sockaddr_LL_Type is record
      Sa_Family   : C.unsigned_short            := Constants.AF_PACKET;
      --  Address family (always AF_PACKET)
      Sa_Protocol : C.unsigned_short            := 0;
      --  Physical layer protocol
      Sa_Ifindex  : C.int                       := 0;
      --  Interface number
      Sa_Hatype   : C.unsigned_short            := 0;
      --  Header type
      Sa_Pkttype  : C.unsigned_char             := 0;
      --  Packet type
      Sa_Halen    : C.unsigned_char             := 0;
      --  Length of address
      Sa_Addr     : Hardware_Addr_Type (1 .. 8) := (others => 0);
      --  Physical layer address
   end record;
   pragma Convention (C, Sockaddr_LL_Type);
   --  Device independent physical layer address

   type If_Req_Type (Name : Netdev_Request_Name := If_Index) is record
      Ifr_Name : C.char_array (1 .. Max_Iface_Name_Len) := (others => C.nul);

      case Name is
         when If_Addr   =>
            Ifr_Addr    : Sockaddr_Type;
         when If_Hwaddr =>
            Ifr_Hwaddr  : Sockaddr_Type;
         when If_Index  =>
            Ifr_Ifindex : C.int   := 0;
         when If_Flags  =>
            Ifr_Flags   : C.short := 0;
      end case;
   end record;
   pragma Unchecked_Union (If_Req_Type);
   pragma Convention (C, If_Req_Type);
   --  Interface request structure (struct ifreq).

   function C_Send
     (S     : C.int;
      Buf   : System.Address;
      Len   : C.int;
      Flags : C.int)
      return C.int;
   pragma Import (C, C_Send, "send");

   function C_Sendto
     (S     : C.int;
      Buf   : System.Address;
      Len   : C.int;
      Flags : C.int;
      To    : System.Address;
      Tolen : C.int)
      return C.int;
   pragma Import (C, C_Sendto, "sendto");

   function C_Bind
     (S       : C.int;
      Name    : System.Address;
      Namelen : C.int)
      return C.int;
   pragma Import (C, C_Bind, "bind");

   function C_Recvfrom
     (S       : C.int;
      Msg     : System.Address;
      Len     : C.int;
      Flags   : C.int;
      From    : System.Address;
      Fromlen : not null access C.int)
      return C.int;
   pragma Import (C, C_Recvfrom, "recvfrom");

   function C_Connect
     (S       : C.int;
      Name    : System.Address;
      Namelen : C.int)
      return C.int;
   pragma Import (C, C_Connect, "connect");

   function To_Sock_Addr (Address : Socket_Addr_Type) return Sockaddr_In_Type;
   --  Return inet sock address for given socket address.

   function Ioctl_Get
     (Socket     : Integer;
      Request    : Netdev_Request_Name;
      Iface_Name : Iface_Name_Type)
      return If_Req_Type;
   --  Execute netdevice ioctl get request on interface with given name. The
   --  specified socket must have been created beforehand. The procedure
   --  returns the resulting interface request record to the caller.

   procedure Ioctl
     (Socket  : Integer;
      Request : C.int;
      If_Req  : not null access If_Req_Type);
   --  Execute netdevice ioctl request on interface with given name and request
   --  type. The specified socket must have been created beforehand.

   function Query_Iface
     (Iface_Name : Iface_Name_Type;
      Request    : Netdev_Request_Name)
      return If_Req_Type;
   --  Query interface with given request.

   procedure Join_Multicast_Group_V4
     (Socket : Integer;
      Group  : Socket_Addr_Type;
      Iface  : Iface_Name_Type := "");
   --  Join the given IPv4 multicast group on the interface specified by name.

   procedure Join_Multicast_Group_V6
     (Socket : Integer;
      Group  : Socket_Addr_Type;
      Iface  : Iface_Name_Type := "");
   --  Join the given IPv6 multicast group on the interface specified by name.

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

   procedure Accept_Socket
     (Socket       :     Integer;
      Sockaddr     :     System.Address;
      Sockaddr_Len :     Integer;
      New_Socket   : out Integer)
   is
      use type C.int;

      function C_Accept
        (S       : C.int;
         Name    : System.Address;
         Namelen : not null access C.int)
         return C.int;
      pragma Import (C, C_Accept, "accept");

      Res : C.int;
      Len : aliased C.int := C.int (Sockaddr_Len);
   begin
      Res := C_Accept (S       => C.int (Socket),
                       Name    => Sockaddr,
                       Namelen => Len'Access);

      if Res = C_Failure then
         raise Socket_Error with "Unable to accept connection on socket - "
           & Get_Errno_String;
      end if;

      New_Socket := Integer (Res);
   end Accept_Socket;

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

   procedure Bind_Socket
     (Socket  : Integer;
      Address : Socket_Addr_Type)
   is
      use type C.int;

      Res   : C.int;
      Value : constant Sockaddr_In_Type := To_Sock_Addr (Address => Address);
   begin
      Res := C_Bind (S       => C.int (Socket),
                     Name    => Value'Address,
                     Namelen => Value'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to bind socket to "
           & To_String (Address => Address) & " - " & Get_Errno_String;
      end if;
   end Bind_Socket;

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

   procedure Bind_Socket
     (Socket : Integer;
      Iface  : Iface_Name_Type)
   is
      use type C.int;

      Res   : C.int;
      Value : Sockaddr_LL_Type;
   begin
      Value.Sa_Protocol := C.unsigned_short
        (Byte_Swapping.Host_To_Network
           (Input => Double_Byte (Constants.ETH_P_IP)));
      Value.Sa_Ifindex  := C.int (Get_Iface_Index (Name => Iface));

      Res := C_Bind (S       => C.int (Socket),
                     Name    => Value'Address,
                     Namelen => Value'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to bind packet socket to interface "
           & String (Iface) & " - " & Get_Errno_String;
      end if;
   end Bind_Socket;

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

   procedure Bind_Unix_Socket
     (Socket : Integer;
      Path   : Unix_Path_Type)
   is
      use type C.int;

      Res    : C.int;
      C_Path : constant C.char_array := C.To_C (String (Path));
      Value  : Sockaddr_Un_Type;
   begin
      OS.Delete_File (Filename => String (Path));

      Value.Pathname (1 .. C_Path'Length) := C_Path;

      Res := C_Bind (S       => C.int (Socket),
                     Name    => Value'Address,
                     Namelen => Value'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to bind unix socket to path "
           & String (Path) & " - " & Get_Errno_String;
      end if;
   end Bind_Unix_Socket;

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

   procedure Close_Socket (Socket : Integer)
   is
      use type C.int;

      function C_Close (Fd : C.int) return C.int;
      pragma Import (C, C_Close, "close");

      Res : C.int;
   begin
      Res := C_Close (C.int (Socket));

      if Res = C_Failure then
         raise Socket_Error with "Unable to close socket: " & Get_Errno_String;
      end if;
   end Close_Socket;

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

   procedure Connect_Socket
     (Socket : Integer;
      Dst    : Socket_Addr_Type)
   is
      use type C.int;

      Res : C.int;
      Sin : constant Sockaddr_In_Type := To_Sock_Addr (Address => Dst);
   begin
      Res := C_Connect (S       => C.int (Socket),
                        Name    => Sin'Address,
                        Namelen => Sin'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to connect socket to address "
           & To_String (Dst) & " - " & Get_Errno_String;
      end if;
   end Connect_Socket;

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

   procedure Connect_Socket
     (Socket : Integer;
      Path   : Unix_Path_Type)
   is
      use type C.int;

      Res    : C.int;
      C_Path : constant C.char_array := C.To_C (String (Path));
      Value  : Sockaddr_Un_Type;
   begin
      Value.Pathname (1 .. C_Path'Length) := C_Path;

      Res := C_Connect (S       => C.int (Socket),
                        Name    => Value'Address,
                        Namelen => Value'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to connect unix socket to path "
           & String (Path) & " - " & Get_Errno_String;
      end if;
   end Connect_Socket;

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

   procedure Create_Socket
     (Socket : out Integer;
      Family :     Family_Type := Family_Inet;
      Mode   :     Mode_Type   := Datagram_Socket)
   is
      use type Interfaces.C.int;

      function C_Socket
        (Domain   : C.int;
         Typ      : C.int;
         Protocol : C.int)
         return C.int;
      pragma Import (C, C_Socket, "socket");

      Res   : C.int;
      Proto : C.int := 0;
   begin
      if Family = Family_Packet then
         Proto := C.int (Byte_Swapping.Host_To_Network
                         (Input => Double_Byte (Constants.ETH_P_IP)));
      end if;

      Res := C_Socket (Domain   => Families (Family),
                       Typ      => Modes (Mode),
                       Protocol => Proto);

      if Res = C_Failure then
         raise Socket_Error with "Unable to create socket (" & Family'Img & "/"
           & Mode'Img & "): " & Get_Errno_String;
      end if;

      Socket := Integer (Res);
   end Create_Socket;

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

   function Get_Iface_Index (Name : Iface_Name_Type) return Positive
   is
      Req : constant If_Req_Type := Query_Iface
        (Iface_Name => Name,
         Request    => If_Index);
   begin
      return Positive (Req.Ifr_Ifindex);
   end Get_Iface_Index;

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

   function Get_Iface_IP (Name : Iface_Name_Type) return IPv4_Addr_Type
   is
      Req : constant If_Req_Type := Query_Iface
        (Iface_Name => Name,
         Request    => If_Addr);

      --  The actual IP address is at range 3 .. 6. The first two bytes of the
      --  result are the sin_port value.

      Offset : constant := 2;
   begin
      return Result : IPv4_Addr_Type do
         for B in Result'Range loop
            Result (B) := Character'Pos
              (C.To_Ada
                 (Req.Ifr_Addr.Sa_Data
                    (C.size_t (B + Offset))));
         end loop;
      end return;
   end Get_Iface_IP;

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

   function Get_Iface_Mac (Name : Iface_Name_Type) return Hardware_Addr_Type
   is
      Req  : constant If_Req_Type := Query_Iface
        (Iface_Name => Name,
         Request    => If_Hwaddr);
   begin
      return Result : Hardware_Addr_Type (1 .. 6) do
         for B in Result'Range loop
            Result (B) := Character'Pos
              (C.To_Ada
                 (Req.Ifr_Hwaddr.Sa_Data
                    (C.size_t (B))));
         end loop;
      end return;
   end Get_Iface_Mac;

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

   procedure Get_Socket_Info
     (Sock_Addr :     Sockaddr_In_Type;
      Source    : out Socket_Addr_Type)
   is
      use type Interfaces.C.unsigned_short;
   begin
      if Sock_Addr.Sin_Family = Constants.Sys.AF_INET then
         Source.Addr_V4 := Sock_Addr.Sin_Addr;
         Source.Port_V4 := Byte_Swapping.Host_To_Network
           (Input => Port_Type (Sock_Addr.Sin_Port));
      elsif Sock_Addr.Sin_Family = Constants.Sys.AF_INET6 then
         Source.Addr_V6 := Sock_Addr.Sin6_Addr;
         Source.Port_V6 := Byte_Swapping.Host_To_Network
           (Input => Port_Type (Sock_Addr.Sin_Port));
      else
         raise Socket_Error with "Invalid source address family";
      end if;
   end Get_Socket_Info;

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

   procedure Ioctl
     (Socket  : Integer;
      Request : C.int;
      If_Req  : not null access If_Req_Type)
   is
      use type C.int;

      function C_Ioctl
        (S    : C.int;
         Req  : C.int;
         Arg  : access If_Req_Type)
         return C.int;
      pragma Import (C, C_Ioctl, "ioctl");

      Ctl_Ret : C.int;
   begin
      Ctl_Ret := C_Ioctl
        (S   => C.int (Socket),
         Req => Request,
         Arg => If_Req);

      if Ctl_Ret = C_Failure then
         raise Socket_Error with "Ioctl (" & Request'Img
           & ") failed on interface '" & C.To_Ada (If_Req.Ifr_Name) & "': "
           & Get_Errno_String;
      end if;
   end Ioctl;

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

   function Ioctl_Get
     (Socket     : Integer;
      Request    : Netdev_Request_Name;
      Iface_Name : Iface_Name_Type)
      return If_Req_Type
   is
      C_Name  : constant C.char_array := C.To_C (String (Iface_Name));
      If_Req  : aliased If_Req_Type (Name => Request);
   begin
      If_Req.Ifr_Name (1 .. C_Name'Length) := C_Name;

      Ioctl (Socket  => Socket,
             Request => Get_Requests (Request),
             If_Req  => If_Req'Access);

      return If_Req;
   end Ioctl_Get;

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

   function Is_Iface_Up (Name : Iface_Name_Type) return Boolean
   is
      use type Interfaces.C.short;

      Req : constant If_Req_Type := Query_Iface
        (Iface_Name => Name,
         Request    => If_Flags);
   begin
      return (Req.Ifr_Flags mod 2) = Constants.IFF_UP;
   end Is_Iface_Up;

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

   procedure Join_Multicast_Group
     (Socket : Integer;
      Group  : Socket_Addr_Type;
      Iface  : Iface_Name_Type := "")
   is
   begin
      if Group.Family = Family_Inet then
         Join_Multicast_Group_V4 (Socket => Socket,
                                  Group  => Group,
                                  Iface  => Iface);
      elsif Group.Family = Family_Inet6 then
         Join_Multicast_Group_V6 (Socket => Socket,
                                  Group  => Group,
                                  Iface  => Iface);
      end if;
   end Join_Multicast_Group;

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

   procedure Join_Multicast_Group_V4
     (Socket : Integer;
      Group  : Socket_Addr_Type;
      Iface  : Iface_Name_Type := "")
   is
      use type Interfaces.C.int;

      type IP_Mreq_Type is record
         Imr_Multiaddr : IPv4_Addr_Type;
         Imr_Interface : C.unsigned;
      end record;
      pragma Convention (C, IP_Mreq_Type);
      --  struct ip_mreq (netinet/in.h).

      Mreq : IP_Mreq_Type
        := (Imr_Multiaddr => Group.Addr_V4,
            Imr_Interface => 0);
      Res  : C.int;
   begin
      if Iface'Length /= 0 then
         Mreq.Imr_Interface := C.unsigned (Get_Iface_Index (Name => Iface));
      end if;

      Res := C_Setsockopt
        (S       => C.int (Socket),
         Level   => Constants.Sys.IPPROTO_IP,
         Optname => Constants.Sys.IP_ADD_MEMBERSHIP,
         Optval  => Mreq'Address,
         Optlen  => Mreq'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to join multicast group "
           & To_String (Address => Group) & ": " & Get_Errno_String;
      end if;
   end Join_Multicast_Group_V4;

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

   procedure Join_Multicast_Group_V6
     (Socket : Integer;
      Group  : Socket_Addr_Type;
      Iface  : Iface_Name_Type := "")
   is
      use type Interfaces.C.int;

      type IPv6_Mreq_Type is record
         IPv6mr_Multiaddr : IPv6_Addr_Type;
         IPv6mr_Interface : C.unsigned;
      end record;
      pragma Convention (C, IPv6_Mreq_Type);
      --  struct ipv6_mreq (netinet/in.h).

      Mreq : IPv6_Mreq_Type
        := (IPv6mr_Multiaddr => Group.Addr_V6,
            IPv6mr_Interface => 0);
      Res  : C.int;
   begin
      if Iface'Length /= 0 then
         Mreq.IPv6mr_Interface := C.unsigned (Get_Iface_Index (Name => Iface));
      end if;

      Res := C_Setsockopt
        (S       => C.int (Socket),
         Level   => Constants.IPPROTO_IPV6,
         Optname => Constants.IPV6_ADD_MEMBERSHIP,
         Optval  => Mreq'Address,
         Optlen  => Mreq'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to join multicast group "
           & To_String (Address => Group) & ": " & Get_Errno_String;
      end if;
   end Join_Multicast_Group_V6;

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

   procedure Listen_Socket
     (Socket  : Integer;
      Backlog : Positive := 1)
   is
      use type Interfaces.C.int;

      function C_Listen
        (Socket  : C.int;
         Backlog : C.int)
         return C.int;
      pragma Import (C, C_Listen, "listen");

      Res : C.int;
   begin
      Res := C_Listen (Socket  => C.int (Socket),
                       Backlog => C.int (Backlog));

      if Res = C_Failure then
         raise Socket_Error with "Unable to listen on socket with backlog"
           & Backlog'Img & " - " & Get_Errno_String;
      end if;
   end Listen_Socket;

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

   function Query_Iface
     (Iface_Name : Iface_Name_Type;
      Request    : Netdev_Request_Name)
      return If_Req_Type
   is
      Req  : If_Req_Type;
      Sock : Integer := -1;
   begin
      Create_Socket (Socket => Sock);

      begin
         Req := Ioctl_Get (Socket     => Sock,
                           Request    => Request,
                           Iface_Name => Iface_Name);

      exception
         when Socket_Error =>
            Close_Socket (Socket => Sock);
            raise;
      end;
      Close_Socket (Socket => Sock);

      return Req;
   end Query_Iface;

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

   procedure Receive_Socket
     (Socket :     Integer;
      Data   : out Ada.Streams.Stream_Element_Array;
      Last   : out Ada.Streams.Stream_Element_Offset;
      Source : out Socket_Addr_Type)
   is
      use type Interfaces.C.int;
      use type Ada.Streams.Stream_Element_Offset;

      Res : C.int;
      Sin : Sockaddr_In_Type;
      Len : aliased C.int := Sin'Size / 8;
   begin
      Res := C_Recvfrom (S       => C.int (Socket),
                         Msg     => Data'Address,
                         Len     => Data'Length,
                         Flags   => 0,
                         From    => Sin'Address,
                         Fromlen => Len'Access);

      if Res = C_Failure then
         raise Socket_Error with "Error receiving data: " & Get_Errno_String;
      end if;

      Last := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);

      if Len /= 0 then
         Get_Socket_Info (Sock_Addr => Sin,
                          Source    => Source);
      else
         Source := No_Addr;
      end if;
   end Receive_Socket;

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

   procedure Receive_Socket
     (Socket      :     Integer;
      Data        : out Ada.Streams.Stream_Element_Array;
      Last        : out Ada.Streams.Stream_Element_Offset;
      Src_HW_Addr : out Hardware_Addr_Type)
   is
      use type Interfaces.C.int;
      use type Ada.Streams.Stream_Element_Offset;

      Res   : C.int;
      Saddr : Sockaddr_LL_Type;
      Len   : aliased C.int := Saddr'Size / 8;
   begin
      Res := C_Recvfrom (S       => C.int (Socket),
                         Msg     => Data'Address,
                         Len     => Data'Length,
                         Flags   => 0,
                         From    => Saddr'Address,
                         Fromlen => Len'Access);

      if Res = C_Failure then
         raise Socket_Error with "Error receiving packet data: "
           & Get_Errno_String;
      end if;

      Src_HW_Addr := Saddr.Sa_Addr (Saddr.Sa_Addr'First .. Src_HW_Addr'Length);
      Last        := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);
   end Receive_Socket;

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

   procedure Receive_Socket
     (Socket :     Integer;
      Data   : out Ada.Streams.Stream_Element_Array;
      Last   : out Ada.Streams.Stream_Element_Offset)
   is
      use type Interfaces.C.int;
      use type Ada.Streams.Stream_Element_Offset;

      function C_Recv
        (S       : C.int;
         Msg     : System.Address;
         Len     : C.int;
         Flags   : C.int)
         return C.int;
      pragma Import (C, C_Recv, "recv");

      Res : C.int;
   begin
      Res := C_Recv (S     => C.int (Socket),
                     Msg   => Data'Address,
                     Len   => Data'Length,
                     Flags => 0);

      if Res = C_Failure then
         raise Socket_Error with "Error receiving packet data: "
           & Get_Errno_String;
      end if;

      Last := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);
   end Receive_Socket;

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

   procedure Send_Socket
     (Socket :     Integer;
      Data   :     Ada.Streams.Stream_Element_Array;
      Last   : out Ada.Streams.Stream_Element_Offset;
      Dst    :     Socket_Addr_Type)
   is
      use type Interfaces.C.int;
      use type Ada.Streams.Stream_Element_Offset;

      Res : C.int;
      Sin : constant Sockaddr_In_Type := To_Sock_Addr (Address => Dst);
   begin
      Res := C_Sendto (S     => C.int (Socket),
                       Buf   => Data'Address,
                       Len   => Data'Length,
                       Flags => 0,
                       To    => Sin'Address,
                       Tolen => Sin'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Error sending data to "
           & To_String (Address => Dst) & " - " & Get_Errno_String;
      end if;

      Last := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);
   end Send_Socket;

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

   procedure Send_Socket
     (Socket :     Integer;
      Data   :     Ada.Streams.Stream_Element_Array;
      Last   : out Ada.Streams.Stream_Element_Offset;
      To     :     Hardware_Addr_Type;
      Iface  :     Iface_Name_Type)
   is
      use type C.int;
      use type Ada.Streams.Stream_Element_Offset;

      Res     : C.int;
      LL_Dest : Sockaddr_LL_Type;
   begin
      LL_Dest.Sa_Ifindex  := C.int (Get_Iface_Index (Name => Iface));
      LL_Dest.Sa_Halen    := To'Length;
      LL_Dest.Sa_Protocol := C.unsigned_short
        (Byte_Swapping.Host_To_Network
           (Input => Double_Byte (Constants.ETH_P_IP)));

      LL_Dest.Sa_Addr (1 .. To'Length) := To;

      Res := C_Sendto (S     => C.int (Socket),
                       Buf   => Data'Address,
                       Len   => Data'Length,
                       Flags => 0,
                       To    => LL_Dest'Address,
                       Tolen => LL_Dest'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable to send packet data on interface "
           & String (Iface) & " to " & To_String (Address => To)
           & " - " & Get_Errno_String;
      end if;

      Last := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);
   end Send_Socket;

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

   procedure Send_Socket
     (Socket :     Integer;
      Data   :     Ada.Streams.Stream_Element_Array;
      Last   : out Ada.Streams.Stream_Element_Offset)
   is
      use type C.int;
      use type Ada.Streams.Stream_Element_Offset;

      Res : C.int;
   begin
      Res := C_Send (S     => C.int (Socket),
                     Buf   => Data'Address,
                     Len   => Data'Length,
                     Flags => 0);

      if Res = C_Failure then
         raise Socket_Error with "Unable to send data on unix socket"
           & " - " & Get_Errno_String;
      end if;

      Last := Data'First + Ada.Streams.Stream_Element_Offset (Res - 1);
   end Send_Socket;

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

   procedure Set_Iface_State
     (Name  : Iface_Name_Type;
      State : Boolean)
   is
      Sock : Integer := -1;
   begin
      Create_Socket (Socket => Sock);

      declare
         C_Name : constant C.char_array := C.To_C (String (Name));
         Req    : aliased If_Req_Type (Name => If_Flags);
      begin
         Req.Ifr_Name (1 .. C_Name'Length) := C_Name;

         if State then
            Req.Ifr_Flags := Constants.IFF_UP;
         else
            Req.Ifr_Flags := 0;
         end if;

         Ioctl (Socket  => Sock,
                Request => Set_Requests (If_Flags),
                If_Req  => Req'Access);

      exception
         when Socket_Error =>
            Close_Socket (Socket => Sock);
            raise;
      end;
      Close_Socket (Socket => Sock);
   end Set_Iface_State;

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

   procedure Set_Socket_Option
     (Socket : Integer;
      Level  : Level_Type := Socket_Level;
      Option : Option_Name_Bool;
      Value  : Boolean)
   is
      use type Interfaces.C.int;

      Val : C.int := C.int (Boolean'Pos (Value));
      Res : C.int;
   begin
      Res := C_Setsockopt
        (S       => C.int (Socket),
         Level   => Levels (Level),
         Optname => Options_Bool (Option),
         Optval  => Val'Address,
         Optlen  => Val'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable set socket option " & Option'Img
           & " to " & Value'Img & ": " & Get_Errno_String;
      end if;
   end Set_Socket_Option;

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

   procedure Set_Socket_Option
     (Socket : Integer;
      Level  : Level_Type := Socket_Level;
      Option : Option_Name_Str;
      Value  : String)
   is
      use type Interfaces.C.int;

      Val : constant C.char_array := C.To_C (Value);
      Res : C.int;
   begin
      Res := C_Setsockopt
        (S       => C.int (Socket),
         Level   => Levels (Level),
         Optname => Options_Str (Option),
         Optval  => Val'Address,
         Optlen  => Val'Size / 8);

      if Res = C_Failure then
         raise Socket_Error with "Unable set socket option " & Option'Img
           & " to '" & Value & "': " & Get_Errno_String;
      end if;
   end Set_Socket_Option;

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

   function To_Sock_Addr (Address : Socket_Addr_Type) return Sockaddr_In_Type
   is
   begin
      case Address.Family is
         when Family_Inet  =>
            return
              (Family     => Family_Inet,
               Sin_Family => Constants.Sys.AF_INET,
               Sin_Port   => C.unsigned_short
                 (Byte_Swapping.Host_To_Network (Input => Address.Port_V4)),
               Sin_Addr   => Address.Addr_V4,
               Sin_Zero   => <>);
         when Family_Inet6 =>
            return
              (Family     => Family_Inet6,
               Sin_Family => Constants.Sys.AF_INET6,
               Sin_Port   => C.unsigned_short
                 (Byte_Swapping.Host_To_Network (Input => Address.Port_V6)),
               Sin6_Addr  => Address.Addr_V6,
               others     => 0);
         when others =>
            raise Socket_Error with "Unable to convert '" & To_String
              (Address => Address) & "' to IPv4 or IPv6 socket address";
      end case;
   end To_Sock_Addr;

end Anet.Sockets.Thin;