fd.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* shttpd - interfaces for control threads to manipulate file descriptors
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* The worker can requests the control thread to send back a passive socket.
  14. Alternatively, the worker can allocate it by itself if it has appropriate
  15. priviles or if it is a control thread itself. This code is for the
  16. latter case.
  17. Support:
  18. IPv6, IPv4: TCP, UDP
  19. Unix: /
  20. */
  21. #ifndef _sHT_SOCKET_H
  22. #define _sHT_SOCKET_H
  23. #include <fcntl.h>
  24. #include <netinet/in.h>
  25. #include <stdint.h>
  26. #include <sys/socket.h>
  27. /* TODO This usage of O_NONBLOCK is Linux-specific. */
  28. enum sHT_sock_type
  29. {
  30. sHT_SOCK_STREAM = SOCK_STREAM | O_NONBLOCK,
  31. sHT_SOCK_STREAM_CLOEXEC = SOCK_STREAM | O_NONBLOCK | O_CLOEXEC,
  32. sHT_SOCK_DGRAM = SOCK_DGRAM | O_NONBLOCK,
  33. sHT_SOCK_DGRAM_CLOEXEC = SOCK_DGRAM | O_NONBLOCK | O_CLOEXEC,
  34. };
  35. /* IPv4 is a subset of IPv6 (via V4-mapped addresses), sort of. */
  36. struct sHT_passive_in6
  37. {
  38. struct sockaddr_in6 addr;
  39. enum sHT_sock_type type;
  40. int backlog;
  41. /* Autocorking is not enabled by default */
  42. /* TODO: allow TCP_DEFER_ACCEPT (OK for HTTP, TLS?) */
  43. };
  44. /* Close a file descriptor. */
  45. void
  46. sHT_close(int fd);
  47. /* Return a fresh IPv6 listening socket, binded to a particular address.
  48. It is O_NONBLOCK. Whether it is O_CLOEXEC depends upon ->type.
  49. The network interface does not have to be up (IP_FREEBIND).
  50. Ports may be reused (SO_REUSEPORT).
  51. TODO: binding to a particular interface (SO_BINDTODEVICE).
  52. In case of an error, return a negative errno value. */
  53. __attribute__((warn_unused_result))
  54. __attribute__((nonnull (1)))
  55. int
  56. sHT_passive_ipv6(const struct sHT_passive_in6 *in);
  57. #endif