2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
24 #include <loc/libloc.h>
25 #include <loc/database.h>
26 #include <loc/network.h>
27 #include <loc/writer.h>
29 int main(int argc
, char** argv
) {
37 // Enable debug logging
38 loc_set_log_priority(ctx
, LOG_DEBUG
);
40 struct loc_network_tree
* tree
;
41 err
= loc_network_tree_new(ctx
, &tree
);
43 fprintf(stderr
, "Could not create the network tree\n");
48 struct loc_network
* network1
;
49 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::/32");
51 fprintf(stderr
, "Could not create the network\n");
55 err
= loc_network_set_country_code(network1
, "XX");
57 fprintf(stderr
, "Could not set country code\n");
61 // Adding network to the tree
62 err
= loc_network_tree_add_network(tree
, network1
);
64 fprintf(stderr
, "Could not add network to the tree\n");
68 struct loc_network
* network2
;
69 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
71 fprintf(stderr
, "Could not create the network\n");
75 err
= loc_network_set_country_code(network2
, "XY");
77 fprintf(stderr
, "Could not set country code\n");
81 // Adding network to the tree
82 err
= loc_network_tree_add_network(tree
, network2
);
84 fprintf(stderr
, "Could not add network to the tree\n");
89 err
= loc_network_tree_dump(tree
);
91 fprintf(stderr
, "Error dumping tree: %d\n", err
);
95 size_t nodes
= loc_network_tree_count_nodes(tree
);
96 printf("The tree has %zu nodes\n", nodes
);
99 struct loc_writer
* writer
;
100 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
104 struct loc_network
* network3
;
105 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
107 fprintf(stderr
, "Could not add network\n");
112 loc_network_set_country_code(network3
, "XX");
114 struct loc_network
* network4
;
115 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
117 fprintf(stderr
, "Could not add network\n");
122 loc_network_set_country_code(network4
, "XY");
125 loc_network_set_asn(network4
, 1024);
129 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
133 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
135 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
138 loc_writer_unref(writer
);
140 loc_network_unref(network1
);
141 loc_network_unref(network2
);
142 loc_network_unref(network3
);
143 loc_network_unref(network4
);
144 loc_network_tree_unref(tree
);
146 // And open it again from disk
147 struct loc_database
* db
;
148 err
= loc_database_new(ctx
, &db
, f
);
150 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
154 // Lookup an address in the subnet
155 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
157 fprintf(stderr
, "Could not look up 2001:db8::\n");
160 loc_network_unref(network1
);
162 // Lookup an address outside the subnet
163 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
165 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
168 loc_network_unref(network1
);