]>
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 | ||
63 | // Dump the tree | |
64 | err = loc_network_tree_dump(tree); | |
65 | if (err) { | |
66 | fprintf(stderr, "Error dumping tree: %d\n", err); | |
67 | exit(EXIT_FAILURE); | |
68 | } | |
69 | ||
940f9c2b MT |
70 | size_t nodes = loc_network_tree_count_nodes(tree); |
71 | printf("The tree has %zu nodes\n", nodes); | |
72 | ||
f3e02bc5 MT |
73 | // Create a database |
74 | struct loc_writer* writer; | |
75 | err = loc_writer_new(ctx, &writer); | |
76 | if (err < 0) | |
77 | exit(EXIT_FAILURE); | |
78 | ||
79 | struct loc_network* network2; | |
80 | err = loc_writer_add_network(writer, &network2, "2001:db8:1::/48"); | |
81 | if (err) { | |
82 | fprintf(stderr, "Could not add network\n"); | |
83 | exit(EXIT_FAILURE); | |
84 | } | |
85 | ||
86 | // Set country code | |
87 | loc_network_set_country_code(network2, "XX"); | |
88 | ||
89 | FILE* f = fopen("test.db", "w"); | |
90 | if (!f) { | |
91 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
92 | exit(EXIT_FAILURE); | |
93 | } | |
94 | ||
95 | err = loc_writer_write(writer, f); | |
96 | if (err) { | |
97 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
98 | exit(EXIT_FAILURE); | |
99 | } | |
100 | fclose(f); | |
101 | ||
102 | loc_writer_unref(writer); | |
103 | ||
3b5f4af2 MT |
104 | loc_network_unref(network1); |
105 | loc_network_tree_unref(tree); | |
106 | loc_unref(ctx); | |
107 | ||
108 | return EXIT_SUCCESS; | |
109 | } |