From 89b07a460a554eb5173fe07f09b8c51c31d53e29 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Wed, 13 Sep 2000 17:45:15 +0000 Subject: [PATCH] Splitted SILC core library. Core library includes now only SILC protocol specific stuff. New utility library includes the old stuff from core library that is more generic purpose stuff. --- lib/silccore/Makefile.am | 14 +-- lib/silccore/silccommand.h | 10 -- lib/silccore/silcprotocol.h | 3 + lib/silcutil/Makefile.am | 39 +++++++ lib/{silccore => silcutil}/silcbuffer.c | 5 + lib/{silccore => silcutil}/silcbuffer.h | 0 lib/{silccore => silcutil}/silcbuffmt.c | 5 + lib/{silccore => silcutil}/silcbuffmt.h | 0 lib/{silccore => silcutil}/silcbufutil.c | 5 + lib/{silccore => silcutil}/silcbufutil.h | 0 lib/{silccore => silcutil}/silcconfig.c | 5 + lib/{silccore => silcutil}/silcconfig.h | 0 lib/{silccore => silcutil}/silclog.c | 126 ++++++++++++++++++---- lib/{silccore => silcutil}/silclog.h | 21 ++++ lib/{silccore => silcutil}/silcmemory.c | 5 + lib/{silccore => silcutil}/silcmemory.h | 0 lib/{silccore => silcutil}/silcnet.c | 5 + lib/{silccore => silcutil}/silcnet.h | 0 lib/{silccore => silcutil}/silcschedule.c | 29 +++-- lib/{silccore => silcutil}/silcschedule.h | 6 +- lib/{silccore => silcutil}/silctask.c | 5 + lib/{silccore => silcutil}/silctask.h | 0 lib/{silccore => silcutil}/silcutil.c | 114 ++++++++++++++++++++ lib/{silccore => silcutil}/silcutil.h | 8 ++ 24 files changed, 354 insertions(+), 51 deletions(-) create mode 100644 lib/silcutil/Makefile.am rename lib/{silccore => silcutil}/silcbuffer.c (96%) rename lib/{silccore => silcutil}/silcbuffer.h (100%) rename lib/{silccore => silcutil}/silcbuffmt.c (97%) rename lib/{silccore => silcutil}/silcbuffmt.h (100%) rename lib/{silccore => silcutil}/silcbufutil.c (92%) rename lib/{silccore => silcutil}/silcbufutil.h (100%) rename lib/{silccore => silcutil}/silcconfig.c (90%) rename lib/{silccore => silcutil}/silcconfig.h (100%) rename lib/{silccore => silcutil}/silclog.c (69%) rename lib/{silccore => silcutil}/silclog.h (82%) rename lib/{silccore => silcutil}/silcmemory.c (86%) rename lib/{silccore => silcutil}/silcmemory.h (100%) rename lib/{silccore => silcutil}/silcnet.c (96%) rename lib/{silccore => silcutil}/silcnet.h (100%) rename lib/{silccore => silcutil}/silcschedule.c (94%) rename lib/{silccore => silcutil}/silcschedule.h (97%) rename lib/{silccore => silcutil}/silctask.c (98%) rename lib/{silccore => silcutil}/silctask.h (100%) rename lib/{silccore => silcutil}/silcutil.c (75%) rename lib/{silccore => silcutil}/silcutil.h (81%) diff --git a/lib/silccore/Makefile.am b/lib/silccore/Makefile.am index d4321eac..62ed6aef 100644 --- a/lib/silccore/Makefile.am +++ b/lib/silccore/Makefile.am @@ -23,24 +23,14 @@ noinst_LIBRARIES = libsilccore.a libsilccore_a_SOURCES = \ id.c \ idcache.c \ - silcbuffer.c \ - silcbuffmt.c \ - silcbufutil.c \ silcchannel.c \ silccommand.c \ - silcconfig.c \ - silclog.c \ - silcmemory.c \ - silcnet.c \ silcpacket.c \ silcprotocol.c \ - silcschedule.c \ - silcsockconn.c \ - silctask.c \ - silcutil.c + silcsockconn.c EXTRA_DIST = *.h INCLUDES = -I. -I.. -I../silccrypt -I../silcmath -I../silcske \ - -I../silcsim -I../.. -I../../includes \ + -I../silcsim -I../.. -I../silcutil -I../../includes \ -I../silcmath/gmp diff --git a/lib/silccore/silccommand.h b/lib/silccore/silccommand.h index 155027d6..70911556 100644 --- a/lib/silccore/silccommand.h +++ b/lib/silccore/silccommand.h @@ -80,16 +80,6 @@ typedef enum { #define SILC_COMMAND_LEAVE 23 #define SILC_COMMAND_NAMES 24 -/* Local commands. Local commands are unofficial commands and - are implementation specific commands. These are used only by the - SILC client to extend user commands. */ -#define SILC_COMMAND_HELP 100 -#define SILC_COMMAND_CLEAR 101 -#define SILC_COMMAND_VERSION 102 -#define SILC_COMMAND_SERVER 103 -#define SILC_COMMAND_MSG 104 -#define SILC_COMMAND_AWAY 105 - /* Reserved */ #define SILC_COMMAND_RESERVED 255 diff --git a/lib/silccore/silcprotocol.h b/lib/silccore/silcprotocol.h index fd564fcb..620ca85b 100644 --- a/lib/silccore/silcprotocol.h +++ b/lib/silccore/silcprotocol.h @@ -39,6 +39,9 @@ typedef unsigned char SilcProtocolState; #define SILC_PROTOCOL_CONN_AUTH_PASSWORD 1 #define SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY 2 +/* Type definition for above auth methods */ +typedef unsigned char SilcProtocolAuthMeth; + /* SILC Protocol Object. diff --git a/lib/silcutil/Makefile.am b/lib/silcutil/Makefile.am new file mode 100644 index 00000000..fc02b272 --- /dev/null +++ b/lib/silcutil/Makefile.am @@ -0,0 +1,39 @@ +# +# Makefile.am +# +# Author: Pekka Riikonen +# +# Copyright (C) 2000 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign + +noinst_LIBRARIES = libsilcutil.a + +libsilcutil_a_SOURCES = \ + silcbuffer.c \ + silcbuffmt.c \ + silcbufutil.c \ + silcconfig.c \ + silclog.c \ + silcmemory.c \ + silcnet.c \ + silcschedule.c \ + silctask.c \ + silcutil.c + +EXTRA_DIST = *.h + +INCLUDES = -I. -I.. -I../silccrypt -I../silcmath -I../silcske \ + -I../silcsim -I../.. -I../silccore -I../../includes \ + -I../silcmath/gmp diff --git a/lib/silccore/silcbuffer.c b/lib/silcutil/silcbuffer.c similarity index 96% rename from lib/silccore/silcbuffer.c rename to lib/silcutil/silcbuffer.c index fc2b6b40..ccaf1f57 100644 --- a/lib/silccore/silcbuffer.c +++ b/lib/silcutil/silcbuffer.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:15 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.2 2000/07/05 06:06:35 priikone * Global cosmetic change. * diff --git a/lib/silccore/silcbuffer.h b/lib/silcutil/silcbuffer.h similarity index 100% rename from lib/silccore/silcbuffer.h rename to lib/silcutil/silcbuffer.h diff --git a/lib/silccore/silcbuffmt.c b/lib/silcutil/silcbuffmt.c similarity index 97% rename from lib/silccore/silcbuffmt.c rename to lib/silcutil/silcbuffmt.c index 95fe7288..3399ccb1 100644 --- a/lib/silccore/silcbuffmt.c +++ b/lib/silcutil/silcbuffmt.c @@ -24,6 +24,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.2 2000/07/05 06:06:35 priikone * Global cosmetic change. * diff --git a/lib/silccore/silcbuffmt.h b/lib/silcutil/silcbuffmt.h similarity index 100% rename from lib/silccore/silcbuffmt.h rename to lib/silcutil/silcbuffmt.h diff --git a/lib/silccore/silcbufutil.c b/lib/silcutil/silcbufutil.c similarity index 92% rename from lib/silccore/silcbufutil.c rename to lib/silcutil/silcbufutil.c index 7599868b..2c89a17c 100644 --- a/lib/silccore/silcbufutil.c +++ b/lib/silcutil/silcbufutil.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.3 2000/07/14 06:08:49 priikone * Added silc_buffer_realloc. Fixed silc_buffer_clone. * diff --git a/lib/silccore/silcbufutil.h b/lib/silcutil/silcbufutil.h similarity index 100% rename from lib/silccore/silcbufutil.h rename to lib/silcutil/silcbufutil.h diff --git a/lib/silccore/silcconfig.c b/lib/silcutil/silcconfig.c similarity index 90% rename from lib/silccore/silcconfig.c rename to lib/silcutil/silcconfig.c index 59fefec7..1cb27bf4 100644 --- a/lib/silccore/silcconfig.c +++ b/lib/silcutil/silcconfig.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.2 2000/07/05 06:06:35 priikone * Global cosmetic change. * diff --git a/lib/silccore/silcconfig.h b/lib/silcutil/silcconfig.h similarity index 100% rename from lib/silccore/silcconfig.h rename to lib/silcutil/silcconfig.h diff --git a/lib/silccore/silclog.c b/lib/silcutil/silclog.c similarity index 69% rename from lib/silccore/silclog.c rename to lib/silcutil/silclog.c index b1ad9186..24f3ddce 100644 --- a/lib/silccore/silclog.c +++ b/lib/silcutil/silclog.c @@ -17,29 +17,13 @@ GNU General Public License for more details. */ -/* - * $Id$ - * $Log$ - * Revision 1.5 2000/07/17 16:46:37 priikone - * Still bug fix in silc_log_format :) - * - * Revision 1.4 2000/07/17 16:44:57 priikone - * Buffer overflow bug fixe in silc_log_format. - * - * Revision 1.3 2000/07/05 06:06:35 priikone - * Global cosmetic change. - * - * Revision 1.2 2000/07/03 05:53:58 priikone - * Fixed file purging bug. The purging should work now ok. - * - * Revision 1.1.1.1 2000/06/27 11:36:55 priikone - * Imported from internal CVS/Added Log headers. - * - * - */ +/* $Id$ */ #include "silcincludes.h" +/* Set TRUE/FALSE to enable/disable debugging */ +int silc_debug; + /* SILC Log name strings. These strings are printed to the log file. */ const SilcLogTypeName silc_log_types[] = { @@ -60,6 +44,17 @@ unsigned int log_warning_size; unsigned int log_error_size; unsigned int log_fatal_size; +/* Log callbacks. If these are set by the application these are used + instead of the default functions in this file. */ +static SilcLogCb info_cb = NULL; +static SilcLogCb warning_cb = NULL; +static SilcLogCb error_cb = NULL; +static SilcLogCb fatal_cb = NULL; + +/* Debug callbacks. If set these are used instead of default ones. */ +static SilcDebugCb debug_cb = NULL; +static SilcDebugHexdumpCb debug_hexdump_cb = NULL; + /* Formats arguments to a string and returns it after allocating memory for it. It must be remembered to free it later. */ @@ -84,6 +79,38 @@ void silc_log_output(const char *filename, unsigned int maxsize, FILE *fp; const SilcLogTypeName *np; + switch(type) + { + case SILC_LOG_INFO: + if (info_cb) { + (*info_cb)(string); + silc_free(string); + return; + } + break; + case SILC_LOG_WARNING: + if (warning_cb) { + (*warning_cb)(string); + silc_free(string); + return; + } + break; + case SILC_LOG_ERROR: + if (error_cb) { + (*error_cb)(string); + silc_free(string); + return; + } + break; + case SILC_LOG_FATAL: + if (fatal_cb) { + (*fatal_cb)(string); + silc_free(string); + return; + } + break; + } + /* Purge the log file if the max size is defined. */ if (maxsize) { fp = fopen(filename, "r"); @@ -124,6 +151,18 @@ void silc_log_output(const char *filename, unsigned int maxsize, void silc_log_output_debug(char *file, char *function, int line, char *string) { + if (!silc_debug) { + silc_free(string); + return; + } + + if (debug_cb) + { + (*debug_cb)(file, function, line, string); + silc_free(string); + return; + } + /* fprintf(stderr, "%s:%s:%d: %s\n", file, function, line, string); */ fprintf(stderr, "%s:%d: %s\n", function, line, string); fflush(stderr); @@ -140,6 +179,18 @@ void silc_log_output_hexdump(char *file, char *function, int off, pos, count; unsigned char *data = (unsigned char *)data_in; + if (!silc_debug) { + silc_free(string); + return; + } + + if (debug_hexdump_cb) + { + (*debug_hexdump_cb)(file, function, line, data_in, len, string); + silc_free(string); + return; + } + /* fprintf(stderr, "%s:%s:%d: %s\n", file, function, line, string); */ fprintf(stderr, "%s:%d: %s\n", function, line, string); silc_free(string); @@ -218,3 +269,38 @@ void silc_log_set_files(char *info, unsigned int info_size, log_error_size = error_size; log_fatal_size = fatal_size; } + +/* Sets log callbacks */ + +void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning, + SilcLogCb error, SilcLogCb fatal) +{ + info_cb = info; + warning_cb = warning; + error_cb = error; + fatal_cb = fatal; +} + +/* Resets log callbacks */ + +void silc_log_reset_callbacks() +{ + info_cb = warning_cb = error_cb = fatal_cb = NULL; +} + +/* Sets debug callbacks */ + +void silc_log_set_debug_callbacks(SilcDebugCb debug, + SilcDebugHexdumpCb debug_hexdump) +{ + debug_cb = debug; + debug_hexdump_cb = debug_hexdump; +} + +/* Resets debug callbacks */ + +void silc_log_reset_debug_callbacks() +{ + debug_cb = NULL; + debug_hexdump_cb = NULL; +} diff --git a/lib/silccore/silclog.h b/lib/silcutil/silclog.h similarity index 82% rename from lib/silccore/silclog.h rename to lib/silcutil/silclog.h index a98e4af4..cd34639f 100644 --- a/lib/silccore/silclog.h +++ b/lib/silcutil/silclog.h @@ -21,6 +21,9 @@ #ifndef SILCLOG_H #define SILCLOG_H +/* Set TRUE/FALSE to enable/disable debugging */ +extern int silc_debug; + /* SILC Log types */ typedef enum { SILC_LOG_INFO, @@ -35,6 +38,18 @@ typedef struct { SilcLogType type; } SilcLogTypeName; +/* Log function callback. */ +typedef void (*SilcLogCb)(char *message); + +/* Debug function callback. */ +typedef void (*SilcDebugCb)(char *file, char *function, + int line, char *message); + +/* Debug hexdump function callback. */ +typedef void (*SilcDebugHexdumpCb)(char *file, char *function, + int line, unsigned char *data, + unsigned int data_len, char *message); + /* Default log filenames */ #define SILC_LOG_FILE_INFO "silcd.log" #define SILC_LOG_FILE_WARNING "silcd_error.log" @@ -100,5 +115,11 @@ void silc_log_set_files(char *info, unsigned int info_size, char *warning, unsigned int warning_size, char *error, unsigned int error_size, char *fatal, unsigned int fatal_size); +void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning, + SilcLogCb error, SilcLogCb fatal); +void silc_log_reset_callbacks(); +void silc_log_set_debug_callbacks(SilcDebugCb debug, + SilcDebugHexdumpCb debug_hexdump); +void silc_log_reset_debug_callbacks(); #endif diff --git a/lib/silccore/silcmemory.c b/lib/silcutil/silcmemory.c similarity index 86% rename from lib/silccore/silcmemory.c rename to lib/silcutil/silcmemory.c index 9a922bf9..e5ebae56 100644 --- a/lib/silccore/silcmemory.c +++ b/lib/silcutil/silcmemory.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.2 2000/07/05 06:05:56 priikone * Assert if system is out of memory. * diff --git a/lib/silccore/silcmemory.h b/lib/silcutil/silcmemory.h similarity index 100% rename from lib/silccore/silcmemory.h rename to lib/silcutil/silcmemory.h diff --git a/lib/silccore/silcnet.c b/lib/silcutil/silcnet.c similarity index 96% rename from lib/silccore/silcnet.c rename to lib/silcutil/silcnet.c index 989020f7..68a820f6 100644 --- a/lib/silccore/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.3 2000/07/05 06:06:35 priikone * Global cosmetic change. * diff --git a/lib/silccore/silcnet.h b/lib/silcutil/silcnet.h similarity index 100% rename from lib/silccore/silcnet.h rename to lib/silcutil/silcnet.h diff --git a/lib/silccore/silcschedule.c b/lib/silcutil/silcschedule.c similarity index 94% rename from lib/silccore/silcschedule.c rename to lib/silcutil/silcschedule.c index 5236e789..0cbf333e 100644 --- a/lib/silccore/silcschedule.c +++ b/lib/silcutil/silcschedule.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.3 2000/07/18 06:51:58 priikone * Debug version bug fixes. * @@ -41,20 +46,32 @@ static SilcSchedule schedule; the timeout task queue hook. This must be called before the schedule is able to work. */ -void silc_schedule_init(SilcTaskQueue fd_queue, - SilcTaskQueue timeout_queue, - SilcTaskQueue generic_queue, +void silc_schedule_init(SilcTaskQueue *fd_queue, + SilcTaskQueue *timeout_queue, + SilcTaskQueue *generic_queue, int max_fd) { int i; SILC_LOG_DEBUG(("Initializing scheduler")); + /* Register the task queues if they are not registered already. In SILC + we have by default three task queues. One task queue for non-timeout + tasks which perform different kind of I/O on file descriptors, timeout + task queue for timeout tasks, and, generic non-timeout task queue whose + tasks apply to all connections. */ + if (!*fd_queue) + silc_task_queue_alloc(fd_queue, TRUE); + if (!*timeout_queue) + silc_task_queue_alloc(timeout_queue, TRUE); + if (!*generic_queue) + silc_task_queue_alloc(generic_queue, TRUE); + /* Initialize the schedule */ memset(&schedule, 0, sizeof(schedule)); - schedule.fd_queue = fd_queue; - schedule.timeout_queue = timeout_queue; - schedule.generic_queue = generic_queue; + schedule.fd_queue = *fd_queue; + schedule.timeout_queue = *timeout_queue; + schedule.generic_queue = *generic_queue; schedule.fd_list.fd = silc_calloc(max_fd, sizeof(int)); schedule.fd_list.last_fd = 0; schedule.fd_list.max_fd = max_fd; diff --git a/lib/silccore/silcschedule.h b/lib/silcutil/silcschedule.h similarity index 97% rename from lib/silccore/silcschedule.h rename to lib/silcutil/silcschedule.h index 87af2d1e..19c38468 100644 --- a/lib/silccore/silcschedule.h +++ b/lib/silcutil/silcschedule.h @@ -110,9 +110,9 @@ typedef struct { typedef SilcScheduleObject SilcSchedule; /* Prototypes */ -void silc_schedule_init(SilcTaskQueue fd_queue, - SilcTaskQueue timeout_queue, - SilcTaskQueue generic_queue, +void silc_schedule_init(SilcTaskQueue *fd_queue, + SilcTaskQueue *timeout_queue, + SilcTaskQueue *generic_queue, int max_fd); int silc_schedule_uninit(); void silc_schedule_stop(); diff --git a/lib/silccore/silctask.c b/lib/silcutil/silctask.c similarity index 98% rename from lib/silccore/silctask.c rename to lib/silcutil/silctask.c index cc177dc1..1bf8388f 100644 --- a/lib/silccore/silctask.c +++ b/lib/silcutil/silctask.c @@ -20,6 +20,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.2 2000/07/05 06:06:35 priikone * Global cosmetic change. * diff --git a/lib/silccore/silctask.h b/lib/silcutil/silctask.h similarity index 100% rename from lib/silccore/silctask.h rename to lib/silcutil/silctask.h diff --git a/lib/silccore/silcutil.c b/lib/silcutil/silcutil.c similarity index 75% rename from lib/silccore/silcutil.c rename to lib/silcutil/silcutil.c index 3a0b77ec..39b9ab1a 100644 --- a/lib/silccore/silcutil.c +++ b/lib/silcutil/silcutil.c @@ -24,6 +24,11 @@ /* * $Id$ * $Log$ + * Revision 1.1 2000/09/13 17:45:16 priikone + * Splitted SILC core library. Core library includes now only + * SILC protocol specific stuff. New utility library includes the + * old stuff from core library that is more generic purpose stuff. + * * Revision 1.4 2000/07/19 07:04:04 priikone * Minor bug fix in silc_encode_pem * @@ -433,3 +438,112 @@ unsigned char *silc_decode_pem(unsigned char *pem, unsigned int pem_len, return data; } + +/* Parse nickname string. The format may be !@ to + support multiple same nicknames. The is the final unifier if same + nickname is on same server. Note, this is only local format and server + does not know anything about these. */ + +int silc_parse_nickname(char *string, char **nickname, char **server, + unsigned int *num) +{ + unsigned int tlen; + char tmp[256]; + + if (!string) + return FALSE; + + if (strchr(string, '!')) { + tlen = strcspn(string, "!"); + memset(tmp, 0, sizeof(tmp)); + memcpy(tmp, string, tlen); + + if (num) + *num = atoi(tmp); + + if (tlen >= strlen(string)) + return FALSE; + + string += tlen + 1; + } + + if (strchr(string, '@')) { + tlen = strcspn(string, "@"); + + if (nickname) { + *nickname = silc_calloc(tlen + 1, sizeof(char)); + memcpy(*nickname, string, tlen); + } + + if (server) { + *server = silc_calloc(strlen(string) - tlen, sizeof(char)); + memcpy(*server, string + tlen + 1, strlen(string) - tlen - 1); + } + } else { + if (nickname) + *nickname = strdup(string); + } + + return TRUE; +} + +/* Parses command line. At most `max_args' is taken. Rest of the line + will be allocated as the last argument if there are more than `max_args' + arguments in the line. Note that the command name is counted as one + argument and is saved. */ + +void silc_parse_command_line(unsigned char *buffer, + unsigned char ***parsed, + unsigned int **parsed_lens, + unsigned int **parsed_types, + unsigned int *parsed_num, + unsigned int max_args) +{ + int i, len = 0; + int argc = 0; + const char *cp = buffer; + + *parsed = silc_calloc(1, sizeof(**parsed)); + *parsed_lens = silc_calloc(1, sizeof(**parsed_lens)); + + /* Get the command first */ + len = strcspn(cp, " "); + (*parsed)[0] = silc_to_upper((char *)cp); + (*parsed_lens)[0] = len; + cp += len + 1; + argc++; + + /* Parse arguments */ + if (strchr(cp, ' ') || strlen(cp) != 0) { + for (i = 1; i < max_args; i++) { + + if (i != max_args - 1) + len = strcspn(cp, " "); + else + len = strlen(cp); + + *parsed = silc_realloc(*parsed, sizeof(**parsed) * (argc + 1)); + *parsed_lens = silc_realloc(*parsed_lens, + sizeof(**parsed_lens) * (argc + 1)); + (*parsed)[argc] = silc_calloc(len + 1, sizeof(char)); + memcpy((*parsed)[argc], cp, len); + (*parsed_lens)[argc] = len; + argc++; + + cp += len; + if (strlen(cp) == 0) + break; + else + cp++; + } + } + + /* Save argument types. Protocol defines all argument types but + this implementation makes sure that they are always in correct + order hence this simple code. */ + *parsed_types = silc_calloc(argc, sizeof(**parsed_types)); + for (i = 0; i < argc; i++) + (*parsed_types)[i] = i; + + *parsed_num = argc; +} diff --git a/lib/silccore/silcutil.h b/lib/silcutil/silcutil.h similarity index 81% rename from lib/silccore/silcutil.h rename to lib/silcutil/silcutil.h index 7607df73..e9c011b3 100644 --- a/lib/silccore/silcutil.h +++ b/lib/silcutil/silcutil.h @@ -35,5 +35,13 @@ char *silc_encode_pem(unsigned char *data, unsigned int len); char *silc_encode_pem_file(unsigned char *data, unsigned int data_len); unsigned char *silc_decode_pem(unsigned char *pem, unsigned int pem_len, unsigned int *ret_len); +int silc_parse_nickname(char *string, char **nickname, char **server, + unsigned int *num); +void silc_parse_command_line(unsigned char *buffer, + unsigned char ***parsed, + unsigned int **parsed_lens, + unsigned int **parsed_types, + unsigned int *parsed_num, + unsigned int max_args); #endif -- 2.24.0