from SILC packets. */
struct SilcCommandPayloadStruct {
SilcCommand cmd;
- uint16 ident;
+ SilcUInt16 ident;
SilcArgumentPayload args;
};
/* Parses command payload returning new command payload structure */
SilcCommandPayload silc_command_payload_parse(const unsigned char *payload,
- uint32 payload_len)
+ SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcCommandPayload new;
+ SilcCommandPayload newp;
unsigned char args_num;
- uint16 p_len;
+ SilcUInt16 p_len;
int ret;
SILC_LOG_DEBUG(("Parsing command payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Command Payload */
ret = silc_buffer_unformat(&buffer,
SILC_STR_UI_SHORT(&p_len),
- SILC_STR_UI_CHAR(&new->cmd),
+ SILC_STR_UI_CHAR(&newp->cmd),
SILC_STR_UI_CHAR(&args_num),
- SILC_STR_UI_SHORT(&new->ident),
+ SILC_STR_UI_SHORT(&newp->ident),
SILC_STR_END);
if (ret == -1) {
- silc_free(new);
+ silc_free(newp);
return NULL;
}
if (p_len != buffer.len) {
SILC_LOG_ERROR(("Incorrect command payload in packet, packet dropped"));
- silc_free(new);
+ silc_free(newp);
return NULL;
}
- if (new->cmd == 0) {
- silc_free(new);
+ if (newp->cmd == 0) {
+ silc_free(newp);
return NULL;
}
silc_buffer_pull(&buffer, SILC_COMMAND_PAYLOAD_LEN);
if (args_num) {
- new->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num);
- if (!new->args) {
- silc_free(new);
+ newp->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num);
+ if (!newp->args) {
+ silc_free(newp);
return NULL;
}
}
silc_buffer_push(&buffer, SILC_COMMAND_PAYLOAD_LEN);
- return new;
+ return newp;
}
/* Encodes Command Payload returning it to SilcBuffer. */
SilcBuffer silc_command_payload_encode(SilcCommand cmd,
- uint32 argc,
+ SilcUInt32 argc,
unsigned char **argv,
- uint32 *argv_lens,
- uint32 *argv_types,
- uint16 ident)
+ SilcUInt32 *argv_lens,
+ SilcUInt32 *argv_types,
+ SilcUInt16 ident)
{
SilcBuffer buffer;
SilcBuffer args = NULL;
- uint32 len = 0;
+ SilcUInt32 len = 0;
SILC_LOG_DEBUG(("Encoding command payload"));
}
len += SILC_COMMAND_PAYLOAD_LEN;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Create Command payload */
silc_buffer_format(buffer,
{
SilcBuffer buffer;
SilcBuffer args = NULL;
- uint32 len = 0;
- uint32 argc = 0;
+ SilcUInt32 len = 0;
+ SilcUInt32 argc = 0;
SILC_LOG_DEBUG(("Encoding command payload"));
}
len += SILC_COMMAND_PAYLOAD_LEN;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer) {
+ if (args)
+ silc_buffer_free(args);
+ return NULL;
+ }
/* Create Command payload */
silc_buffer_format(buffer,
}
/* Encodes Command payload with variable argument list. The arguments
- must be: uint32, unsigned char *, unsigned int, ... One
- {uint32, unsigned char * and unsigned int} forms one argument,
- thus `argc' in case when sending one {uint32, unsigned char *
- and uint32} equals one (1) and when sending two of those it
+ must be: SilcUInt32, unsigned char *, unsigned int, ... One
+ {SilcUInt32, unsigned char * and unsigned int} forms one argument,
+ thus `argc' in case when sending one {SilcUInt32, unsigned char *
+ and SilcUInt32} 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_payload_encode_va(SilcCommand cmd,
- uint16 ident,
- uint32 argc, ...)
+ SilcUInt16 ident,
+ SilcUInt32 argc, ...)
{
va_list ap;
SilcBuffer buffer;
/* Same as above but with va_list. */
SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd,
- uint16 ident,
- uint32 argc, va_list ap)
+ SilcUInt16 ident,
+ SilcUInt32 argc, va_list ap)
{
unsigned char **argv = NULL;
- uint32 *argv_lens = NULL, *argv_types = NULL;
+ SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
unsigned char *x;
- uint32 x_len;
- uint32 x_type;
- SilcBuffer buffer;
+ SilcUInt32 x_len;
+ SilcUInt32 x_type;
+ SilcBuffer buffer = NULL;
int i, k = 0;
if (argc) {
argv = silc_calloc(argc, sizeof(unsigned char *));
- argv_lens = silc_calloc(argc, sizeof(uint32));
- argv_types = silc_calloc(argc, sizeof(uint32));
+ if (!argv)
+ return NULL;
+ argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_lens)
+ return NULL;
+ argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_types)
+ return NULL;
for (i = 0, k = 0; i < argc; i++) {
- x_type = va_arg(ap, uint32);
+ x_type = va_arg(ap, SilcUInt32);
x = va_arg(ap, unsigned char *);
- x_len = va_arg(ap, uint32);
+ x_len = va_arg(ap, SilcUInt32);
if (!x_type || !x || !x_len)
continue;
- argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char));
- memcpy(argv[k], x, x_len);
+ argv[k] = silc_memdup(x, x_len);
+ if (!argv[k])
+ goto out;
argv_lens[k] = x_len;
argv_types[k] = x_type;
k++;
buffer = silc_command_payload_encode(cmd, k, argv, argv_lens,
argv_types, ident);
+ out:
for (i = 0; i < k; i++)
silc_free(argv[i]);
silc_free(argv);
SilcBuffer
silc_command_reply_payload_encode_va(SilcCommand cmd,
SilcCommandStatus status,
- uint16 ident,
- uint32 argc, ...)
+ SilcUInt16 ident,
+ SilcUInt32 argc, ...)
{
va_list ap;
SilcBuffer buffer;
SilcBuffer
silc_command_reply_payload_encode_vap(SilcCommand cmd,
SilcCommandStatus status,
- uint16 ident, uint32 argc,
+ SilcUInt16 ident, SilcUInt32 argc,
va_list ap)
{
unsigned char **argv;
- uint32 *argv_lens = NULL, *argv_types = NULL;
+ SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
unsigned char status_data[2];
unsigned char *x;
- uint32 x_len;
- uint32 x_type;
- SilcBuffer buffer;
+ SilcUInt32 x_len;
+ SilcUInt32 x_type;
+ SilcBuffer buffer = NULL;
int i, k;
argc++;
argv = silc_calloc(argc, sizeof(unsigned char *));
- argv_lens = silc_calloc(argc, sizeof(uint32));
- argv_types = silc_calloc(argc, sizeof(uint32));
+ if (!argv)
+ return NULL;
+ argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_lens) {
+ silc_free(argv);
+ return NULL;
+ }
+ argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_types) {
+ silc_free(argv_lens);
+ silc_free(argv);
+ return NULL;
+ }
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[0] = silc_memdup(status_data, sizeof(status_data));
+ if (!argv[0]) {
+ silc_free(argv_types);
+ silc_free(argv_lens);
+ silc_free(argv);
+ return NULL;
+ }
argv_lens[0] = sizeof(status_data);
argv_types[0] = 1;
for (i = 1, k = 1; i < argc; i++) {
- x_type = va_arg(ap, uint32);
+ x_type = va_arg(ap, SilcUInt32);
x = va_arg(ap, unsigned char *);
- x_len = va_arg(ap, uint32);
+ x_len = va_arg(ap, SilcUInt32);
if (!x_type || !x || !x_len)
continue;
- argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char));
- memcpy(argv[k], x, x_len);
+ argv[k] = silc_memdup(x, x_len);
+ if (!argv[k])
+ goto out;
argv_lens[k] = x_len;
argv_types[k] = x_type;
k++;
buffer = silc_command_payload_encode(cmd, k, argv, argv_lens,
argv_types, ident);
+ out:
for (i = 0; i < k; i++)
silc_free(argv[i]);
silc_free(argv);
/* Returns identifier */
-uint16 silc_command_get_ident(SilcCommandPayload payload)
+SilcUInt16 silc_command_get_ident(SilcCommandPayload payload)
{
return payload->ident;
}
+/* Return command status */
+
+SilcCommandStatus silc_command_get_status(SilcCommandPayload payload)
+{
+ unsigned char *tmp;
+ SilcCommandStatus status;
+
+ if (!payload->args)
+ return 0;
+ tmp = silc_argument_get_arg_type(payload->args, 1, NULL);
+ if (!tmp)
+ return 0;
+
+ SILC_GET16_MSB(status, tmp);
+ return status;
+}
+
/* 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, uint16 ident)
+void silc_command_set_ident(SilcCommandPayload payload, SilcUInt16 ident)
{
payload->ident = ident;
}