Changeset 225


Ignore:
Timestamp:
03/18/08 08:52:00 (16 years ago)
Author:
niltonneto
Message:
 
Location:
trunk/instant_messenger/webjabber
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/instant_messenger/webjabber/webjabber.c

    r216 r225  
    2727// Global variables 
    2828char g_program_name[100];       // Initialised from argv[0] in ParseArgs() 
     29struct allowed_ip * allowed_ips = (struct allowed_ip *)NULL; 
    2930int quantity_of_fd = 0;         // quantity of file descriptor 
    3031 
     32static struct connections * connections = (struct connections *)NULL; 
    3133pthread_mutex_t mutexsum; 
    3234 
    3335// structs 
     36struct allowed_ip 
     37{ 
     38        char * ip; 
     39        struct allowed_ip * next; 
     40}; 
     41 
    3442struct accept 
    3543{ 
     
    4755        int * jabber_fd, 
    4856                * read; 
     57        time_t last_access; 
    4958}; 
    5059 
     
    6574 
    6675void recursive (int, struct in_addr, unsigned short); 
     76int getPermission(char *); 
    6777void ParseArgs (int, char **, struct in_addr *, unsigned short *, struct in_addr *, unsigned short *); 
    6878void Initialise (void); 
     
    7181void Daemonise (void); 
    7282void MainLoop (int, struct in_addr, unsigned short); 
     83void ClearList(void); 
    7384struct accept * AcceptClientConnection (int); 
    7485void VerifyClient(void *); 
    7586struct client * Handshake (struct accept *, struct read_write *); 
    76 struct client * LookFor (char *, char *); 
     87void * withConnections(void * (*func)(), ...); 
     88//struct client * LookFor (char *, char *); 
     89//void * LookFor (struct connections *, char *, char *); 
     90void * LookFor (char *, char *); 
    7791void Reading (void *); 
    7892void Writing (void *); 
     
    99113        int listen_fd; 
    100114 
     115        //pthread_t clear_list_thread; 
     116 
    101117        ParseArgs (argc, argv, &remote_addr, &remote_port, &local_addr, &local_port); 
    102118 
     
    111127        //Daemonise (); 
    112128 
     129        //pthread_mutex_init(&mutexsum, NULL); 
     130        //pthread_create(&clear_list_thread, NULL, (void *) &ClearList, (void *)NULL ); 
     131 
    113132        //MainLoop (listen_fd, remote_addr, remote_port); // never returns 
    114133        recursive(listen_fd, remote_addr, remote_port); 
     134 
     135        //pthread_mutex_destroy(&mutexsum); 
    115136 
    116137        exit (EXIT_SUCCESS); 
     
    189210} 
    190211 
     212int 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 
    191229// Initialise() 
    192230// Setup syslog, signal handlers, and other intialisation. 
    193231void Initialise (void) 
    194232{ 
     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 
    195269        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); 
    197271 
    198272        chdir ("/"); // Change working directory to the root. 
     
    329403void MainLoop (int listen_fd, struct in_addr rem_addr, unsigned short rem_port) 
    330404{ 
    331         pthread_mutex_init(&mutexsum, NULL); 
    332405        //pid_t helper_pid; 
     406        pthread_t clear_list_thread; 
    333407 
    334408        syslog (LOG_INFO, "MainLoop :: listen_fd(fd%d)", listen_fd); 
    335409        //signal (SIGCHLD, SIG_IGN); 
    336410 
     411        pthread_mutex_init(&mutexsum, NULL); 
     412        pthread_create(&clear_list_thread, NULL, (void *) &ClearList, (void *)NULL ); 
    337413        while ( quantity_of_fd < 50 ) 
    338414        { 
     
    347423                pthread_create(&client->accept_thread, NULL, (void *) &VerifyClient, (void *) client); 
    348424        } 
    349  
    350425        pthread_mutex_destroy(&mutexsum); 
     426} 
     427 
     428void 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        } 
    351447} 
    352448 
     
    425521        //printf("\nNew connection [#%d]: %s\n", *(accept->fd), ip); 
    426522 
    427         if ( strcmp(ip, "10.15.20.42") == 0 ) 
     523        if ( getPermission(ip) ) 
    428524                if ( (client = Handshake(accept, read_write)) == (struct client *)NULL ) 
    429525                { 
     
    531627        //printf("  fd[%2d]: client\n", *(accept->fd)); 
    532628 
    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 ) 
    534631        { 
    535632                //puts("\n\n:::::::::::::::: INFO #0 :::::::::::::::::::::\n"); 
     
    573670} 
    574671 
    575 struct client * LookFor (char * user, char * pass) 
     672void * 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) 
     699void * LookFor (char * user, char * pass) 
    576700{ 
    577701        //puts("\n\n:::::::::::::::: LOOKFOR :::::::::::::::::::::\n"); 
    578         static struct connections * connections = (struct connections *)NULL; 
     702        //static struct connections * connections = (struct connections *)NULL; 
    579703 
    580704        //pthread_mutex_lock(&mutexsum); 
     
    597721 
    598722                *(connections->client->jabber_fd) = (int)NULL; 
     723                connections->client->last_access = time(NULL); 
     724 
    599725                connections->next = (struct connections *)NULL; 
    600726 
     
    615741                                        //pthread_mutex_unlock(&mutexsum); 
    616742                                        //puts(":::::::::::::::: ACHOU #1 :::::::::::::::::::::"); 
     743                                        connection->client->last_access = time(NULL); 
    617744                                        return connection->client; 
    618745                                } 
     
    645772 
    646773                *(connection->client->jabber_fd) = (int)NULL; 
     774                connection->client->last_access = time(NULL); 
     775 
    647776                connection->next = (struct connections *)NULL; 
    648777                //pthread_mutex_unlock(&mutexsum); 
     
    687816                { 
    688817                        // 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); 
    690819                        for ( i = 0; i < bytes_rcvd_jabber; i += bytes_sent_client ) 
    691820                        { 
     
    701830                { 
    702831                        // 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); 
    704833                        for ( i = 0; i < bytes_rcvd_client; i += bytes_sent_jabber ) 
    705834                        { 
Note: See TracChangeset for help on using the changeset viewer.