From patchwork Thu Sep 5 09:31:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Bilal_Hatipo=C4=9Flu?= X-Patchwork-Id: 1158319 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="X995NVCu"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Jabyxva5"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46PFn22rRvz9s4Y for ; Thu, 5 Sep 2019 19:31:18 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SbTm8UxkVkXk2cxM2d9j/OJ10pw9x0WFGau0LCBX8io=; b=X995NVCuFZhDkb dNLa/pbZU0yuNwrEMSCaY5LPvF2nzLwNPXAFFuB1zln5uiaoVCw+Wwz47otPW3x0BbvKWwz5Sg3vt 5GbhbhaKqWx2Ulq7RI9rbNp5UKHA1571mKHNRrUQmzsyoytP14LD1iwMYyUSq7ZO9XsA+baFJr7jg YMa0t532mqCrdzg7sqICtJcYCwavzbWsfGLuWgPSgC/onCZRvI6IcK/A36qgfJxHhBHIbEhgPONzR gx2avTmEb5j8VI1248qeaDrek0LSkAzgMUXWpLOowfQawkDUyyXJ6wjBYgzOsicOywAarT6ahLud2 wSUZewu8AE71wKFZaSRg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1i5o6k-0002ND-MN; Thu, 05 Sep 2019 09:31:14 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1i5o6g-0002L2-AN for hostap@lists.infradead.org; Thu, 05 Sep 2019 09:31:11 +0000 Received: by mail-wr1-x444.google.com with SMTP id u16so1909554wrr.0 for ; Thu, 05 Sep 2019 02:31:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=so9lYjcTQftF9a2TEd6siSFvjnCPJuzjyQvSbAjf58g=; b=Jabyxva5wIiHpFln4a0F+VzAEk95O28WAax9qOAku7qc4vUO/lY/WI7p0atyUimpb3 1SV0qWV3a3vpi2rUvUU5sz0QPHtY6SN9aangTFEOv3VzIlTAaiq/xnn6b8mQilyxKQP2 WG3ZYYCIM70m+tpVkX6IwIiUo46BIfZ2zkYwO/V2NdisrmylE7tBWfCNEkjFRxvg1xzj 2SZUPJvG9ILdnIQT44qlTZpnEsfZZOO1wCvFavoFXL/thk3ABMf77ijsD3VOYvin6LmE Fml22J+CzD+tZKRu5dUVhudhgia6mliUJyz3QUv0HexgTcsg47vzBrSMStNdhLmlBmOV 7OEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=so9lYjcTQftF9a2TEd6siSFvjnCPJuzjyQvSbAjf58g=; b=bEsuewu9FR5NCFwQK4oRo6gQUVVy/cfKpFx50UjgPZbcyBFiCRvQeZXEf5oiGFiWrJ SkhUozkAMqxcWbleoG0z3w4efJs1UtzLIBZxIkePv2VHVYfPKG87w+piCROjw/X2+Rrj 9RmIrT67qoCDJcZRROHa816EylwCadf9XYErPCoImu4SwZFoH5ry/+t6RtPHxkPccDmL ZdphJ2FB30UVlaQwLrfsjWaBUccpLFQqwPGjAwKyF0aXVJadgZCsw0ylsZ0LsK+f/uGp HN696txPjkuGU3toXPQtARe/uv4cLiNXLq+FFsfPzA7ELqFnVhT/7/5v1r3/hzXvsvcm CWoQ== X-Gm-Message-State: APjAAAWLxZNWvoXAIMIy0OIXSPWFg6wnrLb0LXFPYUTPLzHU9vGcb6Uq tDzGj9t+8LrvNGC5OcPPhcAL3yWjEdE= X-Google-Smtp-Source: APXvYqxvEjzm/RSAXL41bWkoP91z94mEY3PGnm1YaW0DaSy7AwVHvG6xDoVwbQxgWlItwlSOY6nJwA== X-Received: by 2002:a05:6000:128e:: with SMTP id f14mr1285800wrx.28.1567675866105; Thu, 05 Sep 2019 02:31:06 -0700 (PDT) Received: from bilal-desktop.test.airties.com ([91.220.65.2]) by smtp.gmail.com with ESMTPSA id z189sm2830162wmc.25.2019.09.05.02.31.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 02:31:05 -0700 (PDT) From: bilalhp@gmail.com To: hostap@lists.infradead.org Subject: [PATCH v2] Add "reconnect" cmdline argument to hostapd_cli/wpa_cli Date: Thu, 5 Sep 2019 12:31:00 +0300 Message-Id: <1567675860-19823-1-git-send-email-bilalhp@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1567520371-24875-1-git-send-email-bilal.hatipoglu@airties.com> References: <1567520371-24875-1-git-send-email-bilal.hatipoglu@airties.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190905_023110_391276_9604E46F X-CRM114-Status: GOOD ( 17.34 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:444 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (bilalhp[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Bilal Hatipoglu When newly added "-r" parameter is used, both clis will try to reconnect forever on connection lost until signalled (ctrl+c) or terminated. This is useful only when used with -a to take action to retrieve events or get status and the cli process stays even if hostapd/wpa_supplicant daemons restart for some reason (e.g. configuration change). Signed-off-by: Veli Demirel Signed-off-by: Bilal Hatipoglu Signed-off-by: Veli Demirel Signed-off-by: Bilal Hatipoglu --- hostapd/hostapd_cli.c | 25 +++++++++++++++++++------ wpa_supplicant/wpa_cli.c | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c index 0460243..83ce6f1 100644 --- a/hostapd/hostapd_cli.c +++ b/hostapd/hostapd_cli.c @@ -54,7 +54,7 @@ static void usage(void) fprintf(stderr, "%s\n", hostapd_cli_version); fprintf(stderr, "\n" - "usage: hostapd_cli [-p] [-i] [-hvB] " + "usage: hostapd_cli [-p] [-i] [-hvBr] " "[-a] \\\n" " [-P] [-G] [command..]\n" "\n" @@ -68,6 +68,9 @@ static void usage(void) " -a run in daemon mode executing the action file " "based on events\n" " from hostapd\n" + " -r try to reconnect when client socket is " + "disconnected.\n" + " this is useful only when used with -a\n" " -B run a daemon in the background\n" " -i Interface to listen on (default: first " "interface found in the\n" @@ -2011,12 +2014,13 @@ int main(int argc, char *argv[]) int warning_displayed = 0; int c; int daemonize = 0; + int reconnect = 0; if (os_program_init()) return -1; for (;;) { - c = getopt(argc, argv, "a:BhG:i:p:P:s:v"); + c = getopt(argc, argv, "a:BhG:i:p:P:s:vr"); if (c < 0) break; switch (c) { @@ -2048,6 +2052,9 @@ int main(int argc, char *argv[]) case 's': client_socket_dir = optarg; break; + case 'r': + reconnect = 1; + break; default: usage(); return -1; @@ -2087,8 +2094,7 @@ int main(int argc, char *argv[]) printf("Connection established.\n"); break; } - - if (!interactive) { + if (!interactive && !reconnect) { perror("Failed to connect to hostapd - " "wpa_ctrl_open"); return -1; @@ -2106,8 +2112,15 @@ int main(int argc, char *argv[]) return -1; if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue()) return -1; - - if (interactive) + if (reconnect && action_file && ctrl_ifname != NULL) { + do { + if (ctrl_conn) { + hostapd_cli_action(ctrl_conn); + } + os_sleep(1, 0); + hostapd_cli_reconnect(ctrl_ifname); + } while(!hostapd_cli_quit); + } else if (interactive) hostapd_cli_interactive(); else if (action_file) hostapd_cli_action(ctrl_conn); diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c index 43ac427..7e1e59b 100644 --- a/wpa_supplicant/wpa_cli.c +++ b/wpa_supplicant/wpa_cli.c @@ -52,6 +52,7 @@ static char *ctrl_ifname = NULL; static const char *global = NULL; static const char *pid_file = NULL; static const char *action_file = NULL; +static int reconnect = 0; static int ping_interval = 5; static int interactive = 0; static char *ifname_prefix = NULL; @@ -80,7 +81,7 @@ static void update_ifnames(struct wpa_ctrl *ctrl); static void usage(void) { - printf("wpa_cli [-p] [-i] [-hvB] " + printf("wpa_cli [-p] [-i] [-hvBr] " "[-a] \\\n" " [-P] [-g] [-G] " "\\\n" @@ -91,6 +92,8 @@ static void usage(void) " -a = run in daemon mode executing the action file based on " "events from\n" " wpa_supplicant\n" + " -r = try to reconnect when client socket is disconnected.\n" + " this is useful only when used with -a\n" " -B = run a daemon in the background\n" " default path: " CONFIG_CTRL_IFACE_DIR "\n" " default interface: first interface found in socket path\n"); @@ -4039,7 +4042,9 @@ static void wpa_cli_action_process(const char *msg) wpa_cli_exec(action_file, ifname, pos); } else if (str_starts(pos, WPA_EVENT_TERMINATING)) { printf("wpa_supplicant is terminating - stop monitoring\n"); - wpa_cli_quit = 1; + if (!reconnect) { + wpa_cli_quit = 1; + } } } @@ -4231,6 +4236,10 @@ static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor) if (wpa_ctrl_pending(ctrl) < 0) { printf("Connection to wpa_supplicant lost - trying to " "reconnect\n"); + if (reconnect) { + eloop_terminate(); + return; + } wpa_cli_reconnect(); } } @@ -4578,6 +4587,9 @@ static void wpa_cli_cleanup(void) static void wpa_cli_terminate(int sig, void *ctx) { eloop_terminate(); + if (reconnect) { + wpa_cli_quit = 1; + } } @@ -4655,7 +4667,7 @@ int main(int argc, char *argv[]) return -1; for (;;) { - c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:v"); + c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:vr"); if (c < 0) break; switch (c) { @@ -4690,6 +4702,9 @@ int main(int argc, char *argv[]) case 's': client_socket_dir = optarg; break; + case 'r': + reconnect = 1; + break; default: usage(); return -1; @@ -4712,7 +4727,24 @@ int main(int argc, char *argv[]) if (ctrl_ifname == NULL) ctrl_ifname = wpa_cli_get_default_ifname(); - if (interactive) { + if (reconnect && action_file && ctrl_ifname) { + do { + if (ctrl_conn) { + wpa_cli_action(ctrl_conn); + } else { + os_sleep(1, 0); + } + wpa_cli_close_connection(); + wpa_cli_open_connection(ctrl_ifname, 0); + if (ctrl_conn) { + if (wpa_ctrl_attach(ctrl_conn) != 0) { + wpa_cli_close_connection(); + } else { + wpa_cli_attached = 1; + } + } + } while (!wpa_cli_quit); + } else if (interactive) { wpa_cli_interactive(); } else { if (!global &&