Server.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "Server.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include <signal.h>
  11. #include "SocketUtils.h"
  12. void serverInitDefaults(Server* s)
  13. {
  14. s->maxClients = -1;
  15. s->port = -1;
  16. s->threads = NULL;
  17. s->clientSockets = NULL;
  18. s->clientUser = NULL;
  19. s->connectSocket = -1;
  20. s->hAmount = -1;
  21. s->hIndex = -1;
  22. s->handlers = NULL;
  23. s->directory = NULL;
  24. }
  25. int serverInit(Server* s, int maxClients, short port, char* directory)
  26. {
  27. s->maxClients = maxClients;
  28. s->port = port;
  29. s->hAmount = 0;
  30. s->hIndex = 0;
  31. s->directory = directory;
  32. if(chdir(directory) == -1)
  33. {
  34. perror("cannot change directory");
  35. serverRemove(s);
  36. return -1;
  37. }
  38. // initialize storage for clients
  39. s->threads = malloc(sizeof(pthread_t) * maxClients);
  40. s->clientSockets = malloc(sizeof(int) * maxClients);
  41. s->clientUser = malloc(sizeof(char*) * maxClients);
  42. for(int i = 0; i < maxClients; i++)
  43. {
  44. s->threads[i] = -1;
  45. s->clientSockets[i] = -1;
  46. s->clientUser[i] = NULL;
  47. }
  48. // create the socket for clients to connect
  49. s->connectSocket = socket(AF_INET, SOCK_STREAM, 0);
  50. if(s->connectSocket == -1)
  51. {
  52. perror("Cannot create socket");
  53. serverRemove(s);
  54. return -1;
  55. }
  56. // prevents clients from blocking the port if the server exits
  57. struct linger sl;
  58. sl.l_onoff = 1;
  59. sl.l_linger = 0;
  60. if(setsockopt(s->connectSocket, SOL_SOCKET, SO_LINGER, &sl, sizeof(struct linger)) == -1)
  61. {
  62. perror("Cannot set non lingering");
  63. serverRemove(s);
  64. return -1;
  65. }
  66. // specifies data of the port and binds it
  67. struct sockaddr_in connectSocketData;
  68. memset(&connectSocketData, 0, sizeof(struct sockaddr_in));
  69. connectSocketData.sin_family = AF_INET;
  70. connectSocketData.sin_addr.s_addr = INADDR_ANY;
  71. connectSocketData.sin_port = htons(port);
  72. if(bind(s->connectSocket, (struct sockaddr*) &connectSocketData, sizeof(struct sockaddr_in)) != 0)
  73. {
  74. perror("Cannot bind socket");
  75. serverRemove(s);
  76. return -1;
  77. }
  78. // mark this socket as handler for connection requests
  79. if(listen(s->connectSocket, 5) != 0)
  80. {
  81. perror("Cannot start listening");
  82. serverRemove(s);
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. void serverRemove(Server* s)
  88. {
  89. s->port = -1;
  90. if(s->threads != NULL)
  91. {
  92. for(int i = 0; i < s->maxClients; i++)
  93. {
  94. if(s->threads[i] != -1)
  95. {
  96. printf("%d thread\n", i);
  97. pthread_cancel(s->threads[i]);
  98. pthread_join(s->threads[i], NULL);
  99. }
  100. }
  101. free(s->threads);
  102. s->threads = NULL;
  103. }
  104. if(s->clientSockets != NULL)
  105. {
  106. for(int i = 0; i < s->maxClients; i++)
  107. {
  108. if(s->clientSockets[i] != -1)
  109. {
  110. if(close(s->clientSockets[i]) == -1)
  111. {
  112. printf("%d", i);
  113. perror(" cannot close client socket");
  114. }
  115. else
  116. {
  117. printf("%d closed\n", i);
  118. }
  119. }
  120. }
  121. free(s->clientSockets);
  122. s->clientSockets = NULL;
  123. }
  124. if(s->clientUser != NULL)
  125. {
  126. for(int i = 0; i < s->maxClients; i++)
  127. {
  128. if(s->clientUser[i] != NULL)
  129. {
  130. free(s->clientUser[i]);
  131. s->clientUser[i] = NULL;
  132. }
  133. }
  134. free(s->clientUser);
  135. s->clientUser = NULL;
  136. }
  137. s->maxClients = -1;
  138. if(s->connectSocket != -1)
  139. {
  140. if(close(s->connectSocket) != 0)
  141. {
  142. perror("Cannot close server socket");
  143. }
  144. else
  145. {
  146. printf("connection listener closed\n");
  147. }
  148. s->connectSocket = -1;
  149. }
  150. s->hAmount = -1;
  151. s->hIndex = -1;
  152. if(s->handlers != NULL)
  153. {
  154. free(s->handlers);
  155. s->handlers = NULL;
  156. }
  157. }
  158. static void* clientHandler(void* data)
  159. {
  160. int id = ((ThreadData*) data)->id;
  161. Server* s = ((ThreadData*) data)->server;
  162. Stream in;
  163. while(1)
  164. {
  165. streamInit(&in, 1024);
  166. int size = receiveAll(s->clientSockets[id], &in);
  167. if(size > 0)
  168. {
  169. int package;
  170. if(streamGetInt(&in, &package) == -1)
  171. {
  172. printf("Invalid package %d from %d\n", package, id);
  173. }
  174. else
  175. {
  176. if(package >= 0 && package < s->hIndex)
  177. {
  178. //printf("Received package %d from %d\n", package, id);
  179. if(s->handlers[package](id, s->clientSockets[id], &in))
  180. {
  181. break;
  182. }
  183. }
  184. else
  185. {
  186. printf("Invalid package %d from %d\n", package, id);
  187. }
  188. }
  189. }
  190. else if(size == 0)
  191. {
  192. printf("Client %d closed remote socket\n", id);
  193. streamRemove(&in);
  194. break;
  195. }
  196. else
  197. {
  198. perror("recv error");
  199. streamRemove(&in);
  200. break;
  201. }
  202. streamRemove(&in);
  203. }
  204. if(close(s->clientSockets[id]) != 0)
  205. {
  206. printf("%d", id);
  207. perror(" cannot close client socket");
  208. }
  209. else
  210. {
  211. printf("%d closed\n", id);
  212. }
  213. serverFreeUser(s, id);
  214. s->clientSockets[id] = -1;
  215. return NULL;
  216. }
  217. int serverSend(int clientSocket, Stream* out)
  218. {
  219. if(sendAll(clientSocket, out) == -1)
  220. {
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. void serverWaitForConnection(Server* s)
  226. {
  227. socklen_t addrlen = sizeof(struct sockaddr_in);
  228. while(1)
  229. {
  230. printf("Waiting for connections...\n");
  231. struct sockaddr_in clientSocketData;
  232. int clientSocket = accept(s->connectSocket, (struct sockaddr*) &clientSocketData, &addrlen);
  233. if(clientSocket >= 0)
  234. {
  235. printf("Client connected from %s:%d...\n", inet_ntoa(clientSocketData.sin_addr), (int) ntohs(clientSocketData.sin_port));
  236. int i = 0;
  237. while(1)
  238. {
  239. if(s->clientSockets[i] == -1)
  240. {
  241. if(s->threads[i] != -1)
  242. {
  243. pthread_cancel(s->threads[i]);
  244. pthread_join(s->threads[i], NULL);
  245. }
  246. ThreadData data;
  247. data.id = i;
  248. data.server = s;
  249. if(pthread_create(&s->threads[i], NULL, clientHandler, (void*) &data) != 0)
  250. {
  251. perror("Cannot create thread");
  252. Stream out;
  253. streamInit(&out, 16);
  254. streamWriteChar(&out, -1);
  255. streamWriteChars(&out, "Cannot create thread\n");
  256. serverSend(clientSocket, &out);
  257. streamRemove(&out);
  258. close(clientSocket);
  259. }
  260. else
  261. {
  262. s->clientSockets[i] = clientSocket;
  263. Stream out;
  264. streamInit(&out, 64);
  265. streamWriteChar(&out, 1);
  266. streamWriteChars(&out, "Welcome to the server, please enter one of these commands:\n");
  267. streamWriteChars(&out, "- HELP\n");
  268. streamWriteChars(&out, "- SEND\n");
  269. streamWriteChars(&out, "- LIST\n");
  270. streamWriteChars(&out, "- READ\n");
  271. streamWriteChars(&out, "- DEL\n");
  272. streamWriteChars(&out, "- QUIT\n");
  273. if(serverSend(clientSocket, &out) == -1)
  274. {
  275. perror("Cannot send welcome message");
  276. close(clientSocket);
  277. s->clientSockets[i] = -1;
  278. }
  279. streamRemove(&out);
  280. }
  281. break;
  282. }
  283. i++;
  284. if(i >= s->maxClients)
  285. {
  286. printf("max clients reached\n");
  287. Stream out;
  288. streamInit(&out, 16);
  289. streamWriteChar(&out, -1);
  290. streamWriteChars(&out, "the server is full\n");
  291. serverSend(clientSocket, &out);
  292. streamRemove(&out);
  293. close(clientSocket);
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. void serverRegisterHandler(Server* s, ServerStreamFunction f)
  301. {
  302. if(s->hAmount == s->hIndex)
  303. {
  304. if(s->hAmount == 0)
  305. {
  306. s->handlers = malloc(sizeof(ServerStreamFunction));
  307. s->hAmount = 1;
  308. }
  309. else
  310. {
  311. s->hAmount *= 2;
  312. ServerStreamFunction* functions = malloc(sizeof(ServerStreamFunction) * s->hAmount);
  313. for(int i = 0; i < s->hIndex; i++)
  314. {
  315. functions[i] = s->handlers[i];
  316. }
  317. free(s->handlers);
  318. s->handlers = functions;
  319. }
  320. }
  321. s->handlers[s->hIndex] = f;
  322. s->hIndex++;
  323. }
  324. int serverSetUser(Server* s, int clientId, char* name)
  325. {
  326. if(clientId < 0 || clientId >= s->maxClients)
  327. {
  328. return -1;
  329. }
  330. int l = strlen(name) + 1;
  331. s->clientUser[clientId] = malloc(sizeof(char) * l);
  332. strncpy(s->clientUser[clientId], name, l);
  333. return 0;
  334. }
  335. char* serverGetUser(Server* s, int clientId)
  336. {
  337. if(clientId < 0 || clientId >= s->maxClients)
  338. {
  339. return NULL;
  340. }
  341. return s->clientUser[clientId];
  342. }
  343. void serverFreeUser(Server* s, int clientId)
  344. {
  345. if(clientId < 0 || clientId >= s->maxClients)
  346. {
  347. return;
  348. }
  349. if(s->clientUser[clientId] != NULL)
  350. {
  351. free(s->clientUser[clientId]);
  352. s->clientUser[clientId] = NULL;
  353. }
  354. }