From b53c63ed53cdb7e589f6a5a743cc7fb43396927e Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sat, 26 Sep 2009 20:21:31 +0300 Subject: [PATCH] silcd: Added heartbeat support Added support for sending SILC_PACKET_HEARTBEAT packets to connection, to make sure they keep alive and to detect if they have died. --- TODO | 304 +---------------------------------- apps/silcd/command.c | 4 +- apps/silcd/packet_send.c | 11 +- apps/silcd/server.c | 21 ++- apps/silcd/server.h | 4 +- apps/silcd/server_internal.h | 1 + apps/silcd/server_util.h | 14 +- 7 files changed, 46 insertions(+), 313 deletions(-) diff --git a/TODO b/TODO index d89901f8..ffc4db00 100644 --- a/TODO +++ b/TODO @@ -9,312 +9,12 @@ tested. apps/silcd, The SILC Server ****PARTLY DONE**** =========================== - o Fix/test GETKEY. (***DONE) - - o Fix/test MOTD. (***DONE) - o Rewrite rehash, HUP. - o Heartbeat-keepalive. - - o Port all code to use SILC Toolkit 1.1 APIs. (***DONE) - - o Remove protocol.[ch]. (***DONE) - - o Rewrite connecting accepting. (***DONE) - - o Rewrite async connecting. (***DONE) - - o Test backup router resuming protocol. (***DONE) - - o Check all packet receive routines that they call silc_packet_free. - (***DONE) - - o Add Web statistics module using lib/silchttp. Give out server - statistics. (***DONE) - -SILC Client ****DONE**** +SILC Client =========== - o Porting to new Toolkit API and new Client Library API (***DONE) - - o Improve help files, especially /cmode, /cumode and /key. (***DONE) - -lib/silcclient, The Client Library ***DONE**** +lib/silcclient, The Client Library ================================== - - o silcclient.h clean up and API rewrites. (***DONE) - - o silcclient_entry.h finishing, all entry relates APIs to this header. - (***DONE) - - o SilcChannelEntry, SilcServerEntry, SilcChannelUser, allocating, - freeing, finding, etc. rewrite. Also making them reference counted for - multi threads use. (***DONE) - - o Rewrite client side WHOIS command (for whois -details). (***DONE) - - o Finish all the missing SILC packet processings, rewrites. (***DONE) - - o The client_notify.c rewrite. (***DONE) - - o Resuming to client_register.c (remove client_resume.c) (***DONE) - - o Rekey rewrite. (***DONE) - - o Remove protocol.[ch]. (***DONE) - - o File transfer rewrite. (***DONE) - - o File transfer API documentation. (***DONE) - - o Connection auth request. (***DONE) - - o Password auth test, public key auth test. (***DONE) - - o Starting key exchange directly, rewrite. (***DONE) - - o Channel messages, channel private keys, channel entires, channel - search, etc. rewrite. (***DONE) - - o For many APIs leave the hash context allocations to the caller instead - of using client->sha1hash and client->md5hash, or some kind of thread - safe (no locking) concept. (***DONE) - - o Key agreement rewrite. (***DONE) - - o Connecting to remote client (***DONE) - - o Private message waiting API (in threads) (***TESTING NEEDED) - - o client_attrs.c, attributes rewrite. (***DONE) - - o No SilcBuffer lists back to application in command_reply operations. - Convert them all to real lists and/or structures for easier use. - (***DONE) - - o Nickname formatting rewrite. (***DONE) - - o UDP connections. (***TESTING NEEDED) - - -lib/silcsftp ****DONE**** -============ - - o Porting to use the new util library. (***DONE) - - -lib/silccore/silcpacket.[ch] ****DONE**** -============================ - - o SilcPacketEngine. (***DONE) - - o New SILC Packet API. (***DONE) - - o Implement silc_packet_engine_stop and silc_packet_stream_destroy. (***DONE) - - o IV Included flag support, UDP transport support (***TESTING NEEDED) - - -lib/silccore/silcid.[ch] ****DONE**** -======================== - - o Add silc_id_str2id to accept the destination buffer as argument - and thus not require any memory allocation. Same will happen - with silc_id_payload_* functions. (***DONE) - - o silc_id_str2id, silc_id2str to non-allocating routines. (***DONE) - - -lib/silcskr ****DONE**** -=========== - - o Removing key from the repository is not possible currently. It should - be. (***DONE) - - -lib/silcske/silcske.[ch] ***DONE**** -======================== - - o Responder rekey (***DONE) - - o IV Included flag support in SKE (***DONE) - - o UDP transport changes; retransmission support by using exponential - backoff algorithm. (***DONE) - - o SilcConnAuth header file documentation. (***DONE) - - -lib/silccrypt ****DONE**** -============= - - o Implement PKCS #1 sign/verify with hash OID. (***TESTING NEEDED) - - o Implement SILC Public Key Version 2 handling in sign/verify. Implement - Version (V) identifier (***DONE) - - o SILC PKCS (silcpkcs.h) reorganizing when other PK supports added. - Move the SILC Public Key routines away from the crypto library into - the core library (silccore). silc_pkcs_public/private_key_* routines - to silc_public/private_key_* routines. The silc_public_key_* routines - should also automatically handle SILC Public Keys, and other keys - and certificates as well. Add fe. silcpk.h into silccore. It should - also include the Public Key Payload encoding and decoding routines. - (***DONE) - - o Assembler AES (***DONE) - - -lib/silcutil ****DONE**** -============ - - o The regex code from lib/contrib might compile fine on all platforms. - No need to make it silcutil/unix/ specific. Add them to generic - silcutil.c. (***DONE) - - o Silc FD Stream to WIN32 (lib/silcutil/silcfdstream.h) (***TESTING NEEDED) - - o bool -> SilcBool (***DONE) - - -lib/silcutil/silcbuffer.h ****DONE**** -========================= - - o Remove the `truelen' field from SilcBuffer as it is entirely - redundant since we can get the true length of the buffer by - doing buffer->end - buffer->header. Add silc_buffer_truelen - macro instead. Consider also removing `len' field too since - it effectively is buffer->tail - buffer->data, and adding - silc_buffer_len macro can do the same. These would save - totally 8 bytes of memory per buffer. (***DONE) - - -lib/silcutil/silcbuffmt.[ch] ****DONE**** -============================ - - o SilcStack aware silc_buffer_unformat (***DONE) - - o SilcStack aware silc_buffer_format (***DONE) - - o silc_buffer_format reallocates automatically (***DONE) - - o SILC_STR_OFFSET (***DONE) - - -lib/silcutil/silcstack.[ch] ****DONE**** -=========================== - - o Data stack implementation (***DONE) - - -lib/silcutil/silcstream.[ch] ****DONE**** -============================ - - o Add abstract SilcStream. (***DONE) - - -lib/silcutil/silcsocketstream.[ch] ****DONE**** -================================== - - o Add SilcSocketStream (***DONE) - - o Add SilcSocketStream for WIN32 (***TESTING NEEDED) - - o Test QoS after the changes made to socket stream - - -lib/silcutil/silcschedule*.[ch] ****DONE**** -=============================== - - o Scheduler can be optimized for FD tasks by changing the fd_queue - to SilcHashTable instead of using linked list. We need to do - one-to-one mapping of FD to task and hash table is more efficient - for this usage. - - Also redefine the silc_select to perhaps return a separate - structure of the events that actually occurred, instead of - returning the events in the fd_list which is then traversed - in the generic code to find the changed events. This can be - made faster by having own struct which includes only the - changed events, thus the tarversing is faster since the whole - fd_list is not traversed anymore (it is still traversed in the - silc_select but at least it removes one extra tarversing later - for the same list). - - Other task queues should be changed to use SilcList. (***DONE) - - o Add SILC scheduler's internal routines into a table of implementation - function pointers, that the generic code then takes as extern from - implementation. These are the silc_schedule_internal_* routines. - (***DONE) - - o Change SILC_TASK_CALLBACK to non-static, and remove the macro - SILC_TASK_CALLBACK_GLOBAL. (***DONE) - - o SILC Schedule API changes to WIN32. (***DONE) - - -lib/silcutil/silcasync.[ch] ****DONE**** -=========================== - - o Add SilcAsyncOperation to utility library. Any function that takes - callback as an argument must/should return SilcAsyncOperation. - (***DONE) - - -lib/silcutil/silctime.[ch] ****DONE**** -=========================== - - o SilcTime. (***DONE) - - o system time, universal, generalized. (***DONE) - - -lib/silcutil/silcfsm.[ch] ****DONE**** -========================= - - o SILC Finite State Machine API. Replaces SILC Protocol API (***DONE) - - -lib/silcutil/silcnet*, lib/silcutil/*/silc*net* ****DONE**** -=============================================== - - o Add UDP interface (***DONE) - - o Add UDP interface for WIN32 (***TESTING NEEDED) - - o New network interfaces (***DONE) - - -lib/silcmath ****DONE**** -============ - - o Test on x86_64. (***TESTING NEEDED) - - o Change LTM and TFM function names when importing to SILC tree to avoid - rare linking problems on system that has same named symbols already in - the system. (***DONE) - - -lib/silcutil/symbian/ ****DONE**** -===================== - - o lib/silcutil/symbian routines missing or not completed. - (****TESTING NEEDED) - - o Something needs to be thought to the logging globals as well, - like silc_debug etc. They won't work on EPOC. Perhaps logging - and debugging is to be disabled on EPOC. - - -lib/silcasn1 ****DONE**** -============ - - o ASN.1 library (***DONE) - - o Header documentation missing. (***DONE) - - o Some string encodings missing (copy/paste matter). (***DONE) diff --git a/apps/silcd/command.c b/apps/silcd/command.c index 0ddc787e..2bb776e8 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -16,7 +16,6 @@ GNU General Public License for more details. */ -/* $Id$ */ #include "serverincludes.h" #include "server_internal.h" @@ -4089,6 +4088,9 @@ SILC_TASK_CALLBACK(silc_server_command_detach_cb) idata->sconn->op = NULL; } + silc_schedule_task_del_by_all(server->schedule, 0, silc_server_do_heartbeat, + sock); + /* Close the connection on our side */ client->router = NULL; client->connection = NULL; diff --git a/apps/silcd/packet_send.c b/apps/silcd/packet_send.c index efc334cd..9e6fa133 100644 --- a/apps/silcd/packet_send.c +++ b/apps/silcd/packet_send.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2007 Pekka Riikonen + Copyright (C) 1997 - 2009 Pekka Riikonen 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 @@ -1572,6 +1572,15 @@ void silc_server_send_dest_command_reply(SilcServer server, va_end(ap); } +/* Send the heartbeat packet. */ + +void silc_server_send_heartbeat(SilcServer server, + SilcPacketStream sock) +{ + silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0, + NULL, 0); +} + /* Routine used to send the connection authentication packet. */ void silc_server_send_connection_auth_request(SilcServer server, diff --git a/apps/silcd/server.c b/apps/silcd/server.c index c2ba2247..a07dee24 100644 --- a/apps/silcd/server.c +++ b/apps/silcd/server.c @@ -531,7 +531,7 @@ static void silc_server_packet_parse_type(SilcServer server, case SILC_PACKET_KEY_AGREEMENT: /* - * Received heartbeat. + * Received key agreement. */ if (packet->flags & SILC_PACKET_FLAG_LIST) break; @@ -2649,6 +2649,14 @@ silc_server_accept_auth_compl(SilcConnAuth connauth, SilcBool success, param->qos_rate_limit, param->qos_bytes_limit, param->qos_limit_sec, param->qos_limit_usec); + /* Perform heartbeat */ + if (param->keepalive_secs) { + SILC_LOG_DEBUG(("Perform heartbeat every %d seconds", + param->keepalive_secs)); + silc_schedule_task_add_timeout(server->schedule, silc_server_do_heartbeat, + sock, param->keepalive_secs, 0); + } + silc_server_config_unref(&entry->cconfig); silc_server_config_unref(&entry->sconfig); silc_server_config_unref(&entry->rconfig); @@ -2893,6 +2901,15 @@ static void silc_server_accept_new_connection(SilcNetStatus status, entry->op = silc_ske_responder(ske, packet_stream, ¶ms); } +/* Perform heartbeat */ + +SILC_TASK_CALLBACK(silc_server_do_heartbeat) +{ + SilcServer server = app_context; + SilcPacketStream sock = context; + silc_server_send_heartbeat(server, sock); +} + /********************************** Rekey ***********************************/ @@ -3233,6 +3250,8 @@ void silc_server_free_sock_user_data(SilcServer server, silc_schedule_task_del_by_all(server->schedule, 0, silc_server_do_rekey, sock); + silc_schedule_task_del_by_all(server->schedule, 0, silc_server_do_heartbeat, + sock); /* Cancel active protocols */ if (idata) { diff --git a/apps/silcd/server.h b/apps/silcd/server.h index 81a6f3d9..ddefcc4d 100644 --- a/apps/silcd/server.h +++ b/apps/silcd/server.h @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2005, 2007 Pekka Riikonen + Copyright (C) 1997 - 2009 Pekka Riikonen 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 @@ -189,8 +189,6 @@ SilcBool silc_server_create_channel_key(SilcServer server, SilcChannelEntry silc_server_save_channel_key(SilcServer server, SilcBuffer key_payload, SilcChannelEntry channel); -void silc_server_perform_heartbeat(SilcPacketStream sock, - void *hb_context); void silc_server_announce_get_channel_topic(SilcServer server, SilcChannelEntry channel, SilcBuffer *topic); diff --git a/apps/silcd/server_internal.h b/apps/silcd/server_internal.h index 64cca816..d4edd0d1 100644 --- a/apps/silcd/server_internal.h +++ b/apps/silcd/server_internal.h @@ -210,6 +210,7 @@ do { \ ctype == SILC_CONN_ROUTER ? "Router" : "Unknown") /* Prototypes */ +SILC_TASK_CALLBACK(silc_server_do_heartbeat); SILC_TASK_CALLBACK(silc_server_rekey_final); SILC_TASK_CALLBACK(silc_server_rekey_callback); SILC_TASK_CALLBACK(silc_server_connect_to_router); diff --git a/apps/silcd/server_util.h b/apps/silcd/server_util.h index 79e1add5..edf7dbdb 100644 --- a/apps/silcd/server_util.h +++ b/apps/silcd/server_util.h @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2005, 2007 Pekka Riikonen + Copyright (C) 1997 - 2009 Pekka Riikonen 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 @@ -142,6 +142,10 @@ SilcBool silc_server_check_umode_rights(SilcServer server, SilcClientEntry client, SilcUInt32 mode); +/* Send the heartbeat packet. */ +void silc_server_send_heartbeat(SilcServer server, + SilcPacketStream sock); + /* This function is used to send the notify packets and motd to the incoming client connection. */ void silc_server_send_connect_notifys(SilcServer server, @@ -224,9 +228,9 @@ SilcStatus silc_server_set_channel_pk_list(SilcServer server, /* Verifies the Authentication Payload `auth' with one of the public keys on the `channel' public key list. */ SilcBool silc_server_verify_channel_auth(SilcServer server, - SilcChannelEntry channel, - SilcClientID *client_id, - const unsigned char *auth, - SilcUInt32 auth_len); + SilcChannelEntry channel, + SilcClientID *client_id, + const unsigned char *auth, + SilcUInt32 auth_len); #endif /* SERVER_UTIL_H */ -- 2.24.0