aboutsummaryrefslogtreecommitdiff
path: root/src/topology/friends.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/topology/friends.c')
-rw-r--r--src/topology/friends.c252
1 files changed, 127 insertions, 125 deletions
diff --git a/src/topology/friends.c b/src/topology/friends.c
index 81c7d45e9..a3b15d8f7 100644
--- a/src/topology/friends.c
+++ b/src/topology/friends.c
@@ -36,9 +36,9 @@
36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors 36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
37 */ 37 */
38int 38int
39GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg, 39GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
40 GNUNET_FRIENDS_Callback cb, 40 GNUNET_FRIENDS_Callback cb,
41 void *cb_cls) 41 void *cb_cls)
42{ 42{
43 char *fn; 43 char *fn;
44 char *data; 44 char *data;
@@ -49,94 +49,95 @@ GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg,
49 ssize_t ssize; 49 ssize_t ssize;
50 50
51 if (GNUNET_OK != 51 if (GNUNET_OK !=
52 GNUNET_CONFIGURATION_get_value_filename(cfg, 52 GNUNET_CONFIGURATION_get_value_filename (cfg,
53 "TOPOLOGY", 53 "TOPOLOGY",
54 "FRIENDS", 54 "FRIENDS",
55 &fn)) 55 &fn))
56 { 56 {
57 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 57 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
58 "topology", 58 "topology",
59 "FRIENDS"); 59 "FRIENDS");
60 return GNUNET_SYSERR; 60 return GNUNET_SYSERR;
61 } 61 }
62 if (GNUNET_SYSERR == 62 if (GNUNET_SYSERR ==
63 GNUNET_DISK_directory_create_for_file(fn)) 63 GNUNET_DISK_directory_create_for_file (fn))
64 { 64 {
65 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, 65 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
66 "mkdir", 66 "mkdir",
67 fn); 67 fn);
68 GNUNET_free(fn); 68 GNUNET_free (fn);
69 return GNUNET_SYSERR; 69 return GNUNET_SYSERR;
70 } 70 }
71 if ((GNUNET_OK != 71 if ((GNUNET_OK !=
72 GNUNET_DISK_file_test(fn)) && 72 GNUNET_DISK_file_test (fn)) &&
73 (GNUNET_OK != 73 (GNUNET_OK !=
74 GNUNET_DISK_fn_write(fn, 74 GNUNET_DISK_fn_write (fn,
75 NULL, 75 NULL,
76 0, 76 0,
77 GNUNET_DISK_PERM_USER_READ | 77 GNUNET_DISK_PERM_USER_READ
78 GNUNET_DISK_PERM_USER_WRITE | 78 | GNUNET_DISK_PERM_USER_WRITE
79 GNUNET_DISK_OPEN_CREATE))) 79 | GNUNET_DISK_OPEN_CREATE)))
80 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, 80 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
81 "write", 81 "write",
82 fn); 82 fn);
83 if ((GNUNET_OK != 83 if ((GNUNET_OK !=
84 GNUNET_DISK_file_size(fn, 84 GNUNET_DISK_file_size (fn,
85 &fsize, 85 &fsize,
86 GNUNET_NO, 86 GNUNET_NO,
87 GNUNET_YES)) || 87 GNUNET_YES)) ||
88 (0 == fsize)) 88 (0 == fsize))
89 { 89 {
90 GNUNET_free(fn); 90 GNUNET_free (fn);
91 return GNUNET_OK; 91 return GNUNET_OK;
92 } 92 }
93 data = GNUNET_malloc_large(fsize); 93 data = GNUNET_malloc_large (fsize);
94 if (NULL == data) 94 if (NULL == data)
95 { 95 {
96 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc"); 96 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
97 GNUNET_free(fn); 97 GNUNET_free (fn);
98 return GNUNET_SYSERR; 98 return GNUNET_SYSERR;
99 } 99 }
100 ssize = GNUNET_DISK_fn_read(fn, 100 ssize = GNUNET_DISK_fn_read (fn,
101 data, 101 data,
102 fsize); 102 fsize);
103 if ((ssize < 0) || 103 if ((ssize < 0) ||
104 (fsize != (uint64_t)ssize)) 104 (fsize != (uint64_t) ssize))
105 { 105 {
106 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, 106 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
107 "read", 107 "read",
108 "fn"); 108 "fn");
109 GNUNET_free(fn); 109 GNUNET_free (fn);
110 GNUNET_free(data); 110 GNUNET_free (data);
111 return GNUNET_SYSERR; 111 return GNUNET_SYSERR;
112 } 112 }
113 start = 0; 113 start = 0;
114 pos = 0; 114 pos = 0;
115 while (pos < fsize) 115 while (pos < fsize)
116 {
117 while ((pos < fsize) &&
118 (! isspace ((unsigned char) data[pos])))
119 pos++;
120 if (GNUNET_OK !=
121 GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
122 pos - start,
123 &pid.public_key))
116 { 124 {
117 while ((pos < fsize) && 125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
118 (!isspace((unsigned char)data[pos]))) 126 _ (
119 pos++; 127 "Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
120 if (GNUNET_OK != 128 (unsigned long long) pos,
121 GNUNET_CRYPTO_eddsa_public_key_from_string(&data[start], 129 (int) (pos - start),
122 pos - start, 130 &data[start]);
123 &pid.public_key))
124 {
125 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
126 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
127 (unsigned long long)pos,
128 (int)(pos - start),
129 &data[start]);
130 pos++;
131 start = pos;
132 continue;
133 }
134 pos++; 131 pos++;
135 start = pos; 132 start = pos;
136 cb(cb_cls, &pid); 133 continue;
137 } 134 }
138 GNUNET_free(data); 135 pos++;
139 GNUNET_free(fn); 136 start = pos;
137 cb (cb_cls, &pid);
138 }
139 GNUNET_free (data);
140 GNUNET_free (fn);
140 return GNUNET_OK; 141 return GNUNET_OK;
141} 142}
142 143
@@ -144,7 +145,8 @@ GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg,
144/** 145/**
145 * Handle for writing a friends file. 146 * Handle for writing a friends file.
146 */ 147 */
147struct GNUNET_FRIENDS_Writer { 148struct GNUNET_FRIENDS_Writer
149{
148 /** 150 /**
149 * Handle to the file. 151 * Handle to the file.
150 */ 152 */
@@ -160,41 +162,41 @@ struct GNUNET_FRIENDS_Writer {
160 * @return NULL on error 162 * @return NULL on error
161 */ 163 */
162struct GNUNET_FRIENDS_Writer * 164struct GNUNET_FRIENDS_Writer *
163GNUNET_FRIENDS_write_start(const struct GNUNET_CONFIGURATION_Handle *cfg) 165GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
164{ 166{
165 struct GNUNET_FRIENDS_Writer *w; 167 struct GNUNET_FRIENDS_Writer *w;
166 char *fn; 168 char *fn;
167 169
168 if (GNUNET_OK != 170 if (GNUNET_OK !=
169 GNUNET_CONFIGURATION_get_value_filename(cfg, "TOPOLOGY", "FRIENDS", &fn)) 171 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
170 { 172 {
171 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 173 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
172 "topology", "FRIENDS"); 174 "topology", "FRIENDS");
173 return NULL; 175 return NULL;
174 } 176 }
175 if (GNUNET_OK != 177 if (GNUNET_OK !=
176 GNUNET_DISK_directory_create_for_file(fn)) 178 GNUNET_DISK_directory_create_for_file (fn))
177 { 179 {
178 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
179 _("Directory for file `%s' does not seem to be writable.\n"), 181 _ ("Directory for file `%s' does not seem to be writable.\n"),
180 fn); 182 fn);
181 GNUNET_free(fn); 183 GNUNET_free (fn);
182 return NULL; 184 return NULL;
183 } 185 }
184 if (GNUNET_OK == GNUNET_DISK_file_test(fn)) 186 if (GNUNET_OK == GNUNET_DISK_file_test (fn))
185 GNUNET_DISK_file_backup(fn); 187 GNUNET_DISK_file_backup (fn);
186 w = GNUNET_new(struct GNUNET_FRIENDS_Writer); 188 w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
187 w->fh = GNUNET_DISK_file_open(fn, 189 w->fh = GNUNET_DISK_file_open (fn,
188 GNUNET_DISK_OPEN_CREATE | 190 GNUNET_DISK_OPEN_CREATE
189 GNUNET_DISK_OPEN_WRITE | 191 | GNUNET_DISK_OPEN_WRITE
190 GNUNET_DISK_OPEN_FAILIFEXISTS, 192 | GNUNET_DISK_OPEN_FAILIFEXISTS,
191 GNUNET_DISK_PERM_USER_READ); 193 GNUNET_DISK_PERM_USER_READ);
192 GNUNET_free(fn); 194 GNUNET_free (fn);
193 if (NULL == w->fh) 195 if (NULL == w->fh)
194 { 196 {
195 GNUNET_free(w); 197 GNUNET_free (w);
196 return NULL; 198 return NULL;
197 } 199 }
198 return w; 200 return w;
199} 201}
200 202
@@ -206,12 +208,12 @@ GNUNET_FRIENDS_write_start(const struct GNUNET_CONFIGURATION_Handle *cfg)
206 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 208 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
207 */ 209 */
208int 210int
209GNUNET_FRIENDS_write_stop(struct GNUNET_FRIENDS_Writer *w) 211GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
210{ 212{
211 int ret; 213 int ret;
212 214
213 ret = GNUNET_DISK_file_close(w->fh); 215 ret = GNUNET_DISK_file_close (w->fh);
214 GNUNET_free(w); 216 GNUNET_free (w);
215 return ret; 217 return ret;
216} 218}
217 219
@@ -224,28 +226,28 @@ GNUNET_FRIENDS_write_stop(struct GNUNET_FRIENDS_Writer *w)
224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 226 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
225 */ 227 */
226int 228int
227GNUNET_FRIENDS_write(struct GNUNET_FRIENDS_Writer *w, 229GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
228 const struct GNUNET_PeerIdentity *friend_id) 230 const struct GNUNET_PeerIdentity *friend_id)
229{ 231{
230 char *buf; 232 char *buf;
231 char *ret; 233 char *ret;
232 size_t slen; 234 size_t slen;
233 235
234 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&friend_id->public_key); 236 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
235 GNUNET_asprintf(&buf, 237 GNUNET_asprintf (&buf,
236 "%s\n", 238 "%s\n",
237 ret); 239 ret);
238 GNUNET_free(ret); 240 GNUNET_free (ret);
239 slen = strlen(buf); 241 slen = strlen (buf);
240 if (slen != 242 if (slen !=
241 GNUNET_DISK_file_write(w->fh, 243 GNUNET_DISK_file_write (w->fh,
242 buf, 244 buf,
243 slen)) 245 slen))
244 { 246 {
245 GNUNET_free(buf); 247 GNUNET_free (buf);
246 return GNUNET_SYSERR; 248 return GNUNET_SYSERR;
247 } 249 }
248 GNUNET_free(buf); 250 GNUNET_free (buf);
249 return GNUNET_OK; 251 return GNUNET_OK;
250} 252}
251 253