From 47152b457b432b48f66328e434edaf8cd8333d57 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Thu, 6 Jul 2000 07:11:06 +0000 Subject: [PATCH] Removed status paylaod encoding functions -> not needed anymore. Added encode_reply_payload_va to encode reply packets only. Normal encode_payload_va accepts now argument type as variable argument as well. --- lib/silccore/silccommand.c | 97 ++++++++++++++++++++++++++++---------- lib/silccore/silccommand.h | 7 +-- 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/lib/silccore/silccommand.c b/lib/silccore/silccommand.c index 9564d4e0..cfdf18a0 100644 --- a/lib/silccore/silccommand.c +++ b/lib/silccore/silccommand.c @@ -20,6 +20,12 @@ /* * $Id$ * $Log$ + * Revision 1.3 2000/07/06 07:11:06 priikone + * Removed status paylaod encoding functions -> not needed anymore. + * Added encode_reply_payload_va to encode reply packets only. + * Normal encode_payload_va accepts now argument type as variable + * argument as well. + * * Revision 1.2 2000/07/05 06:06:35 priikone * Global cosmetic change. * @@ -196,11 +202,12 @@ SilcBuffer silc_command_encode_payload(SilcCommand cmd, } /* Encodes Command payload with variable argument list. The arguments - must be: unsigned char *, unsigned int, ... One unsigned char * - and unsigned int forms one argument, hence `argc' in case when - sending one unsigned char * and unsigned int equals one (1) and - when sending two of those it equals two (2), and so on. This has - to be preserved or bad things will happen. */ + must be: unsigned int, unsigned char *, unsigned int, ... One + {unsigned int, unsigned char * and unsigned int} forms one argument, + thus `argc' in case when sending one {unsigned int, unsigned char * + and unsigned int} equals one (1) and when sending two of those it + equals two (2), and so on. This has to be preserved or bad things + will happen. The variable arguments is: {type, data, data_len}. */ SilcBuffer silc_command_encode_payload_va(SilcCommand cmd, unsigned int argc, ...) @@ -210,6 +217,7 @@ SilcBuffer silc_command_encode_payload_va(SilcCommand cmd, unsigned int *argv_lens = NULL, *argv_types = NULL; unsigned char *x; unsigned int x_len; + unsigned int x_type; SilcBuffer buffer; int i; @@ -220,13 +228,70 @@ SilcBuffer silc_command_encode_payload_va(SilcCommand cmd, argv_types = silc_calloc(argc, sizeof(unsigned int)); for (i = 0; i < argc; i++) { + x_type = va_arg(ap, unsigned int); x = va_arg(ap, unsigned char *); x_len = va_arg(ap, unsigned int); argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char)); memcpy(argv[i], x, x_len); argv_lens[i] = x_len; - argv_types[i] = i + 1; + argv_types[i] = x_type; + } + + buffer = silc_command_encode_payload(cmd, argc, argv, + argv_lens, argv_types); + + for (i = 0; i < argc; i++) + silc_free(argv[i]); + silc_free(argv); + silc_free(argv_lens); + silc_free(argv_types); + + return buffer; +} + +/* Same as above except that this is used to encode strictly command + reply packets. The command status message to be returned is sent as + extra argument to this function. The `argc' must not count `status' + as on argument. */ + +SilcBuffer +silc_command_encode_reply_payload_va(SilcCommand cmd, + SilcCommandStatus status, + unsigned int argc, ...) +{ + va_list ap; + unsigned char **argv; + unsigned int *argv_lens = NULL, *argv_types = NULL; + unsigned char status_data[2]; + unsigned char *x; + unsigned int x_len; + unsigned int x_type; + SilcBuffer buffer; + int i; + + va_start(ap, argc); + + argc++; + argv = silc_calloc(argc, sizeof(unsigned char *)); + argv_lens = silc_calloc(argc, sizeof(unsigned int)); + argv_types = silc_calloc(argc, sizeof(unsigned int)); + + SILC_PUT16_MSB(status, status_data); + argv[0] = silc_calloc(sizeof(status_data) + 1, sizeof(unsigned char)); + memcpy(argv[0], status_data, sizeof(status_data)); + argv_lens[0] = sizeof(status_data); + argv_types[0] = 1; + + for (i = 1; i < argc; i++) { + x_type = va_arg(ap, unsigned int); + x = va_arg(ap, unsigned char *); + x_len = va_arg(ap, unsigned int); + + argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char)); + memcpy(argv[i], x, x_len); + argv_lens[i] = x_len; + argv_types[i] = x_type; } buffer = silc_command_encode_payload(cmd, argc, argv, @@ -317,23 +382,3 @@ unsigned char *silc_command_get_arg_type(SilcCommandPayload payload, return payload->argv[i]; } - -/* Encodes command status payload. Status payload is sent as one reply - argument. The returned payload still has to be saved into the - Command Argument payload. */ - -SilcBuffer silc_command_encode_status_payload(SilcCommandStatus status, - unsigned char *data, - unsigned int len) -{ - SilcBuffer sp; - - sp = silc_buffer_alloc(len + 2); - silc_buffer_pull_tail(sp, SILC_BUFFER_END(sp)); - silc_buffer_format(sp, - SILC_STR_UI_SHORT(status), - SILC_STR_UI_XNSTRING(data, len), - SILC_STR_END); - - return sp; -} diff --git a/lib/silccore/silccommand.h b/lib/silccore/silccommand.h index ae8615fb..39a2e289 100644 --- a/lib/silccore/silccommand.h +++ b/lib/silccore/silccommand.h @@ -144,6 +144,10 @@ SilcBuffer silc_command_encode_payload(SilcCommand cmd, unsigned int *argv_types); SilcBuffer silc_command_encode_payload_va(SilcCommand cmd, unsigned int argc, ...); +SilcBuffer +silc_command_encode_reply_payload_va(SilcCommand cmd, + SilcCommandStatus status, + unsigned int argc, ...); void silc_command_free_payload(SilcCommandPayload payload); SilcCommand silc_command_get(SilcCommandPayload payload); unsigned int silc_command_get_arg_num(SilcCommandPayload payload); @@ -154,8 +158,5 @@ unsigned char *silc_command_get_next_arg(SilcCommandPayload payload, unsigned char *silc_command_get_arg_type(SilcCommandPayload payload, unsigned int type, unsigned int *ret_len); -SilcBuffer silc_command_encode_status_payload(SilcCommandStatus status, - unsigned char *data, - unsigned int len); #endif -- 2.24.0