socket.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2022 Nikos Mavrogiannopoulos
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netdb.h>
  25. #include <arpa/inet.h>
  26. #include <netinet/in.h>
  27. #include <sys/select.h>
  28. #include <errno.h>
  29. #include <sys/wait.h>
  30. #include <signal.h>
  31. #include "socket.h"
  32. #define MAX(x,y) ((x)>(y)?(x):(y))
  33. typedef struct socket_list {
  34. int s;
  35. int family;
  36. struct sockaddr addr;
  37. struct socket_list *next;
  38. } socket_list;
  39. static void free_socket_list(socket_list *slist)
  40. {
  41. socket_list *ptr, *oldptr;
  42. for (ptr = slist; ptr != NULL;) {
  43. if (ptr->s >= 0)
  44. close(ptr->s);
  45. oldptr = ptr;
  46. ptr = ptr->next;
  47. free(oldptr);
  48. }
  49. }
  50. static int listen_port(socket_list **slist, int port)
  51. {
  52. struct addrinfo hints, *res, *ptr;
  53. int y, r, s;
  54. char portname[6], strip[64];
  55. socket_list *lm;
  56. snprintf(portname, sizeof(portname), "%d", port);
  57. memset(&hints, 0, sizeof(hints));
  58. hints.ai_socktype = SOCK_STREAM;
  59. hints.ai_flags = AI_PASSIVE;
  60. *slist = NULL;
  61. /* listen to all available (IPv4 and IPv6) address */
  62. if ((r = getaddrinfo(NULL, portname, &hints, &res)) != 0) {
  63. fprintf(stderr, "getaddrinfo() failed: %s\n", gai_strerror(r));
  64. return -1;
  65. }
  66. for (ptr = res; ptr != NULL; ptr = ptr->ai_next) {
  67. s = socket(ptr->ai_family, SOCK_STREAM, 0);
  68. if (s < 0) {
  69. perror("socket() failed");
  70. continue;
  71. }
  72. if (ptr->ai_family == AF_INET)
  73. fprintf(stderr, "Listening on %s:%d\n", inet_ntop(ptr->ai_family,
  74. &((struct sockaddr_in*)ptr->ai_addr)->sin_addr, strip,
  75. sizeof(strip)), port);
  76. else if (ptr->ai_family == AF_INET6)
  77. fprintf(stderr, "Listening on [%s]:%d\n", inet_ntop(ptr->ai_family,
  78. &((struct sockaddr_in6*)ptr->ai_addr)->sin6_addr, strip,
  79. sizeof(strip)), port);
  80. #if defined(IPV6_V6ONLY)
  81. if (ptr->ai_family == AF_INET6) {
  82. y = 1;
  83. /* avoid listen on ipv6 addresses failing
  84. * because already listening on ipv4 addresses: */
  85. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
  86. (const void *) &y, sizeof(y)) < 0) {
  87. perror("setsockopt(IPV6_V6ONLY) failed");
  88. }
  89. }
  90. #endif
  91. y = 1;
  92. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  93. (const void *) &y, sizeof(y)) < 0) {
  94. perror("setsockopt(SO_REUSEADDR) failed");
  95. }
  96. if (bind(s, ptr->ai_addr, ptr->ai_addrlen) < 0) {
  97. perror("bind() failed");
  98. close(s);
  99. continue;
  100. }
  101. if (listen(s, 1024) < 0) {
  102. perror("listen() failed");
  103. close(s);
  104. r = -1;
  105. goto cleanup;
  106. }
  107. lm = calloc(1, sizeof(socket_list));
  108. if (lm == NULL) {
  109. close(s);
  110. r = -1;
  111. goto cleanup;
  112. }
  113. lm->s = s;
  114. lm->family = ptr->ai_family;
  115. memcpy(&lm->addr, ptr->ai_addr, sizeof(*ptr->ai_addr));
  116. lm->next = *slist;
  117. *slist = lm;
  118. }
  119. if (*slist == NULL)
  120. r = -1;
  121. else
  122. r = 0;
  123. cleanup:
  124. freeaddrinfo(res);
  125. fflush(stderr);
  126. return r;
  127. }
  128. static void spawn_process(int fd, const char *jwkdir,
  129. process_request_func pfunc,
  130. socket_list *slist)
  131. {
  132. pid_t pid;
  133. socket_list *ptr;
  134. pid = fork();
  135. if (pid == 0) { /* child */
  136. for (ptr = slist; ptr != NULL; ptr = ptr->next) {
  137. close(ptr->s);
  138. }
  139. /* Ensure that both stdout and stdin are set */
  140. if (dup2(fd, STDOUT_FILENO) < 0) {
  141. perror("dup2");
  142. close(fd);
  143. return;
  144. }
  145. close(fd);
  146. pfunc(jwkdir, STDOUT_FILENO);
  147. free_socket_list(slist);
  148. exit(0);
  149. } else if (pid == -1) {
  150. perror("fork failed");
  151. }
  152. close(fd);
  153. }
  154. static void handle_child(int sig)
  155. {
  156. pid_t pid;
  157. int status;
  158. while ((pid = waitpid(-1, &status, WNOHANG)) > 0);
  159. }
  160. int run_service(const char *jwkdir, int port, process_request_func pfunc)
  161. {
  162. socket_list *slist, *ptr;
  163. int r, n = 0, accept_fd;
  164. fd_set read_fds;
  165. struct timeval tv;
  166. struct sigaction new_action;
  167. /* Set up the structure to specify the new action. */
  168. new_action.sa_handler = handle_child;
  169. sigemptyset (&new_action.sa_mask);
  170. new_action.sa_flags = 0;
  171. sigaction(SIGCHLD, &new_action, NULL);
  172. r = listen_port(&slist, port);
  173. if (r < 0) {
  174. fprintf(stderr, "Could not listen port (%d)\n", port);
  175. return -1;
  176. }
  177. while (1) {
  178. FD_ZERO(&read_fds);
  179. for (ptr = slist; ptr != NULL; ptr = ptr->next) {
  180. if (ptr->s > FD_SETSIZE) {
  181. fprintf(stderr, "exceeded FD_SETSIZE\n");
  182. free_socket_list(slist);
  183. return -1;
  184. }
  185. FD_SET(ptr->s, &read_fds);
  186. n = MAX(n, ptr->s);
  187. }
  188. tv.tv_sec = 1200;
  189. tv.tv_usec = 0;
  190. n = select(n+1, &read_fds, NULL, NULL, &tv);
  191. if (n == -1 && errno == EINTR)
  192. continue;
  193. if (n < 0) {
  194. perror("select");
  195. free_socket_list(slist);
  196. return -1;
  197. }
  198. for (ptr = slist; ptr != NULL; ptr = ptr->next) {
  199. if (FD_ISSET(ptr->s, &read_fds)) {
  200. accept_fd = accept(ptr->s, NULL, 0);
  201. if (accept_fd < 0) {
  202. perror("accept");
  203. continue;
  204. }
  205. spawn_process(accept_fd, jwkdir, pfunc, slist);
  206. }
  207. }
  208. }
  209. return 0;
  210. }