Indentation fixed
[umurmur.git] / src / ban.c
index f69ccf009eba3f71408f69b61d1bae0b9524aeba..75f45b28ba412cd799574edbefa4bbf74e84eff1 100644 (file)
--- a/src/ban.c
+++ b/src/ban.c
@@ -1,5 +1,5 @@
-/* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
-   Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
 
    All rights reserved.
 
@@ -56,10 +56,10 @@ void Ban_init(void)
 
 void Ban_deinit(void)
 {
-       /* Save banlist */      
+       /* Save banlist */
        if (getStrConf(BANFILE) != NULL)
                Ban_saveBanFile();
-               
+
        Ban_clearBanList();
 }
 
@@ -72,10 +72,15 @@ void Ban_UserBan(client_t *client, char *reason)
        if (ban == NULL)
                Log_fatal("Out of memory");
        memset(ban, 0, sizeof(ban_t));
-       
+
        memcpy(ban->hash, client->hash, 20);
-       memcpy(&ban->address, &client->remote_tcp.sin_addr, sizeof(in_addr_t));
-       ban->mask = 128;
+       if (client->remote_tcp.ss_family == AF_INET) {
+               memcpy(&ban->address, &(((struct sockaddr_in*)&client->remote_tcp)->sin_addr), sizeof(in_addr_t));
+               ban->mask = sizeof(in_addr_t);
+       } else {
+               memcpy(&ban->address, &(((struct sockaddr_in6*)&client->remote_tcp)->sin6_addr), 4 * sizeof(in_addr_t));
+               ban->mask = 4 * sizeof(in_addr_t);
+       }
        ban->reason = strdup(reason);
        ban->name = strdup(client->username);
        ban->time = time(NULL);
@@ -86,10 +91,11 @@ void Ban_UserBan(client_t *client, char *reason)
        banlist_changed = true;
        if(getBoolConf(SYNC_BANFILE))
                Ban_saveBanFile();
-       
+
        SSLi_hash2hex(ban->hash, hexhash);
+
        Log_info_client(client, "User kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds",
-                       ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)), ban->duration);
+               ban->reason, hexhash, client->addressString, ban->duration);
 }
 
 
@@ -99,15 +105,15 @@ void Ban_pruneBanned()
        ban_t *ban;
        char hexhash[41];
        uint64_t bantime_long;
-               
+
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
                bantime_long = ban->duration * 1000000LL;
 #ifdef DEBUG
                SSLi_hash2hex(ban->hash, hexhash);
                Log_debug("BL: User %s Reason: '%s' Hash: %s IP: %s Time left: %d",
-                         ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
-                         bantime_long / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
+                       ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
+                       bantime_long / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
 #endif
                /* Duration of 0 = forever */
                if (ban->duration != 0 && Timer_isElapsed(&ban->startTime, bantime_long)) {
@@ -129,11 +135,11 @@ bool_t Ban_isBanned(client_t *client)
        ban_t *ban;
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
-               if (memcmp(ban->hash, client->hash, 20) == 0) 
+               if (memcmp(ban->hash, client->hash, 20) == 0)
                        return true;
        }
        return false;
-       
+
 }
 
 bool_t Ban_isBannedAddr(in_addr_t *addr)
@@ -142,7 +148,7 @@ bool_t Ban_isBannedAddr(in_addr_t *addr)
        ban_t *ban;
        int mask;
        in_addr_t tempaddr1, tempaddr2;
-       
+
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
                mask = ban->mask - 96;
@@ -152,7 +158,7 @@ bool_t Ban_isBannedAddr(in_addr_t *addr)
                        tempaddr1 &= (2 ^ mask) - 1;
                        tempaddr2 &= (2 ^ mask) - 1;
                }
-               if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0) 
+               if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0)
                        return true;
        }
        return false;
@@ -173,7 +179,7 @@ message_t *Ban_getBanList(void)
        char timestr[32];
        char hexhash[41];
        uint8_t address[16];
-       
+
        msg = Msg_banList_create(bancount);
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
@@ -185,7 +191,7 @@ message_t *Ban_getBanList(void)
                memcpy(&address[12], &ban->address, 4);
                memset(&address[10], 0xff, 2); /* IPv4 */
                Msg_banList_addEntry(msg, i++, address, ban->mask, ban->name,
-                                    hexhash, ban->reason, timestr, ban->duration);
+                       hexhash, ban->reason, timestr, ban->duration);
        }
        return msg;
 }
@@ -212,7 +218,7 @@ void Ban_putBanList(message_t *msg, int n_bans)
        char *hexhash, *name, *reason, *start;
        uint32_t duration, mask;
        uint8_t *address;
-       
+
        for (i = 0; i < n_bans; i++) {
                Msg_banList_getEntry(msg, i, &address, &mask, &name, &hexhash, &reason, &start, &duration);
                ban = malloc(sizeof(ban_t));
@@ -253,8 +259,8 @@ static void Ban_saveBanFile(void)
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
                SSLi_hash2hex(ban->hash, hexhash);
-               fprintf(file, "%s,%s,%d,%d,%d,%s,%s\n", hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
-                       ban->mask, ban->time, ban->duration, ban->name, ban->reason);
+               fprintf(file, "%s,%s,%d,%ld,%d,%s,%s\n", hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
+                       ban->mask, (long int)ban->time, ban->duration, ban->name, ban->reason);
        }
        fclose(file);
        banlist_changed = false;
@@ -297,7 +303,7 @@ static void Ban_readBanFile(void)
                p = strtok(NULL, "\n");
                if (p == NULL) break;
                reason = p;
-               
+
                ban = malloc(sizeof(ban_t));
                if (ban == NULL)
                        Log_fatal("Out of memory");