Use Client_find_by_session() instead of a few open-coded loops.
[umurmur.git] / src / messagehandler.c
index f1a753f9e81495764d20f120f9e371ea51959b34..882409ae75e8d25503bec0c98c3a179d2af268e4 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "client.h"
 #include "messages.h"
@@ -284,10 +285,14 @@ void Mh_handle_message(client_t *client, message_t *msg)
                                sendmsg->payload.channelState->channel_id = ch_itr->id;
                                sendmsg->payload.channelState->n_links = ch_itr->linkcount;
 
-                               links = (uint32_t *)malloc(ch_itr->linkcount * sizeof(uint32_t));
+                               links = (uint32_t*)Memory_safeMalloc(
+                                       ch_itr->linkcount,
+                                       sizeof(uint32_t));
                                list_iterate(itr, &ch_itr->channel_links) { /* Iterate links */
+                                       channellist_t *chl;
                                        channel_t *ch;
-                                       ch = list_get_entry(itr, channel_t, link_node);
+                                       chl = list_get_entry(itr, channellist_t, node);
+                                       ch = chl->chan;
                                        links[i++] = ch->id;
                                }
                                sendmsg->payload.channelState->links = links;
@@ -301,7 +306,7 @@ void Mh_handle_message(client_t *client, message_t *msg)
                sendmsg->payload.userState->session = client->sessionId;
                sendmsg->payload.userState->name = strdup(client->username);
                sendmsg->payload.userState->has_channel_id = true;
-               sendmsg->payload.userState->channel_id = ((channel_t *)client->channel)->id;
+               sendmsg->payload.userState->channel_id = client->channel->id;
 
                if (defaultChan->silent) {
                        sendmsg->payload.userState->has_suppress = true;
@@ -319,11 +324,11 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        sendmsg->payload.userState->session = client_itr->sessionId;
                        sendmsg->payload.userState->name = strdup(client_itr->username);
                        sendmsg->payload.userState->has_channel_id = true;
-                       sendmsg->payload.userState->channel_id = ((channel_t *)client_itr->channel)->id;
-                       sendmsg->payload.userState->has_suppress = ((channel_t *)client_itr->channel)->silent;
-                       sendmsg->payload.userState->suppress = ((channel_t *)client_itr->channel)->silent;
+                       sendmsg->payload.userState->channel_id = client_itr->channel->id;
+                       sendmsg->payload.userState->has_suppress = client_itr->channel->silent;
+                       sendmsg->payload.userState->suppress = client_itr->channel->silent;
 
-                       client_itr->isSuppressed = ((channel_t *)client_itr->channel)->silent;
+                       client_itr->isSuppressed = client_itr->channel->silent;
 
                        if (client_itr->self_deaf) {
                                sendmsg->payload.userState->has_self_deaf = true;
@@ -433,10 +438,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        break;
                }
                if (msg->payload.userState->has_session && msg->payload.userState->session != client->sessionId) {
-                       while (Client_iterate(&target) != NULL) {
-                               if (target->sessionId == msg->payload.userState->session)
-                                       break;
-                       }
+                       target = Client_find_by_session(msg->payload.userState->session);
+
                        if (target == NULL) {
                                Log_warn("Client with sessionID %d not found", msg->payload.userState->session);
                                break;
@@ -493,12 +496,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        char *message;
                        uint32_t *tree_id;
 
-                       message = malloc(strlen(client->username) + 32);
-                       if (!message)
-                               Log_fatal("Out of memory");
-                       tree_id = malloc(sizeof(uint32_t));
-                       if (!tree_id)
-                               Log_fatal("Out of memory");
+                       message = Memory_safeMalloc(1, strlen(client->username) + 32);
+                       tree_id = Memory_safeMalloc(1, sizeof(uint32_t));
                        *tree_id = 0;
                        sendmsg = Msg_create(TextMessage);
                        sendmsg->payload.textMessage->message = message;
@@ -553,11 +552,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        }
                }
                if (msg->payload.userState->has_plugin_context) {
-                       if (client->context)
-                               free(client->context);
-                       client->context = malloc(msg->payload.userState->plugin_context.len);
-                       if (client->context == NULL)
-                               Log_fatal("Out of memory");
+                       free(client->context);
+                       client->context = Memory_safeMalloc(1, msg->payload.userState->plugin_context.len);
                        memcpy(client->context, msg->payload.userState->plugin_context.data,
                                   msg->payload.userState->plugin_context.len);
 
@@ -611,21 +607,15 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        int i;
                        client_t *itr;
                        for (i = 0; i < msg->payload.textMessage->n_session; i++) {
-                               itr = NULL;
-                               while (Client_iterate(&itr) != NULL) {
-                                       if (!IS_AUTH(itr))
-                                               continue;
-                                       if (itr->sessionId == msg->payload.textMessage->session[i]) {
-                                               if (!itr->deaf && !itr->self_deaf) {
-                                                       Msg_inc_ref(msg);
-                                                       Client_send_message(itr, msg);
-                                                       Log_debug("Text message to session ID %d", itr->sessionId);
-                                               }
-                                               break;
-                                       }
-                               }
+                               itr = Client_find_by_session(msg->payload.textMessage->session[i]);
+
                                if (itr == NULL)
                                        Log_warn("TextMessage: Session ID %d not found", msg->payload.textMessage->session[i]);
+                               else if (IS_AUTH(itr) && !itr->deaf && !itr->self_deaf) {
+                                       Msg_inc_ref(msg);
+                                       Client_send_message(itr, msg);
+                                       Log_debug("Text message to session ID %d", itr->sessionId);
+                               }
                        } /* for */
                }
                break;
@@ -663,17 +653,17 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        Log_debug("Client version 0x%x", client->version);
                }
                if (msg->payload.version->release) {
-                       if (client->release) free(client->release);
+                       free(client->release);
                        client->release = strdup(msg->payload.version->release);
                        Log_debug("Client release %s", client->release);
                }
                if (msg->payload.version->os) {
-                       if (client->os) free(client->os);
+                       free(client->os);
                        client->os = strdup(msg->payload.version->os);
                        Log_debug("Client OS %s", client->os);
                }
                if (msg->payload.version->os_version) {
-                       if (client->os_version) free(client->os_version);
+                       free(client->os_version);
                        client->os_version = strdup(msg->payload.version->os_version);
                        Log_debug("Client OS version %s", client->os_version);
                }
@@ -800,15 +790,15 @@ void Mh_handle_message(client_t *client, message_t *msg)
 
                if (!msg->payload.userStats->has_session)
                        sendPermissionDenied(client, "Not supported by uMurmur");
-               while (Client_iterate(&target) != NULL) {
-                       if (!IS_AUTH(target))
-                               continue;
-                       if (target->sessionId == msg->payload.userStats->session)
-                               break;
-               }
+
+               target = Client_find_by_session(msg->payload.userStats->session);
+
                if (!target) /* Not found */
                        break;
 
+               if (!IS_AUTH(target)) /* Not authenticated. */
+                       break;
+
                /*
                 * Differences from Murmur:
                 * o Ignoring certificates intentionally
@@ -861,9 +851,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                                sendmsg->payload.userStats->version->os_version = strdup(target->os_version);
 
                        sendmsg->payload.userStats->n_celt_versions = target->codec_count;
-                       sendmsg->payload.userStats->celt_versions = malloc(sizeof(int32_t) * target->codec_count);
-                       if (!sendmsg->payload.userStats->celt_versions)
-                               Log_fatal("Out of memory");
+                       sendmsg->payload.userStats->celt_versions
+                               = Memory_safeMalloc(target->codec_count, sizeof(int32_t));
                        i = 0;
                        while (Client_codec_iterate(target, &codec_itr) != NULL)
                                sendmsg->payload.userStats->celt_versions[i++] = codec_itr->codec;
@@ -872,15 +861,21 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        sendmsg->payload.userStats->opus = target->bOpus;
 
                        /* Address */
-                       sendmsg->payload.userStats->has_address = true;
-                       sendmsg->payload.userStats->address.data = malloc(sizeof(uint8_t) * 16);
-                       if (!sendmsg->payload.userStats->address.data)
-                               Log_fatal("Out of memory");
-                       memset(sendmsg->payload.userStats->address.data, 0, 16);
-                       /* ipv4 representation as ipv6 address. Supposedly correct. */
-                       memcpy(&sendmsg->payload.userStats->address.data[12], &target->remote_tcp.sin_addr, 4);
-                       memset(&sendmsg->payload.userStats->address.data[10], 0xff, 2); /* IPv4 */
-                       sendmsg->payload.userStats->address.len = 16;
+                       if (getBoolConf(SHOW_ADDRESSES)) {
+                               sendmsg->payload.userStats->has_address = true;
+                               sendmsg->payload.userStats->address.data
+                                       = Memory_safeMalloc(16, sizeof(uint8_t));
+                               memset(sendmsg->payload.userStats->address.data, 0, 16);
+                               /* ipv4 representation as ipv6 address. Supposedly correct. */
+                               memset(&sendmsg->payload.userStats->address.data[10], 0xff, 2); /* IPv4 */
+                               if(target->remote_tcp.ss_family == AF_INET)
+                                       memcpy(&sendmsg->payload.userStats->address.data[12], &((struct sockaddr_in*)&target->remote_tcp)->sin_addr, 4);
+                               else
+                                       memcpy(&sendmsg->payload.userStats->address.data[0], &((struct sockaddr_in6*)&target->remote_tcp)->sin6_addr, 16);
+                               sendmsg->payload.userStats->address.len = 16;
+                       } else {
+                               sendmsg->payload.userStats->has_address = false;
+                       }
                }
                /* BW */
                sendmsg->payload.userStats->has_bandwidth = true;
@@ -902,10 +897,9 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        sendPermissionDenied(client, "Permission denied");
                        break;
                }
-               while (Client_iterate(&target) != NULL) {
-                       if (target->sessionId == msg->payload.userRemove->session)
-                               break;
-               }
+
+               target = Client_find_by_session(msg->payload.userRemove->session);
+
                if (target == NULL) {
                        Log_warn("Client with sessionId %d not found", msg->payload.userRemove->session);
                        break;