]>
Commit | Line | Data |
---|---|---|
3b5f4af2 MT |
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 | ||
f3e02bc5 | 17 | #include <errno.h> |
3b5f4af2 MT |
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> | |
f3e02bc5 | 25 | #include <loc/writer.h> |
3b5f4af2 MT |
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 | ||
c051bfad MT |
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 | ||
3b5f4af2 MT |
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 | ||
940f9c2b MT |
90 | size_t nodes = loc_network_tree_count_nodes(tree); |
91 | printf("The tree has %zu nodes\n", nodes); | |
92 | ||
f3e02bc5 MT |
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 | ||
c051bfad MT |
99 | struct loc_network* network3; |
100 | err = loc_writer_add_network(writer, &network3, "2001:db8:1::/48"); | |
f3e02bc5 MT |
101 | if (err) { |
102 | fprintf(stderr, "Could not add network\n"); | |
103 | exit(EXIT_FAILURE); | |
104 | } | |
105 | ||
106 | // Set country code | |
c051bfad | 107 | loc_network_set_country_code(network3, "XX"); |
f3e02bc5 MT |
108 | |
109 | FILE* f = fopen("test.db", "w"); | |
110 | if (!f) { | |
111 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
112 | exit(EXIT_FAILURE); | |
113 | } | |
114 | ||
115 | err = loc_writer_write(writer, f); | |
116 | if (err) { | |
117 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
118 | exit(EXIT_FAILURE); | |
119 | } | |
120 | fclose(f); | |
121 | ||
122 | loc_writer_unref(writer); | |
123 | ||
3b5f4af2 | 124 | loc_network_unref(network1); |
c051bfad MT |
125 | loc_network_unref(network2); |
126 | loc_network_unref(network3); | |
3b5f4af2 MT |
127 | loc_network_tree_unref(tree); |
128 | loc_unref(ctx); | |
129 | ||
130 | return EXIT_SUCCESS; | |
131 | } |