Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #include <signal.h>
- #include <string.h>
- #define MAXCON 100
- int keepAlive = 1;
- int rs;
- int thrcount=0;
- pthread_t* thr[MAXCON];
- struct users {
- char username[1024];
- char password[1024];
- int status;
- };
- typedef struct users users;
- users uservect[1024];
- int usercnt;
- struct thrarg {
- int sock;
- int thrindex;
- struct sockaddr_in caddr;
- int caddr_len;
- };
- typedef struct thrarg thrarg;
- pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
- int findFreeThread() {
- int i;
- for(i=0;i<MAXCON;i++) {
- if(thr[i] == NULL) {
- return i;
- }
- }
- return -1;
- }
- int logcount() {
- int i;
- int k=0;
- for(i=0;i<MAXCON;i++) {
- if(uservect[i].status>0) k++;
- }
- return k;
- }
- int response(char* username, char* password) {
- printf("%s %s \n",username,password);
- int i;
- for(i=0;i<usercnt;i++) {
- if(strcmp(username,uservect[i].username) == 0) {
- if(strcmp(password,uservect[i].password) == 0 ) {
- if(uservect[i].status == 0) {
- uservect[i].status = 1;
- return logcount();
- }
- return -2;
- }
- return -1;
- }
- }
- return -1;
- }
- void logout(char* username) {
- int i;
- for(i=0;i<usercnt;i++) {
- if(strcmp(username,uservect[i].username) == 0) {
- uservect[i].status = 0;
- }
- }
- }
- void serve(thrarg* arg) {
- pthread_mutex_lock(&mtx);
- thrcount++;
- pthread_mutex_unlock(&mtx);
- char buf[1024];
- int k = recv(arg->sock,buf,1024,0);
- if(k<=0) {
- perror("Could not receive");
- }
- buf[k] = 0;
- char username[1024];
- strcpy(username,buf);
- k = recv(arg->sock,buf,1024,0);
- if(k<=0) {
- perror("Could not receive password");
- }
- buf[k] = 0;
- char password[1024];
- strcpy(password,buf);
- pthread_mutex_lock(&mtx);
- printf("Username:%s\nPassword:%s\n",username,password);
- pthread_mutex_unlock(&mtx);
- int resp = response(username,password);
- pthread_mutex_lock(&mtx);
- printf("%d\n",resp);
- pthread_mutex_unlock(&mtx);
- resp = htonl(resp);
- k = send(arg->sock,&resp,sizeof(int),0);
- if(k<=0) {
- perror("Could not send");
- }
- pthread_mutex_lock(&mtx);
- if(resp>0) {
- //logout(username);
- }
- thrcount--;
- free(thr[arg->thrindex]);
- thr[arg->thrindex] = NULL;
- free(arg);
- pthread_mutex_unlock(&mtx);
- }
- void handler() {
- if(close(rs)<0) {
- perror("Could not close socket");
- }
- keepAlive = 0;
- int i;
- for(i=0;i<MAXCON;i++) {
- pthread_t* p = thr[i];
- if(p) {
- pthread_join(*p,NULL);
- }
- }
- return;
- }
- void readUsers() {
- FILE* fd = fopen("users.txt","r");
- if(fd==0) {
- printf("Could not open file!\n");
- exit(3);
- }
- int i=0;
- while( !feof(fd) ) {
- fscanf(fd,"%s %s",uservect[i].username, uservect[i].password);
- //printf("%s %s\n",uservect[i].username,uservect[i].password);
- i++;
- }
- usercnt = i;
- }
- int main() {
- readUsers();
- int rs = socket(AF_INET,SOCK_STREAM,0);
- if(rs < 0) {
- perror("Could not create rendezvous socket!");
- }
- struct sockaddr_in addr,caddr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(1754);
- addr.sin_addr.s_addr = INADDR_ANY;
- if(bind(rs,(struct sockaddr*) &addr,sizeof(addr))<0) {
- perror("Could not bind server");
- }
- if(listen(rs,5) < 0 ) {
- perror("Could not listen");
- }
- signal(SIGINT,handler);
- int sock;
- int len = sizeof(caddr);
- while(keepAlive) {
- sock = accept(rs,(struct sockaddr*) &caddr,&len);
- if((sock<0)&&keepAlive) {
- perror("Could not accept connection!");
- }
- int k = findFreeThread();
- if(k<0) {
- close(sock);
- continue;
- }
- if(len&&keepAlive) {
- printf("Incomming connection from %s on port %d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
- }
- thrarg *arg = malloc(sizeof(thrarg));
- arg->sock = sock;
- arg->thrindex = k;
- arg->caddr = caddr;
- arg->caddr_len = len;
- thr[k] = malloc(sizeof(pthread_t));
- pthread_create(thr[k],NULL,(void*) serve,arg);
- }
- //join remaining threads
- /*
- int i;
- for(i=0;i<MAXCON;i++) {
- pthread_t* p = thr[i];
- if(p) {
- pthread_join(*p,NULL);
- }
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement