forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPInterface_admin.c
222 lines (196 loc) · 8.3 KB
/
UDPInterface_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
212
213
214
215
216
217
218
219
220
221
222
/* 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 "benc/Int.h"
#include "admin/Admin.h"
#include "exception/Jmp.h"
#include "memory/Allocator.h"
#include "net/InterfaceController.h"
#include "util/events/UDPAddrIface.h"
#include "util/events/EventBase.h"
#include "util/events/FakeNetwork.h"
#include "util/platform/Sockaddr.h"
#include "crypto/Key.h"
struct Context
{
struct EventBase* eventBase;
struct Allocator* alloc;
struct Log* logger;
struct Admin* admin;
struct AddrIface* udpIf;
struct InterfaceController* ic;
struct FakeNetwork* fakeNet;
};
static void beginConnection(Dict* args,
void* vcontext,
String* txid,
struct Allocator* requestAlloc)
{
struct Context* ctx = 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* address = Dict_getString(args, String_CONST("address"));
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"));
String* error = NULL;
Log_debug(ctx->logger, "Peering with [%s]", publicKey->bytes);
struct Sockaddr_storage ss;
uint8_t pkBytes[32];
int ret;
if (interfaceNumber && *interfaceNumber < 0) {
error = String_CONST("negative interfaceNumber");
} else if ((ret = Key_parse(publicKey, pkBytes, NULL))) {
error = String_CONST(Key_parse_strerror(ret));
} else if (Sockaddr_parse(address->bytes, &ss)) {
error = String_CONST("unable to parse ip address and port.");
} else if (Sockaddr_getFamily(&ss.addr) != Sockaddr_getFamily(ctx->udpIf->addr)) {
error = String_CONST("different address type than this socket is bound to.");
} else {
struct Sockaddr* addr = &ss.addr;
char* addrPtr = NULL;
int addrLen = Sockaddr_getAddress(&ss.addr, &addrPtr);
Assert_true(addrLen > 0);
struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
if (Bits_isZero(addrPtr, addrLen)) {
// unspec'd address, convert to loopback
if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET) {
addr = Sockaddr_clone(Sockaddr_LOOPBACK, tempAlloc);
} else if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET6) {
addr = Sockaddr_clone(Sockaddr_LOOPBACK6, tempAlloc);
} else {
Assert_failure("Sockaddr which is not AF_INET nor AF_INET6");
}
Sockaddr_setPort(addr, Sockaddr_getPort(&ss.addr));
}
int ret = InterfaceController_bootstrapPeer(
ctx->ic, ifNum, pkBytes, addr, password, login, peerName, ctx->alloc);
Allocator_free(tempAlloc);
if (ret) {
switch(ret) {
case InterfaceController_bootstrapPeer_BAD_IFNUM:
error = String_CONST("no such interface for interfaceNumber");
break;
case InterfaceController_bootstrapPeer_BAD_KEY:
error = String_CONST("invalid cjdns public key.");
break;
case InterfaceController_bootstrapPeer_OUT_OF_SPACE:
error = String_CONST("no more space to register with the switch.");
break;
default:
error = String_CONST("unknown error");
break;
}
} else {
error = String_CONST("none");
}
}
Dict out = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
Admin_sendMessage(&out, txid, ctx->admin);
}
static struct AddrIface* setupLibuvUDP(struct Context* ctx,
struct Sockaddr* addr,
String* txid,
struct Allocator* alloc)
{
struct UDPAddrIface* udpIf = NULL;
struct Jmp jmp;
Jmp_try(jmp) {
udpIf = UDPAddrIface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger);
} Jmp_catch {
String* errStr = String_CONST(jmp.message);
Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
Admin_sendMessage(&out, txid, ctx->admin);
Allocator_free(alloc);
return NULL;
}
return &udpIf->generic;
}
static struct AddrIface* setupFakeUDP(struct FakeNetwork* fakeNet,
struct Sockaddr* addr,
struct Allocator* alloc)
{
struct FakeNetwork_UDPIface* fni = FakeNetwork_iface(fakeNet, addr, alloc);
return &fni->generic;
}
static void newInterface2(struct Context* ctx,
struct Sockaddr* addr,
String* txid,
struct Allocator* requestAlloc)
{
struct Allocator* const alloc = Allocator_child(ctx->alloc);
struct AddrIface* ai;
if (ctx->fakeNet) {
ai = setupFakeUDP(ctx->fakeNet, addr, alloc);
} else {
ai = setupLibuvUDP(ctx, addr, txid, alloc);
}
if (!ai) { return; }
ctx->udpIf = ai;
struct InterfaceController_Iface* ici =
InterfaceController_newIface(ctx->ic, String_CONST("UDP"), alloc);
Iface_plumb(&ici->addrIf, &ai->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);
char* printedAddr = Sockaddr_print(ai->addr, requestAlloc);
Dict_putString(out,
String_CONST("bindAddress"),
String_CONST(printedAddr),
requestAlloc);
Admin_sendMessage(out, txid, ctx->admin);
}
static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
{
struct Context* ctx = vcontext;
String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
struct Sockaddr_storage addr;
if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
Dict out = Dict_CONST(
String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
);
Admin_sendMessage(&out, txid, ctx->admin);
return;
}
newInterface2(ctx, &addr.addr, txid, requestAlloc);
}
void UDPInterface_admin_register(struct EventBase* base,
struct Allocator* alloc,
struct Log* logger,
struct Admin* admin,
struct InterfaceController* ic,
struct FakeNetwork* fakeNet)
{
struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
.eventBase = base,
.alloc = alloc,
.logger = logger,
.admin = admin,
.ic = ic,
.fakeNet = fakeNet
}));
Admin_registerFunction("UDPInterface_new", newInterface, ctx, true,
((struct Admin_FunctionArg[]) {
{ .name = "bindAddress", .required = 0, .type = "String" }
}), admin);
Admin_registerFunction("UDPInterface_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 = "address", .required = 1, .type = "String" },
{ .name = "login", .required = 0, .type = "String" }
}), admin);
}