Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <pthread.h>
  8. #include <signal.h>
  9. #include <string.h>
  10.  
  11. #define MAXCON 100
  12.  
  13. int keepAlive = 1;
  14. int rs;
  15. int thrcount=0;
  16.  
  17. pthread_t* thr[MAXCON];
  18.  
  19. struct users {
  20. char username[1024];
  21. char password[1024];
  22. int status;
  23. };
  24.  
  25. typedef struct users users;
  26.  
  27. users uservect[1024];
  28. int usercnt;
  29.  
  30. struct thrarg {
  31. int sock;
  32. int thrindex;
  33. struct sockaddr_in caddr;
  34. int caddr_len;
  35. };
  36.  
  37. typedef struct thrarg thrarg;
  38.  
  39. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  40.  
  41.  
  42. int findFreeThread() {
  43. int i;
  44. for(i=0;i<MAXCON;i++) {
  45. if(thr[i] == NULL) {
  46. return i;
  47. }
  48. }
  49.  
  50. return -1;
  51. }
  52.  
  53. int logcount() {
  54. int i;
  55. int k=0;
  56. for(i=0;i<MAXCON;i++) {
  57. if(uservect[i].status>0) k++;
  58. }
  59. return k;
  60. }
  61.  
  62. int response(char* username, char* password) {
  63. printf("%s %s \n",username,password);
  64. int i;
  65. for(i=0;i<usercnt;i++) {
  66. if(strcmp(username,uservect[i].username) == 0) {
  67. if(strcmp(password,uservect[i].password) == 0 ) {
  68. if(uservect[i].status == 0) {
  69. uservect[i].status = 1;
  70. return logcount();
  71. }
  72. return -2;
  73. }
  74. return -1;
  75. }
  76.  
  77. }
  78. return -1;
  79. }
  80.  
  81.  
  82. void logout(char* username) {
  83. int i;
  84. for(i=0;i<usercnt;i++) {
  85. if(strcmp(username,uservect[i].username) == 0) {
  86. uservect[i].status = 0;
  87. }
  88. }
  89. }
  90.  
  91.  
  92.  
  93. void serve(thrarg* arg) {
  94. pthread_mutex_lock(&mtx);
  95. thrcount++;
  96. pthread_mutex_unlock(&mtx);
  97.  
  98. char buf[1024];
  99. int k = recv(arg->sock,buf,1024,0);
  100.  
  101. if(k<=0) {
  102. perror("Could not receive");
  103. }
  104.  
  105.  
  106. buf[k] = 0;
  107.  
  108. char username[1024];
  109. strcpy(username,buf);
  110.  
  111. k = recv(arg->sock,buf,1024,0);
  112. if(k<=0) {
  113. perror("Could not receive password");
  114. }
  115.  
  116. buf[k] = 0;
  117.  
  118. char password[1024];
  119. strcpy(password,buf);
  120.  
  121. pthread_mutex_lock(&mtx);
  122. printf("Username:%s\nPassword:%s\n",username,password);
  123. pthread_mutex_unlock(&mtx);
  124.  
  125. int resp = response(username,password);
  126.  
  127. pthread_mutex_lock(&mtx);
  128. printf("%d\n",resp);
  129. pthread_mutex_unlock(&mtx);
  130.  
  131. resp = htonl(resp);
  132. k = send(arg->sock,&resp,sizeof(int),0);
  133. if(k<=0) {
  134. perror("Could not send");
  135. }
  136.  
  137. pthread_mutex_lock(&mtx);
  138. if(resp>0) {
  139. //logout(username);
  140. }
  141.  
  142. thrcount--;
  143. free(thr[arg->thrindex]);
  144. thr[arg->thrindex] = NULL;
  145. free(arg);
  146. pthread_mutex_unlock(&mtx);
  147. }
  148.  
  149.  
  150. void handler() {
  151. if(close(rs)<0) {
  152. perror("Could not close socket");
  153. }
  154.  
  155. keepAlive = 0;
  156.  
  157. int i;
  158. for(i=0;i<MAXCON;i++) {
  159. pthread_t* p = thr[i];
  160. if(p) {
  161. pthread_join(*p,NULL);
  162. }
  163. }
  164. return;
  165. }
  166.  
  167.  
  168. void readUsers() {
  169. FILE* fd = fopen("users.txt","r");
  170. if(fd==0) {
  171. printf("Could not open file!\n");
  172. exit(3);
  173. }
  174.  
  175. int i=0;
  176. while( !feof(fd) ) {
  177. fscanf(fd,"%s %s",uservect[i].username, uservect[i].password);
  178. //printf("%s %s\n",uservect[i].username,uservect[i].password);
  179. i++;
  180. }
  181. usercnt = i;
  182. }
  183.  
  184.  
  185. int main() {
  186. readUsers();
  187.  
  188. int rs = socket(AF_INET,SOCK_STREAM,0);
  189. if(rs < 0) {
  190. perror("Could not create rendezvous socket!");
  191. }
  192.  
  193. struct sockaddr_in addr,caddr;
  194. addr.sin_family = AF_INET;
  195. addr.sin_port = htons(1754);
  196. addr.sin_addr.s_addr = INADDR_ANY;
  197.  
  198. if(bind(rs,(struct sockaddr*) &addr,sizeof(addr))<0) {
  199. perror("Could not bind server");
  200. }
  201.  
  202. if(listen(rs,5) < 0 ) {
  203. perror("Could not listen");
  204. }
  205.  
  206. signal(SIGINT,handler);
  207.  
  208. int sock;
  209. int len = sizeof(caddr);
  210. while(keepAlive) {
  211. sock = accept(rs,(struct sockaddr*) &caddr,&len);
  212.  
  213. if((sock<0)&&keepAlive) {
  214. perror("Could not accept connection!");
  215. }
  216.  
  217. int k = findFreeThread();
  218. if(k<0) {
  219. close(sock);
  220. continue;
  221. }
  222.  
  223. if(len&&keepAlive) {
  224. printf("Incomming connection from %s on port %d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
  225. }
  226.  
  227. thrarg *arg = malloc(sizeof(thrarg));
  228. arg->sock = sock;
  229. arg->thrindex = k;
  230. arg->caddr = caddr;
  231. arg->caddr_len = len;
  232.  
  233. thr[k] = malloc(sizeof(pthread_t));
  234. pthread_create(thr[k],NULL,(void*) serve,arg);
  235.  
  236.  
  237. }
  238.  
  239. //join remaining threads
  240. /*
  241. int i;
  242. for(i=0;i<MAXCON;i++) {
  243. pthread_t* p = thr[i];
  244. if(p) {
  245. pthread_join(*p,NULL);
  246. }
  247. }*/
  248.  
  249. return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement