agpl_1.0.0_b5da3320/src/agpl-geoip.ads

 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
 

--  Uses the CSV database from www.maxmind.com

with Agpl.Types.Ustrings; use Agpl.Types.Ustrings;

with Ada.Containers.Ordered_Maps;

package Agpl.Geoip is

   --  pragma Elaborate_Body;

   ------------------------------------------------------------------------
   -- Types                                                              --
   ------------------------------------------------------------------------
   subtype Country_code is String (1 .. 2);

   Unknown_country : constant Country_code := "??";

   ------------------------------------------------------------------------
   -- Init                                                               --
   ------------------------------------------------------------------------
   --  Call it before any other subroutine.
   --  Path to database.
   procedure Init (Database : in String);

   ------------------------------------------------------------------------
   -- Country_code_from_addr                                             --
   ------------------------------------------------------------------------
   --  Addr in dot format, port optional
   function Country_code_from_addr (Addr : in String) return Country_code;

   ------------------------------------------------------------------------
   -- Country_name_from_addr                                             --
   ------------------------------------------------------------------------
   --  Addr in dot format, port optional
   function Country_name_from_addr (Addr : in String) return String;

   ------------------------------------------------------------------------
   -- Country_name_from_code                                             --
   ------------------------------------------------------------------------
   --  Will work only for countries with existing IP ranges.
   --  "Unknown" otherwise.
   function Country_name_from_code (Code : in Country_code) return String;

   ------------------------------------------------------------------------
   -- Flag_code_from_country_code                                        --
   ------------------------------------------------------------------------
   --  returns "unknown" instead of "??", else the code
   function Flag_code_from_country_code (Code : in Country_code)
      return String;
   pragma Inline (Flag_code_from_country_code);

private

   type Ip_entry is record
      Upper_bound : Long_Long_Integer; -- IP
      Code        : String (1 .. 2);   -- Country code
   end record;

   package IP_maps is new Ada.Containers.Ordered_Maps (
      Long_Long_Integer, Ip_entry, "<", "=");

   IP_table : IP_maps.Map;

   package Code_maps is new Ada.Containers.Ordered_Maps (
      Country_code, Ustring, "<", Agpl.Types.Ustrings.ASU."=");

   Code_table : Code_maps.Map;

   --  Parses a line of CSV and adds it to the tables
   procedure Parse_line (Line : in String);

end Agpl.Geoip;