From: Pekka Riikonen Date: Wed, 24 Sep 2008 15:18:30 +0000 (+0300) Subject: Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch X-Git-Tag: silc.server.1.1.13 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=e9374395ec9747bddd3ea0bfd3e5a17717e97b31;hp=8bb22be757768c18af7a5381b3b18d4983dfa9d4 Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch Signed-off-by: Pekka Riikonen --- diff --git a/apps/silcd/command.c b/apps/silcd/command.c index f35969ec..2bd8198e 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -1472,12 +1472,19 @@ SILC_SERVER_CMD_FUNC(kill) /* Do normal signoff for the destination client */ sock = remote_client->connection; + + if (sock) + silc_packet_stream_ref(sock); + silc_server_remove_from_channels(server, NULL, remote_client, TRUE, (char *)"Killed", TRUE, TRUE); silc_server_free_sock_user_data(server, sock, comment ? comment : (unsigned char *)"Killed"); - if (sock) + if (sock) { + silc_packet_set_context(sock, NULL); silc_server_close_connection(server, sock); + silc_packet_stream_unref(sock); + } } else { /* Router operator killing */ diff --git a/apps/silcd/server_util.c b/apps/silcd/server_util.c index d2fd526c..74ce18ba 100644 --- a/apps/silcd/server_util.c +++ b/apps/silcd/server_util.c @@ -1547,8 +1547,17 @@ void silc_server_kill_client(SilcServer server, if (remote_client->connection) { /* Remove locally conneted client */ SilcPacketStream sock = remote_client->connection; + + if (sock) + silc_packet_stream_ref(sock); + silc_server_free_sock_user_data(server, sock, NULL); - silc_server_close_connection(server, sock); + + if (sock) { + silc_packet_set_context(sock, NULL); + silc_server_close_connection(server, sock); + silc_packet_stream_unref(sock); + } } else { /* Update statistics */ server->stat.clients--; diff --git a/lib/silcclient/client.c b/lib/silcclient/client.c index 7811bfa1..3ab7ba17 100644 --- a/lib/silcclient/client.c +++ b/lib/silcclient/client.c @@ -55,7 +55,7 @@ static void silc_client_connection_finished(SilcFSMThread fsm, SilcClient client = silc_fsm_get_state_context(fsm); /* Signal client that we have finished */ - silc_atomic_sub_int16(&client->internal->conns, 1); + silc_atomic_sub_int32(&client->internal->conns, 1); client->internal->connection_closed = TRUE; SILC_FSM_EVENT_SIGNAL(&client->internal->wait_event); @@ -529,7 +529,7 @@ SILC_FSM_STATE(silc_client_st_run) /* A connection finished */ SILC_LOG_DEBUG(("Event: connection closed")); client->internal->connection_closed = FALSE; - if (silc_atomic_get_int16(&client->internal->conns) == 0 && + if (silc_atomic_get_int32(&client->internal->conns) == 0 && client->internal->stop) SILC_FSM_EVENT_SIGNAL(&client->internal->wait_event); return SILC_FSM_CONTINUE; @@ -538,7 +538,7 @@ SILC_FSM_STATE(silc_client_st_run) if (client->internal->stop) { /* Stop client libarry. If we have running connections, wait until they finish first. */ - if (silc_atomic_get_int16(&client->internal->conns) == 0) { + if (silc_atomic_get_int32(&client->internal->conns) == 0) { SILC_LOG_DEBUG(("Event: stop")); silc_fsm_next(fsm, silc_client_st_stop); } @@ -674,7 +674,7 @@ silc_client_add_connection(SilcClient client, silc_fsm_start(thread, silc_client_connection_st_start); SILC_LOG_DEBUG(("New connection %p", conn)); - silc_atomic_add_int16(&client->internal->conns, 1); + silc_atomic_add_int32(&client->internal->conns, 1); return conn; } @@ -938,7 +938,7 @@ SilcClient silc_client_alloc(SilcClientOperations *ops, nickname_format[sizeof(new_client->internal-> params->nickname_format) - 1] = 0; - silc_atomic_init16(&new_client->internal->conns, 0); + silc_atomic_init32(&new_client->internal->conns, 0); return new_client; } @@ -966,7 +966,7 @@ void silc_client_free(SilcClient client) silc_dlist_uninit(client->internal->ftp_sessions); if (client->internal->lock) silc_mutex_free(client->internal->lock); - silc_atomic_uninit16(&client->internal->conns); + silc_atomic_uninit32(&client->internal->conns); silc_free(client->username); silc_free(client->hostname); silc_free(client->realname); diff --git a/lib/silcclient/client.h b/lib/silcclient/client.h index ac1116d2..d66808dc 100644 --- a/lib/silcclient/client.h +++ b/lib/silcclient/client.h @@ -53,14 +53,17 @@ typedef struct SilcClientEntryInternalStruct { SilcUInt32 key_len; /* Key data length */ SilcClientKeyAgreement ke; /* Current key agreement context or NULL */ + SilcAtomic32 refcnt; /* Reference counter */ + SilcAtomic32 deleted; /* Flag indicating whether the client object is + already scheduled for deletion */ + SilcUInt16 resolve_cmd_ident; /* Command identifier when resolving */ + /* Flags */ unsigned int valid : 1; /* FALSE if this entry is not valid. Entry without nickname is not valid. */ unsigned int generated : 1; /* TRUE if library generated `key' */ unsigned int prv_resp : 1; /* TRUE if we are responder when using private message keys. */ - SilcUInt16 resolve_cmd_ident; /* Command identifier when resolving */ - SilcAtomic8 refcnt; /* Reference counter */ } SilcClientEntryInternal; /* Internal channel entry context */ @@ -81,20 +84,23 @@ typedef struct SilcChannelEntryInternalStruct { SilcHmac hmac; /* Current HMAC */ unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]; /* Current IV */ + SilcAtomic32 refcnt; /* Reference counter */ + SilcAtomic32 deleted; /* Flag indicating whether the + channel object is already + scheduled for deletion */ SilcUInt16 resolve_cmd_ident; /* Channel information resolving identifier. This is used when resolving users, and other stuff that relates to the channel. Not used for the channel resolving itself. */ - SilcAtomic16 refcnt; /* Reference counter */ } SilcChannelEntryInternal; /* Internal server entry context */ typedef struct SilcServerEntryInternalStruct { SilcRwLock lock; /* Read/write lock */ SilcUInt16 resolve_cmd_ident; /* Resolving identifier */ - SilcAtomic8 refcnt; /* Reference counter */ + SilcAtomic32 refcnt; /* Reference counter */ } SilcServerEntryInternal; #endif /* CLIENT_H */ diff --git a/lib/silcclient/client_entry.c b/lib/silcclient/client_entry.c index 0933c3d4..4664dbaa 100644 --- a/lib/silcclient/client_entry.c +++ b/lib/silcclient/client_entry.c @@ -791,7 +791,8 @@ SilcClientEntry silc_client_add_client(SilcClient client, return NULL; silc_rwlock_alloc(&client_entry->internal.lock); - silc_atomic_init8(&client_entry->internal.refcnt, 0); + silc_atomic_init32(&client_entry->internal.refcnt, 0); + silc_atomic_init32(&client_entry->internal.deleted, 1); client_entry->id = *id; client_entry->mode = mode; client_entry->realname = userinfo ? strdup(userinfo) : NULL; @@ -995,7 +996,8 @@ void silc_client_del_client_entry(SilcClient client, silc_client_ftp_session_free_client(client, client_entry); if (client_entry->internal.ke) silc_client_abort_key_agreement(client, conn, client_entry); - silc_atomic_uninit8(&client_entry->internal.refcnt); + silc_atomic_uninit32(&client_entry->internal.deleted); + silc_atomic_uninit32(&client_entry->internal.refcnt); silc_rwlock_free(client_entry->internal.lock); silc_free(client_entry); } @@ -1005,30 +1007,18 @@ void silc_client_del_client_entry(SilcClient client, SilcBool silc_client_del_client(SilcClient client, SilcClientConnection conn, SilcClientEntry client_entry) { - SilcBool ret; - if (!client_entry) return FALSE; - if (silc_atomic_sub_int8(&client_entry->internal.refcnt, 1) > 0) - return FALSE; - - SILC_LOG_DEBUG(("Deleting client %p", client_entry)); - - silc_mutex_lock(conn->internal->lock); - ret = silc_idcache_del_by_context(conn->internal->client_cache, - client_entry, NULL); - silc_mutex_unlock(conn->internal->lock); - - if (ret) { - /* Remove from channels */ - silc_client_remove_from_channels(client, conn, client_entry); + SILC_LOG_DEBUG(("Marking client entry %p deleted")); - /* Free the client entry data */ - silc_client_del_client_entry(client, conn, client_entry); + if (silc_atomic_sub_int32(&client_entry->internal.deleted, 1) != 0) { + SILC_LOG_DEBUG(("Client entry %p already marked deleted")); + return FALSE; } - return ret; + silc_client_unref_client(client, conn, client_entry); + return TRUE; } /* Internal routine used to find client by ID and if not found this creates @@ -1072,10 +1062,10 @@ SilcClientEntry silc_client_ref_client(SilcClient client, SilcClientConnection conn, SilcClientEntry client_entry) { - silc_atomic_add_int8(&client_entry->internal.refcnt, 1); + silc_atomic_add_int32(&client_entry->internal.refcnt, 1); SILC_LOG_DEBUG(("Client %p refcnt %d->%d", client_entry, - silc_atomic_get_int8(&client_entry->internal.refcnt) - 1, - silc_atomic_get_int8(&client_entry->internal.refcnt))); + silc_atomic_get_int32(&client_entry->internal.refcnt) - 1, + silc_atomic_get_int32(&client_entry->internal.refcnt))); return client_entry; } @@ -1084,11 +1074,32 @@ SilcClientEntry silc_client_ref_client(SilcClient client, void silc_client_unref_client(SilcClient client, SilcClientConnection conn, SilcClientEntry client_entry) { - if (client_entry) { - SILC_LOG_DEBUG(("Client %p refcnt %d->%d", client_entry, - silc_atomic_get_int8(&client_entry->internal.refcnt), - silc_atomic_get_int8(&client_entry->internal.refcnt) - 1)); - silc_client_del_client(client, conn, client_entry); + SilcBool ret; + + if (!client_entry) + return; + + SILC_LOG_DEBUG(("Client %p refcnt %d->%d", client_entry, + silc_atomic_get_int32(&client_entry->internal.refcnt), + silc_atomic_get_int32(&client_entry->internal.refcnt) - 1)); + + if (silc_atomic_sub_int32(&client_entry->internal.refcnt, 1) > 0) + return; + + SILC_LOG_DEBUG(("Deleting client %p (%d)", client_entry, + silc_atomic_get_int32(&client_entry->internal.deleted))); + + silc_mutex_lock(conn->internal->lock); + ret = silc_idcache_del_by_context(conn->internal->client_cache, + client_entry, NULL); + silc_mutex_unlock(conn->internal->lock); + + if (ret) { + /* Remove from channels */ + silc_client_remove_from_channels(client, conn, client_entry); + + /* Free the client entry data */ + silc_client_del_client_entry(client, conn, client_entry); } } @@ -1626,7 +1637,8 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, return NULL; silc_rwlock_alloc(&channel->internal.lock); - silc_atomic_init16(&channel->internal.refcnt, 0); + silc_atomic_init32(&channel->internal.refcnt, 0); + silc_atomic_init32(&channel->internal.deleted, 1); channel->id = *channel_id; channel->mode = mode; @@ -1639,7 +1651,7 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, if (!channel->channel_name) { silc_rwlock_free(channel->internal.lock); - silc_atomic_uninit16(&channel->internal.refcnt); + silc_atomic_uninit32(&channel->internal.refcnt); silc_free(channel); return NULL; } @@ -1648,7 +1660,7 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, NULL, NULL, NULL, TRUE); if (!channel->user_list) { silc_rwlock_free(channel->internal.lock); - silc_atomic_uninit16(&channel->internal.refcnt); + silc_atomic_uninit32(&channel->internal.refcnt); silc_free(channel->channel_name); silc_free(channel); return NULL; @@ -1659,7 +1671,7 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, SILC_STRING_UTF8, 256, NULL); if (!channel_namec) { silc_rwlock_free(channel->internal.lock); - silc_atomic_uninit16(&channel->internal.refcnt); + silc_atomic_uninit32(&channel->internal.refcnt); silc_free(channel->channel_name); silc_hash_table_free(channel->user_list); silc_free(channel); @@ -1672,7 +1684,7 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, if (!silc_idcache_add(conn->internal->channel_cache, channel_namec, &channel->id, channel)) { silc_rwlock_free(channel->internal.lock); - silc_atomic_uninit16(&channel->internal.refcnt); + silc_atomic_uninit32(&channel->internal.refcnt); silc_free(channel_namec); silc_free(channel->channel_name); silc_hash_table_free(channel->user_list); @@ -1694,67 +1706,18 @@ SilcChannelEntry silc_client_add_channel(SilcClient client, SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn, SilcChannelEntry channel) { - SilcIDCacheEntry id_cache; - SilcBool ret = TRUE; - SilcCipher key; - SilcHmac hmac; - char *namec; - if (!channel) return FALSE; - if (silc_atomic_sub_int16(&channel->internal.refcnt, 1) > 0) - return FALSE; - - SILC_LOG_DEBUG(("Deleting channel %p", channel)); + SILC_LOG_DEBUG(("Marking channel entry %p deleted")); - silc_mutex_lock(conn->internal->lock); - if (silc_idcache_find_by_context(conn->internal->channel_cache, channel, - &id_cache)) { - namec = id_cache->name; - ret = silc_idcache_del_by_context(conn->internal->channel_cache, - channel, NULL); - silc_free(namec); - } - silc_mutex_unlock(conn->internal->lock); - - if (!ret) + if (silc_atomic_sub_int32(&channel->internal.deleted, 1) != 0) { + SILC_LOG_DEBUG(("Channel entry %p already marked deleted")); return FALSE; - - silc_client_empty_channel(client, conn, channel); - silc_client_del_channel_private_keys(client, conn, channel); - silc_hash_table_free(channel->user_list); - silc_free(channel->channel_name); - silc_free(channel->topic); - if (channel->founder_key) - silc_pkcs_public_key_free(channel->founder_key); - if (channel->internal.send_key) - silc_cipher_free(channel->internal.send_key); - if (channel->internal.receive_key) - silc_cipher_free(channel->internal.receive_key); - if (channel->internal.hmac) - silc_hmac_free(channel->internal.hmac); - if (channel->internal.old_channel_keys) { - silc_dlist_start(channel->internal.old_channel_keys); - while ((key = silc_dlist_get(channel->internal.old_channel_keys))) - silc_cipher_free(key); - silc_dlist_uninit(channel->internal.old_channel_keys); } - if (channel->internal.old_hmacs) { - silc_dlist_start(channel->internal.old_hmacs); - while ((hmac = silc_dlist_get(channel->internal.old_hmacs))) - silc_hmac_free(hmac); - silc_dlist_uninit(channel->internal.old_hmacs); - } - if (channel->channel_pubkeys) - silc_argument_list_free(channel->channel_pubkeys, - SILC_ARGUMENT_PUBLIC_KEY); - silc_atomic_uninit16(&channel->internal.refcnt); - silc_rwlock_free(channel->internal.lock); - silc_schedule_task_del_by_context(conn->client->schedule, channel); - silc_free(channel); - return ret; + silc_client_unref_channel(client, conn, channel); + return TRUE; } /* Replaces the channel ID of the `channel' to `new_id'. Returns FALSE @@ -1806,10 +1769,10 @@ SilcChannelEntry silc_client_ref_channel(SilcClient client, SilcClientConnection conn, SilcChannelEntry channel_entry) { - silc_atomic_add_int16(&channel_entry->internal.refcnt, 1); + silc_atomic_add_int32(&channel_entry->internal.refcnt, 1); SILC_LOG_DEBUG(("Channel %p refcnt %d->%d", channel_entry, - silc_atomic_get_int16(&channel_entry->internal.refcnt) - 1, - silc_atomic_get_int16(&channel_entry->internal.refcnt))); + silc_atomic_get_int32(&channel_entry->internal.refcnt) - 1, + silc_atomic_get_int32(&channel_entry->internal.refcnt))); return channel_entry; } @@ -1818,13 +1781,71 @@ SilcChannelEntry silc_client_ref_channel(SilcClient client, void silc_client_unref_channel(SilcClient client, SilcClientConnection conn, SilcChannelEntry channel_entry) { - if (channel_entry) { - SILC_LOG_DEBUG(("Channel %p refcnt %d->%d", channel_entry, - silc_atomic_get_int16(&channel_entry->internal.refcnt), - silc_atomic_get_int16(&channel_entry->internal.refcnt) - - 1)); - silc_client_del_channel(client, conn, channel_entry); + SilcIDCacheEntry id_cache; + SilcBool ret = TRUE; + SilcCipher key; + SilcHmac hmac; + char *namec; + + if (!channel_entry) + return; + + SILC_LOG_DEBUG(("Channel %p refcnt %d->%d", channel_entry, + silc_atomic_get_int32(&channel_entry->internal.refcnt), + silc_atomic_get_int32(&channel_entry->internal.refcnt) + - 1)); + + if (silc_atomic_sub_int32(&channel_entry->internal.refcnt, 1) > 0) + return; + + SILC_LOG_DEBUG(("Deleting channel %p", channel_entry)); + + silc_mutex_lock(conn->internal->lock); + if (silc_idcache_find_by_context(conn->internal->channel_cache, channel_entry, + &id_cache)) { + namec = id_cache->name; + ret = silc_idcache_del_by_context(conn->internal->channel_cache, + channel_entry, NULL); + silc_free(namec); } + silc_mutex_unlock(conn->internal->lock); + + if (!ret) + return; + + silc_client_empty_channel(client, conn, channel_entry); + silc_client_del_channel_private_keys(client, conn, channel_entry); + silc_hash_table_free(channel_entry->user_list); + silc_free(channel_entry->channel_name); + silc_free(channel_entry->topic); + if (channel_entry->founder_key) + silc_pkcs_public_key_free(channel_entry->founder_key); + if (channel_entry->internal.send_key) + silc_cipher_free(channel_entry->internal.send_key); + if (channel_entry->internal.receive_key) + silc_cipher_free(channel_entry->internal.receive_key); + if (channel_entry->internal.hmac) + silc_hmac_free(channel_entry->internal.hmac); + if (channel_entry->internal.old_channel_keys) { + silc_dlist_start(channel_entry->internal.old_channel_keys); + while ((key = silc_dlist_get(channel_entry->internal.old_channel_keys))) + silc_cipher_free(key); + silc_dlist_uninit(channel_entry->internal.old_channel_keys); + } + if (channel_entry->internal.old_hmacs) { + silc_dlist_start(channel_entry->internal.old_hmacs); + while ((hmac = silc_dlist_get(channel_entry->internal.old_hmacs))) + silc_hmac_free(hmac); + silc_dlist_uninit(channel_entry->internal.old_hmacs); + } + if (channel_entry->channel_pubkeys) + silc_argument_list_free(channel_entry->channel_pubkeys, + SILC_ARGUMENT_PUBLIC_KEY); + silc_atomic_uninit32(&channel_entry->internal.deleted); + silc_atomic_uninit32(&channel_entry->internal.refcnt); + silc_rwlock_free(channel_entry->internal.lock); + silc_schedule_task_del_by_context(conn->client->schedule, channel_entry); + silc_free(channel_entry); } /* Free channel entry list */ @@ -2059,7 +2080,7 @@ SilcServerEntry silc_client_add_server(SilcClient client, return NULL; silc_rwlock_alloc(&server_entry->internal.lock); - silc_atomic_init8(&server_entry->internal.refcnt, 0); + silc_atomic_init32(&server_entry->internal.refcnt, 0); server_entry->id = *server_id; if (server_name) server_entry->server_name = strdup(server_name); @@ -2111,7 +2132,7 @@ SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn, if (!server) return FALSE; - if (silc_atomic_sub_int8(&server->internal.refcnt, 1) > 0) + if (silc_atomic_sub_int32(&server->internal.refcnt, 1) > 0) return FALSE; SILC_LOG_DEBUG(("Deleting server %p", server)); @@ -2130,7 +2151,7 @@ SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn, silc_free(server->server_info); if (server->public_key) silc_pkcs_public_key_free(server->public_key); - silc_atomic_uninit8(&server->internal.refcnt); + silc_atomic_uninit32(&server->internal.refcnt); silc_rwlock_free(server->internal.lock); silc_free(server); @@ -2198,10 +2219,10 @@ SilcServerEntry silc_client_ref_server(SilcClient client, SilcClientConnection conn, SilcServerEntry server_entry) { - silc_atomic_add_int8(&server_entry->internal.refcnt, 1); + silc_atomic_add_int32(&server_entry->internal.refcnt, 1); SILC_LOG_DEBUG(("Server %p refcnt %d->%d", server_entry, - silc_atomic_get_int8(&server_entry->internal.refcnt) - 1, - silc_atomic_get_int8(&server_entry->internal.refcnt))); + silc_atomic_get_int32(&server_entry->internal.refcnt) - 1, + silc_atomic_get_int32(&server_entry->internal.refcnt))); return server_entry; } @@ -2212,8 +2233,8 @@ void silc_client_unref_server(SilcClient client, SilcClientConnection conn, { if (server_entry) { SILC_LOG_DEBUG(("Server %p refcnt %d->%d", server_entry, - silc_atomic_get_int8(&server_entry->internal.refcnt), - silc_atomic_get_int8(&server_entry->internal.refcnt) + silc_atomic_get_int32(&server_entry->internal.refcnt), + silc_atomic_get_int32(&server_entry->internal.refcnt) - 1)); silc_client_del_server(client, conn, server_entry); } diff --git a/lib/silcclient/client_internal.h b/lib/silcclient/client_internal.h index 8b48e733..54582e20 100644 --- a/lib/silcclient/client_internal.h +++ b/lib/silcclient/client_internal.h @@ -108,7 +108,7 @@ struct SilcClientInternalStruct { char *silc_client_version; /* Version set by application */ SilcClientRunning running; /* Running/Stopped callback */ void *running_context; /* Context for runnign callback */ - SilcAtomic16 conns; /* Number of connections in client */ + SilcAtomic32 conns; /* Number of connections in client */ SilcUInt16 next_session_id; /* Next FTP session ID */ /* Events */ diff --git a/lib/silccore/silcpacket.c b/lib/silccore/silcpacket.c index 2c01e6dd..f552ca6d 100644 --- a/lib/silccore/silcpacket.c +++ b/lib/silccore/silcpacket.c @@ -81,7 +81,7 @@ struct SilcPacketStreamStruct { unsigned char *dst_id; /* Destination ID */ SilcUInt32 send_psn; /* Sending sequence */ SilcUInt32 receive_psn; /* Receiving sequence */ - SilcAtomic8 refcnt; /* Reference counter */ + SilcAtomic32 refcnt; /* Reference counter */ SilcUInt8 sid; /* Security ID, set if IV included */ unsigned int src_id_len : 6; unsigned int src_id_type : 2; @@ -695,7 +695,7 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine, return NULL; ps->stream = stream; - silc_atomic_init8(&ps->refcnt, 1); + silc_atomic_init32(&ps->refcnt, 1); silc_mutex_alloc(&ps->lock); /* Allocate out buffer */ @@ -816,7 +816,7 @@ SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream, return NULL; ps->sc = stream->sc; - silc_atomic_init8(&ps->refcnt, 1); + silc_atomic_init32(&ps->refcnt, 1); silc_mutex_alloc(&ps->lock); /* Set the UDP packet stream as underlaying stream */ @@ -884,7 +884,7 @@ void silc_packet_stream_destroy(SilcPacketStream stream) if (!stream) return; - if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0) { + if (silc_atomic_sub_int32(&stream->refcnt, 1) > 0) { if (stream->destroyed) return; stream->destroyed = TRUE; @@ -972,7 +972,7 @@ void silc_packet_stream_destroy(SilcPacketStream stream) silc_free(stream->src_id); silc_free(stream->dst_id); - silc_atomic_uninit8(&stream->refcnt); + silc_atomic_uninit32(&stream->refcnt); silc_mutex_free(stream->lock); silc_free(stream); } @@ -1030,6 +1030,7 @@ static SilcBool silc_packet_stream_link_va(SilcPacketStream stream, stream->process = silc_dlist_init(); if (!stream->process) { silc_mutex_unlock(stream->lock); + silc_free(p); return FALSE; } } @@ -1153,10 +1154,10 @@ SilcBool silc_packet_get_sender(SilcPacket packet, void silc_packet_stream_ref(SilcPacketStream stream) { - silc_atomic_add_int8(&stream->refcnt, 1); + silc_atomic_add_int32(&stream->refcnt, 1); SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream, - silc_atomic_get_int8(&stream->refcnt) - 1, - silc_atomic_get_int8(&stream->refcnt))); + silc_atomic_get_int32(&stream->refcnt) - 1, + silc_atomic_get_int32(&stream->refcnt))); } /* Unreference packet stream */ @@ -1164,11 +1165,11 @@ void silc_packet_stream_ref(SilcPacketStream stream) void silc_packet_stream_unref(SilcPacketStream stream) { SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream, - silc_atomic_get_int8(&stream->refcnt), - silc_atomic_get_int8(&stream->refcnt) - 1)); - if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0) + silc_atomic_get_int32(&stream->refcnt), + silc_atomic_get_int32(&stream->refcnt) - 1)); + if (silc_atomic_sub_int32(&stream->refcnt, 1) > 0) return; - silc_atomic_add_int8(&stream->refcnt, 1); + silc_atomic_add_int32(&stream->refcnt, 1); silc_packet_stream_destroy(stream); } @@ -1323,6 +1324,7 @@ SilcBool silc_packet_set_ids(SilcPacketStream stream, { SilcUInt32 len; unsigned char tmp[32]; + void *tmp_id; if (!src_id && !dst_id) return FALSE; @@ -1332,16 +1334,17 @@ SilcBool silc_packet_set_ids(SilcPacketStream stream, if (src_id) { SILC_LOG_DEBUG(("Setting source ID to packet stream %p", stream)); - silc_free(stream->src_id); if (!silc_id_id2str(src_id, src_id_type, tmp, sizeof(tmp), &len)) { silc_mutex_unlock(stream->lock); return FALSE; } - stream->src_id = silc_memdup(tmp, len); - if (!stream->src_id) { + tmp_id = silc_memdup(tmp, len); + if (!tmp_id) { silc_mutex_unlock(stream->lock); return FALSE; } + silc_free(stream->src_id); + stream->src_id = tmp_id; stream->src_id_type = src_id_type; stream->src_id_len = len; } @@ -1349,16 +1352,17 @@ SilcBool silc_packet_set_ids(SilcPacketStream stream, if (dst_id) { SILC_LOG_DEBUG(("Setting destination ID to packet stream %p", stream)); - silc_free(stream->dst_id); if (!silc_id_id2str(dst_id, dst_id_type, tmp, sizeof(tmp), &len)) { silc_mutex_unlock(stream->lock); return FALSE; } - stream->dst_id = silc_memdup(tmp, len); - if (!stream->dst_id) { + tmp_id = silc_memdup(tmp, len); + if (!tmp_id) { silc_mutex_unlock(stream->lock); return FALSE; } + silc_free(stream->dst_id); + stream->dst_id = tmp_id; stream->dst_id_type = dst_id_type; stream->dst_id_len = len; }