]> git.ipfire.org Git - location/libloc.git/blob - src/perl/Location.xs
Update translations
[location/libloc.git] / src / perl / Location.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #include <stdio.h>
7 #include <string.h>
8
9
10 #include <loc/libloc.h>
11 #include <loc/database.h>
12 #include <loc/network.h>
13
14
15 MODULE = Location PACKAGE = Location
16
17 struct loc_database *
18 init(file)
19 char* file;
20
21 CODE:
22 struct loc_ctx* ctx = NULL;
23
24 // Initialise location context
25 int err = loc_new(&ctx);
26 if (err < 0)
27 croak("Could not initialize libloc context: %d\n", err);
28
29 // Open the database file for reading
30 FILE* f = fopen(file, "r");
31 if (!f) {
32 loc_unref(ctx);
33
34 croak("Could not open file for reading: %s: %s\n",
35 file, strerror(errno));
36 }
37
38 // Parse the database
39 struct loc_database* db = NULL;
40 err = loc_database_new(ctx, &db, f);
41
42 // We can close the database file straight away
43 // because loc_database_new creates a copy of the file descriptor
44 fclose(f);
45
46 if (err) {
47 loc_unref(ctx);
48
49 croak("Could not read database: %s\n", file);
50 }
51
52 // Cleanup
53 loc_unref(ctx);
54
55 RETVAL = db;
56 OUTPUT:
57 RETVAL
58
59 #
60 # Database functions
61 #
62 bool
63 verify(db, keyfile)
64 struct loc_database* db;
65 char* keyfile;
66
67 CODE:
68 // Try to open the keyfile
69 FILE* f = fopen(keyfile, "r");
70 if (!f) {
71 croak("Could not open keyfile %s: %s\n",
72 keyfile, strerror(errno));
73 }
74
75 // Verify the database
76 int status = loc_database_verify(db, f);
77 if (status) {
78 RETVAL = false;
79 fclose(f);
80
81 croak("Could not verify the database signature\n");
82 }
83
84 // Database was validated successfully
85 RETVAL = true;
86
87 // Close the keyfile
88 fclose(f);
89 OUTPUT:
90 RETVAL
91
92 const char*
93 get_vendor(db)
94 struct loc_database* db;
95
96 CODE:
97 // Get vendor
98 RETVAL = loc_database_get_vendor(db);
99 OUTPUT:
100 RETVAL
101
102 const char*
103 get_description(db)
104 struct loc_database* db;
105
106 CODE:
107 // Get database description
108 RETVAL = loc_database_get_description(db);
109 OUTPUT:
110 RETVAL
111
112 const char*
113 get_license(db)
114 struct loc_database* db;
115
116 CODE:
117 // Get database license
118 RETVAL = loc_database_get_license(db);
119 OUTPUT:
120 RETVAL
121
122 #
123 # Lookup functions
124 #
125 SV*
126 lookup_country_code(db, address)
127 struct loc_database* db;
128 char* address;
129
130 CODE:
131 RETVAL = &PL_sv_undef;
132
133 // Lookup network
134 struct loc_network *network;
135 int err = loc_database_lookup_from_string(db, address, &network);
136 if (!err) {
137 // Extract the country code
138 const char* country_code = loc_network_get_country_code(network);
139 RETVAL = newSVpv(country_code, strlen(country_code));
140
141 loc_network_unref(network);
142 }
143 OUTPUT:
144 RETVAL
145
146 SV*
147 lookup_asn(db, address)
148 struct loc_database* db;
149 char* address;
150
151 CODE:
152 RETVAL = &PL_sv_undef;
153
154 // Lookup network
155 struct loc_network *network;
156 int err = loc_database_lookup_from_string(db, address, &network);
157 if (!err) {
158 // Extract the ASN
159 unsigned int as_number = loc_network_get_asn(network);
160 if (as_number > 0) {
161 RETVAL = newSViv(as_number);
162 }
163
164 loc_network_unref(network);
165 }
166 OUTPUT:
167 RETVAL
168
169 #
170 # Get functions
171 #
172 SV*
173 get_continent_code(db, ccode)
174 struct loc_database* db;
175 char* ccode;
176
177 CODE:
178 RETVAL = &PL_sv_undef;
179
180 // Lookup country code
181 struct loc_country *country;
182 int err = loc_database_get_country(db, &country, ccode);
183 if(!err) {
184 //Extract the continent code for the given country code.
185 const char* continent_code = loc_country_get_continent_code(country);
186 RETVAL = newSVpv(continent_code, strlen(continent_code));
187
188 loc_country_unref(country);
189 }
190
191 OUTPUT:
192 RETVAL
193
194 void
195 DESTROY(db)
196 struct loc_database* db;
197
198 CODE:
199 // Close database
200 loc_database_unref(db);