Fix unsafe client removal from list at timeout.
[umurmur.git] / src / client.c
index 367f18dee165e383f892b9c4ae3f2e979a8a4759..9f7576eff20051feb3d96ca36fa53ae8c19799c2 100644 (file)
@@ -1,5 +1,5 @@
-/* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
-   Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
 
    All rights reserved.
 
@@ -45,6 +45,7 @@
 #include "channel.h"
 #include "version.h"
 #include "voicetarget.h"
+#include "ban.h"
 
 extern char system_string[], version_string[];
 
@@ -90,9 +91,9 @@ int Client_getfds(struct pollfd *pollfds)
 
 void Client_janitor()
 {
-       struct dlist *itr;
+       struct dlist *itr, *save;
        int bwTop = maxBandwidth + maxBandwidth / 4;
-       list_iterate(itr, &clients) {
+       list_iterate_safe(itr, save, &clients) {
                client_t *c;
                c = list_get_entry(itr, client_t, node);
                Log_debug("Client %s BW available %d", c->username, c->availableBandwidth);
@@ -106,6 +107,7 @@ void Client_janitor()
                        Client_free(c);
                }
        }
+       Ban_pruneBanned();
 }
 
 void Client_codec_add(client_t *client, int codec)
@@ -146,6 +148,52 @@ codec_t *Client_codec_iterate(client_t *client, codec_t **codec_itr)
        return cd;
 }
 
+void Client_token_add(client_t *client, char *token_string)
+{
+       token_t *token;
+
+       if (client->tokencount >= MAX_TOKENS)
+               return;
+       token = malloc(sizeof(token_t));
+       if (token == NULL)
+               Log_fatal("Out of memory");
+       init_list_entry(&token->node);
+       token->token = strdup(token_string);
+       if (token->token == NULL)
+               Log_fatal("Out of memory");
+       list_add_tail(&token->node, &client->tokens);
+       client->tokencount++;
+}
+
+bool_t Client_token_match(client_t *client, char *str)
+{
+       token_t *token;
+       struct dlist *itr;
+       
+       if (list_empty(&client->tokens))
+               return false;
+       list_iterate(itr, &client->tokens) {
+               token = list_get_entry(itr, token_t, node);
+               if (strncasecmp(token->token, str, MAX_TOKENSIZE) == 0)
+                       return true;
+       }
+       return false;
+}
+
+void Client_token_free(client_t *client)
+{
+       struct dlist *itr, *save;
+       token_t *token;
+       
+       list_iterate_safe(itr, save, &client->tokens) {
+               token = list_get_entry(itr, token_t, node);
+               list_del(&token->node);
+               free(token->token);
+               free(token);
+       }
+       client->tokencount = 0;
+}
+
 void recheckCodecVersions()
 {
        client_t *client_itr = NULL;
@@ -211,8 +259,8 @@ void recheckCodecVersions()
                iCodecBeta = version;
        
        sendmsg = Msg_create(CodecVersion);
-       sendmsg->payload.codecVersion->alpha = version;
-       sendmsg->payload.codecVersion->beta = version;
+       sendmsg->payload.codecVersion->alpha = iCodecAlpha;
+       sendmsg->payload.codecVersion->beta = iCodecBeta;
        sendmsg->payload.codecVersion->prefer_alpha = bPreferAlpha;
        Client_send_message_except(NULL, sendmsg);
        
@@ -242,7 +290,11 @@ int Client_add(int fd, struct sockaddr_in *remote)
 {
        client_t *newclient;
        message_t *sendmsg;
-       
+
+       if (Ban_isBannedAddr((in_addr_t *)&remote->sin_addr)) {
+               Log_info("Address %s banned. Disconnecting", inet_ntoa(remote->sin_addr));
+               return -1;
+       }
        newclient = malloc(sizeof(client_t));
        if (newclient == NULL)
                Log_fatal("Out of memory");
@@ -270,6 +322,7 @@ int Client_add(int fd, struct sockaddr_in *remote)
        init_list_entry(&newclient->node);
        init_list_entry(&newclient->voicetargets);
        init_list_entry(&newclient->codecs);
+       init_list_entry(&newclient->tokens);
        
        list_add_tail(&newclient->node, &clients);
        clientcount++;
@@ -309,6 +362,7 @@ void Client_free(client_t *client)
        }
        Client_codec_free(client);
        Voicetarget_free_all(client);
+       Client_token_free(client);
        
        list_del(&client->node);
        if (client->ssl)
@@ -354,10 +408,10 @@ int Client_read_fd(int fd)
                        break;
                }
        }
-       if (client == NULL)
-               Log_fatal("No client found for fd %d", fd);
-       
-       return Client_read(client);
+       if (client != NULL)
+               return Client_read(client);
+       else
+               return -1;
 }
 
 int Client_read(client_t *client)
@@ -432,7 +486,10 @@ int Client_read(client_t *client)
                        }
                        else {
                                if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL) {
-                                       Log_info_client(client, "Connection closed by peer");
+                                       Log_info_client(client,"Error: %s  - Closing connection", strerror(errno));
+                               }
+                               else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_CONNRESET) {
+                                       Log_info_client(client, "Connection reset by peer");
                                }
                                else {
                                        Log_info_client(client, "SSL error: %d - Closing connection", SSLi_get_error(client->ssl, rc));
@@ -457,10 +514,10 @@ int Client_write_fd(int fd)
                        break;
                }
        }
-       if (client == NULL)
-               Log_fatal("No client found for fd %d", fd);
-       Client_write(client);
-       return 0;
+       if (client != NULL)
+               return Client_write(client);
+       else
+               return -1;
 }
 
 int Client_write(client_t *client)
@@ -487,10 +544,15 @@ int Client_write(client_t *client)
                        return 0;
                }
                else {
-                       if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL)
-                               Log_warn("Client_write: Error: %s  - Closing connection", strerror(errno));
-                       else
-                               Log_warn("Client_write: SSL error: %d - Closing connection.", SSLi_get_error(client->ssl, rc));
+                       if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_SYSCALL) {
+                               Log_info_client(client, "Error: %s  - Closing connection", strerror(errno));
+                       }
+                       else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_CONNRESET) {
+                               Log_info_client(client, "Connection reset by peer");
+                       }
+                       else {
+                               Log_info_client(client, "SSL error: %d - Closing connection.", SSLi_get_error(client->ssl, rc));
+                       }
                        Client_free(client);
                        return -1;
                }
@@ -690,7 +752,6 @@ int Client_read_udp()
                                        memcpy(&itr->remote_udp, &from, sizeof(struct sockaddr_in));
                                        break;
                                }
-                               else Log_warn("Bad cryptstate from peer");
                        }
                } /* while */
        }
@@ -722,7 +783,7 @@ out:
 
 static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data, int len, int poslen)
 {
-       if (IS_AUTH(dst) && dst != src && !dst->deaf) {
+       if (IS_AUTH(dst) && dst != src && !dst->deaf && !dst->self_deaf) {
                if (poslen > 0 && /* Has positional data */
                        src->context != NULL && dst->context != NULL && /* ...both source and destination has context */
                        strcmp(src->context, dst->context) == 0) /* ...and the contexts match */
@@ -747,7 +808,7 @@ int Client_voiceMsg(client_t *client, uint8_t *data, int len)
        channel_t *ch = (channel_t *)client->channel;
        struct dlist *itr;
        
-       if (!client->authenticated || client->mute)
+       if (!client->authenticated || client->mute || client->self_mute)
                goto out;
        
        packetsize = 20 + 8 + 4 + len;