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