server.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/select.h>
  8. #include <signal.h>
  9. #include <stdbool.h>
  10. #include <errno.h>
  11. #include "common.h"
  12. #include "network.h"
  13. char FIELD[(WIDTH+1)*HEIGHT];
  14. volatile sig_atomic_t running;
  15. void int_handler(int s) {
  16. (void)s;
  17. running = false;
  18. }
  19. void put_char_net(int y, int x, char ch) {
  20. FIELD[y*(WIDTH+1)+x] = ch;
  21. }
  22. int main(int argc, char *argv[]) {
  23. (void) argc;
  24. (void) argv;
  25. int sock_listener;
  26. struct addrinfo hints, *result, *rp;
  27. int s;
  28. char hoststr[NI_MAXHOST], servstr[NI_MAXSERV];
  29. memset(&hints, 0, sizeof(hints));
  30. hints.ai_family = AF_INET6;
  31. hints.ai_socktype = SOCK_STREAM;
  32. hints.ai_flags = AI_PASSIVE;
  33. if ((s = getaddrinfo(NULL, PORT, &hints, &result)) != 0) {
  34. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  35. exit(EXIT_FAILURE);
  36. }
  37. for (rp = result; rp != NULL; rp = rp->ai_next) {
  38. if ((sock_listener = socket(rp->ai_family, rp->ai_socktype,
  39. rp->ai_protocol)) == -1) {
  40. perror("socket");
  41. continue;
  42. }
  43. int yes = 1;
  44. int no = 0;
  45. if (setsockopt(sock_listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
  46. perror("setsockopt(SO_REUSEADDR)");
  47. // ignore
  48. }
  49. if (setsockopt(sock_listener, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(int)) == -1) {
  50. perror("setsockopt(IPV6_V6ONLY)");
  51. // ignore
  52. }
  53. if (bind(sock_listener, rp->ai_addr, rp->ai_addrlen) == -1) {
  54. close(sock_listener);
  55. perror("bind");
  56. continue;
  57. }
  58. break;
  59. }
  60. if (rp == NULL) {
  61. exit(EXIT_FAILURE);
  62. }
  63. freeaddrinfo(result);
  64. if (listen(sock_listener, 10) == -1) {
  65. perror("listen");
  66. exit(EXIT_FAILURE);
  67. }
  68. puts("Listening for connections.");
  69. memset(FIELD, ' ', (WIDTH+1)*HEIGHT);
  70. for (int i = 0; i < HEIGHT; ++i) {
  71. FIELD[i*(WIDTH+1)+WIDTH] = '\0';
  72. }
  73. fd_set fds_master, fds_select;
  74. FD_ZERO(&fds_master);
  75. FD_ZERO(&fds_select);
  76. FD_SET(sock_listener, &fds_master);
  77. int maxfd = sock_listener;
  78. running = true;
  79. struct sigaction act;
  80. act.sa_handler = int_handler;
  81. sigaction(SIGINT, &act, NULL);
  82. if (sigaction(SIGINT, &act, NULL) == -1) {
  83. perror("sigaction");
  84. }
  85. while (running) {
  86. fds_select = fds_master;
  87. if (select(maxfd+1, &fds_select, NULL, NULL, NULL) == -1) {
  88. if (errno == EINTR) {
  89. continue;
  90. }
  91. perror("select");
  92. exit(EXIT_FAILURE);
  93. }
  94. for (int fd = 0; fd <= maxfd; fd++) {
  95. if (!FD_ISSET(fd, &fds_select))
  96. continue;
  97. if (fd == sock_listener) {
  98. // new client
  99. struct sockaddr_storage remoteaddr;
  100. socklen_t addrlen = sizeof remoteaddr;
  101. int newfd = accept(sock_listener, (struct sockaddr *)&remoteaddr, &addrlen);
  102. if (newfd == -1) {
  103. perror("accept");
  104. } else {
  105. FD_SET(newfd, &fds_master);
  106. if (newfd > maxfd) maxfd = newfd;
  107. if (getnameinfo((struct sockaddr *)&remoteaddr, addrlen, hoststr, sizeof(hoststr),
  108. servstr, sizeof(servstr), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
  109. printf("Accepted connection from %s:%s as fd %d.\n", hoststr, servstr, newfd);
  110. } else {
  111. printf("Accepted connection at fd %d.\n", newfd);
  112. }
  113. }
  114. } else {
  115. // existing client
  116. uint8_t buf[PKT_SIZE_MAX];
  117. size_t len = sizeof buf;
  118. int type;
  119. if ((type = recv_packet(fd, buf, &len)) <= 0) {
  120. if (type == 0) {
  121. printf("Connection closed by fd %d.\n", fd);
  122. } else {
  123. perror("recv_packet");
  124. }
  125. close(fd);
  126. FD_CLR(fd, &fds_master);
  127. } else {
  128. if (type == PKT_TYPE_HELLO) {
  129. struct packet_init_t pkt_init = {
  130. .size = htons(sizeof(struct packet_init_t)),
  131. .type = PKT_TYPE_INIT
  132. };
  133. memcpy(&pkt_init.data, FIELD, sizeof(pkt_init.data));
  134. printf("sending init\n");
  135. size_t pkt_len = sizeof(struct packet_init_t);
  136. if (send_packet(fd, (uint8_t*)&pkt_init, pkt_len) < pkt_len) {
  137. perror("send_packet");
  138. }
  139. } else if (type == PKT_TYPE_UPDATE) {
  140. struct packet_update_t *pkt = (struct packet_update_t *)buf;
  141. if (pkt->x >= WIDTH || pkt->y >= HEIGHT) {
  142. printf("recvd oob update from fd %d, ignoring.\n", fd);
  143. continue;
  144. }
  145. put_char_net(pkt->y, pkt->x, pkt->ch);
  146. for (int j = 0; j <= maxfd; j++) {
  147. if (j == sock_listener || j == fd) {
  148. continue;
  149. }
  150. if (FD_ISSET(j, &fds_master)) {
  151. if (send_packet(j, buf, len) < len) {
  152. perror("send_packet");
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. puts("Shutting down");
  162. if (close(sock_listener) == -1) {
  163. perror("close");
  164. }
  165. FD_CLR(sock_listener, &fds_master);
  166. int scount = 0;
  167. for (int fd = 0; fd <= maxfd; fd++) {
  168. if (!FD_ISSET(fd, &fds_master))
  169. continue;
  170. scount++;
  171. if (shutdown(fd, SHUT_WR) == -1) {
  172. perror("shutdown");
  173. }
  174. }
  175. while(scount) {
  176. scount = 0;
  177. fds_select = fds_master;
  178. if (select(maxfd+1, &fds_select, NULL, NULL, NULL) == -1) {
  179. perror("select");
  180. exit(EXIT_FAILURE);
  181. }
  182. for (int fd = 0; fd <= maxfd; fd++) {
  183. if (!FD_ISSET(fd, &fds_select))
  184. continue;
  185. int s = recv_dump(fd);
  186. if (s <= 0) {
  187. if (s != 0) {
  188. perror("recv_dump");
  189. }
  190. close(fd);
  191. FD_CLR(fd, &fds_master);
  192. } else {
  193. scount++;
  194. }
  195. }
  196. }
  197. return EXIT_SUCCESS;
  198. }