Improved UTF-8 encoding and decoding, improved toolkit doc,
[silc.git] / apps / silcd / server.c
index 10530e00d7d4cacffbb5f65e07290a0eb3a5b6e9..1cd96bbbc29fe0c991f27cd7c95e83eccf24f389 100644 (file)
@@ -77,8 +77,6 @@ int silc_server_alloc(SilcServer *new_server)
 
 void silc_server_free(SilcServer server)
 {
-  int i;
-
   if (!server)
     return;
 
@@ -94,13 +92,6 @@ void silc_server_free(SilcServer server)
   }
 #endif
 
-  for (i = 0; i < server->config->param.connections_max; i++) {
-    if (!server->sockets[i])
-      continue;
-    silc_socket_free(server->sockets[i]);
-  }
-  silc_free(server->sockets);
-
   silc_server_config_unref(&server->config_ref);
   if (server->rng)
     silc_rng_free(server->rng);
@@ -176,6 +167,7 @@ bool silc_server_init_secondary(SilcServer server)
     silc_socket_alloc(sock_list[sock],
                      SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
     server->sockets[sock_list[sock]] = newsocket;
+    SILC_SET_LISTENER(newsocket);
 
     /* Perform name and address lookups to resolve the listenning address
        and port. */
@@ -319,6 +311,7 @@ bool silc_server_init(SilcServer server)
      is sent as argument for fast referencing in the future. */
   silc_socket_alloc(sock, SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
   server->sockets[sock] = newsocket;
+  SILC_SET_LISTENER(newsocket);
 
   /* Perform name and address lookups to resolve the listenning address
      and port. */
@@ -345,7 +338,6 @@ bool silc_server_init(SilcServer server)
   server->id = id;
   server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
   server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
-  server->id_type = SILC_ID_SERVER;
   server->server_name = server->config->server_info->server_name;
   server->config->server_info->server_name = NULL;
 
@@ -584,9 +576,33 @@ void silc_server_stop(SilcServer server)
   SILC_LOG_DEBUG(("Stopping server"));
 
   if (server->schedule) {
+    int i;
+
+    /* Close all connections */
+    for (i = 0; i < server->config->param.connections_max; i++) {
+      if (!server->sockets[i])
+       continue;
+      if (!SILC_IS_LISTENER(server->sockets[i])) {
+       silc_schedule_task_del_by_context(server->schedule,
+                                         server->sockets[i]);
+       silc_server_disconnect_remote(server, server->sockets[i], 
+                                     SILC_STATUS_OK, 
+                                     "Server is shutting down");
+      } else {
+       silc_socket_free(server->sockets[i]);
+       server->sockets[i] = NULL;
+      }
+    }
+
+    /* We are not connected to network anymore */
+    server->standalone = TRUE;
+
     silc_schedule_stop(server->schedule);
     silc_schedule_uninit(server->schedule);
     server->schedule = NULL;
+
+    silc_free(server->sockets);
+    server->sockets = NULL;
   }
 
   silc_server_protocols_unregister();
@@ -1201,8 +1217,6 @@ silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
 
   context = (void *)server;
 
-  SILC_LOG_DEBUG(("Start"));
-
   /* Check whether we could resolve both IP and FQDN. */
   if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
                    server->config->require_reverse_lookup)) {
@@ -1229,6 +1243,14 @@ silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
                 sock->ip));
 
   /* Listenning port */
+  if (!server->sockets[(SilcUInt32)proto_ctx->context]) {
+    silc_server_disconnect_remote(server, sock,
+                                 SILC_STATUS_ERR_RESOURCE_LIMIT,
+                                 "Connection refused");
+    server->stat.conn_failures++;
+    silc_free(proto_ctx);
+    return;
+  }
   port = server->sockets[(SilcUInt32)proto_ctx->context]->port;
 
   /* Check whether this connection is denied to connect to us. */
@@ -1295,6 +1317,7 @@ silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
      initiator of the protocol thus we will wait for initiation from
      there before we start the protocol. */
   server->stat.auth_attempts++;
+  SILC_LOG_DEBUG(("Starting key exchange protocol"));
   silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
                      &sock->protocol, proto_ctx,
                      silc_server_accept_new_connection_second);
@@ -1380,6 +1403,7 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
   if ((protocol->state == SILC_PROTOCOL_STATE_ERROR) ||
       (protocol->state == SILC_PROTOCOL_STATE_FAILURE)) {
     /* Error occured during protocol */
+    SILC_LOG_DEBUG(("Error key exchange protocol"));
     silc_protocol_free(protocol);
     sock->protocol = NULL;
     silc_ske_free_key_material(ctx->keymat);
@@ -1412,6 +1436,7 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
                                        ctx->ske->prop->hmac,
                                        ctx->ske->prop->group,
                                        ctx->responder)) {
+    SILC_LOG_ERROR(("Error setting key material in use"));
     silc_protocol_free(protocol);
     sock->protocol = NULL;
     silc_ske_free_key_material(ctx->keymat);
@@ -1457,6 +1482,7 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
      but we won't start it yet. We will be receiving party of this
      protocol thus we will wait that connecting party will make
      their first move. */
+  SILC_LOG_DEBUG(("Starting connection authentication protocol"));
   silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
                      &sock->protocol, proto_ctx,
                      silc_server_accept_new_connection_final);
@@ -1488,11 +1514,10 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
   void *id_entry;
   SilcUInt32 hearbeat_timeout = server->config->param.keepalive_secs;
 
-  SILC_LOG_DEBUG(("Start"));
-
   if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
       protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
     /* Error occured during protocol */
+    SILC_LOG_DEBUG(("Error during authentication protocol"));
     silc_protocol_free(protocol);
     sock->protocol = NULL;
     if (ctx->packet)
@@ -1524,6 +1549,11 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
       if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
                                          &server->config->param,
                                          conn->param, ctx->ske)) {
+       SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
+                      sock->ip));
+       silc_server_disconnect_remote(server, sock,
+                                     SILC_STATUS_ERR_BANNED_FROM_SERVER,
+                                     NULL);
        server->stat.auth_failures++;
        goto out;
       }
@@ -1578,6 +1608,11 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
                                            &server->config->param,
                                            rconn ? rconn->param : NULL,
                                            ctx->ske)) {
+         SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
+                        sock->ip));
+         silc_server_disconnect_remote(server, sock,
+                                       SILC_STATUS_ERR_BANNED_FROM_SERVER,
+                                       NULL);
          server->stat.auth_failures++;
          goto out;
        }
@@ -1602,6 +1637,11 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
                                            &server->config->param,
                                            sconn ? sconn->param : NULL,
                                            ctx->ske)) {
+         SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
+                        sock->ip));
+         silc_server_disconnect_remote(server, sock,
+                                       SILC_STATUS_ERR_BANNED_FROM_SERVER,
+                                       NULL);
          server->stat.auth_failures++;
          goto out;
        }
@@ -1745,17 +1785,19 @@ SILC_TASK_CALLBACK(silc_server_packet_process)
   SilcUInt32 sequence = 0;
   int ret;
 
-  if (!sock)
+  if (!sock) {
+    SILC_LOG_DEBUG(("Unknown socket connection"));
     return;
-
-  SILC_LOG_DEBUG(("Processing packet"));
+  }
 
   /* Packet sending */
 
   if (type == SILC_TASK_WRITE) {
     /* Do not send data to disconnected connection */
-    if (SILC_IS_DISCONNECTED(sock))
+    if (SILC_IS_DISCONNECTED(sock)) {
+      SILC_LOG_DEBUG(("Disconnected socket connection, cannot send"));
       return;
+    }
 
     server->stat.packets_sent++;
 
@@ -1887,8 +1929,6 @@ SILC_TASK_CALLBACK(silc_server_packet_parse_real)
   SilcIDListData idata = (SilcIDListData)sock->user_data;
   int ret;
 
-  SILC_LOG_DEBUG(("Start"));
-
   /* Parse the packet */
   if (parse_ctx->normal)
     ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
@@ -1902,8 +1942,10 @@ SILC_TASK_CALLBACK(silc_server_packet_parse_real)
     goto out;
   }
 
-  if (ret == SILC_PACKET_NONE)
+  if (ret == SILC_PACKET_NONE) {
+    SILC_LOG_DEBUG(("Error parsing packet"));
     goto out;
+  }
 
   /* Check that the the current client ID is same as in the client's packet. */
   if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
@@ -1913,6 +1955,7 @@ SILC_TASK_CALLBACK(silc_server_packet_parse_real)
                                packet->src_id_type);
       if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
        silc_free(id);
+       SILC_LOG_DEBUG(("Packet source is not same as sender"));
        goto out;
       }
       silc_free(id);
@@ -2037,7 +2080,8 @@ void silc_server_packet_parse_type(SilcServer server,
   SilcPacketType type = packet->type;
   SilcIDListData idata = (SilcIDListData)sock->user_data;
 
-  SILC_LOG_DEBUG(("Parsing packet type %d", type));
+  SILC_LOG_DEBUG(("Received %s packet [flags %d]",
+                 silc_get_packet_name(type), packet->flags));
 
   /* Parse the packet type */
   switch (type) {
@@ -2046,8 +2090,6 @@ void silc_server_packet_parse_type(SilcServer server,
       SilcStatus status;
       char *message = NULL;
 
-      SILC_LOG_DEBUG(("Disconnect packet"));
-
       if (packet->flags & SILC_PACKET_FLAG_LIST)
        break;
       if (packet->buffer->len < 1)
@@ -2056,7 +2098,8 @@ void silc_server_packet_parse_type(SilcServer server,
       status = (SilcStatus)packet->buffer->data[0];
       if (packet->buffer->len > 1 &&
          silc_utf8_valid(packet->buffer->data + 1, packet->buffer->len - 1))
-       message = silc_memdup(packet->buffer->data, packet->buffer->len);
+       message = silc_memdup(packet->buffer->data + 1,
+                             packet->buffer->len - 1);
 
       SILC_LOG_ERROR(("Disconnected by %s (%s): %s (%d) %s", 
                      sock->ip, sock->hostname,
@@ -2072,7 +2115,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * one protocol for connection executing at once hence this
      * success message is for whatever protocol is executing currently.
      */
-    SILC_LOG_DEBUG(("Success packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     if (sock->protocol)
@@ -2085,7 +2127,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * one protocol for connection executing at once hence this
      * failure message is for whatever protocol is executing currently.
      */
-    SILC_LOG_DEBUG(("Failure packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     if (sock->protocol) {
@@ -2102,7 +2143,6 @@ void silc_server_packet_parse_type(SilcServer server,
     break;
 
   case SILC_PACKET_REJECT:
-    SILC_LOG_DEBUG(("Reject packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     return;
@@ -2113,7 +2153,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Received notify packet. Server can receive notify packets from
      * router. Server then relays the notify messages to clients if needed.
      */
-    SILC_LOG_DEBUG(("Notify packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       silc_server_notify_list(server, sock, packet);
     else
@@ -2129,7 +2168,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * (although probably most common ones) thus they are handled
      * specially.
      */
-    SILC_LOG_DEBUG(("Channel Message packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     idata->last_receive = time(NULL);
@@ -2143,7 +2181,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * locally connected clients on the particular channel. Router
      * never receives this channel and thus is ignored.
      */
-    SILC_LOG_DEBUG(("Channel Key packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_channel_key(server, sock, packet);
@@ -2157,7 +2194,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Recived command. Processes the command request and allocates the
      * command context and calls the command.
      */
-    SILC_LOG_DEBUG(("Command packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_command_process(server, sock, packet);
@@ -2169,7 +2205,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * may be reply to command sent by us or reply to command sent by client
      * that we've routed further.
      */
-    SILC_LOG_DEBUG(("Command Reply packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_command_reply(server, sock, packet);
@@ -2183,7 +2218,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Received private message packet. The packet is coming from either
      * client or server.
      */
-    SILC_LOG_DEBUG(("Private Message packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     idata->last_receive = time(NULL);
@@ -2203,7 +2237,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Key Exchange protocol packets
      */
   case SILC_PACKET_KEY_EXCHANGE:
-    SILC_LOG_DEBUG(("KE packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
 
@@ -2223,7 +2256,6 @@ void silc_server_packet_parse_type(SilcServer server,
     break;
 
   case SILC_PACKET_KEY_EXCHANGE_1:
-    SILC_LOG_DEBUG(("KE 1 packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
 
@@ -2267,7 +2299,6 @@ void silc_server_packet_parse_type(SilcServer server,
     break;
 
   case SILC_PACKET_KEY_EXCHANGE_2:
-    SILC_LOG_DEBUG(("KE 2 packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
 
@@ -2317,7 +2348,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * authentication method for the connection. This packet maybe received
      * at any time.
      */
-    SILC_LOG_DEBUG(("Connection authentication request packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_connection_auth_request(server, sock, packet);
@@ -2329,7 +2359,6 @@ void silc_server_packet_parse_type(SilcServer server,
   case SILC_PACKET_CONNECTION_AUTH:
     /* Start of the authentication protocol. We receive here the
        authentication data and will verify it. */
-    SILC_LOG_DEBUG(("Connection auth packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
 
@@ -2356,7 +2385,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * to distribute information about new registered entities in the
      * SILC network.
      */
-    SILC_LOG_DEBUG(("New ID packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       silc_server_new_id_list(server, sock, packet);
     else
@@ -2369,7 +2397,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * we will use to create initial client ID. After creating new
      * ID we will send it to the client.
      */
-    SILC_LOG_DEBUG(("New Client packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_new_client(server, sock, packet);
@@ -2381,7 +2408,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * information that we may save. This is received after server has
      * connected to us.
      */
-    SILC_LOG_DEBUG(("New Server packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_new_server(server, sock, packet);
@@ -2392,7 +2418,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Received new channel packet. Information about new channel in the
      * network are distributed using this packet.
      */
-    SILC_LOG_DEBUG(("New Channel packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       silc_server_new_channel_list(server, sock, packet);
     else
@@ -2403,7 +2428,6 @@ void silc_server_packet_parse_type(SilcServer server,
     /*
      * Received heartbeat.
      */
-    SILC_LOG_DEBUG(("Heartbeat packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     break;
@@ -2412,7 +2436,6 @@ void silc_server_packet_parse_type(SilcServer server,
     /*
      * Received heartbeat.
      */
-    SILC_LOG_DEBUG(("Key agreement packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_key_agreement(server, sock, packet);
@@ -2423,7 +2446,6 @@ void silc_server_packet_parse_type(SilcServer server,
      * Received re-key packet. The sender wants to regenerate the session
      * keys.
      */
-    SILC_LOG_DEBUG(("Re-key packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_rekey(server, sock, packet);
@@ -2433,7 +2455,6 @@ void silc_server_packet_parse_type(SilcServer server,
     /*
      * The re-key is done.
      */
-    SILC_LOG_DEBUG(("Re-key done packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
 
@@ -2458,7 +2479,6 @@ void silc_server_packet_parse_type(SilcServer server,
 
   case SILC_PACKET_FTP:
     /* FTP packet */
-    SILC_LOG_DEBUG(("FTP packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_ftp(server, sock, packet);
@@ -2466,7 +2486,6 @@ void silc_server_packet_parse_type(SilcServer server,
 
   case SILC_PACKET_RESUME_CLIENT:
     /* Resume client */
-    SILC_LOG_DEBUG(("Resume Client packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_resume_client(server, sock, packet);
@@ -2475,7 +2494,6 @@ void silc_server_packet_parse_type(SilcServer server,
   case SILC_PACKET_RESUME_ROUTER:
     /* Resume router packet received. This packet is received for backup
        router resuming protocol. */
-    SILC_LOG_DEBUG(("Resume router packet"));
     if (packet->flags & SILC_PACKET_FLAG_LIST)
       break;
     silc_server_backup_resume_router(server, sock, packet);
@@ -2485,7 +2503,6 @@ void silc_server_packet_parse_type(SilcServer server,
     SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
     break;
   }
-
 }
 
 /* Creates connection to a remote router. */
@@ -2518,8 +2535,10 @@ SILC_TASK_CALLBACK(silc_server_close_connection_final)
 void silc_server_close_connection(SilcServer server,
                                  SilcSocketConnection sock)
 {
-  if (!server->sockets[sock->sock])
+  if (!server->sockets[sock->sock] && SILC_IS_DISCONNECTED(sock)) {
+    silc_socket_free(sock);
     return;
+  }
 
   SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
                   sock->port,