Merged from silc_1_0_branch.
[silc.git] / lib / silccore / silccommand.c
index 881477d1111756052d3b1c022bb8414cc6ebabcc..a16dfc6fce792b6e0c8edfe1ac0d17265120b219 100644 (file)
 /*
 
-  silccommand.c
+  silccommand.c 
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2000 Pekka Riikonen
+  Copyright (C) 1997 - 2002 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.
-  
+  the Free Software Foundation; version 2 of the License.
+
   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.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.1  2000/06/27 11:36:55  priikone
- * Initial revision
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 #include "silccommand.h"
 
+/******************************************************************************
+
+                              Command Payload
+
+******************************************************************************/
+
 /* Command Payload structure. Contents of this structure is parsed
    from SILC packets. */
 struct SilcCommandPayloadStruct {
   SilcCommand cmd;
-  unsigned int argc;
-  unsigned char **argv;
-  unsigned int *argv_lens;
-  unsigned int *argv_types;
-  unsigned int pos;
+  SilcUInt16 ident;
+  SilcArgumentPayload args;
 };
 
 /* Length of the command payload */
-#define SILC_COMMAND_PAYLOAD_LEN 4
+#define SILC_COMMAND_PAYLOAD_LEN 6
 
 /* Parses command payload returning new command payload structure */
 
-SilcCommandPayload silc_command_parse_payload(SilcBuffer buffer)
+SilcCommandPayload silc_command_payload_parse(const unsigned char *payload,
+                                             SilcUInt32 payload_len)
 {
-  SilcCommandPayload new;
-  unsigned short payload_len = 0;
-  unsigned char args_num = 0;
-  unsigned char arg_num = 0;
-  unsigned int arg_type = 0;
-  unsigned int pull_len = 0;
-  int i = 0;
+  SilcBufferStruct buffer;
+  SilcCommandPayload newp;
+  unsigned char args_num;
+  SilcUInt16 p_len;
+  int ret;
 
   SILC_LOG_DEBUG(("Parsing command payload"));
 
-  new = silc_calloc(1, sizeof(*new));
-  if (!new) {
-    SILC_LOG_ERROR(("Could not allocate new command payload"));
+  silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
+  newp = silc_calloc(1, sizeof(*newp));
+  if (!newp)
     return NULL;
-  }
 
   /* Parse the Command Payload */
-  silc_buffer_unformat(buffer, 
-                      SILC_STR_UI_CHAR(&new->cmd),
-                      SILC_STR_UI_CHAR(&args_num),
-                      SILC_STR_UI_SHORT(&payload_len),
-                      SILC_STR_END);
+  ret = silc_buffer_unformat(&buffer, 
+                            SILC_STR_UI_SHORT(&p_len),
+                            SILC_STR_UI_CHAR(&newp->cmd),
+                            SILC_STR_UI_CHAR(&args_num),
+                            SILC_STR_UI_SHORT(&newp->ident),
+                            SILC_STR_END);
+  if (ret == -1) {
+    silc_free(newp);
+    return NULL;
+  }
 
-  if (payload_len != buffer->len) {
+  if (p_len != buffer.len) {
     SILC_LOG_ERROR(("Incorrect command payload in packet, packet dropped"));
+    silc_free(newp);
     return NULL;
   }
 
-  if (new->cmd == 0)
+  if (newp->cmd == 0) {
+    SILC_LOG_ERROR(("Incorrect command type in command payload"));
+    silc_free(newp);
     return NULL;
+  }
 
-  if (args_num && payload_len) {
-
-    new->argv = silc_calloc(args_num, sizeof(unsigned char *));
-    new->argv_lens = silc_calloc(args_num, sizeof(unsigned int));
-    new->argv_types = silc_calloc(args_num, sizeof(unsigned int));
-
-    silc_buffer_pull(buffer, SILC_COMMAND_PAYLOAD_LEN);
-    pull_len += SILC_COMMAND_PAYLOAD_LEN;
-
-    /* Parse Command Argument Payloads */
-    arg_num = 1;
-    while(arg_num) {
-      silc_buffer_unformat(buffer,
-                          SILC_STR_UI_CHAR(&arg_num),
-                          SILC_STR_UI_CHAR(&arg_type),
-                          SILC_STR_UI_SHORT(&payload_len),
-                          SILC_STR_END);
-
-      /* Check that argument number is correct */
-      if (arg_num != i + 1)
-       goto err;
-
-      new->argv_lens[i] = payload_len;
-      new->argv_types[i] = arg_type;
-
-      /* Get argument data */
-      silc_buffer_pull(buffer, 4);
-      silc_buffer_unformat(buffer,
-                          SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i], 
-                                                     payload_len),
-                          SILC_STR_END);
-      silc_buffer_pull(buffer, payload_len);
-      pull_len += 4 + payload_len;
-
-      i++;
-
-      if (i == args_num)
-       break;
+  silc_buffer_pull(&buffer, SILC_COMMAND_PAYLOAD_LEN);
+  if (args_num) {
+    newp->args = silc_argument_payload_parse(buffer.data, buffer.len, 
+                                            args_num);
+    if (!newp->args) {
+      silc_free(newp);
+      return NULL;
     }
-
-    /* Check the number of arguments */
-    if (arg_num != args_num)
-      goto err;
   }
+  silc_buffer_push(&buffer, SILC_COMMAND_PAYLOAD_LEN);
 
-  new->argc = i;
-  new->pos = 0;
+  return newp;
+}
 
-  silc_buffer_push(buffer, pull_len);
+/* Encodes Command Payload returning it to SilcBuffer. */
 
-  return new;
+SilcBuffer silc_command_payload_encode(SilcCommand cmd,
+                                      SilcUInt32 argc,
+                                      unsigned char **argv,
+                                      SilcUInt32 *argv_lens,
+                                      SilcUInt32 *argv_types,
+                                      SilcUInt16 ident)
+{
+  SilcBuffer buffer;
+  SilcBuffer args = NULL;
+  SilcUInt32 len = 0;
 
- err:
-  if (i) {
-    int k;
+  SILC_LOG_DEBUG(("Encoding command payload"));
 
-    for (k = 0; k < i; k++)
-      silc_free(new->argv[k]);
+  if (argc) {
+    args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
+    if (!args)
+      return NULL;
+    len = args->len;
   }
 
-  silc_free(new->argv);
-  silc_free(new->argv_lens);
-  silc_free(new->argv_types);
+  len += SILC_COMMAND_PAYLOAD_LEN;
+  buffer = silc_buffer_alloc_size(len);
+  if (!buffer)
+    return NULL;
 
-  if (new)
-    silc_free(new);
+  /* Create Command payload */
+  silc_buffer_format(buffer,
+                    SILC_STR_UI_SHORT(len),
+                    SILC_STR_UI_CHAR(cmd),
+                    SILC_STR_UI_CHAR(argc),
+                    SILC_STR_UI_SHORT(ident),
+                    SILC_STR_END);
+
+  /* Add arguments */
+  if (argc) {
+    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_buffer_free(args);
+  }
 
-  return NULL;
+  return buffer;
 }
 
-/* Encodes Command Payload returning it to SilcBuffer. */
+/* Same as above but encode the buffer from SilcCommandPayload structure
+   instead of raw data. */
 
-SilcBuffer silc_command_encode_payload(SilcCommand cmd,
-                                      unsigned int argc,
-                                      unsigned char **argv,
-                                      unsigned int *argv_lens,
-                                      unsigned int *argv_types)
+SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload)
 {
   SilcBuffer buffer;
-  unsigned int len;
-  int i;
+  SilcBuffer args = NULL;
+  SilcUInt32 len = 0;
+  SilcUInt32 argc = 0;
 
   SILC_LOG_DEBUG(("Encoding command payload"));
 
-  len = 1 + 1 + 2;
-  for (i = 0; i < argc; i++)
-    len += 1 + 1 + 2 + argv_lens[i];
+  if (payload->args) {
+    args = silc_argument_payload_encode_payload(payload->args);
+    if (args)
+      len = args->len;
+    argc = silc_argument_get_arg_num(payload->args);
+  }
 
-  buffer = silc_buffer_alloc(len);
-  silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+  len += SILC_COMMAND_PAYLOAD_LEN;
+  buffer = silc_buffer_alloc_size(len);
+  if (!buffer) {
+    if (args)
+      silc_buffer_free(args);
+    return NULL;
+  }
 
   /* Create Command payload */
   silc_buffer_format(buffer,
-                    SILC_STR_UI_CHAR(cmd),
-                    SILC_STR_UI_CHAR(argc),
                     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);
 
-  /* Put arguments */
-  if (argc) {
-    silc_buffer_pull(buffer, 4);
-   
-    for (i = 0; i < argc; i++) {
-      silc_buffer_format(buffer,
-                        SILC_STR_UI_CHAR(i + 1),
-                        SILC_STR_UI_CHAR(argv_types[i]),
-                        SILC_STR_UI_SHORT(argv_lens[i]),
-                        SILC_STR_UI_XNSTRING(argv[i], argv_lens[i]),
-                        SILC_STR_END);
-      silc_buffer_pull(buffer, 4 + argv_lens[i]);
-    }
-
-    silc_buffer_push(buffer, len);
+  /* 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_buffer_free(args);
   }
 
   return buffer;
 }
 
 /* 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. */
-
-SilcBuffer silc_command_encode_payload_va(SilcCommand cmd, 
-                                         unsigned int argc, ...)
+   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, 
+                                         SilcUInt16 ident, 
+                                         SilcUInt32 argc, ...)
 {
   va_list ap;
-  unsigned char **argv;
-  unsigned int *argv_lens = NULL, *argv_types = NULL;
+  SilcBuffer buffer;
+
+  va_start(ap, argc);
+  buffer = silc_command_payload_encode_vap(cmd, ident, argc, ap);
+  va_end(ap);
+
+  return buffer;
+}
+
+/* Same as above but with va_list. */
+
+SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
+                                          SilcUInt16 ident, 
+                                          SilcUInt32 argc, va_list ap)
+{
+  unsigned char **argv = NULL;
+  SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
   unsigned char *x;
-  unsigned int x_len;
+  SilcUInt32 x_len;
+  SilcUInt32 x_type;
+  SilcBuffer buffer = NULL;
+  int i, k = 0;
+
+  if (argc) {
+    argv = silc_calloc(argc, sizeof(unsigned char *));
+    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, SilcUInt32);
+      x = va_arg(ap, unsigned char *);
+      x_len = va_arg(ap, SilcUInt32);
+      
+      if (!x_type || !x || !x_len)
+       continue;
+      
+      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);
+  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_reply_payload_encode_va(SilcCommand cmd, 
+                                    SilcStatus status,
+                                    SilcStatus error,
+                                    SilcUInt16 ident,
+                                    SilcUInt32 argc, ...)
+{
+  va_list ap;
   SilcBuffer buffer;
-  int i;
 
   va_start(ap, argc);
+  buffer = silc_command_reply_payload_encode_vap(cmd, status, error,
+                                                ident, argc, ap);
+  va_end(ap);
+
+  return buffer;
+}
+
+SilcBuffer 
+silc_command_reply_payload_encode_vap(SilcCommand cmd, 
+                                     SilcStatus status,
+                                     SilcStatus error,
+                                     SilcUInt16 ident, SilcUInt32 argc, 
+                                     va_list ap)
+{
+  unsigned char **argv;
+  SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
+  unsigned char status_data[2];
+  unsigned char *x;
+  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(unsigned int));
-  argv_types = silc_calloc(argc, sizeof(unsigned int));
+  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;
+  }
 
-  for (i = 0; i < argc; i++) {
+  status_data[0] = status;
+  status_data[1] = error;
+  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, SilcUInt32);
     x = va_arg(ap, unsigned char *);
-    x_len = va_arg(ap, unsigned int);
+    x_len = va_arg(ap, SilcUInt32);
 
-    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;
+    if (!x_type || !x || !x_len)
+      continue;
+
+    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_encode_payload(cmd, argc, argv
-                                      argv_lens, argv_types);
+  buffer = silc_command_payload_encode(cmd, k, argv, argv_lens
+                                      argv_types, ident);
 
-  for (i = 0; i < argc; i++)
+ out:
+  for (i = 0; i < k; i++)
     silc_free(argv[i]);
   silc_free(argv);
   silc_free(argv_lens);
@@ -242,99 +363,92 @@ SilcBuffer silc_command_encode_payload_va(SilcCommand cmd,
   return buffer;
 }
 
-/* Free's Command Payload */
+/* Frees Command Payload */
 
-void silc_command_free_payload(SilcCommandPayload payload)
+void silc_command_payload_free(SilcCommandPayload payload)
 {
-  int i;
-
   if (payload) {
-    for (i = 0; i < payload->argc; i++)
-      silc_free(payload->argv[i]);
-
-    silc_free(payload->argv);
+    silc_argument_payload_free(payload->args);
     silc_free(payload);
   }
 }
 
-/* Returns the command type in payload */
+/* Returns command */
 
 SilcCommand silc_command_get(SilcCommandPayload payload)
 {
   return payload->cmd;
 }
 
-/* Returns number of arguments in payload */
+/* Retuns arguments payload */
 
-unsigned int silc_command_get_arg_num(SilcCommandPayload payload)
+SilcArgumentPayload silc_command_get_args(SilcCommandPayload payload)
 {
-  return payload->argc;
+  return payload->args;
 }
 
-/* Returns first argument from payload. */
+/* Returns identifier */
 
-unsigned char *silc_command_get_first_arg(SilcCommandPayload payload,
-                                         unsigned int *ret_len)
+SilcUInt16 silc_command_get_ident(SilcCommandPayload payload)
 {
-  payload->pos = 0;
-
-  if (ret_len)
-    *ret_len = payload->argv_lens[payload->pos];
-
-  return payload->argv[payload->pos++];
+  return payload->ident;
 }
 
-/* Returns next argument from payload or NULL if no more arguments. */
+/* Return command status */
 
-unsigned char *silc_command_get_next_arg(SilcCommandPayload payload,
-                                        unsigned int *ret_len)
+bool silc_command_get_status(SilcCommandPayload payload, 
+                            SilcStatus *status,
+                            SilcStatus *error)
 {
-  if (payload->pos >= payload->argc)
-    return NULL;
+  unsigned char *tmp;
+  SilcUInt32 tmp_len;
+
+  if (!payload->args)
+    return 0;
+  tmp = silc_argument_get_arg_type(payload->args, 1, &tmp_len);
+  if (!tmp || tmp_len != 2)
+    return 0;
+
+  /* Check for 1.0 protocol version which didn't have `error' */
+  if (tmp[0] == 0 && tmp[1] != 0) {
+    /* Protocol 1.0 version */
+    SilcStatus s;
+    SILC_GET16_MSB(s, tmp);
+    if (status)
+      *status = s;
+    if (error)
+      *error = 0;
+    if (s >= SILC_STATUS_ERR_NO_SUCH_NICK && error)
+      *error = s;
+    return (s < SILC_STATUS_ERR_NO_SUCH_NICK);
+  }
 
-  if (ret_len)
-    *ret_len = payload->argv_lens[payload->pos];
+  /* Take both status and possible error */
+  if (status)
+    *status = (SilcStatus)tmp[0];
+  if (error)
+    *error = (SilcStatus)tmp[1];
 
-  return payload->argv[payload->pos++];
+  /* If single error occurred have the both `status' and `error' indicate
+     the error value for convenience. */
+  if (tmp[0] >= SILC_STATUS_ERR_NO_SUCH_NICK && error)
+    *error = tmp[0];
+
+  return (tmp[0] < SILC_STATUS_ERR_NO_SUCH_NICK && tmp[1] == SILC_STATUS_OK);
 }
 
-/* Returns argument which type is `type'. */
+/* 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. */
 
-unsigned char *silc_command_get_arg_type(SilcCommandPayload payload,
-                                        unsigned int type,
-                                        unsigned int *ret_len)
+void silc_command_set_ident(SilcCommandPayload payload, SilcUInt16 ident)
 {
-  int i;
-
-  for (i = 0; i < payload->argc; i++)
-    if (payload->argv_types[i] == type)
-      break;
-
-  if (i >= payload->argc)
-    return NULL;
-
-  if (ret_len)
-    *ret_len = payload->argv_lens[i];
-
-  return payload->argv[i];
+  payload->ident = ident;
 }
 
-/* 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. */
+/* Function to set the command to already allocated Command Payload. */
 
-SilcBuffer silc_command_encode_status_payload(SilcCommandStatus status,
-                                             unsigned char *data,
-                                             unsigned int len)
+void silc_command_set_command(SilcCommandPayload payload, SilcCommand command)
 {
-  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;
+  payload->cmd = command;
 }