c9ce132a2cfa616d88f92c619cfd9d2b5cec4c14
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\n", ip
, network_host_name(host
));
39 void unetd_write_hosts(void)
49 asprintf(&tmpfile
, "%s.XXXXXXXX", hosts_file
);
50 fd
= mkstemp(tmpfile
);
63 avl_for_each_element(&networks
, net
, node
)
64 network_write_hosts(net
, f
);
68 if (rename(tmpfile
, hosts_file
))
75 static void add_networks(void)
77 struct cmdline_network
*net
;
78 static struct blob_buf b
;
79 struct blob_attr
*name
;
81 for (net
= cmd_nets
; net
; net
= net
->next
) {
83 if (!blobmsg_add_json_from_string(&b
, net
->data
))
86 blobmsg_parse(&network_policy
[NETWORK_ATTR_NAME
], 1, &name
,
87 blobmsg_data(b
.head
), blobmsg_len(b
.head
));
91 unetd_network_add(blobmsg_get_string(name
), b
.head
);
97 int main(int argc
, char **argv
)
99 struct cmdline_network
*net
;
102 while ((ch
= getopt(argc
, argv
, "D:dh:M:N:P:")) != -1) {
114 net
= calloc(1, sizeof(*net
));
115 net
->next
= cmd_nets
;
120 mssfix_path
= optarg
;
123 global_pex_port
= atoi(optarg
);