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 RETVAL = loc_database_get_vendor(db);
74 struct loc_database* db;
77 // Get database description
78 RETVAL = loc_database_get_description(db);
84 struct loc_database* db;
87 // Get database license
88 RETVAL = loc_database_get_license(db);
96 lookup_country_code(db, address)
97 struct loc_database* db;
101 RETVAL = &PL_sv_undef;
104 struct loc_network *network;
105 int err = loc_database_lookup_from_string(db, address, &network);
107 // Extract the country code
108 const char* country_code = loc_network_get_country_code(network);
109 RETVAL = newSVpv(country_code, strlen(country_code));
111 loc_network_unref(network);
117 lookup_asn(db, address)
118 struct loc_database* db;
122 RETVAL = &PL_sv_undef;
125 struct loc_network *network;
126 int err = loc_database_lookup_from_string(db, address, &network);
129 unsigned int as_number = loc_network_get_asn(network);
131 RETVAL = newSViv(as_number);
134 loc_network_unref(network);
141 struct loc_database* db;
145 loc_database_unref(db);