From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id MFrvDeoBumQOIgEASxT56A (envelope-from ) for ; Fri, 21 Jul 2023 05:56:26 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id yNv+DOoBumSKAQEAG6o9tA (envelope-from ) for ; Fri, 21 Jul 2023 05:56:26 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C04475F633 for ; Fri, 21 Jul 2023 05:56:25 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=kt4V+cBR; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1689911786; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:in-reply-to:in-reply-to:references:references: list-id:list-help:list-unsubscribe:list-subscribe:list-post: dkim-signature; bh=uzLTRLZxR7RKvBN/rjLCCzZg83X5Vgc+3OQXOXn7UsU=; b=jRM1Ecy/mEUH+14lygCbSdrODw5HMgVAywQ9SgMdq1absQbHtGNGdfbKzXMaW+JY/WbayM o2Fdt9sI+mkZv4l5SZxiofzKnxqdZIpx7BjvVbVKd1j/+MUi5Q+NvoAaVD1ehEQMAa3f/G ZzhmxWOL9QIImPQ68NxnOMZIa8bAvZNQFDhs3e5eGZmsKuX8jMxw1M8Khy1V2HSZ5MSgNB Hlpk4F3H2YS9HYFTtbfGjtPh5QrD3FauawS0Hn38GW3XBQfisglGYdZcKPGOLKJNCA1igI 48S7o0de+XgRL3LCQAa03Sa0N9tLHpRXo08NnE0M3HgRm0UvQZ5LyvkOHKWp/A== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1689911786; a=rsa-sha256; cv=none; b=PNodeo4MLv3EaxqCPJS/Y2AKauKKldCPzE2S9JajjCxMMqKDqaBfuovDQzqF5bLJarKy4b 4UmunrUP2GcNHXiCfKDnNNeVVo3ZuBiqCrFv9a6+GGOqTSxt2QtQR9FH8slAvNDEGWx7m6 17BwLsdFM7Y2sbJdo2mA/ffMfLD0DaFUUI3yQ+baQzn6h125ufE9CgxQY4eQF9MWjglTuZ lQGUJHnDH1u5jqG7Qc/PmBEzM5iPN+nV7UJk+sijqygHxA7IqDHdVbDVb0Khiq4vu1Rpki jfefrlrNuMGC9mjMuCfJzotfmonj6Ff2ZMBKkZRV4wd69TKS269EE3+eCn/ESA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=kt4V+cBR; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qMhFN-0003jZ-TP; Thu, 20 Jul 2023 23:56:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qMhFK-0003jI-M3 for guix-patches@gnu.org; Thu, 20 Jul 2023 23:56:02 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qMhFK-0007PO-C5 for guix-patches@gnu.org; Thu, 20 Jul 2023 23:56:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qMhFJ-0006yx-SC for guix-patches@gnu.org; Thu, 20 Jul 2023 23:56:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v5 2/5] services: wireguard: Implement a dynamic IP monitoring feature. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 21 Jul 2023 03:56:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Bruno Victal Cc: 63402@debbugs.gnu.org Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.168991173226784 (code B ref 63402); Fri, 21 Jul 2023 03:56:01 +0000 Received: (at 63402) by debbugs.gnu.org; 21 Jul 2023 03:55:32 +0000 Received: from localhost ([127.0.0.1]:60311 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMhEp-0006xt-FC for submit@debbugs.gnu.org; Thu, 20 Jul 2023 23:55:32 -0400 Received: from mail-qv1-xf2a.google.com ([2607:f8b0:4864:20::f2a]:54775) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMhEk-0006xX-FE for 63402@debbugs.gnu.org; Thu, 20 Jul 2023 23:55:29 -0400 Received: by mail-qv1-xf2a.google.com with SMTP id 6a1803df08f44-63719ad32e7so11127776d6.1 for <63402@debbugs.gnu.org>; Thu, 20 Jul 2023 20:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689911720; x=1690516520; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=uzLTRLZxR7RKvBN/rjLCCzZg83X5Vgc+3OQXOXn7UsU=; b=kt4V+cBRdE5zplmZvD3n65oa+Fvwa8AMxAQ2Sr06K3CluPB+m/LKPyp0sRHpS0hh4f kRYRseW4yx9KHIO2QgvcvLr5di+V+cz2fbOs9nb2FXCmztgWwQjM8bNeTwoSIvHPG5kp 9Dkh6AYN16Yz+5kn/+jpQJhO2E+vS3DmPpnnrh67tsWih71yYAM3EFJlFnEqS3LlGKAg gbPfLKCln9UIfZEJOL5hReBTb6Orz3v9NP8vZMboz0+5uppH4KtG6FW22tioegqQpm/n fWsepV1qEp5Pblh7VapvJymtEaj587c4Kx9v/M3c4j5RqMxjukZWcBg/XMHtBbZSDzyi fs4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689911720; x=1690516520; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=uzLTRLZxR7RKvBN/rjLCCzZg83X5Vgc+3OQXOXn7UsU=; b=a4spru2I+GZvV1cF3VNxFy4roojeIbDRLLkUsMBKKnCu1wgERA/bkXqwegxfnrNoZl GUfpQpXr1ZB2e1uLeVEwpgkY6tD4WeL+ViVewaIJVxjPh6UCrXkJJLn3Bf7y/s+OR8Iv 57dQByphMtqEXHClI6R8V08h2mXDumBagHaFQ6uEojKo1l89+egQtscolRZ2ItiPzZcW cBplp86eRroGdfZcw0SS2ZnLdz1aagPixKUAjq7U3m23aABMikAXz+EVeuzIkrShqhHS XLOjRlW4NLh1CvbHIwXE4ANd6hINew3u8ZSXawZN3sv3ltE9/H30UsQp25lfzRm1ccmq SKdg== X-Gm-Message-State: ABy/qLZZzx8VJt8CpTWU4R0av/2A9j98sLOr+QQJTbniXnwEfKmdnyJI U5qRKzMQ4uMXWtFpwUWZuO+UCi38YWo= X-Google-Smtp-Source: APBJJlGsTI9r8KzEtfbG05t9a+MxuPq5jY8X0wkIHC8tqCmpWstAwX5dbwlcJqASWbSKFM9c3U2Ngw== X-Received: by 2002:a0c:df04:0:b0:635:fa7b:6c1f with SMTP id g4-20020a0cdf04000000b00635fa7b6c1fmr911238qvl.62.1689911720353; Thu, 20 Jul 2023 20:55:20 -0700 (PDT) Received: from hurd (dsl-159-145.b2b2c.ca. [66.158.159.145]) by smtp.gmail.com with ESMTPSA id k2-20020a0c9702000000b0063612e03433sm957068qvd.101.2023.07.20.20.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 20:55:19 -0700 (PDT) From: Maxim Cournoyer References: Date: Thu, 20 Jul 2023 23:55:18 -0400 In-Reply-To: (Bruno Victal's message of "Wed, 24 May 2023 18:25:27 +0100") Message-ID: <87o7k5kjfd.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -5.06 X-Spam-Score: -5.06 X-Migadu-Queue-Id: C04475F633 X-Migadu-Scanner: mx1.migadu.com X-TUID: rJhAzEQH/xH6 Hi Bruno, Bruno Victal writes: > On 2023-05-19 02:59, Maxim Cournoyer wrote: >> +;;; XXX: Copied from (guix scripts pack), changing define to define*. >> +(define-syntax-rule (define-with-source (variable args ...) body body* ...) >> + "Bind VARIABLE to a procedure accepting ARGS defined as BODY, also setting >> +its source property." >> + (begin >> + (define* (variable args ...) >> + body body* ...) >> + (eval-when (load eval) >> + (set-procedure-property! variable 'source >> + '(define* (variable args ...) body body* ...))))) >> + >> +(define (wireguard-service-name interface) >> + "Return the WireGuard service name (a symbol) configured to use INTERFACE." >> + (symbol-append 'wireguard- (string->symbol interface))) >> + >> +(define-with-source (strip-port/maybe endpoint #:key ipv6?) >> + "Strip the colon and port, if present in ENDPOINT, a string." >> + (if ipv6? >> + (if (string-prefix? "[" endpoint) >> + (first (string-split (string-drop endpoint 1) #\])) ;ipv6 >> + endpoint) >> + (first (string-split endpoint #\:)))) ;ipv4 > > [...] > >> + >> +(define (ipv4-address? str) >> + "Return true if STR denotes an IPv4 address." >> + (false-if-exception >> + (->bool (inet-pton AF_INET (strip-port/maybe str))))) > > [...] > >> + >> +(define (ipv6-address? str) >> + "Return true if STR denotes an IPv6 address." >> + (false-if-exception >> + (->bool (inet-pton AF_INET6 (strip-port/maybe str #:ipv6? #t))))) > > You should use getaddrinfo instead, reason being that inet-pton does > not work with zone-indexes or interface names in IPv6 addresses. > I expect that this snippet would get cloned and reused often which > makes it important to get it right even if zone-indexes don't happen > to be of particular interest here. > > I have this snippet that you could adapt to your liking (or use as-is): > > (define* (ip-address? s #:optional family) > "Check if @var{s} is a valid IP address. It optionally accepts a > @var{family} argument, either AF_INET or AF_INET6, which can be used > to exclusively check for IPv4 or IPv6 addresses." > ;; Regrettably square brackets aren't accepted by getaddrinfo() and > ;; must be removed beforehand. > (let ((address (string-trim-both s (char-set #\[ #\]))) > (false-if-exception > (->bool (getaddrinfo address #f AI_NUMERICHOST family)))))) > > > I'd also harmonize the ipv4 check to use getaddrinfo in case you > specialize the snippet above for IPv6 only. (keeps things simpler) Thanks! I've adapted as: --8<---------------cut here---------------start------------->8--- modified gnu/services/vpn.scm @@ -903,15 +903,17 @@ (define-with-source (strip-port/maybe endpoint #:key ipv6?) endpoint) (first (string-split endpoint #\:)))) ;ipv4 -(define (ipv4-address? str) - "Return true if STR denotes an IPv4 address." - (false-if-exception - (->bool (inet-pton AF_INET (strip-port/maybe str))))) - -(define (ipv6-address? str) - "Return true if STR denotes an IPv6 address." - (false-if-exception - (->bool (inet-pton AF_INET6 (strip-port/maybe str #:ipv6? #t))))) +(define* (ipv4-address? address) + "Predicate to check whether ADDRESS is a valid IPv4 address." + (let ((address (strip-port/maybe address))) + (false-if-exception + (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET))))) + +(define* (ipv6-address? address) + "Predicate to check whether ADDRESS is a valid IPv6 address." + (let ((address (strip-port/maybe address #:ipv6? #t))) + (false-if-exception + (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET6))))) (define (host-name? name) "Predicate to check whether NAME is a host name, i.e. not an IP address." --8<---------------cut here---------------end--------------->8--- Since there's some local considerations weaved in (strip-port/maybe), I think it's fine that these live in the vpn.scm module. When need be, we can refactor a more general version and find a suitable home for it. >> + >> +(define (host-name? name) >> + "Predicate to check whether NAME is a host name, i.e. not an IP address." >> + (not (or (ipv6-address? name) (ipv4-address? name)))) > > I'd craft an artificial uri string and extract this information from a uri > record instead, since the above check is likely to reveal insufficient: > > scheme@(guile-user)> (use-modules (web uri)) > scheme@(guile-user)> (define s "example.tld:9999") > scheme@(guile-user)> (uri-host (string->uri (string-append "dummy://" s))) > $5 = "example.tld" > scheme@(guile-user)> (define s "[2001:db8::1234]:9999") > scheme@(guile-user)> (uri-host (string->uri (string-append "dummy://" s))) > $6 = "2001:db8::1234" I'm not sure I understand; In the second case, I'd like it to tell me it's *not* a host name, but it seems like uri-host happily returns IP addresses the same as host names? [...] >> +(define endpoint-host-names >> + (@@ (gnu services vpn) endpoint-host-names)) >> + >> +(test-begin "vpn-services") >> + >> +(test-assert "ipv4-address?" >> + (every ipv4-address? >> + (list "192.95.5.67:1234" >> + "10.0.0.1"))) >> + >> +(test-assert "ipv6-address?" >> + (every ipv6-address? >> + (list "[2607:5300:60:6b0::c05f:543]:2468" >> + "2607:5300:60:6b0::c05f:543" >> + "2345:0425:2CA1:0000:0000:0567:5673:23b5" >> + "2345:0425:2CA1::0567:5673:23b5"))) > > Are these addresses special? > If not, I'd recommend (properly) generating a random ULA prefix > and use it instead. They are not! I derived them from actual IP addresses, adding some fuzz. I've now used unique local IPv6 prefixes. >> + >> +(define %wireguard-peers >> + (list (wireguard-peer >> + (name "dummy1") >> + (public-key "VlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XjoalC8=") >> + (endpoint "some.dynamic-dns.service:53281") >> + (allowed-ips '())) >> + (wireguard-peer >> + (name "dummy2") >> + (public-key "AlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC9=") >> + (endpoint "example.org") >> + (allowed-ips '())) >> + (wireguard-peer >> + (name "dummy3") >> + (public-key "BlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC7=") >> + (endpoint "10.0.0.7:7777") >> + (allowed-ips '())) >> + (wireguard-peer >> + (name "dummy4") >> + (public-key "ClesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC6=") >> + (endpoint "[2345:0425:2CA1::0567:5673:23b5]:44444") >> + (allowed-ips '())))) >> + >> +(test-equal "endpoint-host-names" >> + '(("VlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XjoalC8=" . >> + "some.dynamic-dns.service:53281") >> + ("AlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC9=" . >> + "example.org")) > > I think a comment that explains where these values were obtained from > (or how they were generated) would be helpful for anyone looking at this > in the future. OK, I've now added a comment. -- Thanks, Maxim