1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
8 #include <libubox/uloop.h>
9 #include <libubox/blobmsg_json.h>
12 struct cmdline_network
{
13 struct cmdline_network
*next
;
17 static struct cmdline_network
*cmd_nets
;
18 static const char *hosts_file
;
19 const char *mssfix_path
= UNETD_MSS_BPF_PATH
;
20 const char *data_dir
= UNETD_DATA_DIR
;
21 int global_pex_port
= UNETD_GLOBAL_PEX_PORT
;
25 network_write_hosts(struct network
*net
, FILE *f
)
27 struct network_host
*host
;
28 char ip
[INET6_ADDRSTRLEN
];
30 if (!net
->net_config
.local_host
)
33 avl_for_each_element(&net
->hosts
, host
, node
) {
34 inet_ntop(AF_INET6
, &host
->peer
.local_addr
, ip
, sizeof(ip
));
35 fprintf(f
, "%s\t%s%s%s\n", ip
, network_host_name(host
),
36 net
->config
.domain
? "." : "",
37 net
->config
.domain
? net
->config
.domain
: "");
41 void unetd_write_hosts(void)
51 asprintf(&tmpfile
, "%s.XXXXXXXX", hosts_file
);
52 fd
= mkstemp(tmpfile
);
65 avl_for_each_element(&networks
, net
, node
)
66 network_write_hosts(net
, f
);
70 if (rename(tmpfile
, hosts_file
))
77 static void add_networks(void)
79 struct cmdline_network
*net
;
80 static struct blob_buf b
;
81 struct blob_attr
*name
;
83 for (net
= cmd_nets
; net
; net
= net
->next
) {
85 if (!blobmsg_add_json_from_string(&b
, net
->data
))
88 blobmsg_parse(&network_policy
[NETWORK_ATTR_NAME
], 1, &name
,
89 blobmsg_data(b
.head
), blobmsg_len(b
.head
));
93 unetd_network_add(blobmsg_get_string(name
), b
.head
);
99 int main(int argc
, char **argv
)
101 struct cmdline_network
*net
;
104 while ((ch
= getopt(argc
, argv
, "D:dh:M:N:P:")) != -1) {
116 net
= calloc(1, sizeof(*net
));
117 net
->next
= cmd_nets
;
122 mssfix_path
= optarg
;
125 global_pex_port
= atoi(optarg
);