libssh2

Форк
0
/
keepalive.c 
101 строка · 3.4 Кб
1
/* Copyright (C) Simon Josefsson
2
 * All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms,
5
 * with or without modification, are permitted provided
6
 * that the following conditions are met:
7
 *
8
 *   Redistributions of source code must retain the above
9
 *   copyright notice, this list of conditions and the
10
 *   following disclaimer.
11
 *
12
 *   Redistributions in binary form must reproduce the above
13
 *   copyright notice, this list of conditions and the following
14
 *   disclaimer in the documentation and/or other materials
15
 *   provided with the distribution.
16
 *
17
 *   Neither the name of the copyright holder nor the names
18
 *   of any other contributors may be used to endorse or
19
 *   promote products derived from this software without
20
 *   specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35
 * OF SUCH DAMAGE.
36
 *
37
 * SPDX-License-Identifier: BSD-3-Clause
38
 */
39

40
#include "libssh2_priv.h"
41
#include "transport.h" /* _libssh2_transport_write */
42

43
/* Keep-alive stuff. */
44

45
LIBSSH2_API void
46
libssh2_keepalive_config(LIBSSH2_SESSION *session,
47
                         int want_reply,
48
                         unsigned int interval)
49
{
50
    if(interval == 1)
51
        session->keepalive_interval = 2;
52
    else
53
        session->keepalive_interval = interval;
54
    session->keepalive_want_reply = want_reply ? 1 : 0;
55
}
56

57
LIBSSH2_API int
58
libssh2_keepalive_send(LIBSSH2_SESSION *session,
59
                       int *seconds_to_next)
60
{
61
    time_t now;
62

63
    if(!session->keepalive_interval) {
64
        if(seconds_to_next)
65
            *seconds_to_next = 0;
66
        return 0;
67
    }
68

69
    now = time(NULL);
70

71
    if(session->keepalive_last_sent + session->keepalive_interval <= now) {
72
        /* Format is
73
           "SSH_MSG_GLOBAL_REQUEST || 4-byte len || str || want-reply". */
74
        unsigned char keepalive_data[]
75
            = "\x50\x00\x00\x00\x15keepalive@libssh2.orgW";
76
        size_t len = sizeof(keepalive_data) - 1;
77
        int rc;
78

79
        keepalive_data[len - 1] =
80
            (unsigned char)session->keepalive_want_reply;
81

82
        rc = _libssh2_transport_send(session, keepalive_data, len, NULL, 0);
83
        /* Silently ignore PACKET_EAGAIN here: if the write buffer is
84
           already full, sending another keepalive is not useful. */
85
        if(rc && rc != LIBSSH2_ERROR_EAGAIN) {
86
            _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
87
                           "Unable to send keepalive message");
88
            return rc;
89
        }
90

91
        session->keepalive_last_sent = now;
92
        if(seconds_to_next)
93
            *seconds_to_next = session->keepalive_interval;
94
    }
95
    else if(seconds_to_next) {
96
        *seconds_to_next = (int) (session->keepalive_last_sent - now)
97
            + session->keepalive_interval;
98
    }
99

100
    return 0;
101
}
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.