Server.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define __USE_POSIX199506
  11. #include <signal.h>
  12. #include "SocketUtils.h"
  13. void serverInitDefaults(Server* s)
  14. {
  15. s->maxClients = -1;
  16. s->port = -1;
  17. s->threads = NULL;
  18. s->clientSockets = 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. for(int i = 0; i < maxClients; i++)
  42. {
  43. s->threads[i] = -1;
  44. s->clientSockets[i] = -1;
  45. }
  46. // create the socket for clients to connect
  47. s->connectSocket = socket(AF_INET, SOCK_STREAM, 0);
  48. if(s->connectSocket == -1)
  49. {
  50. perror("Cannot create socket");
  51. serverRemove(s);
  52. return -1;
  53. }
  54. // prevents clients from blocking the port if the server exits
  55. struct linger sl;
  56. sl.l_onoff = 1;
  57. sl.l_linger = 0;
  58. if(setsockopt(s->connectSocket, SOL_SOCKET, SO_LINGER, &sl, sizeof(struct linger)) == -1)
  59. {
  60. perror("Cannot set non lingering");
  61. serverRemove(s);
  62. return -1;
  63. }
  64. // specifies data of the port and binds it
  65. struct sockaddr_in connectSocketData;
  66. memset(&connectSocketData, 0, sizeof(struct sockaddr_in));
  67. connectSocketData.sin_family = AF_INET;
  68. connectSocketData.sin_addr.s_addr = INADDR_ANY;
  69. connectSocketData.sin_port = htons(port);
  70. if(bind(s->connectSocket, (struct sockaddr*) &connectSocketData, sizeof(struct sockaddr_in)) != 0)
  71. {
  72. perror("Cannot bind socket");
  73. serverRemove(s);
  74. return -1;
  75. }
  76. // mark this socket as handler for connection requests
  77. if(listen(s->connectSocket, 5) != 0)
  78. {
  79. perror("Cannot start listening");
  80. serverRemove(s);
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. void serverRemove(Server* s)
  86. {
  87. s->port = -1;
  88. if(s->threads != NULL)
  89. {
  90. for(int i = 0; i < s->maxClients; i++)
  91. {
  92. if(s->threads[i] != -1)
  93. {
  94. printf("%d thread\n", i);
  95. pthread_cancel(s->threads[i]);
  96. pthread_join(s->threads[i], NULL);
  97. }
  98. }
  99. free(s->threads);
  100. s->threads = NULL;
  101. }
  102. if(s->clientSockets != NULL)
  103. {
  104. for(int i = 0; i < s->maxClients; i++)
  105. {
  106. if(s->clientSockets[i] != -1)
  107. {
  108. if(close(s->clientSockets[i]) == -1)
  109. {
  110. printf("%d", i);
  111. perror(" cannot close client socket");
  112. }
  113. else
  114. {
  115. printf("%d closed\n", i);
  116. }
  117. }
  118. }
  119. free(s->clientSockets);
  120. s->clientSockets = NULL;
  121. }
  122. s->maxClients = -1;
  123. if(s->connectSocket != -1)
  124. {
  125. if(close(s->connectSocket) != 0)
  126. {
  127. perror("Cannot close server socket");
  128. }
  129. else
  130. {
  131. printf("connection listener closed\n");
  132. }
  133. s->connectSocket = -1;
  134. }
  135. s->hAmount = -1;
  136. s->hIndex = -1;
  137. if(s->handlers != NULL)
  138. {
  139. free(s->handlers);
  140. s->handlers = NULL;
  141. }
  142. }
  143. static void* clientHandler(void* data)
  144. {
  145. int id = ((ThreadData*) data)->id;
  146. Server* s = ((ThreadData*) data)->server;
  147. Stream in;
  148. while(1)
  149. {
  150. streamInit(&in, 1024);
  151. int size = receiveAll(s->clientSockets[id], &in);
  152. if(size > 0)
  153. {
  154. int package;
  155. if(streamGetInt(&in, &package) == -1)
  156. {
  157. printf("Invalid package %d from %d\n", package, id);
  158. }
  159. else
  160. {
  161. if(package >= 0 && package < s->hIndex)
  162. {
  163. printf("Received package %d from %d\n", package, id);
  164. if(s->handlers[package](id, s->clientSockets[id], &in))
  165. {
  166. break;
  167. }
  168. }
  169. else
  170. {
  171. printf("Invalid package %d from %d\n", package, id);
  172. }
  173. }
  174. }
  175. else if(size == 0)
  176. {
  177. printf("Client %d closed remote socket\n", id);
  178. streamRemove(&in);
  179. break;
  180. }
  181. else
  182. {
  183. perror("recv error");
  184. streamRemove(&in);
  185. break;
  186. }
  187. streamRemove(&in);
  188. }
  189. if(close(s->clientSockets[id]) != 0)
  190. {
  191. printf("%d", id);
  192. perror(" cannot close client socket");
  193. }
  194. else
  195. {
  196. printf("%d closed\n", id);
  197. }
  198. s->clientSockets[id] = -1;
  199. return NULL;
  200. }
  201. int serverSend(int clientSocket, Stream* out)
  202. {
  203. if(sendAll(clientSocket, out) == -1)
  204. {
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. void serverWaitForConnection(Server* s)
  210. {
  211. socklen_t addrlen = sizeof(struct sockaddr_in);
  212. while(1)
  213. {
  214. printf("Waiting for connections...\n");
  215. struct sockaddr_in clientSocketData;
  216. int clientSocket = accept(s->connectSocket, (struct sockaddr*) &clientSocketData, &addrlen);
  217. if(clientSocket >= 0)
  218. {
  219. printf("Client connected from %s:%d...\n", inet_ntoa(clientSocketData.sin_addr), (int) ntohs(clientSocketData.sin_port));
  220. int i = 0;
  221. while(1)
  222. {
  223. if(s->clientSockets[i] == -1)
  224. {
  225. if(s->threads[i] != -1)
  226. {
  227. pthread_cancel(s->threads[i]);
  228. pthread_join(s->threads[i], NULL);
  229. }
  230. ThreadData data;
  231. data.id = i;
  232. data.server = s;
  233. if(pthread_create(&s->threads[i], NULL, clientHandler, (void*) &data) != 0)
  234. {
  235. perror("Cannot create thread");
  236. Stream out;
  237. streamInit(&out, 16);
  238. streamWriteChar(&out, -1);
  239. streamWriteChars(&out, "Cannot create thread\n");
  240. serverSend(clientSocket, &out);
  241. streamRemove(&out);
  242. close(clientSocket);
  243. }
  244. else
  245. {
  246. s->clientSockets[i] = clientSocket;
  247. Stream out;
  248. streamInit(&out, 64);
  249. streamWriteChar(&out, 1);
  250. streamWriteChars(&out, "Welcome to the server, please enter your command:\n");
  251. if(serverSend(clientSocket, &out) == -1)
  252. {
  253. perror("Cannot send welcome message");
  254. close(clientSocket);
  255. s->clientSockets[i] = -1;
  256. }
  257. streamRemove(&out);
  258. }
  259. break;
  260. }
  261. i++;
  262. if(i >= s->maxClients)
  263. {
  264. printf("max clients reached\n");
  265. Stream out;
  266. streamInit(&out, 16);
  267. streamWriteChar(&out, -1);
  268. streamWriteChars(&out, "the server is full\n");
  269. serverSend(clientSocket, &out);
  270. streamRemove(&out);
  271. close(clientSocket);
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. void serverRegisterHandler(Server* s, ServerStreamFunction f)
  279. {
  280. if(s->hAmount == s->hIndex)
  281. {
  282. if(s->hAmount == 0)
  283. {
  284. s->handlers = malloc(sizeof(ServerStreamFunction));
  285. s->hAmount = 1;
  286. }
  287. else
  288. {
  289. s->hAmount *= 2;
  290. ServerStreamFunction* functions = malloc(sizeof(ServerStreamFunction) * s->hAmount);
  291. for(int i = 0; i < s->hIndex; i++)
  292. {
  293. functions[i] = s->handlers[i];
  294. }
  295. free(s->handlers);
  296. s->handlers = functions;
  297. }
  298. }
  299. s->handlers[s->hIndex] = f;
  300. s->hIndex++;
  301. }