client_notify.c
- Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+ Author: Pekka Riikonen <priikone@silcnet.org>
Copyright (C) 1997 - 2001 Pekka Riikonen
#include "clientlibincludes.h"
#include "client_internal.h"
+typedef struct {
+ SilcPacketContext *packet;
+ void *context;
+ SilcSocketConnection sock;
+} *SilcClientNotifyResolve;
+
/* Called when notify is received and some async operation (such as command)
is required before processing the notify message. This calls again the
silc_client_notify_by_server and reprocesses the original notify packet. */
-static void silc_client_notify_by_server_pending(void *context)
+static void silc_client_notify_by_server_pending(void *context, void *context2)
{
- SilcPacketContext *p = (SilcPacketContext *)context;
- silc_client_notify_by_server(p->context, p->sock, p);
- silc_socket_free(p->sock);
+ SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+ silc_client_notify_by_server(res->context, res->sock, res->packet);
+ silc_socket_free(res->sock);
}
/* Destructor for the pending command callback */
static void silc_client_notify_by_server_destructor(void *context)
{
- silc_packet_context_free((SilcPacketContext *)context);
+ SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+ silc_packet_context_free(res->packet);
+ silc_free(res);
}
/* Resolve client information from server by Client ID. */
SilcPacketContext *packet,
SilcClientID *client_id)
{
- SilcPacketContext *p = silc_packet_context_dup(packet);
+ SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
- p->context = (void *)client;
- p->sock = silc_socket_dup(conn->sock);
+ res->packet = silc_packet_context_dup(packet);
+ res->context = client;
+ res->sock = silc_socket_dup(conn->sock);
silc_client_send_command(client, conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident,
1, 3, idp->data, idp->len);
silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
silc_client_notify_by_server_destructor,
- silc_client_notify_by_server_pending, p);
+ silc_client_notify_by_server_pending, res);
silc_buffer_free(idp);
}
SilcIDPayload idp;
SilcClientID *client_id = NULL;
SilcChannelID *channel_id = NULL;
+ SilcServerID *server_id = NULL;
SilcClientEntry client_entry;
SilcClientEntry client_entry2;
SilcChannelEntry channel;
SilcChannelUser chu;
+ SilcServerEntry server;
SilcIDCacheEntry id_cache = NULL;
unsigned char *tmp;
uint32 tmp_len, mode;
/* Get the channel entry */
channel = NULL;
if (silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
channel = (SilcChannelEntry)id_cache->context;
/* Get sender Client ID */
/* If nickname or username hasn't been resolved, do so */
if (!client_entry->nickname || !client_entry->username) {
+ if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
+ client_entry->status &= ~SILC_CLIENT_STATUS_RESOLVING;
+ goto out;
+ }
+ client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
silc_client_notify_by_server_resolve(client, conn, packet, client_id);
goto out;
}
/* Get channel entry */
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
silc_list_add(channel->clients, chu);
}
- /* XXX add support for multiple same nicks on same channel. Check
- for them here */
-
/* Notify application. The channel entry is sent last as this notify
is for channel but application don't know it from the arguments
sent by server. */
if (!channel_id)
goto out;
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
silc_client_remove_from_channels(client, conn, client_entry);
/* Remove from cache */
- silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
- client_entry->id);
+ silc_idcache_del_by_context(conn->client_cache, client_entry);
/* Get signoff message */
tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
client->ops->notify(client, conn, type, client_entry, tmp);
/* Free data */
- if (client_entry->nickname)
- silc_free(client_entry->nickname);
- if (client_entry->server)
- silc_free(client_entry->server);
- if (client_entry->id)
- silc_free(client_entry->id);
- if (client_entry->send_key)
- silc_cipher_free(client_entry->send_key);
- if (client_entry->receive_key)
- silc_cipher_free(client_entry->receive_key);
+ silc_client_del_client_entry(client, client_entry);
break;
case SILC_NOTIFY_TYPE_TOPIC_SET:
if (!channel_id)
goto out;
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
goto out;
silc_free(client_id);
+ client_entry->valid = FALSE;
+
/* Get new Client ID */
tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
if (!tmp)
}
/* Remove the old from cache */
- silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
- client_entry->id);
+ silc_idcache_del_by_context(conn->client_cache, client_entry);
/* Replace old ID entry with new one on all channels. */
silc_client_replace_from_channels(client, conn, client_entry,
client->ops->notify(client, conn, type, client_entry, client_entry2);
/* Free data */
- if (client_entry->nickname)
- silc_free(client_entry->nickname);
- if (client_entry->server)
- silc_free(client_entry->server);
- if (client_entry->id)
- silc_free(client_entry->id);
- if (client_entry->send_key)
- silc_cipher_free(client_entry->send_key);
- if (client_entry->receive_key)
- silc_cipher_free(client_entry->receive_key);
- silc_free(client_entry);
+ silc_client_del_client_entry(client, client_entry);
break;
case SILC_NOTIFY_TYPE_CMODE_CHANGE:
goto out;
}
} else {
- client_entry = NULL;
+ server_id = silc_id_payload_parse_id(tmp, tmp_len);
+ if (!server_id) {
+ silc_id_payload_free(idp);
+ goto out;
+ }
+
+ server = silc_client_get_server_by_id(client, conn, server_id);
+ if (!server) {
+ silc_id_payload_free(idp);
+ silc_free(server_id);
+ goto out;
+ }
+
+ /* Save the pointer to the client_entry pointer */
+ client_entry = (SilcClientEntry)server;
+ silc_free(server_id);
}
- silc_id_payload_free(idp);
-
/* Get the mode */
tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
- if (!tmp)
+ if (!tmp) {
+ silc_id_payload_free(idp);
goto out;
+ }
SILC_GET32_MSB(mode, tmp);
/* Get channel entry */
channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
SILC_ID_CHANNEL);
- if (!channel_id)
+ if (!channel_id) {
+ silc_id_payload_free(idp);
goto out;
+ }
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
- break;
+ &id_cache)) {
+ silc_id_payload_free(idp);
+ goto out;
+ }
channel = (SilcChannelEntry)id_cache->context;
if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
goto out;
- silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
+ silc_hash_make(silc_hmac_get_hash(channel->hmac),
+ channel->key, channel->key_len / 8,
hash);
silc_hmac_set_key(channel->hmac, hash,
- silc_hash_len(channel->hmac->hash));
+ silc_hash_len(silc_hmac_get_hash(channel->hmac)));
memset(hash, 0, sizeof(hash));
}
/* Notify application. The channel entry is sent last as this notify
is for channel but application don't know it from the arguments
sent by server. */
- client->ops->notify(client, conn, type, client_entry, mode, NULL,
- tmp, channel);
+ client->ops->notify(client, conn, type, silc_id_payload_get_type(idp),
+ client_entry, mode, NULL, tmp, channel);
+
+ silc_id_payload_free(idp);
break;
case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
goto out;
/* Find Client entry */
- client_entry =
- silc_client_get_client_by_id(client, conn, client_id);
- if (!client_entry)
+ client_entry = silc_client_get_client_by_id(client, conn, client_id);
+ if (!client_entry) {
+ silc_client_notify_by_server_resolve(client, conn, packet, client_id);
goto out;
+ }
/* Get the mode */
tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
if (!channel_id)
goto out;
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
/* Get the channel entry */
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
+ SILC_LOG_DEBUG(("Old Channel ID id(%s)",
+ silc_id_render(channel->id, SILC_ID_CHANNEL)));
+
/* Free the old ID */
silc_free(channel->id);
if (!channel->id)
goto out;
- id_cache->id = (void *)channel->id;
+ SILC_LOG_DEBUG(("New Channel ID id(%s)",
+ silc_id_render(channel->id, SILC_ID_CHANNEL)));
+
+ /* Remove the old cache entry and create a new one */
+ silc_idcache_del_by_context(conn->channel_cache, channel);
+ silc_idcache_add(conn->channel_cache, channel->channel_name,
+ channel->id, channel, FALSE);
/* Notify application */
client->ops->notify(client, conn, type, channel, channel);
if (!channel_id)
goto out;
if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
- SILC_ID_CHANNEL, &id_cache))
+ &id_cache))
break;
channel = (SilcChannelEntry)id_cache->context;
if (client_entry == conn->local_entry) {
if (conn->current_channel == channel)
conn->current_channel = NULL;
- silc_idcache_del_by_id(conn->channel_cache,
- SILC_ID_CHANNEL, channel->id);
+ silc_idcache_del_by_id(conn->channel_cache, channel->id);
silc_free(channel->channel_name);
silc_free(channel->id);
silc_free(channel->key);
if (client_entry != conn->local_entry) {
/* Remove client from all channels */
silc_client_remove_from_channels(client, conn, client_entry);
- silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
- client_entry->id);
- if (client_entry->nickname)
- silc_free(client_entry->nickname);
- if (client_entry->server)
- silc_free(client_entry->server);
- if (client_entry->id)
- silc_free(client_entry->id);
- if (client_entry->send_key)
- silc_cipher_free(client_entry->send_key);
- if (client_entry->receive_key)
- silc_cipher_free(client_entry->receive_key);
- silc_free(client_entry);
+ silc_client_del_client(client, conn, client_entry);
}
break;
continue;
silc_client_remove_from_channels(client, conn, client_entry);
- silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
- client_entry->id);
- if (client_entry->nickname)
- silc_free(client_entry->nickname);
- if (client_entry->server)
- silc_free(client_entry->server);
- if (client_entry->id)
- silc_free(client_entry->id);
- if (client_entry->send_key)
- silc_cipher_free(client_entry->send_key);
- if (client_entry->receive_key)
- silc_cipher_free(client_entry->receive_key);
- silc_free(client_entry);
+ silc_client_del_client(client, conn, client_entry);
}
silc_free(clients);
out:
silc_notify_payload_free(payload);
- if (client_id)
- silc_free(client_id);
- if (channel_id)
- silc_free(channel_id);
+ silc_free(client_id);
+ silc_free(channel_id);
}