server.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include "users.h"
  10. #include "functions.h"
  11. #define BUFLEN 256
  12. int main(int argc, char** argv)
  13. {
  14. FILE* input;
  15. int n, j, i, fdm, it_socket, jt_socket, numBytesRecived, numBytesSend;
  16. int tmp_ncard, tmp_pin, *block, *attemts, *session;
  17. double tmp_sum;
  18. int serverSocketTCP, clientSocketTCP, find;
  19. char buffer[BUFLEN], command[10];
  20. socklen_t sizeStructSocket;
  21. struct sockaddr_in serverSocketAddrTCP, clientSocketAddrTCP;
  22. fd_set read_fd, tmp_fd;
  23. struct User* database;
  24. if (argc != 3)
  25. serverUsage();
  26. input = fopen(argv[2], "r");
  27. fscanf(input, "%d", &n);
  28. block = (int*)malloc(n * sizeof(int));
  29. attemts = (int*)malloc(n * sizeof(int));
  30. session = (int*)malloc(n * sizeof(int));
  31. database = (struct User*)malloc(n * sizeof(struct User));
  32. for (i = 0; i < n; i++) {
  33. session[i] = 0;
  34. attemts[i] = 0;
  35. block[i] = 0;
  36. fscanf(input, "%s %s %d %d %s %le", database[i].nume, database[i].prenume,
  37. &database[i].ncard, &database[i].pin, database[i].parola, &database[i].sold);
  38. }
  39. FD_ZERO(&read_fd);
  40. FD_ZERO(&tmp_fd);
  41. //Deschidere socket TCP.
  42. serverSocketTCP = socket(PF_INET, SOCK_STREAM, 0);
  43. //Verificare deschidere socket.
  44. if (serverSocketTCP == -1) {
  45. perror("Socket error\n");
  46. return -1;
  47. }
  48. memset((char*)&serverSocketAddrTCP, 0, sizeof(serverSocketAddrTCP));
  49. //Configurare socket.
  50. serverSocketAddrTCP.sin_family = AF_INET;
  51. serverSocketAddrTCP.sin_port = htons(atoi(argv[1]));
  52. //Setare ip socket.
  53. inet_aton("127.0.0.1", &serverSocketAddrTCP.sin_addr);
  54. //Asociere socket <-> port.
  55. if ((bind(serverSocketTCP, (struct sockaddr*)&serverSocketAddrTCP, sizeof(struct sockaddr))) ==
  56. -1) {
  57. perror("Bind TCP error.\n");
  58. return -1;
  59. }
  60. //Setare socket pentru conectarea clientilor, maxim n clienti.
  61. if ((listen(serverSocketTCP, n)) == -1) {
  62. perror("Listen TCP error.\n");
  63. return -1;
  64. }
  65. //Adaugare socket raspunzator de conectarea clientilor in multimea descriptorilor de citire.
  66. FD_SET(serverSocketTCP, &read_fd);
  67. //Adaugare stdin pentru comanda "quit" in multimea descriptorilor de citire.
  68. FD_SET(0, &read_fd);
  69. fdm = serverSocketTCP;
  70. while (1) {
  71. tmp_fd = read_fd;
  72. if (select(fdm + 1, &tmp_fd, NULL, NULL, NULL) == -1) {
  73. perror("Select error.\n");
  74. }
  75. for (it_socket = 0; it_socket <= fdm; it_socket++) {
  76. if (FD_ISSET(it_socket, &tmp_fd)) {
  77. if (it_socket == serverSocketTCP) {
  78. //Conexiune noua.
  79. clientSocketTCP = accept(
  80. serverSocketTCP, (struct sockaddr*)&clientSocketAddrTCP, &sizeStructSocket);
  81. if (clientSocketTCP == -1) {
  82. perror("Accept error.\n");
  83. } else {
  84. //Adaugare socket client nou in multimea descriptorilor de citire.
  85. FD_SET(clientSocketTCP, &read_fd);
  86. if (clientSocketTCP > fdm)
  87. fdm = clientSocketTCP;
  88. }
  89. } else {
  90. if (it_socket == 0) {
  91. //Comanda de la server.
  92. scanf("%s", buffer);
  93. if (strcmp(buffer, "quit") == 0) {
  94. free(block);
  95. free(attemts);
  96. free(session);
  97. free(database);
  98. for (jt_socket = 0; jt_socket <= fdm; jt_socket++) {
  99. if (jt_socket != 0 && jt_socket != serverSocketTCP &&
  100. FD_ISSET(jt_socket, &read_fd)) {
  101. send(jt_socket, buffer, 0, 0);
  102. }
  103. }
  104. FD_ZERO(&read_fd);
  105. FD_ZERO(&tmp_fd);
  106. return 0;
  107. }
  108. } else {
  109. //Primire mesaj din partea unui client.
  110. memset(buffer, 0, BUFLEN);
  111. numBytesRecived = recv(it_socket, buffer, sizeof(buffer), 0);
  112. if (numBytesRecived <= 0) {
  113. if (numBytesRecived < 0) {
  114. perror("Error reciving message.\n");
  115. }
  116. close(it_socket);
  117. FD_CLR(it_socket, &read_fd);
  118. } else {
  119. sscanf(buffer, "%s", &command);
  120. if (strcmp(command, "login") == 0) {
  121. find = 0;
  122. sscanf(buffer, "%s %d %d", &command, &tmp_ncard, &tmp_pin);
  123. for (i = 0; i < n; i++) {
  124. if (database[i].ncard == tmp_ncard) {
  125. find = 1;
  126. //Am gasit numarul de card pe care se incearca autentificarea.
  127. if (session[i] != 0) {
  128. //Sesiune deja deschisa.
  129. sprintf(buffer, "-2 : Sesiune deja deschisa \n");
  130. send(it_socket, buffer, strlen(buffer) + 1, 0);
  131. break;
  132. } else {
  133. //Sesiunea pentru cardul 'i' nu este deschisa.
  134. //Verific daca cardul este blocat.
  135. if (block[i] == 1) {
  136. sprintf(buffer, "-5 : Card blocat \n");
  137. send(it_socket, buffer, strlen(buffer) + 1, 0);
  138. break;
  139. } else {
  140. //Cardul nu este blocat, verific corectitudinea pin-ului.
  141. if (database[i].pin != tmp_pin) {
  142. //Daca este incorect
  143. if (attemts[i] == 2) {
  144. //Blochez cardul.
  145. attemts[i] = 3;
  146. block[i] = 1;
  147. sprintf(buffer, "-5 : Card blocat \n");
  148. send(it_socket, buffer, strlen(buffer) + 1,
  149. 0);
  150. break;
  151. } else {
  152. attemts[i]++;
  153. sprintf(buffer, "-3 : Pin gresit \n");
  154. send(it_socket, buffer, strlen(buffer) + 1,
  155. 0);
  156. break;
  157. }
  158. } else {
  159. //Pin corect.
  160. session[i] = it_socket;
  161. sprintf(buffer, "Welcome %s %s \n",
  162. database[i].nume, database[i].prenume,
  163. i);
  164. send(it_socket, buffer, strlen(buffer) + 1, 0);
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. if (find == 0) {
  172. sprintf(buffer, "-4 : Numar card inexistent\n");
  173. send(it_socket, buffer, strlen(buffer) + 1, 0);
  174. break;
  175. }
  176. }
  177. if (strcmp(command, "logout") == 0) {
  178. for (i = 0; i < n; i++) {
  179. if (session[i] == it_socket) {
  180. //Facem deconectarea.
  181. session[i] = 0;
  182. sprintf(buffer, "Clientul a fost deconectat \n");
  183. send(it_socket, buffer, strlen(buffer) + 1, 0);
  184. break;
  185. }
  186. }
  187. }
  188. if (strcmp(command, "quit") == 0) {
  189. for (i = 0; i < n; i++) {
  190. if (session[i] == it_socket) {
  191. session[i] = 0;
  192. FD_CLR(it_socket, &read_fd);
  193. }
  194. }
  195. }
  196. if (strcmp(command, "listsold") == 0) {
  197. for (i = 0; i < n; i++) {
  198. if (session[i] == it_socket) {
  199. sprintf(buffer, "%.2f \n", database[i].sold, i,
  200. database[i].nume);
  201. send(it_socket, buffer, strlen(buffer) + 1, 0);
  202. break;
  203. }
  204. }
  205. }
  206. if (strcmp(command, "transfer") == 0) {
  207. find = 0;
  208. sscanf(buffer, "%s %d %lf", command, &tmp_ncard, &tmp_sum);
  209. for (i = 0; i < n; i++) {
  210. if (database[i].ncard == tmp_ncard) {
  211. find = 1;
  212. //Daca exista numarul de card introdus
  213. //verific daca am destui bani.
  214. for (j = 0; j < n; j++) {
  215. if (session[j] == it_socket) {
  216. if (database[j].sold >= tmp_sum) {
  217. //Daca am destui bani in cont, trimit cerere confirmare.
  218. sprintf(buffer,
  219. "Transfer %.2lf catre %s %s ? [y/n] \n",
  220. tmp_sum, database[i].nume,
  221. database[i].prenume);
  222. send(it_socket, buffer, strlen(buffer) + 1, 0);
  223. break;
  224. } else {
  225. sprintf(buffer, "-8 : Fonduri insuficiente \n");
  226. send(it_socket, buffer, strlen(buffer) + 1, 0);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. if (find == 0) {
  234. sprintf(buffer, "-4 : Numar card inexistent \n");
  235. send(it_socket, buffer, strlen(buffer) + 1, 0);
  236. break;
  237. }
  238. }
  239. //
  240. if (strcmp(command, "transfer2") == 0) {
  241. sscanf(buffer, "%s %d %lf", command, &tmp_ncard, &tmp_sum);
  242. if (tmp_ncard != 0 && tmp_sum != 0) {
  243. for (i = 0; i < n; i++) {
  244. if (database[i].ncard == tmp_ncard) {
  245. for (j = 0; j < n; j++) {
  246. if (session[j] == it_socket) {
  247. //j sursa, i destinatia
  248. database[i].sold += tmp_sum;
  249. database[j].sold -= tmp_sum;
  250. sprintf(buffer,
  251. "Transfer realizat cu succes \n");
  252. send(it_socket, buffer, strlen(buffer) + 1, 0);
  253. break;
  254. }
  255. }
  256. }
  257. }
  258. } else {
  259. sprintf(buffer, "-9 : Operatie anulata \n");
  260. send(it_socket, buffer, strlen(buffer) + 1, 0);
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. return 0;
  271. }