sockets.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2023 Joan Lledó <jlledom@member.fsf.org>
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  18. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  20. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  24. * OF SUCH DAMAGE.
  25. */
  26. #ifndef HURD_LWIP_POSIX_SOCKET_H
  27. #define HURD_LWIP_POSIX_SOCKET_H
  28. #include <sys/socket.h>
  29. #include <poll.h>
  30. #include <errno.h>
  31. #include LWIP_SOCKET_EXTERNAL_HEADER_INET_H
  32. typedef size_t msg_iovlen_t;
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #ifdef _HAVE_SA_LEN
  37. #define HAVE_SA_LEN _HAVE_SA_LEN
  38. #else
  39. #define HAVE_SA_LEN 0
  40. #endif /* _HAVE_SA_LEN */
  41. /* Address length safe read and write */
  42. #if HAVE_SA_LEN
  43. #define IP4ADDR_SOCKADDR_SET_LEN(sin) \
  44. (sin)->sin_len = sizeof(struct sockaddr_in)
  45. #define IP6ADDR_SOCKADDR_SET_LEN(sin6) \
  46. (sin6)->sin6_len = sizeof(struct sockaddr_in6)
  47. #define IPADDR_SOCKADDR_GET_LEN(addr) \
  48. (addr)->sa.sa_len
  49. #else
  50. #define IP4ADDR_SOCKADDR_SET_LEN(addr)
  51. #define IP6ADDR_SOCKADDR_SET_LEN(addr)
  52. #define IPADDR_SOCKADDR_GET_LEN(addr) \
  53. ((addr)->sa.sa_family == AF_INET ? sizeof(struct sockaddr_in) \
  54. : ((addr)->sa.sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0))
  55. #endif /* HAVE_SA_LEN */
  56. #define SIN_ZERO_LEN sizeof (struct sockaddr) - \
  57. __SOCKADDR_COMMON_SIZE - \
  58. sizeof (in_port_t) - \
  59. sizeof (struct in_addr)
  60. #if !defined IOV_MAX
  61. #define IOV_MAX 0xFFFF
  62. #elif IOV_MAX > 0xFFFF
  63. #error "IOV_MAX larger than supported by LwIP"
  64. #endif /* IOV_MAX */
  65. #define LWIP_SELECT_MAXNFDS (FD_SETSIZE + LWIP_SOCKET_OFFSET)
  66. #if LWIP_UDP && LWIP_UDPLITE
  67. /*
  68. * Options for level IPPROTO_UDPLITE
  69. */
  70. #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
  71. #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
  72. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  73. #if 0
  74. void lwip_socket_thread_init(void); /* LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore */
  75. void lwip_socket_thread_cleanup(void); /* LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore */
  76. int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  77. int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
  78. int lwip_shutdown(int s, int how);
  79. int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
  80. int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
  81. int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
  82. int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
  83. int lwip_close(int s);
  84. int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
  85. int lwip_listen(int s, int backlog);
  86. ssize_t lwip_recv(int s, void *mem, size_t len, int flags);
  87. ssize_t lwip_read(int s, void *mem, size_t len);
  88. ssize_t lwip_readv(int s, const struct iovec *iov, int iovcnt);
  89. ssize_t lwip_recvfrom(int s, void *mem, size_t len, int flags,
  90. struct sockaddr *from, socklen_t *fromlen);
  91. ssize_t lwip_recvmsg(int s, struct msghdr *message, int flags);
  92. ssize_t lwip_send(int s, const void *dataptr, size_t size, int flags);
  93. ssize_t lwip_sendmsg(int s, const struct msghdr *message, int flags);
  94. ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags,
  95. const struct sockaddr *to, socklen_t tolen);
  96. int lwip_socket(int domain, int type, int protocol);
  97. ssize_t lwip_write(int s, const void *dataptr, size_t size);
  98. ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt);
  99. #if LWIP_SOCKET_SELECT
  100. int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  101. struct timeval *timeout);
  102. #endif
  103. #if LWIP_SOCKET_POLL
  104. int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
  105. #endif
  106. int lwip_ioctl(int s, long cmd, void *argp);
  107. int lwip_fcntl(int s, int cmd, int val);
  108. const char *lwip_inet_ntop(int af, const void *src, char *dst, socklen_t size);
  109. int lwip_inet_pton(int af, const char *src, void *dst);
  110. #endif
  111. /* Unsupported identifiers */
  112. #ifndef SO_NO_CHECK
  113. #define SO_NO_CHECK 0xFF
  114. #endif
  115. #ifndef SO_BINDTODEVICE
  116. #define SO_BINDTODEVICE 0xFE
  117. #endif
  118. #ifndef MSG_MORE
  119. #define MSG_MORE 0x0
  120. #endif
  121. #ifndef TCP_KEEPALIVE
  122. #define TCP_KEEPALIVE 0xFF
  123. #endif
  124. #ifndef TCP_KEEPIDLE
  125. #define TCP_KEEPIDLE 0xFE
  126. #endif
  127. #ifndef TCP_KEEPINTVL
  128. #define TCP_KEEPINTVL 0xFD
  129. #endif
  130. #ifndef TCP_KEEPCNT
  131. #define TCP_KEEPCNT 0xFC
  132. #endif
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* HURD_LWIP_POSIX_SOCKET_H */