Changeset 4754
- Timestamp:
- 05/12/07 01:59:41 (1 year ago)
- Files:
-
- wired/trunk/wired/banlist.c (modified) (6 diffs)
- wired/trunk/wired/banlist.h (modified) (1 diff)
- wired/trunk/wired/chats.c (modified) (7 diffs)
- wired/trunk/wired/chats.h (modified) (4 diffs)
- wired/trunk/wired/commands.c (modified) (12 diffs)
- wired/trunk/wired/main.c (modified) (2 diffs)
- wired/trunk/wired/server.c (modified) (1 diff)
- wired/trunk/wired/users.c (modified) (4 diffs)
- wired/trunk/wired/users.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
wired/trunk/wired/banlist.c
r4576 r4754 34 34 #include "settings.h" 35 35 36 #define WD_TEMPBANS_TIMER_INTERVAL 60.037 38 39 36 struct _wd_tempban { 40 37 wi_runtime_base_t base; … … 42 39 wi_string_t *ip; 43 40 wi_time_interval_t interval; 41 wi_timer_t *timer; 44 42 }; 45 43 typedef struct _wd_tempban wd_tempban_t; 46 44 47 45 48 static void wd_tempbans_update_expired(wi_timer_t *); 49 46 static wd_tempban_t * wd_tempban_with_ip(wi_string_t *); 50 47 static wd_tempban_t * wd_tempban_alloc(void); 51 48 static wd_tempban_t * wd_tempban_init_with_ip(wd_tempban_t *, wi_string_t *); … … 53 50 static wi_string_t * wd_tempban_description(wi_runtime_instance_t *); 54 51 52 static void wd_tempban_expire_timer(wi_timer_t *); 55 53 56 static wi_array_t *wd_tempbans; 57 static wi_ timer_t *wd_tempbans_timer;54 55 static wi_hash_t *wd_tempbans; 58 56 59 57 static wi_runtime_id_t wd_tempban_runtime_id = WI_RUNTIME_ID_NULL; … … 68 66 69 67 70 void wd_ tempbans_init(void) {68 void wd_banlist_init(void) { 71 69 wd_tempban_runtime_id = wi_runtime_register_class(&wd_tempban_runtime_class); 72 70 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()); 114 72 } 115 73 … … 118 76 #pragma mark - 119 77 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; 78 wi_boolean_t wd_banlist_ip_is_banned(wi_string_t *ip) { 159 79 wi_file_t *file; 160 80 wi_string_t *string; 161 wd_tempban_t *tempban;162 81 wi_boolean_t banned = false; 163 82 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); 177 86 178 87 if(banned) … … 200 109 201 110 202 void wd_ tempban(wi_string_t *ip) {111 void wd_banlist_add_temporary_ban_for_ip(wi_string_t *ip) { 203 112 wd_tempban_t *tempban; 204 113 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); 210 126 } 127 128 129 130 #pragma mark - 131 132 static 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 138 static 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 144 static 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 153 static 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 162 static 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 176 static 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 30 30 #define WD_BANLIST_H 1 31 31 32 void wd_banlist_init(void); 32 33 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 *); 34 wi_boolean_t wd_banlist_ip_is_banned(wi_string_t *); 35 void wd_banlist_add_temporary_ban_for_ip(wi_string_t *); 38 36 39 37 #endif /* WD_BANLIST_H */ wired/trunk/wired/chats.c
r4750 r4754 35 35 #include "server.h" 36 36 37 #define WD_PUBLIC_CID 1 38 39 37 40 struct _wd_chat { 38 41 wi_runtime_base_t base; … … 120 123 121 124 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 130 125 wd_chat_t * wd_chats_chat_with_cid(wd_cid_t cid) { 131 126 wd_chat_t *chat; … … 156 151 wi_array_unlock(chat->users); 157 152 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) 159 154 wi_hash_remove_data_for_key(wd_chats, key); 160 155 } … … 249 244 250 245 wi_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); 258 251 259 252 return contains; … … 288 281 wi_array_unlock(chat->users); 289 282 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) { 291 284 wi_hash_wrlock(wd_chats); 292 285 wi_hash_remove_data_for_key(wd_chats, wi_number_with_int32(chat->cid)); … … 296 289 297 290 291 292 #pragma mark - 298 293 299 294 void wd_chat_reply_user_list(wd_chat_t *chat) { … … 329 324 330 325 331 #pragma mark -332 333 326 void wd_chat_reply_topic(wd_chat_t *chat) { 334 327 wi_string_t *string; wired/trunk/wired/chats.h
r4750 r4754 34 34 #include "users.h" 35 35 36 #define WD_PUBLIC_CID 137 38 39 36 typedef uint32_t wd_cid_t; 40 37 … … 47 44 48 45 void wd_chats_add_chat(wd_chat_t *); 49 void wd_chats_remove_chat(wd_chat_t *);50 46 wd_chat_t * wd_chats_chat_with_cid(wd_cid_t); 51 47 void wd_chats_remove_user(wd_user_t *); … … 56 52 void wd_chat_add_user_and_broadcast(wd_chat_t *, wd_user_t *); 57 53 void wd_chat_remove_user(wd_chat_t *, wd_user_t *); 54 58 55 void wd_chat_reply_user_list(wd_chat_t *); 59 60 56 void wd_chat_reply_topic(wd_chat_t *); 61 57 void wd_chat_broadcast_topic(wd_chat_t *); … … 68 64 wi_array_t * wd_chat_users(wd_chat_t *); 69 65 66 70 67 wd_topic_t * wd_topic_with_string(wi_string_t *); 71 68 wired/trunk/wired/commands.c
r4750 r4754 407 407 wd_user_identifier(other_user)); 408 408 409 wd_ tempban(wd_user_ip(other_user));409 wd_banlist_add_temporary_ban_for_ip(wd_user_ip(other_user)); 410 410 411 411 wd_user_set_state(other_user, WD_USER_DISCONNECTED); … … 606 606 chat = wd_chats_chat_with_cid(cid); 607 607 608 if(!chat) 609 return; 610 611 if(wd_chat_contains_user(chat, user)) 608 if(!chat || wd_chat_contains_user(chat, user)) 612 609 return; 613 610 … … 910 907 ip = wd_user_ip(user); 911 908 912 if(wd_ ip_is_banned(ip)) {909 if(wd_banlist_ip_is_banned(ip)) { 913 910 wd_reply(511, WI_STR("Banned")); 914 911 wi_log_info(WI_STR("Connection from %@ denied, host is banned"), … … 1089 1086 chat = wd_chats_chat_with_cid(cid); 1090 1087 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)) 1098 1089 return; 1099 1090 … … 1119 1110 chat = wd_chats_chat_with_cid(cid); 1120 1111 1121 if(!chat) 1122 return; 1123 1124 if(wd_chat_contains_user(chat, user)) 1112 if(!chat || wd_chat_contains_user(chat, user)) 1125 1113 return; 1126 1114 … … 1185 1173 1186 1174 cid = wi_string_uint32(WI_ARRAY(arguments, 0)); 1187 1188 if(cid == WD_PUBLIC_CID)1189 return;1190 1191 1175 chat = wd_chats_chat_with_cid(cid); 1192 1176 1193 if(!chat )1177 if(!chat || chat == wd_public_chat) 1194 1178 return; 1195 1179 … … 1255 1239 chat = wd_chats_chat_with_cid(cid); 1256 1240 1257 if(!chat) 1258 return; 1259 1260 if(!wd_chat_contains_user(chat, user)) 1241 if(!chat || !wd_chat_contains_user(chat, user)) 1261 1242 return; 1262 1243 … … 1392 1373 if(!account) { 1393 1374 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)); 1397 1377 1398 1378 return; … … 1405 1385 if(!wi_is_equal(account->password, password)) { 1406 1386 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)); 1410 1389 1411 1390 return; … … 1589 1568 chat = wd_chats_chat_with_cid(cid); 1590 1569 1591 if(!chat) 1592 return; 1593 1594 if(!wd_chat_contains_user(chat, user)) 1570 if(!chat || !wd_chat_contains_user(chat, user)) 1595 1571 return; 1596 1572 … … 1689 1665 return; 1690 1666 1691 if(c id == WD_PUBLIC_CID) {1667 if(chat == wd_public_chat) { 1692 1668 if(!wd_user_account(user)->set_topic) { 1693 1669 wd_reply(516, WI_STR("Permission Denied")); … … 1801 1777 cid = wi_string_uint32(WI_ARRAY(arguments, 0)); 1802 1778 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)) 1808 1781 return; 1809 1782 wired/trunk/wired/main.c
r4750 r4754 190 190 wd_files_init(); 191 191 wd_news_init(); 192 wd_ tempbans_init();192 wd_banlist_init(); 193 193 wd_trackers_init(); 194 194 wd_transfers_init(); … … 237 237 wd_signals_init(); 238 238 wd_block_signals(); 239 wd_tempbans_schedule();240 239 wd_users_schedule(); 241 240 wd_trackers_schedule(); wired/trunk/wired/server.c
r4750 r4754 302 302 303 303 /* spawn a user thread */ 304 user = w i_autorelease(wd_user_init_with_socket(wd_user_alloc(), socket));304 user = wd_user_with_socket(socket); 305 305 306 306 if(!wi_thread_create_thread(wd_control_thread, user)) wired/trunk/wired/users.c
r4750 r4754 121 121 static void wd_users_update_idle(wi_timer_t *); 122 122 123 static wd_user_t * wd_user_alloc(void); 124 static wd_user_t * wd_user_init_with_socket(wd_user_t *, wi_socket_t *); 123 125 static void wd_user_dealloc(wi_runtime_instance_t *); 124 126 static wi_string_t * wd_user_description(wi_runtime_instance_t *); 125 127 126 static wd_uid_t _wd_user_next_uid(void);128 static wd_uid_t wd_user_next_uid(void); 127 129 128 130 … … 264 266 #pragma mark - 265 267 266 wd_user_t * wd_user_alloc(void) { 268 wd_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 276 static wd_user_t * wd_user_alloc(void) { 267 277 return wi_runtime_create_instance(wd_user_runtime_id, sizeof(wd_user_t)); 268 278 } … … 270 280 271 281 272 wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) {282 static wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) { 273 283 wi_address_t *address; 274 284 275 user->uid = _wd_user_next_uid();285 user->uid = wd_user_next_uid(); 276 286 user->socket = wi_retain(socket); 277 287 user->state = WD_USER_CONNECTED; … … 331 341 #pragma mark - 332 342 333 static wd_uid_t _wd_user_next_uid(void) {343 static wd_uid_t wd_user_next_uid(void) { 334 344 wd_uid_t uid; 335 345 wired/trunk/wired/users.h
r4750 r4754 51 51 typedef uint32_t wd_icon_t; 52 52 53 54 53 typedef struct _wd_user wd_user_t; 55 54 … … 65 64 wd_user_t * wd_users_user_for_thread(void); 66 65 67 wd_user_t * wd_user_alloc(void); 68 wd_user_t * wd_user_init_with_socket(wd_user_t *, wi_socket_t *); 66 wd_user_t * wd_user_with_socket(wi_socket_t *); 69 67 70 68 void wd_user_broadcast_status(wd_user_t *);
