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
23 struct loc_ctx* ctx = NULL;
25 // Initialise location context
26 int err = loc_new(&ctx);
28 croak("Could not initialize libloc context: %d\n", err);
30 // Open the database file for reading
31 FILE* f = fopen(file, "r");
35 croak("Could not open file for reading: %s: %s\n",
36 file, strerror(errno));
40 struct loc_database* db = NULL;
41 err = loc_database_new(ctx, &db, f);
43 // We can close the database file straight away
44 // because loc_database_new creates a copy of the file descriptor
50 croak("Could not read database: %s\n", file);
53 // Try to open the keyfile
54 f = fopen(keyfile, "r");
56 loc_database_unref(db);
59 croak("Could not open keyfile %s: %s\n",
60 keyfile, strerror(errno));
63 // Verify the database
64 int status = loc_database_verify(db, f);
66 loc_database_unref(db);
70 croak("Could not verify the database signature\n");
88 struct loc_database* db;
92 RETVAL = loc_database_get_vendor(db);
98 struct loc_database* db;
101 // Get database description
102 RETVAL = loc_database_get_description(db);
108 struct loc_database* db;
111 // Get database license
112 RETVAL = loc_database_get_license(db);
120 lookup_country_code(db, address)
121 struct loc_database* db;
125 RETVAL = &PL_sv_undef;
128 struct loc_network *network;
129 int err = loc_database_lookup_from_string(db, address, &network);
131 // Extract the country code
132 const char* country_code = loc_network_get_country_code(network);
133 RETVAL = newSVpv(country_code, strlen(country_code));
135 loc_network_unref(network);
141 lookup_asn(db, address)
142 struct loc_database* db;
146 RETVAL = &PL_sv_undef;
149 struct loc_network *network;
150 int err = loc_database_lookup_from_string(db, address, &network);
153 unsigned int as_number = loc_network_get_asn(network);
155 RETVAL = newSViv(as_number);
158 loc_network_unref(network);
165 struct loc_database* db;
169 loc_database_unref(db);