updates.
[silc.git] / apps / silcd / server.c
index 7fd9b86c132636732bcb8650cef84a5b3bc54309..639c3bbb732a8eb75d3b55028ba544717ac42e77 100644 (file)
@@ -121,12 +121,14 @@ int silc_server_init(SilcServer server)
   assert(server->config);
 
   /* Set public and private keys */
-  server->public_key = server->config->server_keys->public_key;
-  server->private_key = server->config->server_keys->private_key;
-  if (!server->public_key || !server->private_key) {
+  if (!server->config->server_keys ||
+      !server->config->server_keys->public_key || 
+      !server->config->server_keys->private_key) {
     SILC_LOG_ERROR(("Server public key and/or private key does not exist"));
     return FALSE;
   }
+  server->public_key = server->config->server_keys->public_key;
+  server->private_key = server->config->server_keys->private_key;
 
   /* XXX After server is made as Silc Server Library this can be given
      as argument, for now this is hard coded */
@@ -514,6 +516,9 @@ SILC_TASK_CALLBACK(silc_server_connect_router)
   SilcServerKEInternalContext *proto_ctx;
   int sock;
 
+  SILC_LOG_INFO(("Connecting to the router %s on port %d", 
+                sconn->remote_host, sconn->remote_port));
+
   /* Connect to remote host */
   sock = silc_net_create_connection(sconn->remote_port, 
                                    sconn->remote_host);
@@ -855,7 +860,7 @@ SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
 
   /* Add the connected router to local server list */
   server->standalone = FALSE;
-  id_entry = silc_idlist_add_server(server->local_list, sock->hostname,
+  id_entry = silc_idlist_add_server(server->local_list, strdup(sock->hostname),
                                    SILC_ROUTER, ctx->dest_id, NULL, sock);
   if (!id_entry) {
     if (ctx->dest_id)
@@ -957,15 +962,19 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection)
      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. */
-  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"));
-    server->stat.conn_failures++;
-    return;
+  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);
   }
-  if (!newsocket->hostname)
-    newsocket->hostname = strdup(newsocket->ip);
   newsocket->port = silc_net_get_remote_port(sock);
 
   SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
@@ -1296,7 +1305,8 @@ SILC_TASK_CALLBACK(silc_server_packet_process)
     if (sock->outbuf->data - sock->outbuf->head)
      silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
 
-    ret = silc_server_packet_send_real(server, sock, TRUE);
+    /* Send the packet */
+    ret = silc_packet_send(sock, TRUE);
 
     /* If returned -2 could not write to connection now, will do
        it later. */
@@ -1370,7 +1380,7 @@ SILC_TASK_CALLBACK(silc_server_packet_process)
   if (idata) {
     idata->last_receive = time(NULL);
     cipher = idata->receive_key;
-    hmac = idata->hmac;
+    hmac = idata->hmac_receive;
   }
  
   /* Process the packet. This will call the parser that will then
@@ -1412,7 +1422,7 @@ static int silc_server_packet_decrypt_check(SilcPacketType packet_type,
 
   return FALSE;
 }
-
+  
 /* Parses whole packet, received earlier. */
 
 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
@@ -1421,12 +1431,14 @@ SILC_TASK_CALLBACK(silc_server_packet_parse_real)
   SilcServer server = (SilcServer)parse_ctx->context;
   SilcSocketConnection sock = parse_ctx->sock;
   SilcPacketContext *packet = parse_ctx->packet;
+  SilcIDListData idata = (SilcIDListData)sock->user_data;
   int ret;
 
   SILC_LOG_DEBUG(("Start"));
 
   /* Decrypt the received packet */
-  ret = silc_packet_decrypt(parse_ctx->cipher, parse_ctx->hmac, 
+  ret = silc_packet_decrypt(idata ? idata->receive_key : NULL, 
+                           idata ? idata->hmac_receive : NULL, 
                            packet->buffer, packet,
                            silc_server_packet_decrypt_check, parse_ctx);
   if (ret < 0)
@@ -1947,8 +1959,7 @@ void silc_server_packet_parse_type(SilcServer server,
 
       /* Let the protocol handle the packet */
       sock->protocol->execute(server->timeout_queue, 0, 
-                             sock->protocol, sock->sock,
-                             0, 100000);
+                             sock->protocol, sock->sock, 0, 0);
     } else {
       SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
                      "protocol active, packet dropped."));
@@ -1991,7 +2002,12 @@ SILC_TASK_CALLBACK(silc_server_close_connection_final)
 void silc_server_close_connection(SilcServer server,
                                  SilcSocketConnection sock)
 {
-  SILC_LOG_DEBUG(("Closing connection %d", sock->sock));
+  SILC_LOG_INFO(("Closing connection %s:%d [%s] (%d)", sock->hostname,
+                sock->port,  
+                (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
+                 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
+                 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
+                 "Router"), sock->sock));
 
   /* We won't listen for this connection anymore */
   silc_schedule_unset_listen_fd(sock->sock);
@@ -2080,7 +2096,7 @@ void silc_server_free_client_data(SilcServer server,
     if (sock->outbuf->data - sock->outbuf->head)
      silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
 
-    silc_server_packet_send_real(server, sock, TRUE);
+    silc_packet_send(sock, TRUE);
 
     SILC_SET_CONNECTION_FOR_INPUT(sock->sock);
     SILC_UNSET_OUTBUF_PENDING(sock);
@@ -2796,7 +2812,7 @@ SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
 
   silc_task_register(server->timeout_queue, 0, 
                     silc_server_channel_key_rekey,
-                    (void *)rekey, 3600 + 5, 0,
+                    (void *)rekey, 3600, 0,
                     SILC_TASK_TIMEOUT,
                     SILC_TASK_PRI_NORMAL);
 }
@@ -2867,7 +2883,7 @@ void silc_server_create_channel_key(SilcServer server,
                                     silc_server_channel_key_rekey);
     silc_task_register(server->timeout_queue, 0, 
                       silc_server_channel_key_rekey,
-                      (void *)channel->rekey, 3600 + 5, 0,
+                      (void *)channel->rekey, 3600, 0,
                       SILC_TASK_TIMEOUT,
                       SILC_TASK_PRI_NORMAL);
   }
@@ -2972,7 +2988,7 @@ SilcChannelEntry silc_server_save_channel_key(SilcServer server,
                                     silc_server_channel_key_rekey);
     silc_task_register(server->timeout_queue, 0, 
                       silc_server_channel_key_rekey,
-                      (void *)channel->rekey, 3600 + 5, 0,
+                      (void *)channel->rekey, 3600, 0,
                       SILC_TASK_TIMEOUT,
                       SILC_TASK_PRI_NORMAL);
   }
@@ -3664,11 +3680,13 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
     return;
   }
 
+#if 0
   /* Take the keys into use */
   if (ctx->pfs == TRUE)
     silc_server_protocol_rekey_generate_pfs(server, ctx);
   else
     silc_server_protocol_rekey_generate(server, ctx);
+#endif
 
   /* Cleanup */
   silc_protocol_free(protocol);