private-lib-plat-unix.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * Included from lib/private-lib-core.h if no explicit platform
  25. */
  26. #include <fcntl.h>
  27. #include <strings.h>
  28. #include <unistd.h>
  29. #include <netinet/in.h>
  30. #include <netinet/tcp.h>
  31. #include <arpa/inet.h>
  32. #include <poll.h>
  33. #include <netdb.h>
  34. #ifndef __cplusplus
  35. #include <errno.h>
  36. #endif
  37. #include <netdb.h>
  38. #include <signal.h>
  39. #include <sys/socket.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/time.h>
  43. #include <sys/mman.h>
  44. #include <sys/un.h>
  45. #if defined(LWS_HAVE_EVENTFD)
  46. #include <sys/eventfd.h>
  47. #endif
  48. #if defined(__APPLE__)
  49. #include <machine/endian.h>
  50. #endif
  51. #if defined(__FreeBSD__)
  52. #include <sys/endian.h>
  53. #endif
  54. #if defined(__linux__)
  55. #include <endian.h>
  56. #include <linux/if_packet.h>
  57. #include <net/if.h>
  58. #endif
  59. #if defined(__QNX__)
  60. #include <gulliver.h>
  61. #if defined(__LITTLEENDIAN__)
  62. #define BYTE_ORDER __LITTLEENDIAN__
  63. #define LITTLE_ENDIAN __LITTLEENDIAN__
  64. #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
  65. #endif
  66. #if defined(__BIGENDIAN__)
  67. #define BYTE_ORDER __BIGENDIAN__
  68. #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
  69. #define BIG_ENDIAN __BIGENDIAN__
  70. #endif
  71. #endif
  72. #if defined(LWS_HAVE_PTHREAD_H)
  73. #include <pthread.h>
  74. typedef pthread_mutex_t lws_mutex_t;
  75. #define lws_mutex_init(x) pthread_mutex_init(&(x), NULL)
  76. #define lws_mutex_destroy(x) pthread_mutex_destroy(&(x))
  77. #define lws_mutex_lock(x) pthread_mutex_lock(&(x))
  78. #define lws_mutex_unlock(x) pthread_mutex_unlock(&(x))
  79. #endif
  80. #if defined(__sun) && defined(__GNUC__)
  81. #include <arpa/nameser_compat.h>
  82. #if !defined (BYTE_ORDER)
  83. #define BYTE_ORDER __BYTE_ORDER__
  84. #endif
  85. #if !defined(LITTLE_ENDIAN)
  86. #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  87. #endif
  88. #if !defined(BIG_ENDIAN)
  89. #define BIG_ENDIAN __ORDER_BIG_ENDIAN__
  90. #endif
  91. #endif /* sun + GNUC */
  92. #if !defined(BYTE_ORDER)
  93. #define BYTE_ORDER __BYTE_ORDER
  94. #endif
  95. #if !defined(LITTLE_ENDIAN)
  96. #define LITTLE_ENDIAN __LITTLE_ENDIAN
  97. #endif
  98. #if !defined(BIG_ENDIAN)
  99. #define BIG_ENDIAN __BIG_ENDIAN
  100. #endif
  101. #if defined(LWS_BUILTIN_GETIFADDRS)
  102. #include "./misc/getifaddrs.h"
  103. #else
  104. #if defined(__HAIKU__)
  105. #define _BSD_SOURCE
  106. #endif
  107. #include <ifaddrs.h>
  108. #endif
  109. #if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__)
  110. #include <syslog.h>
  111. #if defined(__ANDROID__)
  112. #include <sys/resource.h>
  113. #endif
  114. #else
  115. #include <sys/syslog.h>
  116. #endif
  117. #ifdef __QNX__
  118. # include "netinet/tcp_var.h"
  119. # define TCP_KEEPINTVL TCPCTL_KEEPINTVL
  120. # define TCP_KEEPIDLE TCPCTL_KEEPIDLE
  121. # define TCP_KEEPCNT TCPCTL_KEEPCNT
  122. #endif
  123. #define LWS_ERRNO errno
  124. #define LWS_EAGAIN EAGAIN
  125. #define LWS_EALREADY EALREADY
  126. #define LWS_EINPROGRESS EINPROGRESS
  127. #define LWS_EINTR EINTR
  128. #define LWS_EISCONN EISCONN
  129. #define LWS_ENOTCONN ENOTCONN
  130. #define LWS_EWOULDBLOCK EWOULDBLOCK
  131. #define LWS_EADDRINUSE EADDRINUSE
  132. #define lws_set_blocking_send(wsi)
  133. #define LWS_SOCK_INVALID (-1)
  134. struct lws_context;
  135. struct lws *
  136. wsi_from_fd(const struct lws_context *context, int fd);
  137. int
  138. insert_wsi(const struct lws_context *context, struct lws *wsi);
  139. int
  140. lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
  141. uint8_t *gateway_ip);
  142. void
  143. delete_from_fd(const struct lws_context *context, int fd);
  144. #ifndef LWS_NO_FORK
  145. #ifdef LWS_HAVE_SYS_PRCTL_H
  146. #include <sys/prctl.h>
  147. #endif
  148. #endif
  149. #define compatible_close(x) close(x)
  150. #define compatible_file_close(fd) close(fd)
  151. #define lws_plat_socket_offset() (0)
  152. /*
  153. * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
  154. * but happily have something equivalent in the SO_NOSIGPIPE flag.
  155. */
  156. #ifdef __APPLE__
  157. /* iOS SDK 12+ seems to define it, undef it for compatibility both ways */
  158. #undef MSG_NOSIGNAL
  159. #define MSG_NOSIGNAL SO_NOSIGPIPE
  160. #endif
  161. /*
  162. * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
  163. * POSIX 2008.
  164. */
  165. #if defined(__sun) && !defined(MSG_NOSIGNAL)
  166. #define MSG_NOSIGNAL 0
  167. #endif
  168. int
  169. lws_plat_BINDTODEVICE(int fd, const char *ifname);
  170. int
  171. lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, int canned_len,
  172. int n, int fd, const char *iface);
  173. int
  174. lws_plat_if_up(const char *ifname, int fd, int up);