]>
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> | |
2a30e4de | 24 | #include <loc/database.h> |
3b5f4af2 | 25 | #include <loc/network.h> |
f3e02bc5 | 26 | #include <loc/writer.h> |
3b5f4af2 MT |
27 | |
28 | int main(int argc, char** argv) { | |
29 | int err; | |
30 | ||
31 | struct loc_ctx* ctx; | |
32 | err = loc_new(&ctx); | |
33 | if (err < 0) | |
34 | exit(EXIT_FAILURE); | |
35 | ||
36 | struct loc_network_tree* tree; | |
37 | err = loc_network_tree_new(ctx, &tree); | |
38 | if (err) { | |
39 | fprintf(stderr, "Could not create the network tree\n"); | |
40 | exit(EXIT_FAILURE); | |
41 | } | |
42 | ||
43 | // Create a network | |
44 | struct loc_network* network1; | |
45 | err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32"); | |
46 | if (err) { | |
47 | fprintf(stderr, "Could not create the network\n"); | |
48 | exit(EXIT_FAILURE); | |
49 | } | |
50 | ||
51 | err = loc_network_set_country_code(network1, "XX"); | |
52 | if (err) { | |
53 | fprintf(stderr, "Could not set country code\n"); | |
54 | exit(EXIT_FAILURE); | |
55 | } | |
56 | ||
57 | // Adding network to the tree | |
58 | err = loc_network_tree_add_network(tree, network1); | |
59 | if (err) { | |
60 | fprintf(stderr, "Could not add network to the tree\n"); | |
61 | exit(EXIT_FAILURE); | |
62 | } | |
63 | ||
c051bfad MT |
64 | struct loc_network* network2; |
65 | err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48"); | |
66 | if (err) { | |
67 | fprintf(stderr, "Could not create the network\n"); | |
68 | exit(EXIT_FAILURE); | |
69 | } | |
70 | ||
71 | err = loc_network_set_country_code(network2, "XY"); | |
72 | if (err) { | |
73 | fprintf(stderr, "Could not set country code\n"); | |
74 | exit(EXIT_FAILURE); | |
75 | } | |
76 | ||
77 | // Adding network to the tree | |
78 | err = loc_network_tree_add_network(tree, network2); | |
79 | if (err) { | |
80 | fprintf(stderr, "Could not add network to the tree\n"); | |
81 | exit(EXIT_FAILURE); | |
82 | } | |
83 | ||
3b5f4af2 MT |
84 | // Dump the tree |
85 | err = loc_network_tree_dump(tree); | |
86 | if (err) { | |
87 | fprintf(stderr, "Error dumping tree: %d\n", err); | |
88 | exit(EXIT_FAILURE); | |
89 | } | |
90 | ||
940f9c2b MT |
91 | size_t nodes = loc_network_tree_count_nodes(tree); |
92 | printf("The tree has %zu nodes\n", nodes); | |
93 | ||
f3e02bc5 MT |
94 | // Create a database |
95 | struct loc_writer* writer; | |
5ce881d4 | 96 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
f3e02bc5 MT |
97 | if (err < 0) |
98 | exit(EXIT_FAILURE); | |
99 | ||
c051bfad | 100 | struct loc_network* network3; |
83a5d737 | 101 | err = loc_writer_add_network(writer, &network3, "2001:db8::/64"); |
f3e02bc5 MT |
102 | if (err) { |
103 | fprintf(stderr, "Could not add network\n"); | |
104 | exit(EXIT_FAILURE); | |
105 | } | |
106 | ||
107 | // Set country code | |
c051bfad | 108 | loc_network_set_country_code(network3, "XX"); |
f3e02bc5 | 109 | |
83a5d737 MT |
110 | struct loc_network* network4; |
111 | err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64"); | |
112 | if (err) { | |
113 | fprintf(stderr, "Could not add network\n"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
116 | ||
117 | // Set country code | |
118 | loc_network_set_country_code(network4, "XY"); | |
119 | ||
9a339aa0 MT |
120 | // Set ASN |
121 | loc_network_set_asn(network4, 1024); | |
122 | ||
6254dca6 | 123 | FILE* f = tmpfile(); |
f3e02bc5 MT |
124 | if (!f) { |
125 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
126 | exit(EXIT_FAILURE); | |
127 | } | |
128 | ||
22c7b98b | 129 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
f3e02bc5 MT |
130 | if (err) { |
131 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
132 | exit(EXIT_FAILURE); | |
133 | } | |
f3e02bc5 MT |
134 | loc_writer_unref(writer); |
135 | ||
3b5f4af2 | 136 | loc_network_unref(network1); |
c051bfad MT |
137 | loc_network_unref(network2); |
138 | loc_network_unref(network3); | |
83a5d737 | 139 | loc_network_unref(network4); |
3b5f4af2 | 140 | loc_network_tree_unref(tree); |
2a30e4de MT |
141 | |
142 | // And open it again from disk | |
2a30e4de MT |
143 | struct loc_database* db; |
144 | err = loc_database_new(ctx, &db, f); | |
145 | if (err) { | |
146 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
147 | exit(EXIT_FAILURE); | |
148 | } | |
149 | ||
e477e813 | 150 | // Lookup an address in the subnet |
2a30e4de MT |
151 | err = loc_database_lookup_from_string(db, "2001:db8::", &network1); |
152 | if (err) { | |
e477e813 | 153 | fprintf(stderr, "Could not look up 2001:db8::\n"); |
2a30e4de MT |
154 | exit(EXIT_FAILURE); |
155 | } | |
156 | loc_network_unref(network1); | |
157 | ||
e477e813 | 158 | // Lookup an address outside the subnet |
2a30e4de | 159 | err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1); |
e477e813 MT |
160 | if (err == 0) { |
161 | fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n"); | |
2a30e4de MT |
162 | exit(EXIT_FAILURE); |
163 | } | |
164 | loc_network_unref(network1); | |
165 | ||
3b5f4af2 | 166 | loc_unref(ctx); |
6254dca6 | 167 | fclose(f); |
3b5f4af2 MT |
168 | |
169 | return EXIT_SUCCESS; | |
170 | } |