]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network.c
1007bd1df3f19fcdf7897fc0ca71d92a0ddc22ff
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/network.h>
25 #include <loc/writer.h>
27 int main(int argc
, char** argv
) {
35 struct loc_network_tree
* tree
;
36 err
= loc_network_tree_new(ctx
, &tree
);
38 fprintf(stderr
, "Could not create the network tree\n");
43 struct loc_network
* network1
;
44 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::/32");
46 fprintf(stderr
, "Could not create the network\n");
50 err
= loc_network_set_country_code(network1
, "XX");
52 fprintf(stderr
, "Could not set country code\n");
56 // Adding network to the tree
57 err
= loc_network_tree_add_network(tree
, network1
);
59 fprintf(stderr
, "Could not add network to the tree\n");
63 struct loc_network
* network2
;
64 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
66 fprintf(stderr
, "Could not create the network\n");
70 err
= loc_network_set_country_code(network2
, "XY");
72 fprintf(stderr
, "Could not set country code\n");
76 // Adding network to the tree
77 err
= loc_network_tree_add_network(tree
, network2
);
79 fprintf(stderr
, "Could not add network to the tree\n");
84 err
= loc_network_tree_dump(tree
);
86 fprintf(stderr
, "Error dumping tree: %d\n", err
);
90 size_t nodes
= loc_network_tree_count_nodes(tree
);
91 printf("The tree has %zu nodes\n", nodes
);
94 struct loc_writer
* writer
;
95 err
= loc_writer_new(ctx
, &writer
);
99 struct loc_network
* network3
;
100 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
102 fprintf(stderr
, "Could not add network\n");
107 loc_network_set_country_code(network3
, "XX");
109 struct loc_network
* network4
;
110 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
112 fprintf(stderr
, "Could not add network\n");
117 loc_network_set_country_code(network4
, "XY");
120 loc_network_set_asn(network4
, 1024);
122 FILE* f
= fopen("test.db", "w");
124 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
128 err
= loc_writer_write(writer
, f
);
130 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
135 loc_writer_unref(writer
);
137 loc_network_unref(network1
);
138 loc_network_unref(network2
);
139 loc_network_unref(network3
);
140 loc_network_unref(network4
);
141 loc_network_tree_unref(tree
);