diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-10-07 18:08:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-10-07 18:08:05 +0000 |
commit | 91dfeebc1a2e840f88904aab7f43ce214c3eb03a (patch) | |
tree | 36ac52a744e89799ea51f1d572b7cf7302f6ba8d /src/topology/friends.c | |
parent | 2866a3a85ea457f29f4746ccae57af369746c5fa (diff) | |
download | gnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.tar.gz gnunet-91dfeebc1a2e840f88904aab7f43ce214c3eb03a.zip |
-moving friends file parsing logic into its own library
Diffstat (limited to 'src/topology/friends.c')
-rw-r--r-- | src/topology/friends.c | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/src/topology/friends.c b/src/topology/friends.c new file mode 100644 index 000000000..c0915fa15 --- /dev/null +++ b/src/topology/friends.c | |||
@@ -0,0 +1,165 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2013 Christian Grothoff | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file topology/friends.c | ||
23 | * @brief library to read and write the FRIENDS file | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_friends_lib.h" | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Parse the FRIENDS file. | ||
32 | * | ||
33 | * @param cfg our configuration | ||
34 | * @param cb function to call on each friend found | ||
35 | * @param cb_cls closure for @a cb | ||
36 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors | ||
37 | */ | ||
38 | int | ||
39 | GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
40 | GNUNET_FRIENDS_Callback cb, | ||
41 | void *cb_cls) | ||
42 | { | ||
43 | char *fn; | ||
44 | char *data; | ||
45 | size_t pos; | ||
46 | size_t start; | ||
47 | struct GNUNET_PeerIdentity pid; | ||
48 | uint64_t fsize; | ||
49 | |||
50 | if (GNUNET_OK != | ||
51 | GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn)) | ||
52 | { | ||
53 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
54 | "topology", "FRIENDS"); | ||
55 | return GNUNET_SYSERR; | ||
56 | } | ||
57 | if ( (GNUNET_OK != GNUNET_DISK_file_test (fn)) && | ||
58 | (GNUNET_OK != GNUNET_DISK_fn_write (fn, NULL, 0, | ||
59 | GNUNET_DISK_PERM_USER_READ | | ||
60 | GNUNET_DISK_PERM_USER_WRITE)) ) | ||
61 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); | ||
62 | if ( (GNUNET_OK != | ||
63 | GNUNET_DISK_file_size (fn, | ||
64 | &fsize, | ||
65 | GNUNET_NO, GNUNET_YES)) || | ||
66 | (0 == fsize) ) | ||
67 | { | ||
68 | GNUNET_free (fn); | ||
69 | return GNUNET_OK; | ||
70 | } | ||
71 | data = GNUNET_malloc_large (fsize); | ||
72 | if (NULL == data) | ||
73 | { | ||
74 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | ||
75 | GNUNET_free (fn); | ||
76 | return GNUNET_SYSERR; | ||
77 | } | ||
78 | if (fsize != GNUNET_DISK_fn_read (fn, data, fsize)) | ||
79 | { | ||
80 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "read", "fn"); | ||
81 | GNUNET_free (fn); | ||
82 | GNUNET_free (data); | ||
83 | return GNUNET_SYSERR; | ||
84 | } | ||
85 | start = 0; | ||
86 | pos = 0; | ||
87 | while (pos < fsize) | ||
88 | { | ||
89 | while ((pos < fsize) && isspace ((unsigned char) data[pos])) | ||
90 | pos++; | ||
91 | if (GNUNET_OK != | ||
92 | GNUNET_CRYPTO_ecc_public_sign_key_from_string (&data[start], | ||
93 | pos - start, | ||
94 | &pid.public_key)) | ||
95 | { | ||
96 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
97 | _("Syntax error in topology specification at offset %llu, skipping bytes `%.*s'.\n"), | ||
98 | (unsigned long long) pos, | ||
99 | (int) (pos - start), | ||
100 | &data[start]); | ||
101 | pos++; | ||
102 | start = pos; | ||
103 | continue; | ||
104 | } | ||
105 | pos++; | ||
106 | start = pos; | ||
107 | cb (cb_cls, &pid); | ||
108 | } | ||
109 | GNUNET_free (data); | ||
110 | GNUNET_free (fn); | ||
111 | return GNUNET_OK; | ||
112 | } | ||
113 | |||
114 | |||
115 | /** | ||
116 | * Handle for writing a friends file. | ||
117 | */ | ||
118 | struct GNUNET_FRIENDS_Writer | ||
119 | { | ||
120 | }; | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Start writing a fresh FRIENDS file. Will make a backup of the | ||
125 | * old one. | ||
126 | * | ||
127 | * @param cfg configuration to use. | ||
128 | * @return NULL on error | ||
129 | */ | ||
130 | struct GNUNET_FRIENDS_Writer * | ||
131 | GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
132 | { | ||
133 | return NULL; | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Finish writing out the friends file. | ||
139 | * | ||
140 | * @param w write handle | ||
141 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
142 | */ | ||
143 | int | ||
144 | GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w) | ||
145 | { | ||
146 | return GNUNET_SYSERR; | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Add a friend to the friends file. | ||
152 | * | ||
153 | * @param w write handle | ||
154 | * @param friend friend to add | ||
155 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
156 | */ | ||
157 | int | ||
158 | GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w, | ||
159 | const struct GNUNET_PeerIdentity *friend) | ||
160 | { | ||
161 | return GNUNET_SYSERR; | ||
162 | } | ||
163 | |||
164 | |||
165 | /* end of friends.c */ | ||