X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccore%2Fsilccommand.c;h=14ea4e9edd16dff7c1b176651fd52c312af018fd;hb=8cf8c85f3fdfcbae751f606bbce44a2cd0ef5bbf;hp=6c45cdb87535ad3908406e2b1f10b1c12935f9fa;hpb=ca321a7eee00d2507f83e068461076a6f9f1eb86;p=silc.git diff --git a/lib/silccore/silccommand.c b/lib/silccore/silccommand.c index 6c45cdb8..14ea4e9e 100644 --- a/lib/silccore/silccommand.c +++ b/lib/silccore/silccommand.c @@ -126,6 +126,49 @@ SilcBuffer silc_command_payload_encode(SilcCommand cmd, return buffer; } +/* Same as above but encode the buffer from SilcCommandPayload structure + instead of raw data. */ + +SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload) +{ + SilcBuffer buffer; + SilcBuffer args = NULL; + unsigned int len = 0; + unsigned int argc = 0; + + SILC_LOG_DEBUG(("Encoding command payload")); + + if (payload->args) { + args = silc_argument_payload_encode_payload(payload->args); + len = args->len; + argc = silc_argument_get_arg_num(payload->args); + } + + len += SILC_COMMAND_PAYLOAD_LEN; + buffer = silc_buffer_alloc(len); + silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer)); + + /* Create Command payload */ + silc_buffer_format(buffer, + SILC_STR_UI_SHORT(len), + SILC_STR_UI_CHAR(payload->cmd), + SILC_STR_UI_CHAR(argc), + SILC_STR_UI_SHORT(payload->ident), + SILC_STR_END); + + /* Add arguments */ + if (args) { + silc_buffer_pull(buffer, SILC_COMMAND_PAYLOAD_LEN); + silc_buffer_format(buffer, + SILC_STR_UI_XNSTRING(args->data, args->len), + SILC_STR_END); + silc_buffer_push(buffer, SILC_COMMAND_PAYLOAD_LEN); + silc_free(args); + } + + return buffer; +} + /* Encodes Command payload with variable argument list. The arguments must be: unsigned int, unsigned char *, unsigned int, ... One {unsigned int, unsigned char * and unsigned int} forms one argument, @@ -304,3 +347,12 @@ unsigned short silc_command_get_ident(SilcCommandPayload payload) { return payload->ident; } + +/* Function to set identifier to already allocated Command Payload. Command + payloads are frequentlly resent in SILC and thusly this makes it easy + to set the identifier. */ + +void silc_command_set_ident(SilcCommandPayload payload, unsigned short ident) +{ + payload->ident = ident; +}