X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=utils.c;h=e01b633c09e0143ebf98dbdb62ef77deecacaf73;hb=4789adf1e55cf51a8b556fba0c7c31bb5f430c7d;hp=6b53c229190686b69bf7d8401843bcd720dd5dbf;hpb=486aa750a164d41905beb61afec89268e3eb7f48;p=project%2Fnetifd.git diff --git a/utils.c b/utils.c index 6b53c22..e01b633 100644 --- a/utils.c +++ b/utils.c @@ -17,6 +17,11 @@ #include #include +#include + +#ifdef __APPLE__ +#include +#endif void __vlist_simple_init(struct vlist_simple_tree *tree, int offset) @@ -114,6 +119,7 @@ int parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask) { char *astr = alloca(strlen(str) + 1); + int ret = 0; strcpy(astr, str); if (!split_netmask(astr, netmask, af == AF_INET6)) @@ -127,7 +133,23 @@ parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask) return 0; } - return inet_pton(af, astr, addr); + ret = inet_pton(af, astr, addr); + if (ret > 0) { + if (af == AF_INET) { + struct in_addr *ip4_addr = (struct in_addr *)addr; + uint32_t host_addr = ntohl(ip4_addr->s_addr); + + if (IN_EXPERIMENTAL(host_addr)) { + return 0; + } + } + else if (af == AF_INET6) { + if (IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) { + return 0; + } + } + } + return ret; } char * @@ -168,3 +190,45 @@ crc32_file(FILE *fp) return c ^ 0xFFFFFFFF; } + +bool check_pid_path(int pid, const char *exe) +{ + int proc_exe_len; + int exe_len = strlen(exe); + +#ifdef __APPLE__ + char proc_exe_buf[PROC_PIDPATHINFO_SIZE]; + + proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf)); +#else + char proc_exe[32]; + char *proc_exe_buf = alloca(exe_len); + + sprintf(proc_exe, "/proc/%d/exe", pid); + proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len); +#endif + + if (proc_exe_len != exe_len) + return false; + + return !memcmp(exe, proc_exe_buf, exe_len); +} + +static const char * const uci_validate_name[__BLOBMSG_TYPE_LAST] = { + [BLOBMSG_TYPE_STRING] = "string", + [BLOBMSG_TYPE_ARRAY] = "list(string)", + [BLOBMSG_TYPE_INT32] = "uinteger", + [BLOBMSG_TYPE_BOOL] = "bool", +}; + +const char* +uci_get_validate_string(const struct uci_blob_param_list *p, int i) +{ + if (p->validate[i]) + return p->validate[i]; + + else if (uci_validate_name[p->params[i].type]) + return uci_validate_name[p->params[i].type]; + + return p->validate[BLOBMSG_TYPE_STRING]; +}