blob: ae4f473d4cabacc7c577a8622306060a9e9461b4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001
2#include "piped.h"
3#include "piped_uci.h"
4#include "piped_util.h"
5#include "piped_nw.h"
6#include "piped_dhcp.h"
7#include "piped_dns.h"
8
9static int pd_add_del_dns6(struct pd_context *pdc, struct pd_iface *pdi,
10 bool add)
11{
12 struct uci_context *c = pdc->c;
13
14 if (!pdi_test(pdi, PDI_DNS6P) && !pdi_test(pdi, PDI_DNS6S))
15 return 0;
16
17 if (add) {
18 if (pdi_test(pdi, PDI_DNS6P) && pd_uci_add_list(c,
19 UCI_PKG_DHCP,
20 UCI_SEC_LAN,
21 UCI_OPT_DNS,
22 pdi->dns6p)) {
23 PD_ERR(pd_add_del_dns6, "add dns6p failed\n");
24 return 1;
25 }
26 if (pdi_test(pdi, PDI_DNS6S) && pd_uci_add_list(c, UCI_PKG_DHCP,
27 UCI_SEC_LAN,
28 UCI_OPT_DNS,
29 pdi->dns6s)) {
30 PD_ERR(pd_add_del_dns61, "add dns6s failed\n");
31 return 1;
32 }
33 } else {
34 if (pdi_test(pdi, PDI_DNS6P) && pd_uci_del_from_list(c,
35 UCI_PKG_DHCP,
36 UCI_SEC_LAN,
37 UCI_OPT_DNS,
38 pdi->dns6p)) {
39 PD_ERR(pd_add_del_dns62, "del dns6p failed\n");
40 return 1;
41 }
42 if (pdi_test(pdi, PDI_DNS6S) && pd_uci_del_from_list(c,
43 UCI_PKG_DHCP,
44 UCI_SEC_LAN,
45 UCI_OPT_DNS,
46 pdi->dns6s)) {
47 PD_ERR(pd_add_del_dns63, "del dns6p failed\n");
48 return 1;
49 }
50 }
51
52 pd_set(&pdc->status, PDC_ODHCPD_CHANGED);
53
54 return 0;
55}
56
57static int pd_add_del_dns4(struct pd_context *pdc, struct pd_iface *pdi,
58 bool add)
59{
60 struct uci_context *c = pdc->c;
61 char options[MAX_DHCP_OPT_STR_LEN];
62
63 if (!pdi_test(pdi, PDI_DNS4P) && !pdi_test(pdi, PDI_DNS4S))
64 return 0;
65
66 strcpy(options, UCI_DHCP_OPTNUM_DNS);
67
68 if (pdi_test(pdi, PDI_DNS4P)) {
69 strcat(options, ",");
70 strcat(options, pdi->dns4p);
71 }
72 if (pdi_test(pdi, PDI_DNS4S)) {
73 strcat(options, ",");
74 strcat(options, pdi->dns4s);
75 }
76
77 if (add && pd_uci_add_list(c, UCI_PKG_DHCP, UCI_SEC_LAN,
78 UCI_OPT_DHCP_OPT, options)) {
79 PD_ERR(pd_add_del_dns4, "dhcp add dns4 failed\n");
80 return 1;
81 } else if (!add && pd_uci_del_from_list(c, UCI_PKG_DHCP, UCI_SEC_LAN,
82 UCI_OPT_DHCP_OPT, options)) {
83 PD_ERR(pd_add_del_dns41, "dhcp del dns4 failed\n");
84 return 1;
85 }
86
87 pd_set(&pdc->status, PDC_DNSMASQ_CHANGED);
88
89 return 0;
90}
91
92int pd_dns4_add(struct pd_context *pdc, struct pd_iface *pdi)
93{
94 if (pd_add_del_dns4(pdc, pdi, true)) {
95 PD_ERR(pd_dns4_add, "pd_add_del_dns4 failed for %s\n", pdi->sec);
96 return 1;
97 }
98
99 return 0;
100}
101
102int pd_dns4_del(struct pd_context *pdc, struct pd_iface *pdi)
103{
104 if (pd_add_del_dns4(pdc, pdi, false)) {
105 PD_ERR(pd_dns4_del, "pd_add_del_dns4 failed for %s\n", pdi->sec);
106 return 1;
107 }
108
109 return 0;
110}
111
112int pd_dns4_change(struct pd_context *pdc, struct pd_iface *oldi,
113 struct pd_iface *newi)
114{
115 if (pd_dns4_del(pdc, oldi)) {
116 PD_ERR(pd_dns4_change, "pd_dns4_del failed\n");
117 return 1;
118 }
119 if (pd_dns4_add(pdc, newi)) {
120 PD_ERR(pd_dns4_change1, "pd_dns4_add failed\n");
121 return 1;
122 }
123
124 return 0;
125}
126
127
128int pd_dns6_add(struct pd_context *pdc, struct pd_iface *pdi)
129{
130 if (pd_add_del_dns6(pdc, pdi, true)) {
131 PD_ERR(pd_dns6_add, "pd_add_del_dns6 failed for %s\n", pdi->sec);
132 return 1;
133 }
134
135 return 0;
136}
137int pd_dns6_change(struct pd_context *pdc, struct pd_iface *oldi,
138 struct pd_iface *newi)
139{
140 if (pd_dns6_del(pdc, oldi)) {
141 PD_ERR(pd_dns6_change, "pd_dns6_del failed\n");
142 return 1;
143 }
144 if (pd_dns6_add(pdc, newi)) {
145 PD_ERR(pd_dns6_change1, "pd_dns6_add failed\n");
146 return 1;
147 }
148
149 return 0;
150}
151
152int pd_dns6_del(struct pd_context *pdc, struct pd_iface *pdi)
153{
154
155 if (pd_add_del_dns6(pdc, pdi, false)) {
156 PD_ERR(pd_dns6_del, "pd_add_del_dns6 failed\n");
157 return 1;
158 }
159
160 return 0;
161}
162
163void pd_dns_clean(struct pd_context *pdc)
164{
165 struct uci_context *c = pdc->c;
166
167 if (pd_uci_del(c, UCI_PKG_DHCP, UCI_SEC_LAN, UCI_OPT_DNS, NULL))
168 return;
169
170 PD_INFO(pd_dns_clean, "cleaning dns configuration\n");
171
172 pd_set(&pdc->status, PDC_ODHCPD_CHANGED);
173}