]>
Commit | Line | Data |
---|---|---|
ec684c1a MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2019 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 | ||
23 | #include <loc/libloc.h> | |
57146963 | 24 | #include <loc/country.h> |
ec684c1a MT |
25 | #include <loc/database.h> |
26 | #include <loc/network.h> | |
27 | #include <loc/writer.h> | |
28 | ||
29 | int main(int argc, char** argv) { | |
30 | struct loc_country* country; | |
31 | int err; | |
32 | ||
57146963 MT |
33 | // Check some valid country codes |
34 | if (!loc_country_code_is_valid("XX")) { | |
35 | fprintf(stderr, "Valid country code detected as invalid: %s\n", "XX"); | |
36 | exit(EXIT_FAILURE); | |
37 | } | |
38 | ||
39 | // Check some invalid country codes | |
40 | if (loc_country_code_is_valid("X1")) { | |
41 | fprintf(stderr, "Invalid country code detected as valid: %s\n", "X1"); | |
42 | exit(EXIT_FAILURE); | |
43 | } | |
44 | ||
ec684c1a MT |
45 | struct loc_ctx* ctx; |
46 | err = loc_new(&ctx); | |
47 | if (err < 0) | |
48 | exit(EXIT_FAILURE); | |
49 | ||
50 | // Create a database | |
51 | struct loc_writer* writer; | |
726f9984 | 52 | err = loc_writer_new(ctx, &writer, NULL); |
ec684c1a MT |
53 | if (err < 0) |
54 | exit(EXIT_FAILURE); | |
55 | ||
56 | // Create a country | |
57146963 | 57 | err = loc_writer_add_country(writer, &country, "XX"); |
ec684c1a MT |
58 | if (err) { |
59 | fprintf(stderr, "Could not create country\n"); | |
60 | exit(EXIT_FAILURE); | |
61 | } | |
62 | ||
63 | // Set name & continent | |
64 | loc_country_set_name(country, "Testistan"); | |
57146963 | 65 | loc_country_set_continent_code(country, "YY"); |
ec684c1a MT |
66 | |
67 | // Free country | |
68 | loc_country_unref(country); | |
69 | ||
8bbddd9a MT |
70 | // Add another country |
71 | err = loc_writer_add_country(writer, &country, "YY"); | |
72 | if (err) { | |
73 | fprintf(stderr, "Could not create country: YY\n"); | |
74 | exit(EXIT_FAILURE); | |
75 | } | |
76 | loc_country_unref(country); | |
77 | ||
726f9984 | 78 | FILE* f = fopen("test.db", "w+"); |
ec684c1a MT |
79 | if (!f) { |
80 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
81 | exit(EXIT_FAILURE); | |
82 | } | |
83 | ||
84 | err = loc_writer_write(writer, f); | |
85 | if (err) { | |
86 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
87 | exit(EXIT_FAILURE); | |
88 | } | |
89 | fclose(f); | |
90 | ||
91 | loc_writer_unref(writer); | |
92 | ||
93 | // And open it again from disk | |
94 | f = fopen("test.db", "r"); | |
95 | if (!f) { | |
96 | fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno)); | |
97 | exit(EXIT_FAILURE); | |
98 | } | |
99 | ||
100 | struct loc_database* db; | |
101 | err = loc_database_new(ctx, &db, f); | |
102 | if (err) { | |
103 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
104 | exit(EXIT_FAILURE); | |
105 | } | |
106 | ||
107 | // Lookup an address in the subnet | |
8bbddd9a | 108 | err = loc_database_get_country(db, &country, "YY"); |
ec684c1a | 109 | if (err) { |
8bbddd9a | 110 | fprintf(stderr, "Could not find country: YY\n"); |
ec684c1a MT |
111 | exit(EXIT_FAILURE); |
112 | } | |
113 | loc_country_unref(country); | |
114 | ||
115 | loc_database_unref(db); | |
116 | loc_unref(ctx); | |
117 | ||
118 | return EXIT_SUCCESS; | |
119 | } |