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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <arpa/inet.h>
#include <libubox/uloop.h>
#include <libubox/blobmsg_json.h>
#include "unetd.h"
struct cmdline_network {
struct cmdline_network *next;
char *data;
};
static struct cmdline_network *cmd_nets;
static const char *hosts_file;
const char *mssfix_path = UNETD_MSS_BPF_PATH;
const char *data_dir = UNETD_DATA_DIR;
int global_pex_port = UNETD_GLOBAL_PEX_PORT;
static bool debug;
#ifdef UBUS_SUPPORT
static struct udebug ud;
static struct udebug_buf udb_log;
static const struct udebug_buf_meta meta_log = {
.name = "log",
.format = UDEBUG_FORMAT_STRING
};
static struct udebug_ubus_ring rings[] = {
{
.buf = &udb_log,
.meta = &meta_log,
.default_entries = 1024,
.default_size = 64 * 1024,
}
};
#endif
bool unetd_debug_active(void)
{
#ifdef UBUS_SUPPORT
if (udebug_buf_valid(&udb_log))
return true;
#endif
return debug;
}
static void __attribute__((format (printf, 1, 0)))
unetd_udebug_vprintf(const char *format, va_list ap)
{
#ifdef UBUS_SUPPORT
if (!udebug_buf_valid(&udb_log))
return;
udebug_entry_init(&udb_log);
udebug_entry_vprintf(&udb_log, format, ap);
udebug_entry_add(&udb_log);
#endif
}
void unetd_debug_printf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (debug) {
va_list ap2;
va_copy(ap2, ap);
vfprintf(stderr, format, ap2);
va_end(ap2);
}
unetd_udebug_vprintf(format, ap);
va_end(ap);
}
#ifdef UBUS_SUPPORT
void unetd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
bool enabled)
{
udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
}
#endif
static void
network_write_hosts(struct network *net, FILE *f)
{
struct network_host *host;
char ip[INET6_ADDRSTRLEN];
if (!net->net_config.local_host)
return;
avl_for_each_element(&net->hosts, host, node) {
inet_ntop(AF_INET6, &host->peer.local_addr, ip, sizeof(ip));
fprintf(f, "%s\t%s%s%s\n", ip, network_host_name(host),
net->config.domain ? "." : "",
net->config.domain ? net->config.domain : "");
}
}
void unetd_write_hosts(void)
{
struct network *net;
char *tmpfile = NULL;
FILE *f;
int fd;
if (!hosts_file)
return;
if (asprintf(&tmpfile, "%s.XXXXXXXX", hosts_file) < 0)
return;
fd = mkstemp(tmpfile);
if (fd < 0) {
perror("mkstemp");
goto out;
}
chmod(tmpfile, 0644);
f = fdopen(fd, "w");
if (!f) {
close(fd);
goto out;
}
avl_for_each_element(&networks, net, node)
network_write_hosts(net, f);
fclose(f);
if (rename(tmpfile, hosts_file))
unlink(tmpfile);
out:
free(tmpfile);
}
static void add_networks(void)
{
struct cmdline_network *net;
static struct blob_buf b;
struct blob_attr *name;
for (net = cmd_nets; net; net = net->next) {
blob_buf_init(&b, 0);
if (!blobmsg_add_json_from_string(&b, net->data))
continue;
blobmsg_parse(&network_policy[NETWORK_ATTR_NAME], 1, &name,
blobmsg_data(b.head), blobmsg_len(b.head));
if (!name)
continue;
unetd_network_add(blobmsg_get_string(name), b.head);
}
blob_buf_free(&b);
}
int main(int argc, char **argv)
{
struct cmdline_network *net;
const char *unix_socket = NULL;
int ch;
while ((ch = getopt(argc, argv, "D:dh:u:M:N:P:")) != -1) {
switch (ch) {
case 'D':
data_dir = optarg;
break;
case 'd':
debug = true;
break;
case 'h':
hosts_file = optarg;
break;
case 'N':
net = calloc(1, sizeof(*net));
net->next = cmd_nets;
net->data = optarg;
cmd_nets = net;
break;
case 'M':
mssfix_path = optarg;
break;
case 'P':
global_pex_port = atoi(optarg);
break;
case 'u':
unix_socket = optarg;
break;
}
}
uloop_init();
#ifdef UBUS_SUPPORT
udebug_init(&ud);
udebug_auto_connect(&ud, NULL);
for (size_t i = 0; i < ARRAY_SIZE(rings); i++)
udebug_ubus_ring_init(&ud, &rings[i]);
#endif
unetd_ubus_init();
unetd_write_hosts();
global_pex_open(unix_socket);
add_networks();
uloop_run();
pex_close();
network_free_all();
uloop_done();
return 0;
}
|