]>
git.ipfire.org Git - location/libloc.git/blob - src/test-network.c
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");
64 err
= loc_network_tree_dump(tree
);
66 fprintf(stderr
, "Error dumping tree: %d\n", err
);
70 size_t nodes
= loc_network_tree_count_nodes(tree
);
71 printf("The tree has %zu nodes\n", nodes
);
74 struct loc_writer
* writer
;
75 err
= loc_writer_new(ctx
, &writer
);
79 struct loc_network
* network2
;
80 err
= loc_writer_add_network(writer
, &network2
, "2001:db8:1::/48");
82 fprintf(stderr
, "Could not add network\n");
87 loc_network_set_country_code(network2
, "XX");
89 FILE* f
= fopen("test.db", "w");
91 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
95 err
= loc_writer_write(writer
, f
);
97 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
102 loc_writer_unref(writer
);
104 loc_network_unref(network1
);
105 loc_network_tree_unref(tree
);