1 #define PERL_NO_GET_CONTEXT
10 #include <loc/libloc.h>
11 #include <loc/database.h>
12 #include <loc/network.h>
15 MODULE = Location PACKAGE = Location
22 struct loc_ctx* ctx = NULL;
24 // Initialise location context
25 int err = loc_new(&ctx);
27 croak("Could not initialize libloc context: %d\n", err);
29 // Open the database file for reading
30 FILE* f = fopen(file, "r");
34 croak("Could not open file for reading: %s: %s\n",
35 file, strerror(errno));
39 struct loc_database* db = NULL;
40 err = loc_database_new(ctx, &db, f);
42 // We can close the database file straight away
43 // because loc_database_new creates a copy of the file descriptor
49 croak("Could not read database: %s\n", file);
64 struct loc_database* db;
68 // Try to open the keyfile
69 FILE* f = fopen(keyfile, "r");
71 croak("Could not open keyfile %s: %s\n",
72 keyfile, strerror(errno));
75 // Verify the database
76 int status = loc_database_verify(db, f);
81 croak("Could not verify the database signature\n");
84 // Database was validated successfully
94 struct loc_database* db;
98 RETVAL = loc_database_get_vendor(db);
104 struct loc_database* db;
107 // Get database description
108 RETVAL = loc_database_get_description(db);
114 struct loc_database* db;
117 // Get database license
118 RETVAL = loc_database_get_license(db);
126 lookup_country_code(db, address)
127 struct loc_database* db;
131 RETVAL = &PL_sv_undef;
134 struct loc_network *network;
135 int err = loc_database_lookup_from_string(db, address, &network);
137 // Extract the country code
138 const char* country_code = loc_network_get_country_code(network);
139 RETVAL = newSVpv(country_code, strlen(country_code));
141 loc_network_unref(network);
147 lookup_asn(db, address)
148 struct loc_database* db;
152 RETVAL = &PL_sv_undef;
155 struct loc_network *network;
156 int err = loc_database_lookup_from_string(db, address, &network);
159 unsigned int as_number = loc_network_get_asn(network);
161 RETVAL = newSViv(as_number);
164 loc_network_unref(network);
173 get_continent_code(db, ccode)
174 struct loc_database* db;
178 RETVAL = &PL_sv_undef;
180 // Lookup country code
181 struct loc_country *country;
182 int err = loc_database_get_country(db, &country, ccode);
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));
188 loc_country_unref(country);
196 struct loc_database* db;
200 loc_database_unref(db);