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.
23 #include <loc/libloc.h>
24 #include <loc/database.h>
25 #include <loc/network.h>
26 #include <loc/writer.h>
28 int main(int argc
, char** argv
) {
36 struct loc_network_tree
* tree
;
37 err
= loc_network_tree_new(ctx
, &tree
);
39 fprintf(stderr
, "Could not create the network tree\n");
44 struct loc_network
* network1
;
45 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::/32");
47 fprintf(stderr
, "Could not create the network\n");
51 err
= loc_network_set_country_code(network1
, "XX");
53 fprintf(stderr
, "Could not set country code\n");
57 // Adding network to the tree
58 err
= loc_network_tree_add_network(tree
, network1
);
60 fprintf(stderr
, "Could not add network to the tree\n");
64 struct loc_network
* network2
;
65 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
67 fprintf(stderr
, "Could not create the network\n");
71 err
= loc_network_set_country_code(network2
, "XY");
73 fprintf(stderr
, "Could not set country code\n");
77 // Adding network to the tree
78 err
= loc_network_tree_add_network(tree
, network2
);
80 fprintf(stderr
, "Could not add network to the tree\n");
85 err
= loc_network_tree_dump(tree
);
87 fprintf(stderr
, "Error dumping tree: %d\n", err
);
91 size_t nodes
= loc_network_tree_count_nodes(tree
);
92 printf("The tree has %zu nodes\n", nodes
);
95 struct loc_writer
* writer
;
96 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
100 struct loc_network
* network3
;
101 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
103 fprintf(stderr
, "Could not add network\n");
108 loc_network_set_country_code(network3
, "XX");
110 struct loc_network
* network4
;
111 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
113 fprintf(stderr
, "Could not add network\n");
118 loc_network_set_country_code(network4
, "XY");
121 loc_network_set_asn(network4
, 1024);
125 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
129 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
131 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
134 loc_writer_unref(writer
);
136 loc_network_unref(network1
);
137 loc_network_unref(network2
);
138 loc_network_unref(network3
);
139 loc_network_unref(network4
);
140 loc_network_tree_unref(tree
);
142 // And open it again from disk
143 struct loc_database
* db
;
144 err
= loc_database_new(ctx
, &db
, f
);
146 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
150 // Lookup an address in the subnet
151 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
153 fprintf(stderr
, "Could not look up 2001:db8::\n");
156 loc_network_unref(network1
);
158 // Lookup an address outside the subnet
159 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
161 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
164 loc_network_unref(network1
);