forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETHInterface_admin.c
211 lines (185 loc) · 8.14 KB
/
ETHInterface_admin.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* 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 <https://www.gnu.org/licenses/>.
*/
#include "interface/ETHInterface_admin.h"
#include "interface/ETHInterface.h"
#include "benc/Int.h"
#include "admin/Admin.h"
#include "crypto/Key.h"
#include "exception/Jmp.h"
#include "memory/Allocator.h"
#include "net/InterfaceController.h"
#include "util/AddrTools.h"
#include "util/Identity.h"
struct Context
{
struct EventBase* eventBase;
struct Allocator* alloc;
struct Log* logger;
struct Admin* admin;
struct InterfaceController* ic;
Identity
};
static void beginConnection(Dict* args,
void* vcontext,
String* txid,
struct Allocator* requestAlloc)
{
struct Context* ctx = Identity_check((struct Context*) vcontext);
String* password = Dict_getString(args, String_CONST("password"));
String* login = Dict_getString(args, String_CONST("login"));
String* publicKey = Dict_getString(args, String_CONST("publicKey"));
String* macAddress = Dict_getString(args, String_CONST("macAddress"));
int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
String* peerName = Dict_getString(args, String_CONST("peerName"));
char* error = "none";
uint8_t pkBytes[32];
struct ETHInterface_Sockaddr sockaddr = {
.generic = {
.addrLen = ETHInterface_Sockaddr_SIZE
}
};
if (Key_parse(publicKey, pkBytes, NULL)) {
error = "invalid publicKey";
} else if (macAddress->len < 17 || AddrTools_parseMac(sockaddr.mac, macAddress->bytes)) {
error = "invalid macAddress";
} else {
int ret = InterfaceController_bootstrapPeer(
ctx->ic, ifNum, pkBytes, &sockaddr.generic, password, login, peerName, ctx->alloc);
if (ret == InterfaceController_bootstrapPeer_BAD_IFNUM) {
error = "invalid interfaceNumber";
} else if (ret == InterfaceController_bootstrapPeer_BAD_KEY) {
error = "invalid publicKey";
} else if (ret == InterfaceController_bootstrapPeer_OUT_OF_SPACE) {
error = "no more space to register with the switch.";
} else if (ret) {
error = "InterfaceController_bootstrapPeer(internal_error)";
}
}
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_new(error, requestAlloc), requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
}
static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
struct Context* const ctx = Identity_check((struct Context*) vcontext);
String* const bindDevice = Dict_getString(args, String_CONST("bindDevice"));
struct Allocator* const alloc = Allocator_child(ctx->alloc);
struct ETHInterface* ethIf = NULL;
struct Jmp jmp;
Jmp_try(jmp) {
ethIf = ETHInterface_new(
ctx->eventBase, bindDevice->bytes, alloc, &jmp.handler, ctx->logger);
} Jmp_catch {
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
Allocator_free(alloc);
return;
}
String* ifname = String_printf(requestAlloc, "ETH/%s", bindDevice->bytes);
struct InterfaceController_Iface* ici = InterfaceController_newIface(ctx->ic, ifname, alloc);
Iface_plumb(&ici->addrIf, ðIf->generic.iface);
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
Dict_putInt(out, String_CONST("interfaceNumber"), ici->ifNum, requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
}
static void beacon(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
int64_t* stateP = Dict_getInt(args, String_CONST("state"));
int64_t* ifNumP = Dict_getInt(args, String_CONST("interfaceNumber"));
uint32_t ifNum = (ifNumP) ? ((uint32_t) *ifNumP) : 0;
uint32_t state = (stateP) ? ((uint32_t) *stateP) : 0xffffffff;
struct Context* ctx = Identity_check((struct Context*) vcontext);
char* error = NULL;
int ret = InterfaceController_beaconState(ctx->ic, ifNum, state);
if (ret == InterfaceController_beaconState_NO_SUCH_IFACE) {
error = "invalid interfaceNumber";
} else if (ret == InterfaceController_beaconState_INVALID_STATE) {
error = "invalid state";
} else if (ret) {
error = "internal";
}
if (error) {
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_CONST(error), requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
return;
}
char* stateStr = "disabled";
if (state == InterfaceController_beaconState_newState_ACCEPT) {
stateStr = "accepting";
} else if (state == InterfaceController_beaconState_newState_SEND) {
stateStr = "sending and accepting";
}
Dict out = Dict_CONST(
String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
String_CONST("state"), Int_OBJ(state), Dict_CONST(
String_CONST("stateName"), String_OBJ(String_CONST(stateStr)), NULL
)));
Admin_sendMessage(&out, txid, ctx->admin);
}
static void listDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
struct Context* ctx = Identity_check((struct Context*) vcontext);
List* devices = NULL;
struct Jmp jmp;
Jmp_try(jmp) {
devices = ETHInterface_listDevices(requestAlloc, &jmp.handler);
} Jmp_catch {
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
return;
}
Dict* out = Dict_new(requestAlloc);
Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
Dict_putList(out, String_CONST("devices"), devices, requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
}
void ETHInterface_admin_register(struct EventBase* base,
struct Allocator* alloc,
struct Log* logger,
struct Admin* admin,
struct InterfaceController* ic)
{
struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
.eventBase = base,
.alloc = alloc,
.logger = logger,
.admin = admin,
.ic = ic
}));
Identity_set(ctx);
Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
((struct Admin_FunctionArg[]) {
{ .name = "bindDevice", .required = 1, .type = "String" }
}), admin);
Admin_registerFunction("ETHInterface_beginConnection",
beginConnection, ctx, true, ((struct Admin_FunctionArg[]) {
{ .name = "interfaceNumber", .required = 0, .type = "Int" },
{ .name = "password", .required = 0, .type = "String" },
{ .name = "publicKey", .required = 1, .type = "String" },
{ .name = "macAddress", .required = 1, .type = "String" },
{ .name = "login", .required = 0, .type = "String" }
}), admin);
Admin_registerFunction("ETHInterface_beacon", beacon, ctx, true,
((struct Admin_FunctionArg[]) {
{ .name = "interfaceNumber", .required = 0, .type = "Int" },
{ .name = "state", .required = 0, .type = "Int" }
}), admin);
Admin_registerFunction("ETHInterface_listDevices", listDevices, ctx, true, NULL, admin);
}