fd.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- file descriptor manipulation (Unix) */
  4. /* The worker can requests the control thread to send back a passive socket.
  5. Alternatively, the worker can allocate it by itself if it has appropriate
  6. priviles or if it is a control thread itself. This code is for the
  7. latter case.
  8. Support:
  9. IPv6, IPv4: TCP, UDP
  10. Unix: /
  11. */
  12. #ifndef _sHT_SOCKET_H
  13. #define _sHT_SOCKET_H
  14. #include <fcntl.h>
  15. #include <netinet/in.h>
  16. #include <stdint.h>
  17. #include <sys/socket.h>
  18. /* TODO This usage of O_NONBLOCK is Linux-specific. */
  19. enum sHT_sock_type
  20. {
  21. sHT_SOCK_STREAM = SOCK_STREAM | O_NONBLOCK,
  22. sHT_SOCK_STREAM_CLOEXEC = SOCK_STREAM | O_NONBLOCK | O_CLOEXEC,
  23. sHT_SOCK_DGRAM = SOCK_DGRAM | O_NONBLOCK,
  24. sHT_SOCK_DGRAM_CLOEXEC = SOCK_DGRAM | O_NONBLOCK | O_CLOEXEC,
  25. };
  26. /* IPv4 is a subset of IPv6 (via V4-mapped addresses), sort of. */
  27. struct sHT_passive_in6
  28. {
  29. struct sockaddr_in6 addr;
  30. enum sHT_sock_type type;
  31. int backlog;
  32. /* Autocorking is not enabled by default */
  33. /* TODO: allow TCP_DEFER_ACCEPT (OK for HTTP, TLS?) */
  34. };
  35. /* Close a file descriptor. */
  36. void
  37. sHT_close(int fd);
  38. /* Return a fresh IPv6 listening socket, binded to a particular address.
  39. It is O_NONBLOCK. Whether it is O_CLOEXEC depends upon ->type.
  40. The network interface does not have to be up (IP_FREEBIND).
  41. Ports may be reused (SO_REUSEPORT).
  42. TODO: binding to a particular interface (SO_BINDTODEVICE).
  43. In case of an error, return a negative errno value. */
  44. __attribute__((warn_unused_result))
  45. __attribute__((nonnull (1)))
  46. int
  47. sHT_passive_ipv6(const struct sHT_passive_in6 *in);
  48. #endif