updates.
[silc.git] / apps / silcd / server.c
index 1160f10bbab9d9ad9e123ef517fbeba77511555d..707e1d2d385beb1dfb2f656fd54517528da5562e 100644 (file)
@@ -931,87 +931,58 @@ SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
   sock->protocol = NULL;
 }
 
-/* Accepts new connections to the server. Accepting new connections are
-   done in three parts to make it async. */
-
-SILC_TASK_CALLBACK(silc_server_accept_new_connection)
+/* Host lookup callbcak that is called after the incoming connection's
+   IP and FQDN lookup is performed. This will actually check the acceptance
+   of the incoming connection and will register the key exchange protocol
+   for this connection. */
+
+static void 
+silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
+                                        void *context)
 {
   SilcServer server = (SilcServer)context;
-  SilcSocketConnection newsocket;
   SilcServerKEInternalContext *proto_ctx;
-  int sock, port;
   void *cconfig, *sconfig, *rconfig;
   SilcServerConfigSectionDenyConnection *deny;
+  int port;
 
-  SILC_LOG_DEBUG(("Accepting new connection"));
-
-  server->stat.conn_attempts++;
-
-  sock = silc_net_accept_connection(server->sock);
-  if (sock < 0) {
-    SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
-    server->stat.conn_failures++;
-    return;
-  }
+  SILC_LOG_DEBUG(("Start"));
 
-  /* Check max connections */
-  if (sock > SILC_SERVER_MAX_CONNECTIONS) {
-    SILC_LOG_ERROR(("Refusing connection, server is full"));
+  /* Check whether we could resolve both IP and FQDN. */
+  if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
+                   server->params->require_reverse_mapping)) {
+    SILC_LOG_ERROR(("IP/DNS lookup failed %s",
+                   sock->hostname ? sock->hostname :
+                   sock->ip ? sock->ip : ""));
     server->stat.conn_failures++;
+    silc_server_disconnect_remote(server, sock,
+                                 "Server closed connection: Unknown host");
     return;
   }
 
-  /* Set socket options */
-  silc_net_set_socket_nonblock(sock);
-  silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
-
-  /* We don't create a ID yet, since we don't know what type of connection
-     this is yet. But, we do add the connection to the socket table. */
-  silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
-  server->sockets[sock] = newsocket;
-
-  /* XXX This MUST be done async as this will block the entire server
-     process. Either we have to do our own resolver stuff or in the future
-     we can use threads. */
-  /* Perform name and address lookups for the remote host. */
-  if (!silc_net_check_host_by_sock(sock, &newsocket->hostname, 
-                                  &newsocket->ip)) {
-    if ((server->params->require_reverse_mapping && !newsocket->hostname) ||
-       !newsocket->ip) {
-      SILC_LOG_ERROR(("IP/DNS lookup failed %s",
-                     newsocket->hostname ? newsocket->hostname :
-                     newsocket->ip ? newsocket->ip : ""));
-      server->stat.conn_failures++;
-      return;
-    }
-    if (!newsocket->hostname)
-      newsocket->hostname = strdup(newsocket->ip);
-  }
-  newsocket->port = silc_net_get_remote_port(sock);
-
   /* Register the connection for network input and output. This sets
      that scheduler will listen for incoming packets for this connection 
      and sets that outgoing packets may be sent to this connection as well.
      However, this doesn't set the scheduler for outgoing traffic, it
      will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
      later when outgoing data is available. */
-  SILC_REGISTER_CONNECTION_FOR_IO(sock);
+  SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
 
-  SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
-                newsocket->ip));
+  SILC_LOG_INFO(("Incoming connection from %s (%s)", sock->hostname,
+                sock->ip));
 
-  port = server->sockets[fd]->port; /* Listenning port */
+  port = server->sockets[server->sock]->port; /* Listenning port */
 
   /* Check whether this connection is denied to connect to us. */
-  deny = silc_server_config_denied_conn(server->config, newsocket->ip, port);
+  deny = silc_server_config_denied_conn(server->config, sock->ip, port);
   if (!deny)
-    deny = silc_server_config_denied_conn(server->config, newsocket->hostname,
+    deny = silc_server_config_denied_conn(server->config, sock->hostname,
                                          port);
   if (deny) {
     /* The connection is denied */
     SILC_LOG_INFO(("Connection %s (%s) is denied", 
-                   newsocket->hostname, newsocket->ip));
-    silc_server_disconnect_remote(server, newsocket, deny->comment ?
+                   sock->hostname, sock->ip));
+    silc_server_disconnect_remote(server, sock, deny->comment ?
                                  deny->comment :
                                  "Server closed connection: "
                                  "Connection refused");
@@ -1023,23 +994,23 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection)
      have to check all configurations since we don't know what type of
      connection this is. */
   if (!(cconfig = silc_server_config_find_client_conn(server->config,
-                                                     newsocket->ip, port)))
+                                                     sock->ip, port)))
     cconfig = silc_server_config_find_client_conn(server->config,
-                                                 newsocket->hostname, 
+                                                 sock->hostname, 
                                                  port);
   if (!(sconfig = silc_server_config_find_server_conn(server->config,
-                                                    newsocket->ip, 
+                                                    sock->ip, 
                                                     port)))
     sconfig = silc_server_config_find_server_conn(server->config,
-                                                 newsocket->hostname,
+                                                 sock->hostname,
                                                  port);
   if (!(rconfig = silc_server_config_find_router_conn(server->config,
-                                                    newsocket->ip, port)))
+                                                    sock->ip, port)))
     rconfig = silc_server_config_find_router_conn(server->config,
-                                                 newsocket->hostname, 
+                                                 sock->hostname, 
                                                  port);
   if (!cconfig && !sconfig && !rconfig) {
-    silc_server_disconnect_remote(server, newsocket
+    silc_server_disconnect_remote(server, sock
                                  "Server closed connection: "
                                  "Connection refused");
     server->stat.conn_failures++;
@@ -1052,7 +1023,7 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection)
      sent as context for the protocol. */
   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
   proto_ctx->server = context;
-  proto_ctx->sock = newsocket;
+  proto_ctx->sock = sock;
   proto_ctx->rng = server->rng;
   proto_ctx->responder = TRUE;
   proto_ctx->cconfig = cconfig;
@@ -1065,7 +1036,7 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection)
      there before we start the protocol. */
   server->stat.auth_attempts++;
   silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE, 
-                     &newsocket->protocol, proto_ctx, 
+                     &sock->protocol, proto_ctx, 
                      silc_server_accept_new_connection_second);
 
   /* Register a timeout task that will be executed if the connector
@@ -1073,13 +1044,57 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection)
      now, this is a hard coded limit. After 60 secs the connection will
      be closed if the key exchange protocol has not been started. */
   proto_ctx->timeout_task = 
-    silc_task_register(server->timeout_queue, newsocket->sock, 
+    silc_task_register(server->timeout_queue, sock->sock, 
                       silc_server_timeout_remote,
                       context, 60, 0,
                       SILC_TASK_TIMEOUT,
                       SILC_TASK_PRI_LOW);
 }
 
+/* Accepts new connections to the server. Accepting new connections are
+   done in three parts to make it async. */
+
+SILC_TASK_CALLBACK(silc_server_accept_new_connection)
+{
+  SilcServer server = (SilcServer)context;
+  SilcSocketConnection newsocket;
+  int sock;
+
+  SILC_LOG_DEBUG(("Accepting new connection"));
+
+  server->stat.conn_attempts++;
+
+  sock = silc_net_accept_connection(server->sock);
+  if (sock < 0) {
+    SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
+    server->stat.conn_failures++;
+    return;
+  }
+
+  /* Check max connections */
+  if (sock > SILC_SERVER_MAX_CONNECTIONS) {
+    SILC_LOG_ERROR(("Refusing connection, server is full"));
+    server->stat.conn_failures++;
+    return;
+  }
+
+  /* Set socket options */
+  silc_net_set_socket_nonblock(sock);
+  silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
+
+  /* We don't create a ID yet, since we don't know what type of connection
+     this is yet. But, we do add the connection to the socket table. */
+  silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
+  server->sockets[sock] = newsocket;
+
+  /* Perform asynchronous host lookup. This will lookup the IP and the
+     FQDN of the remote connection. After the lookup is done the connection
+     is accepted further. */
+  silc_socket_host_lookup(newsocket, TRUE, 
+                         silc_server_accept_new_connection_lookup, context, 
+                         server->timeout_queue);
+}
+
 /* Second part of accepting new connection. Key exchange protocol has been
    performed and now it is time to do little connection authentication
    protocol to figure out whether this connection is client or server