diff options
-rw-r--r-- | common.c | 34 | ||||
-rw-r--r-- | common.h | 3 | ||||
-rw-r--r-- | lease.c | 20 | ||||
-rw-r--r-- | wg-dynamic-client.c | 14 |
4 files changed, 29 insertions, 42 deletions
@@ -39,15 +39,16 @@ static void request_ip(enum wg_dynamic_key key, union kvalues kv, void **dest) fatal("calloc()"); break; - case WGKEY_IPV4: - memcpy(&r->ipv4, &ip->ip4, sizeof r->ipv4); - r->cidrv4 = ip->cidr; - r->has_ipv4 = true; - break; - case WGKEY_IPV6: - memcpy(&r->ipv6, &ip->ip6, sizeof r->ipv6); - r->cidrv6 = ip->cidr; - r->has_ipv6 = true; + case WGKEY_IP: + if (ip->family == AF_INET) { + memcpy(&r->ipv4, &ip->ip4, sizeof r->ipv4); + r->cidrv4 = ip->cidr; + r->has_ipv4 = true; + } else { + memcpy(&r->ipv6, &ip->ip6, sizeof r->ipv6); + r->cidrv6 = ip->cidr; + r->has_ipv6 = true; + } break; case WGKEY_LEASESTART: r->start = kv.u32; @@ -80,12 +81,6 @@ static bool parse_ip_cidr(struct wg_combined_ip *ip, char *value) char *endptr; char *sep; - if (value[0] == '\0') { - memset(ip, 0, ip->family == AF_INET ? 4 : 16); - ip->cidr = 0; - return true; - } - sep = strchr(value, '/'); if (!sep) return false; @@ -111,10 +106,9 @@ static bool parse_value(enum wg_dynamic_key key, char *str, union kvalues *kv) struct wg_combined_ip *ip; switch (key) { - case WGKEY_IPV4: - case WGKEY_IPV6: + case WGKEY_IP: ip = &kv->ip; - ip->family = (key == WGKEY_IPV4) ? AF_INET : AF_INET6; + ip->family = strchr(str, ':') ? AF_INET6 : AF_INET; if (!parse_ip_cidr(ip, str)) return false; @@ -329,14 +323,14 @@ size_t serialize_request_ip(bool send, char *buf, size_t len, if (!inet_ntop(AF_INET, &rip->ipv4, addrbuf, sizeof addrbuf)) fatal("inet_ntop()"); - print_to_buf(buf, len, &off, "ipv4=%s/32\n", addrbuf); + print_to_buf(buf, len, &off, "ip=%s/32\n", addrbuf); } if (rip->has_ipv6) { if (!inet_ntop(AF_INET6, &rip->ipv6, addrbuf, sizeof addrbuf)) fatal("inet_ntop()"); - print_to_buf(buf, len, &off, "ipv6=%s/128\n", addrbuf); + print_to_buf(buf, len, &off, "ip=%s/128\n", addrbuf); } if (rip->start && rip->leasetime) @@ -29,8 +29,7 @@ static const uint16_t WG_DYNAMIC_PORT = 970; /* ASCII sum of "wireguard" */ E(WGKEY_REQUEST_IP, "request_ip") \ E(WGKEY_ENDCMD, "") \ /* CMD END */ \ - E(WGKEY_IPV4, "ipv4") \ - E(WGKEY_IPV6, "ipv6") \ + E(WGKEY_IP, "ip") \ E(WGKEY_LEASESTART, "leasestart") \ E(WGKEY_LEASETIME, "leasetime") \ E(WGKEY_ERRNO, "errno") \ @@ -194,8 +194,8 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, const struct in_addr *ipv4, const struct in6_addr *ipv6) { - bool delete_ipv4 = ipv4 && !ipv4->s_addr; - bool delete_ipv6 = ipv6 && IN6_IS_ADDR_UNSPECIFIED(ipv6); + bool delete_ipv4 = !ipv4 || (ipv4 && !ipv4->s_addr); + bool delete_ipv6 = !ipv6 || (ipv6 && IN6_IS_ADDR_UNSPECIFIED(ipv6)); struct wg_dynamic_lease *lease; struct timespec tp; khiter_t k; @@ -207,23 +207,19 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, lease->lladdr = *lladdr; } - if (lease->ipv4.s_addr && - (delete_ipv4 || - (ipv4 && memcmp(&lease->ipv4, ipv4, sizeof(*ipv4))))) { + if (delete_ipv4 && lease->ipv4.s_addr) { if (ipp_del_v4(&ipns, &lease->ipv4, 32)) die("ipp_del_v4()\n"); memset(&lease->ipv4, 0, sizeof(lease->ipv4)); } - if (!IN6_IS_ADDR_UNSPECIFIED(&lease->ipv6) && - (delete_ipv6 || - (ipv6 && memcmp(&lease->ipv6, ipv6, sizeof(*ipv6))))) { + if (delete_ipv6 && !IN6_IS_ADDR_UNSPECIFIED(&lease->ipv6)) { if (ipp_del_v6(&ipns, &lease->ipv6, 128)) die("ipp_del_v6()\n"); memset(&lease->ipv6, 0, sizeof(lease->ipv6)); } - if (!ipv4) { /* Wants random IPv4 address? */ + if (ipv4 && !ipv4->s_addr) { if (!ipns.total_ipv4) { debug("IPv4 pool empty\n"); memset(&lease->ipv4, 0, sizeof(lease->ipv4)); @@ -233,7 +229,7 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, ipns.total_ipv4); ipp_addnth_v4(&ipns, &lease->ipv4, index); } - } else if (ipv4->s_addr) { + } else if (ipv4) { if (!memcmp(&lease->ipv4, ipv4, sizeof(*ipv4))) { debug("extending(v4)\n"); } else { @@ -245,7 +241,7 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, } } - if (!ipv6) { /* Wants random IPv6 address? */ + if (ipv6 && IN6_IS_ADDR_UNSPECIFIED(ipv6)) { if (!ipns.totalh_ipv6 && !ipns.totall_ipv6) { debug("IPv6 pool empty\n"); memset(&lease->ipv6, 0, sizeof(lease->ipv6)); @@ -264,7 +260,7 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, index_l, ipns.totalh_ipv6, ipns.totall_ipv6); ipp_addnth_v6(&ipns, &lease->ipv6, index_l, index_h); } - } else if (!IN6_IS_ADDR_UNSPECIFIED(ipv6)) { + } else if (ipv6) { if (!memcmp(&lease->ipv6, ipv6, sizeof(*ipv6))) { debug("extending(v6)\n"); } else { diff --git a/wg-dynamic-client.c b/wg-dynamic-client.c index 2edb413..7284e3b 100644 --- a/wg-dynamic-client.c +++ b/wg-dynamic-client.c @@ -42,9 +42,9 @@ static void usage() static void cleanup() { if (ipv4_assigned && ipm_deladdr_v4(device->ifindex, &ipv4)) - debug("Failed to cleanup ipv4 address"); + debug("Failed to cleanup ipv4 address\n"); if (ipv6_assigned && ipm_deladdr_v6(device->ifindex, &ipv6)) - debug("Failed to cleanup ipv6 address"); + debug("Failed to cleanup ipv6 address\n"); if (sockfd >= 0) close(sockfd); @@ -111,14 +111,12 @@ static int request_ip(struct wg_dynamic_request_ip *rip) if (connect(sockfd, (struct sockaddr *)&dstaddr, sizeof(dstaddr))) fatal("connect()"); - if (ipv4_assigned) { + rip->has_ipv4 = rip->has_ipv6 = true; + if (ipv4_assigned) memcpy(&rip->ipv4, &ipv4, sizeof rip->ipv4); - rip->has_ipv4 = true; - } - if (ipv6_assigned) { + + if (ipv6_assigned) memcpy(&rip->ipv6, &ipv6, sizeof rip->ipv6); - rip->has_ipv6 = true; - } msglen = serialize_request_ip(true, (char *)buf, RECV_BUFSIZE, rip); do { |