/
Configurator.c
160 lines (140 loc) · 5.74 KB
/
Configurator.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "admin/AdminClient.h"
#include "admin/Configurator.h"
#include "benc/String.h"
#include "benc/Dict.h"
#include "benc/Int.h"
#include "memory/Allocator.h"
#include "util/Log.h"
#include <event2/event.h>
struct Context
{
struct Log* logger;
struct Allocator* alloc;
struct AdminClient* client;
};
static void showMsg(struct AdminClient_Result* res, struct Context* ctx)
{
Log_keys(ctx->logger, "message bytes = [%s]", res->messageBytes);
#ifndef Log_KEYS
Log_critical(ctx->logger, "enable Log_LEVEL=KEYS to see message content.");
#endif
}
static void rpcCall(String* function, Dict* args, struct Context* ctx, struct Allocator* alloc)
{
struct AdminClient_Result* res = AdminClient_rpcCall(function, args, ctx->client, alloc);
if (res->err) {
Log_critical(ctx->logger,
"Failed to make function call [%s], error: [%s]",
AdminClient_errorString(res->err),
function->bytes);
showMsg(res, ctx);
exit(-1);
}
String* error = Dict_getString(res->responseDict, String_CONST("error"));
if (error && !String_equals(error, String_CONST("none"))) {
Log_critical(ctx->logger,
"Router responses with error: [%s]\nCalling function: [%s]",
error->bytes,
function->bytes);
showMsg(res, ctx);
exit(-1);
}
}
static void authorizedPasswords(List* list, struct Context* ctx)
{
uint32_t count = List_size(list);
for (uint32_t i = 0; i < count; i++) {
Dict* d = List_getDict(list, i);
Log_info(ctx->logger, "Checking authorized password %d.", i);
if (!d) {
Log_critical(ctx->logger, "Not a dictionary type %d.", i);
exit(-1);
}
String* passwd = Dict_getString(d, String_CONST("password"));
if (!passwd) {
Log_critical(ctx->logger, "Must specify a password %d.", i);
exit(-1);
}
}
Log_info(ctx->logger, "Flushing existing authorized passwords");
rpcCall(String_CONST("AuthorizedPasswords_flush"), NULL, ctx, ctx->alloc);
for (uint32_t i = 0; i < count; i++) {
Dict* d = List_getDict(list, i);
String* passwd = Dict_getString(d, String_CONST("password"));
Log_info(ctx->logger, "Adding authorized password #[%d].", i);
Dict args = Dict_CONST(
String_CONST("authType"), Int_OBJ(1), Dict_CONST(
String_CONST("password"), String_OBJ(passwd), NULL
));
struct Allocator* child = ctx->alloc->child(ctx->alloc);
rpcCall(String_CONST("AuthorizedPasswords_add"), &args, ctx, child);
child->free(child);
}
}
static void udpInterface(Dict* config, struct Context* ctx)
{
Dict* udp = Dict_getDict(config, String_CONST("UDPInterface"));
if (udp) {
// Setup the interface.
String* bindStr = Dict_getString(udp, String_CONST("bind"));
Dict* d = Dict_new(ctx->alloc);
if (bindStr) {
Dict_putString(d, String_CONST("bindAddress"), bindStr, ctx->alloc);
}
rpcCall(String_CONST("UDPInterface_new"), d, ctx, ctx->alloc);
// Make the connections.
Dict* connectTo = Dict_getDict(udp, String_CONST("connectTo"));
if (connectTo) {
struct Dict_Entry* entry = *connectTo;
while (entry != NULL) {
String* key = (String*) entry->key;
if (entry->val->type != Object_DICT) {
Log_critical(ctx->logger, "interfaces.UDPInterface.connectTo: entry [%s] "
"is not a dictionary type.", key->bytes);
exit(-1);
}
Dict* value = entry->val->as.dictionary;
Log_keys(ctx->logger, "Attempting to connect to node [%s].", key->bytes);
struct Allocator* perCallAlloc = ctx->alloc->child(ctx->alloc);
Dict_putString(value, String_CONST("address"), key, perCallAlloc);
rpcCall(String_CONST("UDPInterface_beginConnection"), value, ctx, perCallAlloc);
perCallAlloc->free(perCallAlloc);
entry = entry->next;
}
}
}
}
void Configurator_config(Dict* config,
struct sockaddr_storage* addr,
int addrLen,
String* adminPassword,
struct event_base* eventBase,
struct Log* logger,
struct Allocator* alloc)
{
struct Allocator* tempAlloc = alloc->child(alloc);
struct AdminClient* client =
AdminClient_new(addr, addrLen, adminPassword, eventBase, logger, tempAlloc);
struct Context ctx = { .logger = logger, .alloc = tempAlloc, .client = client };
List* authedPasswords = Dict_getList(config, String_CONST("authorizedPasswords"));
if (authedPasswords) {
authorizedPasswords(authedPasswords, &ctx);
}
Dict* ifaces = Dict_getDict(config, String_CONST("interfaces"));
udpInterface(ifaces, &ctx);
tempAlloc->free(tempAlloc);
}