]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network.c
test: Write two test networks to the database
[people/ms/libloc.git] / src / test-network.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
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.
10
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.
15 */
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <loc/libloc.h>
24 #include <loc/network.h>
25 #include <loc/writer.h>
26
27 int main(int argc, char** argv) {
28 int err;
29
30 struct loc_ctx* ctx;
31 err = loc_new(&ctx);
32 if (err < 0)
33 exit(EXIT_FAILURE);
34
35 struct loc_network_tree* tree;
36 err = loc_network_tree_new(ctx, &tree);
37 if (err) {
38 fprintf(stderr, "Could not create the network tree\n");
39 exit(EXIT_FAILURE);
40 }
41
42 // Create a network
43 struct loc_network* network1;
44 err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32");
45 if (err) {
46 fprintf(stderr, "Could not create the network\n");
47 exit(EXIT_FAILURE);
48 }
49
50 err = loc_network_set_country_code(network1, "XX");
51 if (err) {
52 fprintf(stderr, "Could not set country code\n");
53 exit(EXIT_FAILURE);
54 }
55
56 // Adding network to the tree
57 err = loc_network_tree_add_network(tree, network1);
58 if (err) {
59 fprintf(stderr, "Could not add network to the tree\n");
60 exit(EXIT_FAILURE);
61 }
62
63 struct loc_network* network2;
64 err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
65 if (err) {
66 fprintf(stderr, "Could not create the network\n");
67 exit(EXIT_FAILURE);
68 }
69
70 err = loc_network_set_country_code(network2, "XY");
71 if (err) {
72 fprintf(stderr, "Could not set country code\n");
73 exit(EXIT_FAILURE);
74 }
75
76 // Adding network to the tree
77 err = loc_network_tree_add_network(tree, network2);
78 if (err) {
79 fprintf(stderr, "Could not add network to the tree\n");
80 exit(EXIT_FAILURE);
81 }
82
83 // Dump the tree
84 err = loc_network_tree_dump(tree);
85 if (err) {
86 fprintf(stderr, "Error dumping tree: %d\n", err);
87 exit(EXIT_FAILURE);
88 }
89
90 size_t nodes = loc_network_tree_count_nodes(tree);
91 printf("The tree has %zu nodes\n", nodes);
92
93 // Create a database
94 struct loc_writer* writer;
95 err = loc_writer_new(ctx, &writer);
96 if (err < 0)
97 exit(EXIT_FAILURE);
98
99 struct loc_network* network3;
100 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
101 if (err) {
102 fprintf(stderr, "Could not add network\n");
103 exit(EXIT_FAILURE);
104 }
105
106 // Set country code
107 loc_network_set_country_code(network3, "XX");
108
109 struct loc_network* network4;
110 err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64");
111 if (err) {
112 fprintf(stderr, "Could not add network\n");
113 exit(EXIT_FAILURE);
114 }
115
116 // Set country code
117 loc_network_set_country_code(network4, "XY");
118
119 FILE* f = fopen("test.db", "w");
120 if (!f) {
121 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
122 exit(EXIT_FAILURE);
123 }
124
125 err = loc_writer_write(writer, f);
126 if (err) {
127 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
128 exit(EXIT_FAILURE);
129 }
130 fclose(f);
131
132 loc_writer_unref(writer);
133
134 loc_network_unref(network1);
135 loc_network_unref(network2);
136 loc_network_unref(network3);
137 loc_network_unref(network4);
138 loc_network_tree_unref(tree);
139 loc_unref(ctx);
140
141 return EXIT_SUCCESS;
142 }