{
struct telnet_connection *telnet_connection;
struct telnet_service *telnet_service = connection->service->priv;
- int i;
- telnet_connection = malloc(sizeof(struct telnet_connection));
+ telnet_connection = calloc(1, sizeof(struct telnet_connection));
if (!telnet_connection) {
LOG_ERROR("Failed to allocate telnet connection.");
connection->priv = telnet_connection;
/* initialize telnet connection information */
- telnet_connection->closed = false;
- telnet_connection->line_size = 0;
- telnet_connection->line_cursor = 0;
telnet_connection->prompt = strdup("> ");
telnet_connection->prompt_visible = true;
telnet_connection->state = TELNET_STATE_DATA;
telnet_write(connection, "\r", 1);
telnet_prompt(connection);
- /* initialize history */
- for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
- telnet_connection->history[i] = NULL;
- telnet_connection->next_history = 0;
- telnet_connection->current_history = 0;
telnet_load_history(telnet_connection);
log_add_callback(telnet_log_callback, connection);
while ((usr_cmd_pos < t_con->line_cursor) && isspace(t_con->line[usr_cmd_pos]))
usr_cmd_pos++;
- /* user command length */
+ /* check user command length */
+ if (t_con->line_cursor < usr_cmd_pos) {
+ telnet_bell(connection);
+ return;
+ }
size_t usr_cmd_len = t_con->line_cursor - usr_cmd_pos;
/* optimize multiple spaces in the user command,
return ERROR_OK;
}
+static const struct service_driver telnet_service_driver = {
+ .name = "telnet",
+ .new_connection_during_keep_alive_handler = NULL,
+ .new_connection_handler = telnet_new_connection,
+ .input_handler = telnet_input,
+ .connection_closed_handler = telnet_connection_closed,
+ .keep_client_alive_handler = NULL,
+};
+
int telnet_init(char *banner)
{
if (strcmp(telnet_port, "disabled") == 0) {
telnet_service->banner = banner;
- int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED,
- telnet_new_connection, telnet_input, telnet_connection_closed,
+ int ret = add_service(&telnet_service_driver, telnet_port, CONNECTION_LIMIT_UNLIMITED,
telnet_service);
if (ret != ERROR_OK) {