From libssh2-devel-bounces@cool.haxx.se  Wed Jan 17 21:23:21 2018
Return-Path: <libssh2-devel-bounces@cool.haxx.se>
Received: from www.haxx.se (mail [127.0.0.1])
	by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTP id w0HKMivJ030630;
	Wed, 17 Jan 2018 21:23:12 +0100
Received: from NAM01-BY2-obe.outbound.protection.outlook.com
 (mail-by2nam01on0078.outbound.protection.outlook.com [104.47.34.78])
 by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTPS id w0HKMfMj030573
 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT)
 for <libssh2-devel@cool.haxx.se>; Wed, 17 Jan 2018 21:22:42 +0100
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=meyersound2.onmicrosoft.com; s=selector1-meyersound-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version;
 bh=VWJ057SVkdFMYVVTEUZo7DRPXFnU3T0zgtOrpJLbx4I=;
 b=FxaF4A5ocT3fPRadSkPqz3zMuyW+WP3z5R0J+aSFT3vBU30i6N4DmzyJcimQwt010TNgjlIe9C7ZY2/ZEnYufzP5tCzgRSfHnPUEpDphCmgbYvKcRIiqp4Uz7eSwRxpfmOjaz/lK02BLG+8Itb24QbpJhyjaOfirgUKnf+2zWaI=
Received: from CY4PR18MB1302.namprd18.prod.outlook.com (10.172.181.149) by
 CY4PR18MB1302.namprd18.prod.outlook.com (10.172.181.149) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id
 15.20.407.7; Wed, 17 Jan 2018 20:22:37 +0000
Received: from CY4PR18MB1302.namprd18.prod.outlook.com ([10.172.181.149]) by
 CY4PR18MB1302.namprd18.prod.outlook.com ([10.172.181.149]) with mapi id
 15.20.0407.012; Wed, 17 Jan 2018 20:22:37 +0000
From: Jeremy Friesner <jeremyf@meyersound.com>
To: "libssh2-devel@cool.haxx.se" <libssh2-devel@cool.haxx.se>
Subject: How should my libssh2-based client handle gratuitous keepalives from
 the ssh server?
Thread-Topic: How should my libssh2-based client handle gratuitous keepalives
 from the ssh server?
Thread-Index: AQHTj9DqFTSG7EhtJkyDdE6rYfhcIQ==
Date: Wed, 17 Jan 2018 20:22:37 +0000
Message-ID: <E02C1C10-904F-4DF7-88B9-25156BBEF28F@meyersound.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
authentication-results: spf=none (sender IP is )
 smtp.mailfrom=jeremyf@meyersound.com; 
x-originating-ip: [104.35.164.173]
x-ms-publictraffictype: Email
x-microsoft-exchange-diagnostics: 1; CY4PR18MB1302;
 7:2kaIpcIOyrluXDREnzhxCxQFmL/n0EvQ4wNTFnv8/EMRwpw59XAZ9s/L/UcsM7Zm+WHbf71BkpsvFNmjHj0zpT9qcmqAPDoS9xE6Za3CRA+GYEjrIioVCM7NpOJlVqv24tK1nLNlcCl+v0ZElPxKznSijFmdU8RoX3wXCRPYwFVYqXh80tucI/cxnXFoeOcw+WJnHrfh8WlznlOScN3C0XL/IM5HW/6gbJPeJE/tp2VP0zEF/kYY3lP/rVH2qYTC
x-ms-exchange-antispam-srfa-diagnostics: SSOS;
x-ms-office365-filtering-correlation-id: e5999702-0d21-4c82-21b3-08d55de80cee
x-microsoft-antispam: UriScan:; BCL:0; PCL:0;
 RULEID:(7020095)(4652020)(5600026)(4604075)(3008032)(2017052603307)(7153060)(7193020);
 SRVR:CY4PR18MB1302; 
x-ms-traffictypediagnostic: CY4PR18MB1302:
x-microsoft-antispam-prvs: <CY4PR18MB13026AB7FC817C58A0732A9EDAE90@CY4PR18MB1302.namprd18.prod.outlook.com>
x-exchange-antispam-report-test: UriScan:(158342451672863)(21532816269658);
x-exchange-antispam-report-cfa-test: BCL:0; PCL:0;
 RULEID:(6040470)(2401047)(8121501046)(5005006)(3002001)(3231023)(944501161)(93006095)(93001095)(10201501046)(6041268)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123560045)(20161123564045)(20161123562045)(6072148)(201708071742011);
 SRVR:CY4PR18MB1302; BCL:0; PCL:0; RULEID:(100000803101)(100110400095);
 SRVR:CY4PR18MB1302; 
x-forefront-prvs: 0555EC8317
x-forefront-antispam-report: SFV:NSPM;
 SFS:(10009020)(366004)(396003)(376002)(346002)(39850400004)(39380400002)(53754006)(189003)(199004)(68736007)(33656002)(6116002)(3846002)(3660700001)(97736004)(99286004)(82746002)(305945005)(83716003)(2501003)(316002)(66066001)(86362001)(7736002)(3280700002)(59450400001)(478600001)(53936002)(6916009)(6486002)(2351001)(6506007)(6512007)(6436002)(5640700003)(2900100001)(81166006)(81156014)(105586002)(25786009)(5660300001)(8676002)(26005)(102836004)(2906002)(36756003)(14454004)(106356001)(8936002)(77096007);
 DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR18MB1302;
 H:CY4PR18MB1302.namprd18.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords;
 MX:1; A:1; LANG:en; 
received-spf: None (protection.outlook.com: meyersound.com does not designate
 permitted sender hosts)
x-microsoft-antispam-message-info: xm7MIJJLYb9JAGO/Lq40QSyCb9KKhz2+ey2K1PW10c+G8w5OZNdJiRlEwKv2uEeRvU/re8OQfBPSG5XmlRdUQQ==
spamdiagnosticoutput: 1:99
spamdiagnosticmetadata: NSPM
Content-ID: <E486B046933E4D42ABA4FDB0FB80D8DE@namprd18.prod.outlook.com>
MIME-Version: 1.0
X-OriginatorOrg: meyersound.com
X-MS-Exchange-CrossTenant-Network-Message-Id: e5999702-0d21-4c82-21b3-08d55de80cee
X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Jan 2018 20:22:37.0553 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: cb4b06fc-3bde-44f1-aa47-cc60a7ce81f5
X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR18MB1302
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id w0HKMfMj030573
X-BeenThere: libssh2-devel@cool.haxx.se
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: libssh2 development <libssh2-devel.cool.haxx.se>
List-Unsubscribe: <https://cool.haxx.se/cgi-bin/mailman/options/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=unsubscribe>
List-Archive: <http://cool.haxx.se/pipermail/libssh2-devel/>
List-Post: <mailto:libssh2-devel@cool.haxx.se>
List-Help: <mailto:libssh2-devel-request@cool.haxx.se?subject=help>
List-Subscribe: <https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=subscribe>
Reply-To: libssh2 development <libssh2-devel@cool.haxx.se>
Content-Type: text/plain; charset="utf-8"
Errors-To: libssh2-devel-bounces@cool.haxx.se
Sender: "libssh2-devel" <libssh2-devel-bounces@cool.haxx.se>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id w0HKMivJ030630

Hi all,

I’ve got a GUI client program that uses libssh2 to connect via SSH to a vanilla sshd server (i.e. the one that ships with Debian Linux).  Once it connects, it opens an ssh channel to send a few shell commands that collect some data from the server, and when it gets the responses back it closes the channel while keeping the SSH session open, and waits for the user to do something.  This is all implemented using the non-blocking model, with an event loop based around select().

This is all working fine (and has been working fine for a number of years), but I’ve noticed that with some newer servers we connect to, after about 15 seconds of connected idle time, the sshd server starts spontaneously sending unrequested keepalive messages to the client.  These messages are 68 bytes long each, and the server sends four of them, and then closes the connection (presumably because my client didnt’ respond to the keepalives).

If I look in the server’s /etc/ssh/sshd_config file, sure enough there is this:

ClientAliveInterval 15
ClientAliveCountMax 4

… so one work-around for the problem would probably be to change the server’s settings to disable this behavior, but I’d prefer to have a client that can handle keepalives properly, since I might not always be have permission to change settings on the server.

So my question is, how can my non-blocking client correctly handle (and respond to) these keepalive packets?  Currently it just goes into a spinloop (because select() sees the bytes available-for-read on the socket, but I don’t know what libssh2 function to call in order to absorb them).  I can recv() the bytes directly and throw them away myself, but that risks messing up libssh2’s internal state machines if I accidentally read any non-keepalive data, and in any case it doesn’t send a response back to the server, so the server will still hang up on me after a minute.  I tried calling libssh2_poll() to see if that would allow the libssh2 code to read and handle the keepalives, but that didn’t seem to have any effect.

Is there a recommended way to handle this issue?

Thanks,
Jeremy

_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

From libssh2-devel-bounces@cool.haxx.se  Sat Jan 20 01:58:23 2018
Return-Path: <libssh2-devel-bounces@cool.haxx.se>
Received: from www.haxx.se (mail [127.0.0.1])
	by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTP id w0K0vjeS026826;
	Sat, 20 Jan 2018 01:58:14 +0100
Received: from foo.stuge.se (foo.stuge.se [212.116.89.98])
 by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTPS id w0K0vhWI026792
 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT)
 for <libssh2-devel@cool.haxx.se>; Sat, 20 Jan 2018 01:57:43 +0100
Received: (qmail 25651 invoked by uid 1000); 20 Jan 2018 00:45:14 -0000
Date: Sat, 20 Jan 2018 00:45:14 +0000
From: Peter Stuge <peter@stuge.se>
To: libssh2-devel@cool.haxx.se
Subject: Re: How should my libssh2-based client handle gratuitous keepalives
 from the ssh server?
Message-ID: <20180120004514.GO4167@stuge.se>
References: <E02C1C10-904F-4DF7-88B9-25156BBEF28F@meyersound.com>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <E02C1C10-904F-4DF7-88B9-25156BBEF28F@meyersound.com>
X-BeenThere: libssh2-devel@cool.haxx.se
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: libssh2 development <libssh2-devel.cool.haxx.se>
List-Unsubscribe: <https://cool.haxx.se/cgi-bin/mailman/options/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=unsubscribe>
List-Archive: <http://cool.haxx.se/pipermail/libssh2-devel/>
List-Post: <mailto:libssh2-devel@cool.haxx.se>
List-Help: <mailto:libssh2-devel-request@cool.haxx.se?subject=help>
List-Subscribe: <https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=subscribe>
Reply-To: libssh2 development <libssh2-devel@cool.haxx.se>
Content-Type: text/plain; charset="utf-8"
Errors-To: libssh2-devel-bounces@cool.haxx.se
Sender: "libssh2-devel" <libssh2-devel-bounces@cool.haxx.se>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id w0K0vjeS026826

Jeremy Friesner wrote:
> ClientAliveInterval 15
..
> Is there a recommended way to handle this issue?

No. I don't think libssh2 supports dealing with packet when idle at all.

The real solution is to add a generic protocol handler function, which
will need a fair bit of work, but I think not a lot of rework.


//Peter
_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

From libssh2-devel-bounces@cool.haxx.se  Mon Jan 22 19:51:13 2018
Return-Path: <libssh2-devel-bounces@cool.haxx.se>
Received: from www.haxx.se (mail [127.0.0.1])
	by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTP id w0MIoTwD019897;
	Mon, 22 Jan 2018 19:51:03 +0100
Received: from NAM01-SN1-obe.outbound.protection.outlook.com
 (mail-sn1nam01on0049.outbound.protection.outlook.com [104.47.32.49])
 by giant.haxx.se (8.15.2/8.15.2/Debian-4) with ESMTPS id w0MIoQIn019756
 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT)
 for <libssh2-devel@cool.haxx.se>; Mon, 22 Jan 2018 19:50:27 +0100
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=meyersound2.onmicrosoft.com; s=selector1-meyersound-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version;
 bh=SDYY3X9+qFXZZItXmA6XojdsCw4dXXvqMaCJX8e9RWY=;
 b=GW3BKrzdyAwbBr4dV2QHkHtTMP8Sg4zuPMyVavFETDJA5iDZOIm9ZalNcDfH38aR3hgBk3J3MDk7UuRzXuDpq9WqlH8xswUcoW3EchwJIpWBSPMslqC8pjsO9gAgwTn7z5iz2ORTSH/glwolipxJM+lkk8O23Q+nPdwyQFPYmck=
Received: from MWHPR18MB1310.namprd18.prod.outlook.com (10.175.6.148) by
 MWHPR18MB1309.namprd18.prod.outlook.com (10.175.6.147) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id
 15.20.428.17; Mon, 22 Jan 2018 18:50:19 +0000
Received: from MWHPR18MB1310.namprd18.prod.outlook.com ([10.175.6.148]) by
 MWHPR18MB1310.namprd18.prod.outlook.com ([10.175.6.148]) with mapi id
 15.20.0428.019; Mon, 22 Jan 2018 18:50:19 +0000
From: Jeremy Friesner <jeremyf@meyersound.com>
To: libssh2 development <libssh2-devel@cool.haxx.se>
Subject: Re: How should my libssh2-based client handle gratuitous keepalives
 from the ssh server?
Thread-Topic: How should my libssh2-based client handle gratuitous keepalives
 from the ssh server?
Thread-Index: AQHTj9Dr/rWhXlxngUysSEALubRBHKN78DwAgART0oA=
Date: Mon, 22 Jan 2018 18:50:19 +0000
Message-ID: <E947E44F-E9CB-4138-98A0-3EE079AFBE36@meyersound.com>
References: <E02C1C10-904F-4DF7-88B9-25156BBEF28F@meyersound.com>
 <20180120004514.GO4167@stuge.se>
In-Reply-To: <20180120004514.GO4167@stuge.se>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
authentication-results: spf=none (sender IP is )
 smtp.mailfrom=jeremyf@meyersound.com; 
x-originating-ip: [104.35.164.173]
x-ms-publictraffictype: Email
x-microsoft-exchange-diagnostics: 1; MWHPR18MB1309;
 7:9JUGpATrAuc4yN4QyVXkzCHRsKWhWVG0T6YsCDGVkqr0yZ/PX+OfYSbJA7oCds+BzjF41gp20x8MpCcD5LTbihUTmsfd2gucw3CaSmwCOxeme1cdjVRxe8sMbYEGnNWjgF+NtlWJNOK8aMqCI59Knkgv4isb4LuCzPmK07XdcRucRovYQxT6Ej6mXpdOTrzjlHHabdB5RUTxKMFXsBU6TKWzVH87oDBJifJJEhY/GNjGdNibFrYsI1oHLzXGw1PL
x-ms-exchange-antispam-srfa-diagnostics: SSOS;
x-ms-office365-filtering-correlation-id: d193efe0-533c-427c-8a6d-08d561c8fc4c
x-microsoft-antispam: UriScan:; BCL:0; PCL:0;
 RULEID:(7020095)(4652020)(5600026)(4604075)(3008032)(2017052603307)(7153060)(7193020);
 SRVR:MWHPR18MB1309; 
x-ms-traffictypediagnostic: MWHPR18MB1309:
x-microsoft-antispam-prvs: <MWHPR18MB13095312E8F05372F0A999ACDAEC0@MWHPR18MB1309.namprd18.prod.outlook.com>
x-exchange-antispam-report-test: UriScan:(158342451672863);
x-exchange-antispam-report-cfa-test: BCL:0; PCL:0;
 RULEID:(6040501)(2401047)(5005006)(8121501046)(3231023)(2400081)(944501161)(93006095)(93001095)(3002001)(10201501046)(6041288)(20161123560045)(20161123558120)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);
 SRVR:MWHPR18MB1309; BCL:0; PCL:0; RULEID:(100000803101)(100110400095);
 SRVR:MWHPR18MB1309; 
x-forefront-prvs: 0560A2214D
x-forefront-antispam-report: SFV:NSPM;
 SFS:(10009020)(346002)(376002)(366004)(39380400002)(396003)(39840400004)(199004)(189003)(51914003)(6486002)(2906002)(3846002)(8676002)(3280700002)(6116002)(3660700001)(99286004)(8936002)(76176011)(81166006)(81156014)(2950100002)(6916009)(86362001)(6512007)(6306002)(7736002)(229853002)(305945005)(5660300001)(2900100001)(83716003)(6436002)(36756003)(106356001)(66066001)(105586002)(82746002)(33656002)(25786009)(6246003)(97736004)(6506007)(77096007)(53546011)(26005)(478600001)(966005)(14454004)(53936002)(316002)(102836004)(68736007);
 DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR18MB1309;
 H:MWHPR18MB1310.namprd18.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords;
 A:1; MX:1; LANG:en; 
received-spf: None (protection.outlook.com: meyersound.com does not designate
 permitted sender hosts)
x-microsoft-antispam-message-info: Urc0PHZPH+K7AxTNyKxMnpnJz6bSb5us03nJ99TPHxuO802MB/z3YxcMCj9LhtJ/TMlEHaL3oAhZijCcNIOOPg==
spamdiagnosticoutput: 1:99
spamdiagnosticmetadata: NSPM
Content-ID: <D77DE20F54459240B48A78717577D653@namprd18.prod.outlook.com>
MIME-Version: 1.0
X-OriginatorOrg: meyersound.com
X-MS-Exchange-CrossTenant-Network-Message-Id: d193efe0-533c-427c-8a6d-08d561c8fc4c
X-MS-Exchange-CrossTenant-originalarrivaltime: 22 Jan 2018 18:50:19.5771 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: cb4b06fc-3bde-44f1-aa47-cc60a7ce81f5
X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR18MB1309
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id w0MIoQIn019756
X-BeenThere: libssh2-devel@cool.haxx.se
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: libssh2 development <libssh2-devel.cool.haxx.se>
List-Unsubscribe: <https://cool.haxx.se/cgi-bin/mailman/options/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=unsubscribe>
List-Archive: <http://cool.haxx.se/pipermail/libssh2-devel/>
List-Post: <mailto:libssh2-devel@cool.haxx.se>
List-Help: <mailto:libssh2-devel-request@cool.haxx.se?subject=help>
List-Subscribe: <https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel>, 
 <mailto:libssh2-devel-request@cool.haxx.se?subject=subscribe>
Reply-To: libssh2 development <libssh2-devel@cool.haxx.se>
Content-Type: text/plain; charset="utf-8"
Errors-To: libssh2-devel-bounces@cool.haxx.se
Sender: "libssh2-devel" <libssh2-devel-bounces@cool.haxx.se>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by giant.haxx.se id w0MIoTwD019897

Hi Peter,

Thanks for the information.

For the record, I worked around the problem by adding code such that when my libssh socket starts select()-ing as read-for-read during what should be an idle period, my program responds by setting up a command channel and sending a dummy ssh command (e.g. “echo hello”) to the server.  That appears to be sufficient to cause libssh2 to aborb the received keepalive-ping-data and to keep the sshd server happy that my client is still alive.

-Jeremy

> On Jan 19, 2018, at 4:45 PM, Peter Stuge <peter@stuge.se> wrote:
> 
> Jeremy Friesner wrote:
>> ClientAliveInterval 15
> ..
>> Is there a recommended way to handle this issue?
> 
> No. I don't think libssh2 supports dealing with packet when idle at all.
> 
> The real solution is to add a generic protocol handler function, which
> will need a fair bit of work, but I think not a lot of rework.
> 
> 
> //Peter
> _______________________________________________
> libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel


_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

