blob: 3e2b6836397e7d864d12ee843bc84e1742007815 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <unistd.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <stdio.h>
8#include <fcntl.h>
9#include <string.h>
10
11#include "piped.h"
12#include "piped_util.h"
13#include "piped_uci.h"
14
15void pd_path_init(struct pd_path *p)
16{
17 strcpy(p->buf, MPIPE_PATH);
18 p->iptr = p->buf + sizeof(MPIPE_PATH) - 1;
19 p->fptr = NULL;
20}
21
22void pd_path_set_iface(struct pd_path *p, const char *ifname)
23{
24 strcpy(p->iptr, ifname);
25 strcat(p->iptr, "/");
26 p->fptr = p->iptr + strlen(ifname) + 1;
27}
28
29void pd_path_set_file(struct pd_path *p, const char *fname)
30{
31 strcpy(p->fptr, fname);
32}
33
34int __pd_write_str(char *path, const char *s)
35{
36 int fd = open(path, O_WRONLY);
37
38 if (fd == -1) {
39 PD_ERR(__pd_write_str, "Failed to open file: %s\n", path);
40 return 1;
41 }
42
43 if (write(fd, s, strlen(s)) == -1) {
44 PD_ERR(__pd_write_str1, "Failed to write to file: %s\n", path);
45 return 1;
46 }
47
48 if (close(fd)) {
49 PD_ERR(__pd_write_str2, "Failed to close file: %s\n", path);
50 return 1;
51 }
52
53 return 0;
54}
55
56int pd_write_str(struct pd_path *p, const char *s)
57{
58 return __pd_write_str(p->buf, s);
59}
60
61int __pd_write_bool(char *buf, bool b)
62{
63 char s[2];
64 s[0] = b ? '1' : '0';
65 s[1] = '\0';
66
67 return __pd_write_str(buf, s);
68}
69
70int pd_write_bool(struct pd_path *p, bool b)
71{
72 return __pd_write_bool(p->buf, b);
73}
74
75int __pd_read(char *path, char *buf, int len)
76{
77 FILE *fp = fopen(path, "r");
78 size_t rc = 0;
79 if (fp != NULL) {
80 rc = fread(buf, sizeof(char), len - 1, fp);
81 if (!rc)
82 {
83 PD_ERR(__pd_read, "Failed to read file: %s\n", path);
84 }
85 else
86 {
87 buf[++rc] = '\0';
88 strtok(buf, "\n");
89 }
90 fclose(fp);
91 }
92 /* trim newline */
93 return !rc;
94}
95
96int pd_read(struct pd_path *p, char *buf, int len)
97{
98 return __pd_read(p->buf, buf, len);
99}
100
101int __pd_read_bool(char *path, bool *ans)
102{
103 int ret, tmp;
104 char buf[4];
105 if ((ret = __pd_read(path, buf, 4)))
106 return ret;
107
108 sscanf(buf, "%d", &tmp);
109
110 if (tmp)
111 *ans = true;
112 else
113 *ans = false;
114 return 0;
115}
116
117int pd_read_bool(struct pd_path *p, bool *ans)
118{
119 return __pd_read_bool(p->buf, ans);
120}
121
122int pd_file_exists(struct pd_path *p)
123{
124 return (access(p->buf, F_OK) != -1);
125}
126
127int validate_ip(const char *addr, int family)
128{
129 unsigned char buf[sizeof(struct in6_addr)];
130
131 if (family != AF_INET6 && family != AF_INET)
132 return 1;
133
134 return inet_pton(family, addr, buf) != 1;
135}
136
137int ipv6_split_pref(const char *ip6addr, char *addr_buf, char *pref_buf)
138{
139 char *del_ptr;
140 int del_idx;
141
142 if (!ip6addr)
143 return 1;
144
145 del_ptr = strchr(ip6addr, '/');
146
147 if (!del_ptr)
148 return 1;
149
150 strcpy(pref_buf, del_ptr + 1);
151 del_idx = (int)(del_ptr - ip6addr);
152
153 strncpy(addr_buf, ip6addr, del_idx);
154 addr_buf[del_idx] = '\0';
155
156 return 0;
157}
158
159int validate_prefixlen(const char *preflen)
160{
161 int prefixlen = atoi(preflen);
162 return (prefixlen < 0 || prefixlen > 128);
163}
164
165int str_starts_with(const char *str, const char *pre)
166{
167 int prelen = strlen(pre);
168 int strelen = strlen(str);
169
170 if (strelen < prelen)
171 return 0;
172 return strncmp(pre, str, prelen) == 0;
173}
174
175int ipv4_change_oct(int oct)
176{
177 return oct ^ 0xFF;
178}
179
180void ipv4_get_alias(const char *ip1, char *ip2)
181{
182 int ip[4];
183 if (sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]) != 4 ) {
184 strcpy(ip2, EMPTY_IPV4);
185 return;
186 }
187 if (ip[3] != 255 && ip[3] != 0)
188 sprintf(ip2, IPV4_FMT, ip[0], ip[1], ip[2], ipv4_change_oct(ip[3]));
189 else {
190 if (ip[2] != 255 && ip[2] != 0)
191 sprintf(ip2, IPV4_FMT, ip[0], ip[1], ipv4_change_oct(ip[2]), ipv4_change_oct(ip[3]));
192 else
193 sprintf(ip2, IPV4_FMT, ip[0], ipv4_change_oct(ip[1]), ipv4_change_oct(ip[2]), ipv4_change_oct(ip[3]));
194 }
195
196}
197
198int ipv4_get_last_oct(const char *ip1)
199{
200 int ip[4];
201 sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]);
202 return ip[3];
203}
204
205int ipv4_get_third_oct(const char *ip1)
206{
207 int ip[4];
208 sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]);
209 return ip[2];
210}
211
212int ipv4_get_second_oct(const char *ip1)
213{
214 int ip[4];
215 sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]);
216 return ip[1];
217}
218