Changeset 225 for trunk/instant_messenger
- Timestamp:
- 03/18/08 08:52:00 (17 years ago)
- Location:
- trunk/instant_messenger/webjabber
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/instant_messenger/webjabber/webjabber.c
r216 r225 27 27 // Global variables 28 28 char g_program_name[100]; // Initialised from argv[0] in ParseArgs() 29 struct allowed_ip * allowed_ips = (struct allowed_ip *)NULL; 29 30 int quantity_of_fd = 0; // quantity of file descriptor 30 31 32 static struct connections * connections = (struct connections *)NULL; 31 33 pthread_mutex_t mutexsum; 32 34 33 35 // structs 36 struct allowed_ip 37 { 38 char * ip; 39 struct allowed_ip * next; 40 }; 41 34 42 struct accept 35 43 { … … 47 55 int * jabber_fd, 48 56 * read; 57 time_t last_access; 49 58 }; 50 59 … … 65 74 66 75 void recursive (int, struct in_addr, unsigned short); 76 int getPermission(char *); 67 77 void ParseArgs (int, char **, struct in_addr *, unsigned short *, struct in_addr *, unsigned short *); 68 78 void Initialise (void); … … 71 81 void Daemonise (void); 72 82 void MainLoop (int, struct in_addr, unsigned short); 83 void ClearList(void); 73 84 struct accept * AcceptClientConnection (int); 74 85 void VerifyClient(void *); 75 86 struct client * Handshake (struct accept *, struct read_write *); 76 struct client * LookFor (char *, char *); 87 void * withConnections(void * (*func)(), ...); 88 //struct client * LookFor (char *, char *); 89 //void * LookFor (struct connections *, char *, char *); 90 void * LookFor (char *, char *); 77 91 void Reading (void *); 78 92 void Writing (void *); … … 99 113 int listen_fd; 100 114 115 //pthread_t clear_list_thread; 116 101 117 ParseArgs (argc, argv, &remote_addr, &remote_port, &local_addr, &local_port); 102 118 … … 111 127 //Daemonise (); 112 128 129 //pthread_mutex_init(&mutexsum, NULL); 130 //pthread_create(&clear_list_thread, NULL, (void *) &ClearList, (void *)NULL ); 131 113 132 //MainLoop (listen_fd, remote_addr, remote_port); // never returns 114 133 recursive(listen_fd, remote_addr, remote_port); 134 135 //pthread_mutex_destroy(&mutexsum); 115 136 116 137 exit (EXIT_SUCCESS); … … 189 210 } 190 211 212 int getPermission(char * ip) 213 { 214 struct allowed_ip * allow_ip = allowed_ips; 215 int allow = 0; 216 217 while ( !allow && allow_ip->next != (struct allowed_ip *)NULL ) 218 { 219 //printf("strcmp(%s, %s)\n\n", ip, allow_ip->ip); 220 if ( strcmp(ip, allow_ip->ip) == 0 ) 221 allow = 1; 222 else 223 allow_ip = allow_ip->next; 224 } 225 226 return allow; 227 } 228 191 229 // Initialise() 192 230 // Setup syslog, signal handlers, and other intialisation. 193 231 void Initialise (void) 194 232 { 233 FILE * allowed_ips_fd; 234 struct allowed_ip * allow_ip; 235 236 if ( !(allowed_ips_fd = fopen("webjabber.cfg", "r")) ) 237 syslog (LOG_ERR, "Impossivel abrir o arquivo 'webjabber.cfg'"), exit (EXIT_FAILURE); 238 239 while ( !feof(allowed_ips_fd) ) 240 { 241 char * line = (char *) malloc(16 * sizeof(char)); 242 243 bzero(line, 16); 244 fgets(line, 16, allowed_ips_fd); 245 246 if ( strlen(line) ) 247 { 248 if ( line[strlen(line) - 1] == '\n' ) 249 line[strlen(line) - 1] = '\0'; 250 251 if ( allowed_ips == (struct allowed_ip *)NULL ) 252 allow_ip = allowed_ips = (struct allowed_ip *) malloc(sizeof(struct allowed_ip)); 253 else 254 { 255 allow_ip = allowed_ips; 256 while ( allow_ip->next != (struct allowed_ip *)NULL ) 257 allow_ip = allow_ip->next; 258 allow_ip->next = (struct allowed_ip *) malloc(sizeof(struct allowed_ip)); 259 allow_ip = allow_ip->next; 260 } 261 allow_ip->next = (struct allowed_ip *)NULL; 262 allow_ip->ip = line; 263 } 264 } 265 266 char * initial_path = (char *) malloc(128 * sizeof(char)); 267 getcwd(initial_path, 128); 268 195 269 openlog (g_program_name, LOG_PID, LOG_USER); 196 syslog (LOG_INFO, "%s started ", g_program_name);270 syslog (LOG_INFO, "%s started in path [#%d] %s", g_program_name, strlen(initial_path), initial_path); 197 271 198 272 chdir ("/"); // Change working directory to the root. … … 329 403 void MainLoop (int listen_fd, struct in_addr rem_addr, unsigned short rem_port) 330 404 { 331 pthread_mutex_init(&mutexsum, NULL);332 405 //pid_t helper_pid; 406 pthread_t clear_list_thread; 333 407 334 408 syslog (LOG_INFO, "MainLoop :: listen_fd(fd%d)", listen_fd); 335 409 //signal (SIGCHLD, SIG_IGN); 336 410 411 pthread_mutex_init(&mutexsum, NULL); 412 pthread_create(&clear_list_thread, NULL, (void *) &ClearList, (void *)NULL ); 337 413 while ( quantity_of_fd < 50 ) 338 414 { … … 347 423 pthread_create(&client->accept_thread, NULL, (void *) &VerifyClient, (void *) client); 348 424 } 349 350 425 pthread_mutex_destroy(&mutexsum); 426 } 427 428 void ClearList(void) 429 { 430 for ( ; ; ) 431 { 432 pthread_mutex_lock(&mutexsum); 433 struct connections * connection = connections; 434 while ( connection != (struct connections *)NULL ) 435 { 436 if ( *(connection->client->jabber_fd) != (int)NULL && time(NULL) - connection->client->last_access > 70 ) 437 { 438 shutdown(*(connection->client->jabber_fd), SHUT_RDWR); 439 close(*(connection->client->jabber_fd)); 440 *(connection->client->jabber_fd) = (int)NULL; 441 } 442 connection = connection->next; 443 } 444 pthread_mutex_unlock(&mutexsum); 445 sleep(5); 446 } 351 447 } 352 448 … … 425 521 //printf("\nNew connection [#%d]: %s\n", *(accept->fd), ip); 426 522 427 if ( strcmp(ip, "10.15.20.42") == 0)523 if ( getPermission(ip) ) 428 524 if ( (client = Handshake(accept, read_write)) == (struct client *)NULL ) 429 525 { … … 531 627 //printf(" fd[%2d]: client\n", *(accept->fd)); 532 628 533 if ( (client = LookFor (user, pass)) != (struct client *)NULL ) 629 //if ( (client = LookFor (user, pass)) != (struct client *)NULL ) 630 if ( (client = (struct client *) withConnections(LookFor, user, pass)) != (struct client *)NULL ) 534 631 { 535 632 //puts("\n\n:::::::::::::::: INFO #0 :::::::::::::::::::::\n"); … … 573 670 } 574 671 575 struct client * LookFor (char * user, char * pass) 672 void * withConnections(void * (*func)(), ...) 673 { 674 //static struct connections * connections = (struct connections *)NULL; 675 if ( (*func) == LookFor ) 676 { 677 va_list ap; 678 char * user, 679 * pass; 680 void * _return; 681 682 va_start(ap, (*func)); 683 user = va_arg(ap, char *); 684 pass = va_arg(ap, char *); 685 va_end(ap); 686 687 pthread_mutex_lock(&mutexsum); 688 _return = (*func)(user, pass); 689 pthread_mutex_unlock(&mutexsum); 690 return _return; 691 } 692 else 693 { 694 } 695 } 696 697 //struct client * LookFor (char * user, char * pass) 698 //void * LookFor (struct connections * connections, char * user, char * pass) 699 void * LookFor (char * user, char * pass) 576 700 { 577 701 //puts("\n\n:::::::::::::::: LOOKFOR :::::::::::::::::::::\n"); 578 static struct connections * connections = (struct connections *)NULL;702 //static struct connections * connections = (struct connections *)NULL; 579 703 580 704 //pthread_mutex_lock(&mutexsum); … … 597 721 598 722 *(connections->client->jabber_fd) = (int)NULL; 723 connections->client->last_access = time(NULL); 724 599 725 connections->next = (struct connections *)NULL; 600 726 … … 615 741 //pthread_mutex_unlock(&mutexsum); 616 742 //puts(":::::::::::::::: ACHOU #1 :::::::::::::::::::::"); 743 connection->client->last_access = time(NULL); 617 744 return connection->client; 618 745 } … … 645 772 646 773 *(connection->client->jabber_fd) = (int)NULL; 774 connection->client->last_access = time(NULL); 775 647 776 connection->next = (struct connections *)NULL; 648 777 //pthread_mutex_unlock(&mutexsum); … … 687 816 { 688 817 // descomentar para ver oq esta passado de informacao 689 //printf(" write(#%d/client)[%d]:\n{\n%s.......\n}\n", client_fd, strlen(buf_2_client), buf_2_client);818 //printf("(jabber/client[#%d])[%d]:\n{\n%s.......\n}\n", client_fd, strlen(buf_2_client), buf_2_client); 690 819 for ( i = 0; i < bytes_rcvd_jabber; i += bytes_sent_client ) 691 820 { … … 701 830 { 702 831 // descomentar para ver oq esta passado de informacao 703 //printf(" write(#%d/jabber)[%d]:\n{\n%s.......\n}\n", jabber_fd, strlen(buf_2_jabber), buf_2_jabber);832 //printf("(client/jabber[#%d])[%d]:\n{\n%s.......\n}\n", jabber_fd, strlen(buf_2_jabber), buf_2_jabber); 704 833 for ( i = 0; i < bytes_rcvd_client; i += bytes_sent_jabber ) 705 834 {
Note: See TracChangeset
for help on using the changeset viewer.