forked from codyps/peerduct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg_json.c
More file actions
75 lines (57 loc) · 1.51 KB
/
cfg_json.c
File metadata and controls
75 lines (57 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "cfg_json.h"
#include "yajl/yajl_tree.h"
#include <stdio.h>
#include <string.h> /* strlen */
static int base64_decode(void *dst, size_t dst_sz, const char *src, size_t src_sz)
{
/* TODO: is there some good code for this? some standard lib? (openssl
* has it, though it seems a silly reason to pull in all of openssl) */
return -1;
}
int config_get_id(struct config *cfg, struct kademlia_id *id)
{
const char * path[] = { "id", NULL };
yajl_val v = yajl_tree_get(cfg->root, path, yajl_t_string);
if (v) {
char *id_str = YAJL_GET_STRING(v);
return base64_decode(id->bytes, sizeof(id->bytes), id_str, strlen(id_str));
} else {
return -1;
}
}
static void get_peers(struct config *cfg)
{
/* belh */
}
void config_destroy(struct config *cfg)
{
yajl_tree_free(cfg->root);
}
int config_load(struct config *cfg, char *config_file)
{
char file_data[65536];
char errbuf[1024];
file_data[0] = errbuf[0] = 0;
FILE *fd = fopen(config_file, "rb");
if (!fd) {
fprintf(stderr, "failed to open file \"%s\"\n", config_file);
return -1;
}
size_t rd = fread(file_data, 1, sizeof(file_data), fd);
if ((rd == 0 && !feof(fd)) || ferror(fd)) {
fprintf(stderr, "error encountered on file read\n");
return -2;
}
cfg->root = yajl_tree_parse(file_data, errbuf, sizeof(errbuf));
if (cfg->root == NULL) {
fprintf(stderr, "parse error: ");
if (strlen(errbuf))
fprintf(stderr, " %s", errbuf);
else
fprintf(stderr, "unknown error");
fprintf(stderr, "\n");
return -3;
}
get_peers(cfg);
return 0;
}