cd398438f253e4bea8bfa00fa5ce6389d4954f90
1 // SPDX-License-Identifier: GPL-2.0+
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
;
23 network_write_hosts(struct network
*net
, FILE *f
)
25 struct network_host
*host
;
26 char ip
[INET6_ADDRSTRLEN
];
28 if (!net
->net_config
.local_host
)
31 avl_for_each_element(&net
->hosts
, host
, node
) {
32 inet_ntop(AF_INET6
, &host
->peer
.local_addr
, ip
, sizeof(ip
));
33 fprintf(f
, "%s\t%s\n", ip
, network_host_name(host
));
37 void unetd_write_hosts(void)
47 asprintf(&tmpfile
, "%s.XXXXXXXX", hosts_file
);
48 fd
= mkstemp(tmpfile
);
61 avl_for_each_element(&networks
, net
, node
)
62 network_write_hosts(net
, f
);
66 if (rename(tmpfile
, hosts_file
))
73 static void add_networks(void)
75 struct cmdline_network
*net
;
76 static struct blob_buf b
;
77 struct blob_attr
*name
;
79 for (net
= cmd_nets
; net
; net
= net
->next
) {
81 if (!blobmsg_add_json_from_string(&b
, net
->data
))
84 blobmsg_parse(&network_policy
[NETWORK_ATTR_NAME
], 1, &name
,
85 blobmsg_data(b
.head
), blobmsg_len(b
.head
));
89 unetd_network_add(blobmsg_get_string(name
), b
.head
);
95 int main(int argc
, char **argv
)
97 struct cmdline_network
*net
;
100 while ((ch
= getopt(argc
, argv
, "dh:M:N:")) != -1) {
109 net
= calloc(1, sizeof(*net
));
110 net
->next
= cmd_nets
;
115 mssfix_path
= optarg
;