Changeset 4754

Show
Ignore:
Timestamp:
05/12/07 01:59:41 (1 year ago)
Author:
morris
Message:

A bit of refactoring

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wired/trunk/wired/banlist.c

    r4576 r4754  
    3434#include "settings.h" 
    3535 
    36 #define WD_TEMPBANS_TIMER_INTERVAL              60.0 
    37  
    38  
    3936struct _wd_tempban { 
    4037        wi_runtime_base_t                                       base; 
     
    4239        wi_string_t                                                     *ip; 
    4340        wi_time_interval_t                                      interval; 
     41        wi_timer_t                                                      *timer; 
    4442}; 
    4543typedef struct _wd_tempban                              wd_tempban_t; 
    4644 
    4745 
    48 static void                                                             wd_tempbans_update_expired(wi_timer_t *); 
    49  
     46static wd_tempban_t *                                   wd_tempban_with_ip(wi_string_t *); 
    5047static wd_tempban_t *                                   wd_tempban_alloc(void); 
    5148static wd_tempban_t *                                   wd_tempban_init_with_ip(wd_tempban_t *, wi_string_t *); 
     
    5350static wi_string_t *                                    wd_tempban_description(wi_runtime_instance_t *); 
    5451 
     52static void                                                             wd_tempban_expire_timer(wi_timer_t *); 
    5553 
    56 static wi_array_t                                               *wd_tempbans; 
    57 static wi_timer_t                                              *wd_tempbans_timer
     54 
     55static wi_hash_t                                               *wd_tempbans
    5856 
    5957static wi_runtime_id_t                                  wd_tempban_runtime_id = WI_RUNTIME_ID_NULL; 
     
    6866 
    6967 
    70 void wd_tempbans_init(void) { 
     68void wd_banlist_init(void) { 
    7169        wd_tempban_runtime_id = wi_runtime_register_class(&wd_tempban_runtime_class); 
    7270 
    73         wd_tempbans = wi_array_init(wi_array_alloc()); 
    74  
    75         wd_tempbans_timer = wi_timer_init_with_function(wi_timer_alloc(), 
    76                                                                                                         wd_tempbans_update_expired, 
    77                                                                                                         WD_TEMPBANS_TIMER_INTERVAL, 
    78                                                                                                         true); 
    79 
    80  
    81  
    82  
    83 void wd_tempbans_schedule(void) { 
    84         wi_timer_schedule(wd_tempbans_timer); 
    85 
    86  
    87  
    88  
    89 static void wd_tempbans_update_expired(wi_timer_t *timer) { 
    90         wd_tempban_t            *tempban; 
    91         wi_time_interval_t      interval; 
    92         wi_uinteger_t           i, count; 
    93          
    94         count = wi_array_count(wd_tempbans); 
    95          
    96         if(count > 0) { 
    97                 interval = wi_time_interval(); 
    98  
    99                 wi_array_wrlock(wd_tempbans); 
    100                  
    101                 for(i = 0; i < count; i++) { 
    102                         tempban = WI_ARRAY(wd_tempbans, i); 
    103  
    104                         if(tempban->interval + wd_settings.bantime < interval) { 
    105                                 wi_array_remove_data_at_index(wd_tempbans, i); 
    106                                  
    107                                 count--; 
    108                                 i--; 
    109                         } 
    110                 } 
    111                  
    112                 wi_array_unlock(wd_tempbans); 
    113         } 
     71        wd_tempbans = wi_hash_init(wi_hash_alloc()); 
    11472} 
    11573 
     
    11876#pragma mark - 
    11977 
    120 static wd_tempban_t * wd_tempban_alloc(void) { 
    121         return wi_runtime_create_instance(wd_tempban_runtime_id, sizeof(wd_tempban_t)); 
    122 
    123  
    124  
    125  
    126 static wd_tempban_t * wd_tempban_init_with_ip(wd_tempban_t *tempban, wi_string_t *ip) { 
    127         tempban->ip                     = wi_retain(ip); 
    128         tempban->interval       = wi_time_interval(); 
    129          
    130         return tempban; 
    131 
    132  
    133  
    134  
    135 static void wd_tempban_dealloc(wi_runtime_instance_t *instance) { 
    136         wd_tempban_t            *tempban = instance; 
    137  
    138         wi_release(tempban->ip); 
    139 
    140  
    141  
    142  
    143 static wi_string_t * wd_tempban_description(wi_runtime_instance_t *instance) { 
    144         wd_tempban_t            *tempban = instance; 
    145          
    146         return wi_string_with_format(WI_STR("<%@ %p>{ip = %@, time_remaining = %.0f}"), 
    147                 wi_runtime_class_name(tempban), 
    148                 tempban, 
    149                 tempban->ip, 
    150                 wi_time_interval() - tempban->interval); 
    151 
    152  
    153  
    154  
    155 #pragma mark - 
    156  
    157 wi_boolean_t wd_ip_is_banned(wi_string_t *ip) { 
    158         wi_enumerator_t         *enumerator; 
     78wi_boolean_t wd_banlist_ip_is_banned(wi_string_t *ip) { 
    15979        wi_file_t                       *file; 
    16080        wi_string_t                     *string; 
    161         wd_tempban_t            *tempban; 
    16281        wi_boolean_t            banned = false; 
    16382 
    164         wi_array_rdlock(wd_tempbans); 
    165          
    166         enumerator = wi_array_data_enumerator(wd_tempbans); 
    167          
    168         while((tempban = wi_enumerator_next_data(enumerator))) { 
    169                 if(wi_is_equal(ip, tempban->ip)) { 
    170                         banned = true; 
    171  
    172                         break; 
    173                 } 
    174         } 
    175  
    176         wi_array_unlock(wd_tempbans); 
     83        wi_hash_rdlock(wd_tempbans); 
     84        banned = (wi_hash_data_for_key(wd_tempbans, ip) != NULL); 
     85        wi_hash_unlock(wd_tempbans); 
    17786         
    17887        if(banned) 
     
    200109 
    201110 
    202 void wd_tempban(wi_string_t *ip) { 
     111void wd_banlist_add_temporary_ban_for_ip(wi_string_t *ip) { 
    203112        wd_tempban_t    *tempban; 
    204113         
    205         tempban = wd_tempban_init_with_ip(wd_tempban_alloc(), ip); 
    206         wi_array_wrlock(wd_tempbans); 
    207         wi_array_add_data(wd_tempbans, tempban); 
    208         wi_array_unlock(wd_tempbans); 
    209         wi_release(tempban); 
     114        tempban = wd_tempban_with_ip(ip); 
     115        tempban->timer = wi_timer_init_with_function(wi_timer_alloc(), 
     116                                                                                wd_tempban_expire_timer, 
     117                                                                                wd_settings.bantime, 
     118                                                                                false); 
     119 
     120        wi_timer_set_data(tempban->timer, tempban); 
     121        wi_timer_schedule(tempban->timer); 
     122         
     123        wi_hash_wrlock(wd_tempbans); 
     124        wi_hash_set_data_for_key(wd_tempbans, tempban, tempban->ip); 
     125        wi_hash_unlock(wd_tempbans); 
    210126} 
     127 
     128 
     129 
     130#pragma mark - 
     131 
     132static wd_tempban_t * wd_tempban_with_ip(wi_string_t *ip) { 
     133        return wi_autorelease(wd_tempban_init_with_ip(wd_tempban_alloc(), ip)); 
     134} 
     135 
     136 
     137 
     138static wd_tempban_t * wd_tempban_alloc(void) { 
     139        return wi_runtime_create_instance(wd_tempban_runtime_id, sizeof(wd_tempban_t)); 
     140} 
     141 
     142 
     143 
     144static wd_tempban_t * wd_tempban_init_with_ip(wd_tempban_t *tempban, wi_string_t *ip) { 
     145        tempban->ip                     = wi_retain(ip); 
     146        tempban->interval       = wi_time_interval(); 
     147         
     148        return tempban; 
     149} 
     150 
     151 
     152 
     153static void wd_tempban_dealloc(wi_runtime_instance_t *instance) { 
     154        wd_tempban_t            *tempban = instance; 
     155         
     156        wi_release(tempban->ip); 
     157        wi_release(tempban->timer); 
     158} 
     159 
     160 
     161 
     162static wi_string_t * wd_tempban_description(wi_runtime_instance_t *instance) { 
     163        wd_tempban_t            *tempban = instance; 
     164         
     165        return wi_string_with_format(WI_STR("<%@ %p>{ip = %@, time_remaining = %.0f}"), 
     166                wi_runtime_class_name(tempban), 
     167                tempban, 
     168                tempban->ip, 
     169                wi_time_interval() - tempban->interval); 
     170} 
     171 
     172 
     173 
     174#pragma mark - 
     175 
     176static void wd_tempban_expire_timer(wi_timer_t *timer) { 
     177        wd_tempban_t            *tempban; 
     178         
     179        tempban = wi_timer_data(timer); 
     180         
     181        wi_hash_rdlock(wd_tempbans); 
     182        wi_hash_remove_data_for_key(wd_tempbans, tempban->ip); 
     183        wi_hash_unlock(wd_tempbans); 
     184} 
  • wired/trunk/wired/banlist.h

    r4508 r4754  
    3030#define WD_BANLIST_H 1 
    3131 
     32void                                                    wd_banlist_init(void); 
    3233 
    33 void                                                    wd_tempbans_init(void); 
    34 void                                                    wd_tempbans_schedule(void); 
    35  
    36 wi_boolean_t                                    wd_ip_is_banned(wi_string_t *); 
    37 void                                                    wd_tempban(wi_string_t *); 
     34wi_boolean_t                                    wd_banlist_ip_is_banned(wi_string_t *); 
     35void                                                    wd_banlist_add_temporary_ban_for_ip(wi_string_t *); 
    3836 
    3937#endif /* WD_BANLIST_H */ 
  • wired/trunk/wired/chats.c

    r4750 r4754  
    3535#include "server.h" 
    3636 
     37#define WD_PUBLIC_CID                           1 
     38 
     39 
    3740struct _wd_chat { 
    3841        wi_runtime_base_t                               base; 
     
    120123 
    121124 
    122 void wd_chats_remove_chat(wd_chat_t *chat) { 
    123         wi_hash_wrlock(wd_chats); 
    124         wi_hash_remove_data_for_key(wd_chats, wi_number_with_int32(chat->cid)); 
    125         wi_hash_unlock(wd_chats); 
    126 } 
    127  
    128  
    129  
    130125wd_chat_t * wd_chats_chat_with_cid(wd_cid_t cid) { 
    131126        wd_chat_t               *chat; 
     
    156151                wi_array_unlock(chat->users); 
    157152 
    158                 if(chat->cid != WD_PUBLIC_CID && wi_array_count(chat->users) == 0) 
     153                if(chat != wd_public_chat && wi_array_count(chat->users) == 0) 
    159154                        wi_hash_remove_data_for_key(wd_chats, key); 
    160155        } 
     
    249244 
    250245wi_boolean_t wd_chat_contains_user(wd_chat_t *chat, wd_user_t *user) { 
    251         wi_boolean_t    contains = false; 
    252  
    253         if(chat) { 
    254                 wi_array_rdlock(chat->users); 
    255                 contains = wi_array_contains_data(chat->users, user); 
    256                 wi_array_unlock(chat->users); 
    257         } 
     246        wi_boolean_t    contains; 
     247 
     248        wi_array_rdlock(chat->users); 
     249        contains = wi_array_contains_data(chat->users, user); 
     250        wi_array_unlock(chat->users); 
    258251 
    259252        return contains; 
     
    288281        wi_array_unlock(chat->users); 
    289282 
    290         if(chat->cid != WD_PUBLIC_CID && wi_array_count(chat->users) == 0) { 
     283        if(chat != wd_public_chat && wi_array_count(chat->users) == 0) { 
    291284                wi_hash_wrlock(wd_chats); 
    292285                wi_hash_remove_data_for_key(wd_chats, wi_number_with_int32(chat->cid)); 
     
    296289 
    297290 
     291 
     292#pragma mark - 
    298293 
    299294void wd_chat_reply_user_list(wd_chat_t *chat) { 
     
    329324 
    330325 
    331 #pragma mark - 
    332  
    333326void wd_chat_reply_topic(wd_chat_t *chat) { 
    334327        wi_string_t             *string; 
  • wired/trunk/wired/chats.h

    r4750 r4754  
    3434#include "users.h" 
    3535 
    36 #define WD_PUBLIC_CID                                   1 
    37  
    38  
    3936typedef uint32_t                                                wd_cid_t; 
    4037 
     
    4744 
    4845void                                                                    wd_chats_add_chat(wd_chat_t *); 
    49 void                                                                    wd_chats_remove_chat(wd_chat_t *); 
    5046wd_chat_t *                                                             wd_chats_chat_with_cid(wd_cid_t); 
    5147void                                                                    wd_chats_remove_user(wd_user_t *); 
     
    5652void                                                                    wd_chat_add_user_and_broadcast(wd_chat_t *, wd_user_t *); 
    5753void                                                                    wd_chat_remove_user(wd_chat_t *, wd_user_t *); 
     54 
    5855void                                                                    wd_chat_reply_user_list(wd_chat_t *); 
    59  
    6056void                                                                    wd_chat_reply_topic(wd_chat_t *); 
    6157void                                                                    wd_chat_broadcast_topic(wd_chat_t *); 
     
    6864wi_array_t *                                                    wd_chat_users(wd_chat_t *); 
    6965 
     66 
    7067wd_topic_t *                                                    wd_topic_with_string(wi_string_t *); 
    7168 
  • wired/trunk/wired/commands.c

    r4750 r4754  
    407407                wd_user_identifier(other_user)); 
    408408 
    409         wd_tempban(wd_user_ip(other_user)); 
     409        wd_banlist_add_temporary_ban_for_ip(wd_user_ip(other_user)); 
    410410 
    411411        wd_user_set_state(other_user, WD_USER_DISCONNECTED); 
     
    606606        chat = wd_chats_chat_with_cid(cid); 
    607607         
    608         if(!chat) 
    609                 return; 
    610  
    611         if(wd_chat_contains_user(chat, user)) 
     608        if(!chat || wd_chat_contains_user(chat, user)) 
    612609                return; 
    613610 
     
    910907        ip = wd_user_ip(user); 
    911908 
    912         if(wd_ip_is_banned(ip)) { 
     909        if(wd_banlist_ip_is_banned(ip)) { 
    913910                wd_reply(511, WI_STR("Banned")); 
    914911                wi_log_info(WI_STR("Connection from %@ denied, host is banned"), 
     
    10891086        chat = wd_chats_chat_with_cid(cid); 
    10901087 
    1091         if(!chat) 
    1092                 return; 
    1093  
    1094         if(!wd_chat_contains_user(chat, user)) 
    1095                 return; 
    1096  
    1097         if(wd_chat_contains_user(chat, other_user)) 
     1088        if(!chat || !wd_chat_contains_user(chat, user) || wd_chat_contains_user(chat, other_user)) 
    10981089                return; 
    10991090 
     
    11191110        chat = wd_chats_chat_with_cid(cid); 
    11201111 
    1121         if(!chat) 
    1122                 return; 
    1123  
    1124         if(wd_chat_contains_user(chat, user)) 
     1112        if(!chat || wd_chat_contains_user(chat, user)) 
    11251113                return; 
    11261114 
     
    11851173 
    11861174        cid = wi_string_uint32(WI_ARRAY(arguments, 0)); 
    1187  
    1188         if(cid == WD_PUBLIC_CID) 
    1189                 return; 
    1190  
    11911175        chat = wd_chats_chat_with_cid(cid); 
    11921176 
    1193         if(!chat
     1177        if(!chat || chat == wd_public_chat
    11941178                return; 
    11951179 
     
    12551239        chat = wd_chats_chat_with_cid(cid); 
    12561240         
    1257         if(!chat) 
    1258                 return; 
    1259  
    1260         if(!wd_chat_contains_user(chat, user)) 
     1241        if(!chat || !wd_chat_contains_user(chat, user)) 
    12611242                return; 
    12621243 
     
    13921373        if(!account) { 
    13931374                wd_reply(510, WI_STR("Login Failed")); 
    1394                 wi_log_info(WI_STR("Login from %@ failed: %@"), 
    1395                         wd_user_identifier(user), 
    1396                         WI_STR("No such account")); 
     1375                wi_log_info(WI_STR("Login from %@ failed: No such account"), 
     1376                        wd_user_identifier(user)); 
    13971377 
    13981378                return; 
     
    14051385        if(!wi_is_equal(account->password, password)) { 
    14061386                wd_reply(510, WI_STR("Login Failed")); 
    1407                 wi_log_info(WI_STR("Login from %@ failed: %@"), 
    1408                         wd_user_identifier(user), 
    1409                         WI_STR("Wrong password")); 
     1387                wi_log_info(WI_STR("Login from %@ failed: Wrong password"), 
     1388                        wd_user_identifier(user)); 
    14101389 
    14111390                return; 
     
    15891568        chat = wd_chats_chat_with_cid(cid); 
    15901569         
    1591         if(!chat) 
    1592                 return; 
    1593  
    1594         if(!wd_chat_contains_user(chat, user)) 
     1570        if(!chat || !wd_chat_contains_user(chat, user)) 
    15951571                return; 
    15961572 
     
    16891665                return; 
    16901666 
    1691         if(cid == WD_PUBLIC_CID) { 
     1667        if(chat == wd_public_chat) { 
    16921668                if(!wd_user_account(user)->set_topic) { 
    16931669                        wd_reply(516, WI_STR("Permission Denied")); 
     
    18011777        cid = wi_string_uint32(WI_ARRAY(arguments, 0)); 
    18021778        chat = wd_chats_chat_with_cid(cid); 
    1803  
    1804         if(!chat) 
    1805                 return; 
    1806  
    1807         if(!wd_chat_contains_user(chat, user)) 
     1779         
     1780        if(!chat || !wd_chat_contains_user(chat, user)) 
    18081781                return; 
    18091782 
  • wired/trunk/wired/main.c

    r4750 r4754  
    190190        wd_files_init(); 
    191191        wd_news_init(); 
    192         wd_tempbans_init(); 
     192        wd_banlist_init(); 
    193193        wd_trackers_init(); 
    194194        wd_transfers_init(); 
     
    237237        wd_signals_init(); 
    238238        wd_block_signals(); 
    239         wd_tempbans_schedule(); 
    240239        wd_users_schedule(); 
    241240        wd_trackers_schedule(); 
  • wired/trunk/wired/server.c

    r4750 r4754  
    302302                 
    303303                /* spawn a user thread */ 
    304                 user = wi_autorelease(wd_user_init_with_socket(wd_user_alloc(), socket)); 
     304                user = wd_user_with_socket(socket); 
    305305 
    306306                if(!wi_thread_create_thread(wd_control_thread, user)) 
  • wired/trunk/wired/users.c

    r4750 r4754  
    121121static void                                                             wd_users_update_idle(wi_timer_t *); 
    122122 
     123static wd_user_t *                                              wd_user_alloc(void); 
     124static wd_user_t *                                              wd_user_init_with_socket(wd_user_t *, wi_socket_t *); 
    123125static void                                                             wd_user_dealloc(wi_runtime_instance_t *); 
    124126static wi_string_t *                                    wd_user_description(wi_runtime_instance_t *); 
    125127 
    126 static wd_uid_t                                                 _wd_user_next_uid(void); 
     128static wd_uid_t                                                 wd_user_next_uid(void); 
    127129 
    128130 
     
    264266#pragma mark - 
    265267 
    266 wd_user_t * wd_user_alloc(void) { 
     268wd_user_t * wd_user_with_socket(wi_socket_t *socket) { 
     269        return wi_autorelease(wd_user_init_with_socket(wd_user_alloc(), socket)); 
     270
     271 
     272 
     273 
     274#pragma mark - 
     275 
     276static wd_user_t * wd_user_alloc(void) { 
    267277        return wi_runtime_create_instance(wd_user_runtime_id, sizeof(wd_user_t)); 
    268278} 
     
    270280 
    271281 
    272 wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) { 
     282static wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) { 
    273283        wi_address_t    *address; 
    274284 
    275         user->uid                               = _wd_user_next_uid(); 
     285        user->uid                               = wd_user_next_uid(); 
    276286        user->socket                    = wi_retain(socket); 
    277287        user->state                             = WD_USER_CONNECTED; 
     
    331341#pragma mark - 
    332342 
    333 static wd_uid_t _wd_user_next_uid(void) { 
     343static wd_uid_t wd_user_next_uid(void) { 
    334344        wd_uid_t        uid; 
    335345         
  • wired/trunk/wired/users.h

    r4750 r4754  
    5151typedef uint32_t                                                wd_icon_t; 
    5252 
    53  
    5453typedef struct _wd_user                                 wd_user_t; 
    5554 
     
    6564wd_user_t *                                                             wd_users_user_for_thread(void); 
    6665 
    67 wd_user_t *                                                             wd_user_alloc(void); 
    68 wd_user_t *                                                             wd_user_init_with_socket(wd_user_t *, wi_socket_t *); 
     66wd_user_t *                                                             wd_user_with_socket(wi_socket_t *); 
    6967 
    7068void                                                                    wd_user_broadcast_status(wd_user_t *);