| |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <unistd.h> |
| #include <stdlib.h> |
| #include <errno.h> |
| #include <stdio.h> |
| #include <fcntl.h> |
| #include <string.h> |
| |
| #include "piped.h" |
| #include "piped_util.h" |
| #include "piped_uci.h" |
| |
| void pd_path_init(struct pd_path *p) |
| { |
| strcpy(p->buf, MPIPE_PATH); |
| p->iptr = p->buf + sizeof(MPIPE_PATH) - 1; |
| p->fptr = NULL; |
| } |
| |
| void pd_path_set_iface(struct pd_path *p, const char *ifname) |
| { |
| strcpy(p->iptr, ifname); |
| strcat(p->iptr, "/"); |
| p->fptr = p->iptr + strlen(ifname) + 1; |
| } |
| |
| void pd_path_set_file(struct pd_path *p, const char *fname) |
| { |
| strcpy(p->fptr, fname); |
| } |
| |
| int __pd_write_str(char *path, const char *s) |
| { |
| int fd = open(path, O_WRONLY); |
| |
| if (fd == -1) { |
| PD_ERR(__pd_write_str, "Failed to open file: %s\n", path); |
| return 1; |
| } |
| |
| if (write(fd, s, strlen(s)) == -1) { |
| PD_ERR(__pd_write_str1, "Failed to write to file: %s\n", path); |
| return 1; |
| } |
| |
| if (close(fd)) { |
| PD_ERR(__pd_write_str2, "Failed to close file: %s\n", path); |
| return 1; |
| } |
| |
| return 0; |
| } |
| |
| int pd_write_str(struct pd_path *p, const char *s) |
| { |
| return __pd_write_str(p->buf, s); |
| } |
| |
| int __pd_write_bool(char *buf, bool b) |
| { |
| char s[2]; |
| s[0] = b ? '1' : '0'; |
| s[1] = '\0'; |
| |
| return __pd_write_str(buf, s); |
| } |
| |
| int pd_write_bool(struct pd_path *p, bool b) |
| { |
| return __pd_write_bool(p->buf, b); |
| } |
| |
| int __pd_read(char *path, char *buf, int len) |
| { |
| FILE *fp = fopen(path, "r"); |
| size_t rc = 0; |
| if (fp != NULL) { |
| rc = fread(buf, sizeof(char), len - 1, fp); |
| if (!rc) |
| { |
| PD_ERR(__pd_read, "Failed to read file: %s\n", path); |
| } |
| else |
| { |
| buf[++rc] = '\0'; |
| strtok(buf, "\n"); |
| } |
| fclose(fp); |
| } |
| /* trim newline */ |
| return !rc; |
| } |
| |
| int pd_read(struct pd_path *p, char *buf, int len) |
| { |
| return __pd_read(p->buf, buf, len); |
| } |
| |
| int __pd_read_bool(char *path, bool *ans) |
| { |
| int ret, tmp; |
| char buf[4]; |
| if ((ret = __pd_read(path, buf, 4))) |
| return ret; |
| |
| sscanf(buf, "%d", &tmp); |
| |
| if (tmp) |
| *ans = true; |
| else |
| *ans = false; |
| return 0; |
| } |
| |
| int pd_read_bool(struct pd_path *p, bool *ans) |
| { |
| return __pd_read_bool(p->buf, ans); |
| } |
| |
| int pd_file_exists(struct pd_path *p) |
| { |
| return (access(p->buf, F_OK) != -1); |
| } |
| |
| int validate_ip(const char *addr, int family) |
| { |
| unsigned char buf[sizeof(struct in6_addr)]; |
| |
| if (family != AF_INET6 && family != AF_INET) |
| return 1; |
| |
| return inet_pton(family, addr, buf) != 1; |
| } |
| |
| int ipv6_split_pref(const char *ip6addr, char *addr_buf, char *pref_buf) |
| { |
| char *del_ptr; |
| int del_idx; |
| |
| if (!ip6addr) |
| return 1; |
| |
| del_ptr = strchr(ip6addr, '/'); |
| |
| if (!del_ptr) |
| return 1; |
| |
| strcpy(pref_buf, del_ptr + 1); |
| del_idx = (int)(del_ptr - ip6addr); |
| |
| strncpy(addr_buf, ip6addr, del_idx); |
| addr_buf[del_idx] = '\0'; |
| |
| return 0; |
| } |
| |
| int validate_prefixlen(const char *preflen) |
| { |
| int prefixlen = atoi(preflen); |
| return (prefixlen < 0 || prefixlen > 128); |
| } |
| |
| int str_starts_with(const char *str, const char *pre) |
| { |
| int prelen = strlen(pre); |
| int strelen = strlen(str); |
| |
| if (strelen < prelen) |
| return 0; |
| return strncmp(pre, str, prelen) == 0; |
| } |
| |
| int ipv4_change_oct(int oct) |
| { |
| return oct ^ 0xFF; |
| } |
| |
| void ipv4_get_alias(const char *ip1, char *ip2) |
| { |
| int ip[4]; |
| if (sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]) != 4 ) { |
| strcpy(ip2, EMPTY_IPV4); |
| return; |
| } |
| if (ip[3] != 255 && ip[3] != 0) |
| sprintf(ip2, IPV4_FMT, ip[0], ip[1], ip[2], ipv4_change_oct(ip[3])); |
| else { |
| if (ip[2] != 255 && ip[2] != 0) |
| sprintf(ip2, IPV4_FMT, ip[0], ip[1], ipv4_change_oct(ip[2]), ipv4_change_oct(ip[3])); |
| else |
| sprintf(ip2, IPV4_FMT, ip[0], ipv4_change_oct(ip[1]), ipv4_change_oct(ip[2]), ipv4_change_oct(ip[3])); |
| } |
| |
| } |
| |
| int ipv4_get_last_oct(const char *ip1) |
| { |
| int ip[4]; |
| sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]); |
| return ip[3]; |
| } |
| |
| int ipv4_get_third_oct(const char *ip1) |
| { |
| int ip[4]; |
| sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]); |
| return ip[2]; |
| } |
| |
| int ipv4_get_second_oct(const char *ip1) |
| { |
| int ip[4]; |
| sscanf(ip1, IPV4_FMT, &ip[0], &ip[1], &ip[2], &ip[3]); |
| return ip[1]; |
| } |
| |