1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
| #gcc server.c -o server -lpthread -w
#include <ctype.h> #include <fcntl.h> #include <pthread.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/epoll.h> #include <sys/file.h> #include <sys/param.h> #include <sys/stat.h> #include <sys/types.h> #include <syslog.h> #include <time.h> #include <unistd.h>
#include "clientinfo.h" #include "threadpool.h"
#define CLK_TCK 1000 #define MAX_USER_NUM 0x50 #define LOCK_TIME 1000 * 60 * 10 #define MAX_THREAD_NUM 4
typedef struct online_user { char userfifo[0x50]; char statfifo[0x50]; char username[0x50]; char passwd[0x50]; struct online_user* next; } USERLIST;
typedef struct { char userfifo[0x50]; char statfifo[0x50]; char username[0x50]; char passwd[0x50]; int login_times; clock_t start_time; clock_t end_time; } USER_INFO;
USERLIST* ONLINE_USERLIST; int regist_fd, login_fd, msg_fd, logout_fd; int user_num = 0, oneline_user_num = 0; USER_INFO USERINFO_DATA[MAX_USER_NUM]; tpool_t* pool; pthread_mutex_t usernum_mutex, onlinenum_mutex, list_mutex;
void online_user_add(USERLIST* list) { USERLIST* p = ONLINE_USERLIST; while ((p->next) != NULL) { p = p->next; } p->next = list; }
void oneline_user_delete(char* str) { USERLIST* p = ONLINE_USERLIST; while (strcmp(p->next->username, str)) { p = p->next; } USERLIST* temp = p->next; p->next = temp->next; free(temp); }
void get_oneline_username(char* usernamebuf) { USERLIST* p = ONLINE_USERLIST; char* namebuf_index = usernamebuf; while ((p->next) != NULL) { p = p->next; strcpy(namebuf_index, p->username); namebuf_index += strlen(p->username); *(namebuf_index) = ' '; *(++namebuf_index) = ' '; } }
void epoll_add_event(int epoll_fd, int fd, int event) { struct epoll_event ev; ev.events = event; ev.data.fd = fd; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); }
void create_fifo(char* str) { int res; if (access(str, F_OK) == -1) { res = mkfifo(str, 0777); if (res < 0) errorinfo("[x] mkfifo failed"); } }
char* gettime() { clock_t current_time; time(¤t_time); return asctime(gmtime(¤t_time)); }
void create_log(char* username) { char userlog[0x50]; char log_buffer[0x50]; sprintf(userlog, "/home/ubuntu/oslab/serverinfo/user_log/%s_log", username);
int log_fd = open(userlog, O_RDWR | O_NONBLOCK | O_CREAT | O_APPEND); if (log_fd < 0) errorinfo("open user_log failed"); sprintf(log_buffer, " %s regist %s \n", username, gettime()); int n = write(log_fd, log_buffer, strlen(log_buffer + 1)); close(log_fd); }
void login_log(char* username) { char userlog[0x50]; char log_buffer[0x50]; sprintf(userlog, "/home/ubuntu/oslab/serverinfo/user_log/%s_log", username);
int log_fd = open(userlog, O_RDWR | O_NONBLOCK | O_APPEND); if (log_fd < 0) errorinfo("open user_log failed"); sprintf(log_buffer, "%s login %s \n", username, gettime()); int n = write(log_fd, log_buffer, strlen(log_buffer + 1)); close(log_fd); }
void logout_log(char* username) { char userlog[0x50]; char log_buffer[0x50]; sprintf(userlog, "/home/ubuntu/oslab/serverinfo/user_log/%s_log", username);
int log_fd = open(userlog, O_RDWR | O_NONBLOCK | O_APPEND); if (log_fd < 0) errorinfo("open user_log failed"); sprintf(log_buffer, "%s logout %s \n", username, gettime()); int n = write(log_fd, log_buffer, strlen(log_buffer + 1)); close(log_fd); }
void msg_log(char* sender, char* receiver, int success) { char sender_log[0x50]; char receiver_log[0x50]; char log_buffer[0x50]; sprintf(sender_log, "/home/ubuntu/oslab/serverinfo/user_log/%s_log", sender); int sender_log_fd = open(sender_log, O_RDWR | O_NONBLOCK | O_APPEND);
if (success) { sprintf(receiver_log, "/home/ubuntu/oslab/serverinfo/user_log/%s_log", receiver); int receiver_log_fd = open(receiver_log, O_RDWR | O_NONBLOCK | O_APPEND); if (sender_log_fd < 0 || receiver_log_fd < 0) errorinfo("open user_log failed"); sprintf(log_buffer, "%s send messaget to %s (success). %s\n", sender, receiver, gettime()); int n, m; n = write(receiver_log_fd, log_buffer, strlen(log_buffer + 1)); m = write(sender_log_fd, log_buffer, strlen(log_buffer + 1)); close(receiver_log_fd); close(sender_log_fd); } else { if (sender_log_fd < 0) errorinfo("open user_log failed"); sprintf(log_buffer, "%s send messaget to %s (failed). %s\n", sender, receiver, gettime()); int n = write(sender_log_fd, log_buffer, strlen(log_buffer + 1)); close(sender_log_fd); } }
void fifo_init() { create_fifo(REG_FIFO); create_fifo(LOGIN_FIFO); create_fifo(MSG_FIFO); create_fifo(LOGOUT_FIFO); regist_fd = open(REG_FIFO, O_RDWR | O_NONBLOCK); if (regist_fd < 0) errorinfo("[x] open regist FIFO failed"); login_fd = open(LOGIN_FIFO, O_RDWR | O_NONBLOCK); if (login_fd < 0) errorinfo("[x] open login FIFO failed"); logout_fd = open(LOGOUT_FIFO, O_RDWR | O_NONBLOCK); if (logout_fd < 0) errorinfo("[x] open logout FIFO failed"); msg_fd = open(MSG_FIFO, O_RDWR | O_NONBLOCK); if (msg_fd < 0) errorinfo("[x] open msg FIFO failed"); }
void profile_init() { ONLINE_USERLIST = (USERLIST*)malloc(sizeof(USERLIST)); ONLINE_USERLIST->next = NULL;
pthread_mutex_init(&usernum_mutex, NULL); pthread_mutex_init(&onlinenum_mutex, NULL); pthread_mutex_init(&list_mutex, NULL);
pool = NULL; if (create_tpool(&pool, MAX_THREAD_NUM) != 0) { errorinfo("create_tpool failed!"); } }
void* login_thread() { int res; REG_LOGIN_INFO login_info; int statinfo_fd; int is_success = 0; int is_overtimes = 0; res = read(login_fd, &login_info, sizeof(REG_LOGIN_INFO)); if (res > 0) { for (int i = 0; i < user_num; i++) { if (!strcmp(login_info.username, USERINFO_DATA[i].username)) { if (USERINFO_DATA[i].login_times >= 4) { is_overtimes = 1; USERINFO_DATA[i].end_time = clock(); if (((USERINFO_DATA[i].end_time - USERINFO_DATA[i].start_time)) > LOCK_TIME) {
is_overtimes = 0; USERINFO_DATA[i].login_times = 0; } else { break; } }
if (!strcmp(login_info.passwd, USERINFO_DATA[i].passwd)) { char buf[10]; itoa(LOGIN_SUCCESS, buf, 10); statinfo_fd = open(login_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] login_thread open statfifo failed"); write(statinfo_fd, buf, 10); close(statinfo_fd); is_success = 1;
pthread_mutex_lock(&list_mutex);
if (!(ONLINE_USERLIST->next)) {
USERLIST* user = (USERLIST*)malloc(sizeof(USERLIST)); strcpy(user->userfifo, login_info.userfifo); strcpy(user->username, login_info.username); strcpy(user->statfifo, login_info.statfifo); strcpy(user->passwd, login_info.passwd); user->next = NULL; ONLINE_USERLIST->next = user; } else { USERLIST* user = (USERLIST*)malloc(sizeof(USERLIST)); strcpy(user->userfifo, login_info.userfifo); strcpy(user->username, login_info.username); strcpy(user->statfifo, login_info.statfifo); strcpy(user->passwd, login_info.passwd); user->next = NULL; online_user_add(user); }
pthread_mutex_unlock(&list_mutex);
pthread_mutex_lock(&onlinenum_mutex); oneline_user_num++; pthread_mutex_unlock(&onlinenum_mutex); login_log(login_info.username); break; } else { if (!USERINFO_DATA[i].start_time) { USERINFO_DATA[i].start_time = clock(); } USERINFO_DATA[i].login_times++; } } } if (!is_success) { char buf[10]; if (is_overtimes) { itoa(LOGIN_OVERTIMES, buf, 10); } else { itoa(LOGIN_FAILED, buf, 10); } statinfo_fd = open(login_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] login_thread open statfifo failed"); write(statinfo_fd, buf, 10); close(statinfo_fd); } } }
void* regist_thread() { int res; REG_LOGIN_INFO regist_info; int statinfo_fd; int is_success = 1;
res = read(regist_fd, ®ist_info, sizeof(REG_LOGIN_INFO)); if (res > 0) { for (int i = 0; i < user_num; i++) { if (!strcmp(regist_info.username, USERINFO_DATA[i].username)) { is_success = 0; break; } } if (is_success) { pthread_mutex_lock(&usernum_mutex); int i = user_num++; pthread_mutex_unlock(&usernum_mutex); strcpy(USERINFO_DATA[i].username, regist_info.username); strcpy(USERINFO_DATA[i].passwd, regist_info.passwd); strcpy(USERINFO_DATA[i].userfifo, regist_info.userfifo); strcpy(USERINFO_DATA[i].statfifo, regist_info.statfifo); char buf[10]; itoa(REGIST_SUCCESS, buf, 10); statinfo_fd = open(regist_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] regist_thread open statfifo failed");
create_log(regist_info.username);
write(statinfo_fd, buf, 10); close(statinfo_fd); } else { char buf[10]; itoa(REGIST_FAILED, buf, 10); statinfo_fd = open(regist_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] regist_thread open statfifo failed"); write(statinfo_fd, buf, 10); close(statinfo_fd); } } }
void* msg_thread() { int res; MESSAGE_INFO msg_info; int receiver_fd; int is_send = 1;
res = read(msg_fd, &msg_info, sizeof(MESSAGE_INFO)); if (res > 0) { if (strcmp(msg_info.receiver, SERVER_NAME)) { char receiver_fifo[0x50]; sprintf(receiver_fifo, "/home/ubuntu/oslab/clientinfo/userfifo/client_%s_FIFO", msg_info.receiver); receiver_fd = open(receiver_fifo, O_WRONLY | O_NONBLOCK); if (receiver_fd < 0) { puts("[x] msg_thread open failed"); is_send = 0; } char buf[10]; char sender_fifo[0x50]; sprintf(sender_fifo, "/home/ubuntu/oslab/clientinfo/statfifo/client_%s_stat", msg_info.sender);
if (is_send) { write(receiver_fd, &msg_info, sizeof(MESSAGE_INFO)); close(receiver_fd); msg_log(msg_info.sender, msg_info.receiver, is_send);
itoa(SEND_SUCCESS, buf, 10); int sender_fd = open(sender_fifo, O_WRONLY | O_NONBLOCK); if (sender_fd < 0) errorinfo("[x] msg_thread open userfifo failed"); write(sender_fd, buf, 10); close(sender_fd); } else { msg_log(msg_info.sender, msg_info.receiver, is_send);
itoa(SEND_FAILED, buf, 10); int sender_fd = open(sender_fifo, O_WRONLY | O_NONBLOCK); if (sender_fd < 0) errorinfo("[x] msg_thread open userfifo failed"); write(sender_fd, buf, 10); close(sender_fd); } } else {
char receiver_fifo[0x50];
sprintf(receiver_fifo, "/home/ubuntu/oslab/clientinfo/userfifo/client_%s_FIFO", msg_info.sender); receiver_fd = open(receiver_fifo, O_WRONLY | O_NONBLOCK); char buf[10]; get_oneline_username(msg_info.message); itoa(oneline_user_num, buf, 10); strcpy(msg_info.receiver, buf); strcpy(msg_info.sender, SERVER_NAME); write(receiver_fd, &msg_info, sizeof(MESSAGE_INFO)); close(receiver_fd); } } }
void* logout_thread() { int res; LOGOUT_INFO logout_info; int statinfo_fd; int is_find = 0; res = read(logout_fd, &logout_info, sizeof(LOGOUT_INFO)); if (res > 0) { for (int i = 0; i < user_num; i++) { if (!strcmp(USERINFO_DATA[i].username, logout_info.username)) { is_find = 1; break; } }
if (is_find) { pthread_mutex_lock(&list_mutex); oneline_user_delete(logout_info.username); pthread_mutex_unlock(&list_mutex);
pthread_mutex_lock(&onlinenum_mutex); oneline_user_num--; pthread_mutex_unlock(&onlinenum_mutex); char buf[10]; itoa(LOGOUT_SUCCESS, buf, 10); statinfo_fd = open(logout_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] logout_thread open statfifo failed");
logout_log(logout_info.username); write(statinfo_fd, buf, 10); close(statinfo_fd); } else { char buf[10]; itoa(LOGOUT_FAILED, buf, 10); statinfo_fd = open(logout_info.statfifo, O_WRONLY | O_NONBLOCK); if (statinfo_fd < 0) errorinfo("[x] logout_thread open statfifo failed");
write(statinfo_fd, buf, 10); close(statinfo_fd); } } }
void thread_init() { pthread_t ptd_1, ptd_2, ptd_3, ptd_4; pthread_create(&ptd_1, NULL, regist_thread, NULL); pthread_create(&ptd_2, NULL, login_thread, NULL); pthread_create(&ptd_3, NULL, msg_thread, NULL); pthread_create(&ptd_4, NULL, logout_thread, NULL); }
void monitor() { struct epoll_event events[4]; int task_num = 0; int epoll_fd = epoll_create(4); if (epoll_fd < 0) { errorinfo("[x] epoll create failed"); } epoll_add_event(epoll_fd, login_fd, EPOLLIN | EPOLLET); epoll_add_event(epoll_fd, regist_fd, EPOLLIN | EPOLLET); epoll_add_event(epoll_fd, msg_fd, EPOLLIN | EPOLLET); epoll_add_event(epoll_fd, logout_fd, EPOLLIN | EPOLLET); while (1) { int num = epoll_wait(epoll_fd, events, 4, -1); if (num < 0) errorinfo("[x] epoll wait"); if (num == 0) continue; pthread_t ptd_1, ptd_2, ptd_3, ptd_4; for (int i = 0; i < num; i++) { struct epoll_event ev = events[i]; if (ev.data.fd == login_fd && ev.events & EPOLLIN) { add_task_2_tpool(pool, login_thread, NULL); epoll_add_event(epoll_fd, login_fd, EPOLLIN | EPOLLET); } else if (ev.data.fd == regist_fd && ev.events & EPOLLIN) { add_task_2_tpool(pool, regist_thread, NULL); epoll_add_event(epoll_fd, regist_fd, EPOLLIN | EPOLLET); } else if (ev.data.fd == msg_fd && ev.events & EPOLLIN) { add_task_2_tpool(pool, msg_thread, NULL); epoll_add_event(epoll_fd, msg_fd, EPOLLIN | EPOLLET); } else if (ev.data.fd == logout_fd && ev.events & EPOLLIN) { add_task_2_tpool(pool, logout_thread, NULL); epoll_add_event(epoll_fd, logout_fd, EPOLLIN | EPOLLET); } } } }
int run_server() { fifo_init(); profile_init(); monitor(); } int main() { signal(SIGTTOU, SIG_IGN); signal(SIGTTIN, SIG_IGN); signal(SIGTSTP, SIG_IGN); signal(SIGHUP, SIG_IGN); int pid, i; pid = fork(); if (pid > 0) { exit(0); } else if (pid < 0) { errorinfo("[x] fork error"); } for (i = 0; i < NOFILE; close(i++)) { chdir("/"); umask(0); signal(SIGCHLD, SIG_IGN); }
syslog(LOG_USER | LOG_INFO, "my chat_server process start \n");
run_server(); }
|