]>
Commit | Line | Data |
---|---|---|
49da4308 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2022 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 <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <syslog.h> | |
20 | ||
21 | #include <libloc/libloc.h> | |
22 | #include <libloc/address.h> | |
23 | #include <libloc/private.h> | |
24 | ||
25 | static int perform_tests(struct loc_ctx* ctx, const int family) { | |
26 | struct in6_addr address = IN6ADDR_ANY_INIT; | |
27 | const char* e = NULL; | |
28 | const char* s = NULL; | |
29 | ||
30 | // Reset IP address | |
31 | loc_address_reset(&address, family); | |
32 | ||
33 | if (!loc_address_all_zeroes(&address)) { | |
34 | fprintf(stderr, "IP address isn't all zeroes\n"); | |
35 | return 1; | |
36 | } | |
37 | ||
38 | if (loc_address_all_ones(&address)) { | |
39 | fprintf(stderr, "IP address isn't all ones\n"); | |
40 | return 1; | |
41 | } | |
42 | ||
43 | switch (family) { | |
44 | case AF_INET6: | |
45 | e = "::"; | |
46 | break; | |
47 | ||
48 | case AF_INET: | |
49 | e = "0.0.0.0"; | |
50 | break; | |
51 | } | |
52 | ||
53 | // Convert this to a string a few times | |
54 | for (unsigned int i = 0; i < 100; i++) { | |
55 | s = loc_address_str(&address); | |
56 | ||
57 | printf("Iteration %d: %s\n", i, s); | |
58 | ||
59 | if (strcmp(s, e) != 0) { | |
60 | fprintf(stderr, "IP address was formatted in an invalid format: %s\n", s); | |
61 | return 1; | |
62 | } | |
63 | } | |
64 | ||
65 | // Increment the IP address | |
66 | loc_address_increment(&address); | |
67 | ||
68 | switch (family) { | |
69 | case AF_INET6: | |
70 | e = "::1"; | |
71 | break; | |
72 | ||
73 | case AF_INET: | |
74 | e = "0.0.0.1"; | |
75 | break; | |
76 | } | |
77 | ||
78 | s = loc_address_str(&address); | |
79 | ||
80 | printf("Incremented IP address to %s\n", s); | |
81 | ||
82 | if (strcmp(s, e) != 0) { | |
83 | printf("IP address has been incremented incorrectly: %s\n", s); | |
84 | return 1; | |
85 | } | |
86 | ||
87 | if (loc_address_all_zeroes(&address)) { | |
88 | printf("IP address shouldn't be all zeroes any more\n"); | |
89 | return 1; | |
90 | } | |
91 | ||
92 | if (loc_address_all_ones(&address)) { | |
93 | printf("IP address shouldn't be all ones any more\n"); | |
94 | return 1; | |
95 | } | |
96 | ||
97 | // Decrement the IP address | |
98 | loc_address_decrement(&address); | |
99 | ||
100 | s = loc_address_str(&address); | |
101 | ||
102 | printf("Incremented IP address to %s\n", s); | |
103 | ||
104 | if (!loc_address_all_zeroes(&address)) { | |
105 | printf("IP address hasn't been decremented correctly: %s\n", | |
106 | loc_address_str(&address)); | |
107 | return 1; | |
108 | } | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | int main(int argc, char** argv) { | |
114 | struct loc_ctx* ctx = NULL; | |
115 | int r = EXIT_FAILURE; | |
116 | ||
117 | int err = loc_new(&ctx); | |
118 | if (err < 0) | |
119 | exit(r); | |
120 | ||
121 | // Enable debug logging | |
122 | loc_set_log_priority(ctx, LOG_DEBUG); | |
123 | ||
124 | // Perform all tests for IPv6 | |
125 | r = perform_tests(ctx, AF_INET6); | |
126 | if (r) | |
127 | goto ERROR; | |
128 | ||
129 | // Perform all tests for IPv4 | |
130 | r = perform_tests(ctx, AF_INET); | |
131 | if (r) | |
132 | goto ERROR; | |
133 | ||
134 | ERROR: | |
135 | loc_unref(ctx); | |
136 | ||
137 | return r; | |
138 | } |