--- lbbs/src/net_server.c 2025/10/25 03:11:11 1.74 +++ lbbs/src/net_server.c 2025/11/04 13:49:51 1.77 @@ -1,18 +1,10 @@ -/*************************************************************************** - net_server.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * net_server + * - network server with SSH support + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "bbs.h" #include "bbs_main.h" @@ -333,11 +325,6 @@ static int fork_server(void) ssh_bind_free(sshbind); - ssh_callbacks_init(&channel_cb); - ssh_callbacks_init(&server_cb); - - ssh_set_server_callbacks(SSH_session, &server_cb); - ssh_timeout = 60; // second if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) { @@ -345,12 +332,18 @@ static int fork_server(void) goto cleanup; } + ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD); + + ssh_callbacks_init(&server_cb); + ssh_callbacks_init(&channel_cb); + + ssh_set_server_callbacks(SSH_session, &server_cb); + if (ssh_handle_key_exchange(SSH_session)) { log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session)); goto cleanup; } - ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD); event = ssh_event_new(); ssh_event_add_session(event, SSH_session); @@ -375,6 +368,22 @@ static int fork_server(void) ssh_set_channel_callbacks(SSH_channel, &channel_cb); + do + { + ret = ssh_event_dopoll(event, 100); // 0.1 second + if (ret == SSH_ERROR) + { + ssh_channel_close(SSH_channel); + } + + if (ret == SSH_AGAIN) // loop until SSH connection is fully established + { + /* Executed only once, once the child process starts. */ + cdata.event = event; + break; + } + } while (ssh_channel_is_open(SSH_channel)); + ssh_timeout = 0; if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) { @@ -670,7 +679,7 @@ int net_server(const char *hostaddr, in_ { if (load_file(data_files_load_startup[i]) < 0) { - log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); + log_error("load_file(%s) error\n", data_files_load_startup[i]); } } log_common("Reload data files successfully\n");