Added SILC Thread Queue API
[crypto.git] / apps / silcd / command.h
index d7ed494402f85815512825d49220991aab85bb6e..b9b7e2fef61d6c38cc6834cda95eb4d426020eea 100644 (file)
@@ -1,16 +1,16 @@
+
 /*
 
   servercommand.h
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2000 Pekka Riikonen
+  Copyright (C) 1997 - 2005, 2007 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
@@ -21,8 +21,8 @@
 #ifndef COMMAND_H
 #define COMMAND_H
 
-/* 
-   Structure holding one command and pointer to its function. 
+/*
+   Structure holding one command and pointer to its function.
 
    SilcCommandCb cb
 
@@ -35,7 +35,7 @@
    SilcCommandFlag flags
 
        Flags for the command. These set how command behaves on different
-       situations. 
+       situations.
 
 */
 typedef struct {
@@ -50,27 +50,27 @@ extern SilcServerCommand silc_command_list[];
 /* Context sent as argument to all commands */
 typedef struct {
   SilcServer server;
-  SilcSocketConnection sock;
+  SilcPacketStream sock;
   SilcCommandPayload payload;
-  SilcPacketContext *packet;
-  int pending;
+  SilcArgumentPayload args;
+  SilcPacket packet;
+  int pending;                 /* Command is being re-processed when TRUE */
+  int users;                   /* Reference counter */
 } *SilcServerCommandContext;
 
 /* Structure holding pending commands. If command is pending it will be
-   executed after command reply has been received and executed. 
-   Pending commands are used in cases where the original command request
-   had to be forwarded to router. After router replies the pending
-   command is re-executed. */
+   executed after command reply has been received and executed. */
 typedef struct SilcServerCommandPendingStruct {
   SilcCommand reply_cmd;
-  void *context;
+  SilcUInt16 ident;
+  unsigned int reply_check : 8;
   SilcCommandCb callback;
-
+  void *context;
+  SilcTask timeout;
   struct SilcServerCommandPendingStruct *next;
 } SilcServerCommandPending;
 
-/* List of pending commands */
-extern SilcServerCommandPending *silc_command_pending;
+#include "command_reply.h"
 
 /* Macros */
 
@@ -78,60 +78,53 @@ extern SilcServerCommandPending *silc_command_pending;
 #define SILC_SERVER_CMD(func, cmd, flags) \
 { silc_server_command_##func, SILC_COMMAND_##cmd, flags }
 
-/* Macro used to declare command functions */
+/* Macro used to declare command functions. The `context' will be the
+   SilcServerCommandContext and the `context2' is the
+   SilcServerCommandReplyContext if this function is called from the
+   command reply as pending command callback. Otherwise `context2'
+   is NULL. */
 #define SILC_SERVER_CMD_FUNC(func) \
-void silc_server_command_##func(void *context)
-
-/* Macro used to execute commands */
-#define SILC_SERVER_COMMAND_EXEC(ctx)                          \
-do {                                                           \
-  SilcServerCommand *cmd;                                      \
-                                                               \
-  for (cmd = silc_command_list; cmd->cb; cmd++)                        \
-    if (cmd->cmd == silc_command_get(ctx->payload)) {          \
-      cmd->cb(ctx);                                            \
-      break;                                                   \
-    }                                                          \
-                                                               \
-  if (cmd == NULL) {                                           \
-    SILC_LOG_ERROR(("Unknown command, packet dropped"));       \
-    silc_free(ctx);                                            \
-    return;                                                    \
-  }                                                            \
-} while(0)
-
-/* Checks for pending commands */
-#define SILC_SERVER_COMMAND_CHECK_PENDING(ctx)         \
-do {                                                   \
-  if (silc_command_pending) {                          \
-    SilcServerCommandPending *r;                       \
-    SilcCommand cmd;                                   \
-                                                       \
-    cmd = silc_command_get(payload);                   \
-    for (r = silc_command_pending; r; r = r->next) {   \
-      if (r->reply_cmd == cmd) {                       \
-       ctx->context = r->context;                      \
-       ctx->callback = r->callback;                    \
-       break;                                          \
-      }                                                        \
-    }                                                  \
-  }                                                    \
-} while(0)
-
-/* Executed pending command */
-#define SILC_SERVER_COMMAND_EXEC_PENDING(ctx, cmd)     \
-do {                                                   \
-  if (ctx->callback) {                                 \
-    (*ctx->callback)(ctx->context);                    \
-    silc_server_command_pending_del(cmd);              \
-  }                                                    \
+void silc_server_command_##func(void *context, void *context2)
+
+/* Executed pending command. The first argument to the callback function
+   is the user specified context. The second argument is always the
+   SilcServerCommandReply context. */
+#define SILC_SERVER_PENDING_EXEC(ctx, cmd)                             \
+do {                                                                   \
+  int _i;                                                              \
+  for (_i = 0; _i < ctx->callbacks_count; _i++)                                \
+    if (ctx->callbacks[_i].callback)                                   \
+      (*ctx->callbacks[_i].callback)(ctx->callbacks[_i].context, ctx); \
+  silc_server_command_pending_del(ctx->server, cmd, ctx->ident);       \
 } while(0)
 
 /* Prototypes */
-void silc_server_command_pending(SilcCommand reply_cmd,
+void silc_server_command_process(SilcServer server,
+                                SilcPacketStream sock,
+                                SilcPacket packet);
+SilcServerCommandContext silc_server_command_alloc();
+void silc_server_command_free(SilcServerCommandContext ctx);
+SilcServerCommandContext
+silc_server_command_dup(SilcServerCommandContext ctx);
+SilcBool silc_server_command_pending(SilcServer server,
+                                SilcCommand reply_cmd,
+                                SilcUInt16 ident,
                                 SilcCommandCb callback,
                                 void *context);
-void silc_server_command_pending_del(SilcCommand reply_cmd);
+SilcBool silc_server_command_pending_timed(SilcServer server,
+                                      SilcCommand reply_cmd,
+                                      SilcUInt16 ident,
+                                      SilcCommandCb callback,
+                                      void *context,
+                                      SilcUInt16 timeout);
+void silc_server_command_pending_del(SilcServer server,
+                                    SilcCommand reply_cmd,
+                                    SilcUInt16 ident);
+SilcServerCommandPendingCallbacks
+silc_server_command_pending_check(SilcServer server,
+                                 SilcCommand command,
+                                 SilcUInt16 ident,
+                                 SilcUInt32 *callbacks_count);
 SILC_SERVER_CMD_FUNC(whois);
 SILC_SERVER_CMD_FUNC(whowas);
 SILC_SERVER_CMD_FUNC(identify);
@@ -143,22 +136,26 @@ SILC_SERVER_CMD_FUNC(invite);
 SILC_SERVER_CMD_FUNC(quit);
 SILC_SERVER_CMD_FUNC(kill);
 SILC_SERVER_CMD_FUNC(info);
-SILC_SERVER_CMD_FUNC(connect);
+SILC_SERVER_CMD_FUNC(stats);
 SILC_SERVER_CMD_FUNC(ping);
 SILC_SERVER_CMD_FUNC(oper);
-SILC_SERVER_CMD_FUNC(pass);
-SILC_SERVER_CMD_FUNC(admin);
 SILC_SERVER_CMD_FUNC(join);
 SILC_SERVER_CMD_FUNC(motd);
 SILC_SERVER_CMD_FUNC(umode);
 SILC_SERVER_CMD_FUNC(cmode);
+SILC_SERVER_CMD_FUNC(cumode);
 SILC_SERVER_CMD_FUNC(kick);
-SILC_SERVER_CMD_FUNC(ignore);
-SILC_SERVER_CMD_FUNC(restart);
-SILC_SERVER_CMD_FUNC(close);
-SILC_SERVER_CMD_FUNC(die);
+SILC_SERVER_CMD_FUNC(ban);
+SILC_SERVER_CMD_FUNC(detach);
+SILC_SERVER_CMD_FUNC(watch);
 SILC_SERVER_CMD_FUNC(silcoper);
 SILC_SERVER_CMD_FUNC(leave);
-SILC_SERVER_CMD_FUNC(names);
+SILC_SERVER_CMD_FUNC(users);
+SILC_SERVER_CMD_FUNC(getkey);
+SILC_SERVER_CMD_FUNC(service);
+
+SILC_SERVER_CMD_FUNC(connect);
+SILC_SERVER_CMD_FUNC(close);
+SILC_SERVER_CMD_FUNC(shutdown);
 
 #endif