]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-network.c
Start a Python module
[people/ms/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>
22
23#include <loc/libloc.h>
24#include <loc/network.h>
f3e02bc5 25#include <loc/writer.h>
3b5f4af2
MT
26
27int main(int argc, char** argv) {
28 int err;
29
30 struct loc_ctx* ctx;
31 err = loc_new(&ctx);
32 if (err < 0)
33 exit(EXIT_FAILURE);
34
35 struct loc_network_tree* tree;
36 err = loc_network_tree_new(ctx, &tree);
37 if (err) {
38 fprintf(stderr, "Could not create the network tree\n");
39 exit(EXIT_FAILURE);
40 }
41
42 // Create a network
43 struct loc_network* network1;
44 err = loc_network_new_from_string(ctx, &network1, "2001:db8::/32");
45 if (err) {
46 fprintf(stderr, "Could not create the network\n");
47 exit(EXIT_FAILURE);
48 }
49
50 err = loc_network_set_country_code(network1, "XX");
51 if (err) {
52 fprintf(stderr, "Could not set country code\n");
53 exit(EXIT_FAILURE);
54 }
55
56 // Adding network to the tree
57 err = loc_network_tree_add_network(tree, network1);
58 if (err) {
59 fprintf(stderr, "Could not add network to the tree\n");
60 exit(EXIT_FAILURE);
61 }
62
63 // Dump the tree
64 err = loc_network_tree_dump(tree);
65 if (err) {
66 fprintf(stderr, "Error dumping tree: %d\n", err);
67 exit(EXIT_FAILURE);
68 }
69
f3e02bc5
MT
70 // Create a database
71 struct loc_writer* writer;
72 err = loc_writer_new(ctx, &writer);
73 if (err < 0)
74 exit(EXIT_FAILURE);
75
76 struct loc_network* network2;
77 err = loc_writer_add_network(writer, &network2, "2001:db8:1::/48");
78 if (err) {
79 fprintf(stderr, "Could not add network\n");
80 exit(EXIT_FAILURE);
81 }
82
83 // Set country code
84 loc_network_set_country_code(network2, "XX");
85
86 FILE* f = fopen("test.db", "w");
87 if (!f) {
88 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
89 exit(EXIT_FAILURE);
90 }
91
92 err = loc_writer_write(writer, f);
93 if (err) {
94 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
95 exit(EXIT_FAILURE);
96 }
97 fclose(f);
98
99 loc_writer_unref(writer);
100
3b5f4af2
MT
101 loc_network_unref(network1);
102 loc_network_tree_unref(tree);
103 loc_unref(ctx);
104
105 return EXIT_SUCCESS;
106}