]> git.ipfire.org Git - people/ms/libloc.git/blame - src/perl/Location.xs
perl: Make verify() a function on the database
[people/ms/libloc.git] / src / perl / Location.xs
CommitLineData
1ea740e9
SS
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
1ea740e9
SS
9
10#include <loc/libloc.h>
11#include <loc/database.h>
12#include <loc/network.h>
13
14
15MODULE = Location PACKAGE = Location
16
17struct loc_database *
3bbf013a 18init(file)
e5a1a0bd 19 char* file;
1ea740e9
SS
20
21 CODE:
da1039da 22 struct loc_ctx* ctx = NULL;
1ea740e9 23
74ad091d 24 // Initialise location context
da1039da 25 int err = loc_new(&ctx);
1ea740e9 26 if (err < 0)
5242283a 27 croak("Could not initialize libloc context: %d\n", err);
1ea740e9 28
74ad091d 29 // Open the database file for reading
1ea740e9
SS
30 FILE* f = fopen(file, "r");
31 if (!f) {
c820094d
MT
32 loc_unref(ctx);
33
5242283a
MT
34 croak("Could not open file for reading: %s: %s\n",
35 file, strerror(errno));
1ea740e9
SS
36 }
37
74ad091d 38 // Parse the database
c820094d 39 struct loc_database* db = NULL;
1ea740e9 40 err = loc_database_new(ctx, &db, f);
46d6c767
MT
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
1ea740e9 46 if (err) {
c820094d
MT
47 loc_unref(ctx);
48
5242283a 49 croak("Could not read database: %s\n", file);
1ea740e9
SS
50 }
51
3bbf013a
MT
52 // Cleanup
53 loc_unref(ctx);
54
55 RETVAL = db;
56 OUTPUT:
57 RETVAL
58
59#
60# Database functions
61#
62bool
63verify(db, keyfile)
64 struct loc_database* db;
65 char* keyfile;
66
67 CODE:
2061ff77 68 // Try to open the keyfile
3bbf013a 69 FILE* f = fopen(keyfile, "r");
2061ff77 70 if (!f) {
2061ff77
SS
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) {
3bbf013a 78 RETVAL = false;
2061ff77
SS
79 fclose(f);
80
81 croak("Could not verify the database signature\n");
82 }
83
3bbf013a
MT
84 // Database was validated successfully
85 RETVAL = true;
86
2061ff77
SS
87 // Close the keyfile
88 fclose(f);
1ea740e9
SS
89 OUTPUT:
90 RETVAL
91
777959b2
MT
92const char*
93get_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
973c5f03
SS
102const char*
103get_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
733bd087
SS
112const char*
113get_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
46fbc975
SS
122#
123# Lookup functions
124#
6aa97cac 125SV*
70f45781 126lookup_country_code(db, address)
e5a1a0bd
MT
127 struct loc_database* db;
128 char* address;
1ea740e9
SS
129
130 CODE:
6aa97cac
SS
131 RETVAL = &PL_sv_undef;
132
74ad091d 133 // Lookup network
1ea740e9 134 struct loc_network *network;
d3daf87c 135 int err = loc_database_lookup_from_string(db, address, &network);
6aa97cac
SS
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));
1ea740e9 140
6aa97cac 141 loc_network_unref(network);
1ea740e9 142 }
1ea740e9
SS
143 OUTPUT:
144 RETVAL
145
8d4f5f67
SS
146SV*
147lookup_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
1ea740e9
SS
169void
170DESTROY(db)
34eed014 171 struct loc_database* db;
da1039da 172
1ea740e9 173 CODE:
74ad091d 174 // Close database
1ea740e9 175 loc_database_unref(db);