]>
Commit | Line | Data |
---|---|---|
a17c353b 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 | ||
17 | #include <errno.h> | |
18 | #include <stdio.h> | |
19 | #include <stddef.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <syslog.h> | |
23 | ||
24 | #include <loc/libloc.h> | |
25 | #include <loc/network.h> | |
26 | #include <loc/network-list.h> | |
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 | // Enable debug logging | |
37 | loc_set_log_priority(ctx, LOG_DEBUG); | |
38 | ||
39 | // Create a network | |
40 | struct loc_network* network1; | |
41 | err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32"); | |
42 | if (err) { | |
43 | fprintf(stderr, "Could not create the network1\n"); | |
44 | exit(EXIT_FAILURE); | |
45 | } | |
46 | ||
47 | struct loc_network* subnet1; | |
48 | err = loc_network_new_from_string(ctx, &subnet1, "2001:db8:a::/48"); | |
49 | if (err) { | |
50 | fprintf(stderr, "Could not create the subnet1\n"); | |
51 | exit(EXIT_FAILURE); | |
52 | } | |
53 | ||
54 | struct loc_network* subnet2; | |
55 | err = loc_network_new_from_string(ctx, &subnet2, "2001:db8:b::/48"); | |
56 | if (err) { | |
57 | fprintf(stderr, "Could not create the subnet2\n"); | |
58 | exit(EXIT_FAILURE); | |
59 | } | |
60 | ||
7c6983ad MT |
61 | struct loc_network* subnet3; |
62 | err = loc_network_new_from_string(ctx, &subnet3, "2001:db8:c::/48"); | |
63 | if (err) { | |
64 | fprintf(stderr, "Could not create the subnet3\n"); | |
65 | exit(EXIT_FAILURE); | |
66 | } | |
67 | ||
68 | struct loc_network* subnet4; | |
69 | err = loc_network_new_from_string(ctx, &subnet4, "2001:db8:d::/48"); | |
70 | if (err) { | |
71 | fprintf(stderr, "Could not create the subnet4\n"); | |
72 | exit(EXIT_FAILURE); | |
73 | } | |
74 | ||
d3a5f691 MT |
75 | struct loc_network* subnet5; |
76 | err = loc_network_new_from_string(ctx, &subnet5, "2001:db8:e::/48"); | |
77 | if (err) { | |
78 | fprintf(stderr, "Could not create the subnet5\n"); | |
79 | exit(EXIT_FAILURE); | |
80 | } | |
81 | ||
82 | struct loc_network* subnet6; | |
83 | err = loc_network_new_from_string(ctx, &subnet6, "2001:db8:1::/48"); | |
84 | if (err) { | |
85 | fprintf(stderr, "Could not create the subnet6\n"); | |
86 | exit(EXIT_FAILURE); | |
87 | } | |
88 | ||
a17c353b MT |
89 | // Make a list with both subnets |
90 | struct loc_network_list* subnets; | |
91 | err = loc_network_list_new(ctx, &subnets); | |
92 | if (err) { | |
93 | fprintf(stderr, "Could not create subnets list\n"); | |
94 | exit(EXIT_FAILURE); | |
95 | } | |
96 | ||
97 | size_t size = loc_network_list_size(subnets); | |
98 | if (size > 0) { | |
99 | fprintf(stderr, "The list is not empty: %zu\n", size); | |
100 | exit(EXIT_FAILURE); | |
101 | } | |
102 | ||
103 | err = loc_network_list_push(subnets, subnet1); | |
104 | if (err) { | |
105 | fprintf(stderr, "Could not add subnet1 to subnets list\n"); | |
106 | exit(EXIT_FAILURE); | |
107 | } | |
108 | ||
109 | if (loc_network_list_empty(subnets)) { | |
110 | fprintf(stderr, "The subnets list reports that it is empty\n"); | |
111 | exit(EXIT_FAILURE); | |
112 | } | |
113 | ||
114 | err = loc_network_list_push(subnets, subnet2); | |
115 | if (err) { | |
116 | fprintf(stderr, "Could not add subnet2 to subnets list\n"); | |
117 | exit(EXIT_FAILURE); | |
118 | } | |
119 | ||
7c6983ad MT |
120 | // Add the fourth one next |
121 | err = loc_network_list_push(subnets, subnet4); | |
122 | if (err) { | |
123 | fprintf(stderr, "Could not add subnet4 to subnets list\n"); | |
124 | exit(EXIT_FAILURE); | |
125 | } | |
126 | ||
127 | // Add the third one | |
128 | err = loc_network_list_push(subnets, subnet3); | |
129 | if (err) { | |
130 | fprintf(stderr, "Could not add subnet3 to subnets list\n"); | |
131 | exit(EXIT_FAILURE); | |
132 | } | |
133 | ||
d3a5f691 MT |
134 | // Add more subnets |
135 | err = loc_network_list_push(subnets, subnet5); | |
136 | if (err) { | |
137 | fprintf(stderr, "Could not add subnet5 to subnets list\n"); | |
138 | exit(EXIT_FAILURE); | |
139 | } | |
140 | ||
141 | err = loc_network_list_push(subnets, subnet6); | |
142 | if (err) { | |
143 | fprintf(stderr, "Could not add subnet6 to subnets list\n"); | |
144 | exit(EXIT_FAILURE); | |
145 | } | |
146 | ||
7c6983ad MT |
147 | loc_network_list_dump(subnets); |
148 | ||
a17c353b | 149 | size = loc_network_list_size(subnets); |
d3a5f691 | 150 | if (size != 6) { |
a17c353b MT |
151 | fprintf(stderr, "Network list is reporting an incorrect size: %zu\n", size); |
152 | exit(EXIT_FAILURE); | |
153 | } | |
154 | ||
155 | // Exclude subnet1 from network1 | |
156 | struct loc_network_list* excluded = loc_network_exclude(network1, subnet1); | |
157 | if (!excluded) { | |
158 | fprintf(stderr, "Received an empty result from loc_network_exclude() for subnet1\n"); | |
159 | exit(EXIT_FAILURE); | |
160 | } | |
161 | ||
162 | loc_network_list_dump(excluded); | |
163 | ||
164 | // Exclude all subnets from network1 | |
165 | excluded = loc_network_exclude_list(network1, subnets); | |
166 | if (!excluded) { | |
167 | fprintf(stderr, "Received an empty result from loc_network_exclude() for subnets\n"); | |
168 | exit(EXIT_FAILURE); | |
169 | } | |
170 | ||
171 | loc_network_list_dump(excluded); | |
172 | ||
173 | if (excluded) | |
174 | loc_network_list_unref(excluded); | |
175 | ||
176 | loc_network_list_unref(subnets); | |
177 | loc_network_unref(network1); | |
178 | loc_network_unref(subnet1); | |
179 | loc_network_unref(subnet2); | |
180 | loc_unref(ctx); | |
181 | ||
182 | return EXIT_SUCCESS; | |
183 | } |