ua_architecture.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2016-2017 (c) Julius Pfrommer, Fraunhofer IOSB
  5. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  6. */
  7. #ifdef UA_ARCHITECTURE_POSIX
  8. #ifndef PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
  9. #define PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
  10. #include "ua_architecture_base.h"
  11. /* Enable POSIX features */
  12. #if !defined(_XOPEN_SOURCE)
  13. # define _XOPEN_SOURCE 600
  14. #endif
  15. #ifndef _DEFAULT_SOURCE
  16. # define _DEFAULT_SOURCE
  17. #endif
  18. /* On older systems we need to define _BSD_SOURCE.
  19. * _DEFAULT_SOURCE is an alias for that. */
  20. #ifndef _BSD_SOURCE
  21. # define _BSD_SOURCE
  22. #endif
  23. #include <errno.h>
  24. #include <arpa/inet.h>
  25. #include <netinet/in.h>
  26. #include <netdb.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/select.h>
  29. #include <sys/types.h>
  30. #include <net/if.h>
  31. #ifndef UA_sleep_ms
  32. # include <unistd.h>
  33. # define UA_sleep_ms(X) usleep(X * 1000)
  34. #endif
  35. #define OPTVAL_TYPE int
  36. #include <fcntl.h>
  37. #include <unistd.h> // read, write, close
  38. #ifdef __QNX__
  39. # include <sys/socket.h>
  40. #endif
  41. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  42. # include <sys/param.h>
  43. # if defined(BSD)
  44. # include<sys/socket.h>
  45. # endif
  46. #endif
  47. #if !defined(__CYGWIN__)
  48. # include <netinet/tcp.h>
  49. #endif
  50. /* unsigned int for windows and workaround to a glibc bug */
  51. /* Additionally if GNU_LIBRARY is not defined, it may be using
  52. * musl libc (e.g. Docker Alpine) */
  53. #if defined(__OpenBSD__) || \
  54. (defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \
  55. (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \
  56. !defined(__GNU_LIBRARY__))
  57. # define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
  58. # define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
  59. #else
  60. # define UA_fd_set(fd, fds) FD_SET(fd, fds)
  61. # define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
  62. #endif
  63. #define UA_access access
  64. #define UA_IPV6 1
  65. #define UA_SOCKET int
  66. #define UA_INVALID_SOCKET -1
  67. #define UA_ERRNO errno
  68. #define UA_INTERRUPTED EINTR
  69. #define UA_AGAIN EAGAIN
  70. #define UA_EAGAIN EAGAIN
  71. #define UA_WOULDBLOCK EWOULDBLOCK
  72. #define UA_ERR_CONNECTION_PROGRESS EINPROGRESS
  73. #define UA_ENABLE_LOG_COLORS
  74. #define UA_getnameinfo getnameinfo
  75. #define UA_send send
  76. #define UA_recv recv
  77. #define UA_sendto sendto
  78. #define UA_recvfrom recvfrom
  79. #define UA_htonl htonl
  80. #define UA_ntohl ntohl
  81. #define UA_close close
  82. #define UA_select select
  83. #define UA_shutdown shutdown
  84. #define UA_socket socket
  85. #define UA_bind bind
  86. #define UA_listen listen
  87. #define UA_accept accept
  88. #define UA_connect connect
  89. #define UA_getaddrinfo getaddrinfo
  90. #define UA_getsockopt getsockopt
  91. #define UA_setsockopt setsockopt
  92. #define UA_freeaddrinfo freeaddrinfo
  93. #define UA_gethostname gethostname
  94. #define UA_inet_pton inet_pton
  95. #if UA_IPV6
  96. # define UA_if_nametoindex if_nametoindex
  97. #endif
  98. #ifdef UA_ENABLE_MALLOC_SINGLETON
  99. extern void * (*UA_globalMalloc)(size_t size);
  100. extern void (*UA_globalFree)(void *ptr);
  101. extern void * (*UA_globalCalloc)(size_t nelem, size_t elsize);
  102. extern void * (*UA_globalRealloc)(void *ptr, size_t size);
  103. # define UA_free(ptr) UA_globalFree(ptr)
  104. # define UA_malloc(size) UA_globalMalloc(size)
  105. # define UA_calloc(num, size) UA_globalCalloc(num, size)
  106. # define UA_realloc(ptr, size) UA_globalRealloc(ptr, size)
  107. #endif
  108. #include <stdlib.h>
  109. #ifndef UA_free
  110. # define UA_free free
  111. #endif
  112. #ifndef UA_malloc
  113. # define UA_malloc malloc
  114. #endif
  115. #ifndef UA_calloc
  116. # define UA_calloc calloc
  117. #endif
  118. #ifndef UA_realloc
  119. # define UA_realloc realloc
  120. #endif
  121. #include <stdio.h>
  122. #define UA_snprintf snprintf
  123. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  124. char *errno_str = strerror(errno); \
  125. LOG; \
  126. }
  127. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP(LOG) { \
  128. const char *errno_str = gai_strerror(errno); \
  129. LOG; \
  130. }
  131. #include "ua_architecture_functions.h"
  132. #if defined(__APPLE__) && defined(_SYS_QUEUE_H_)
  133. // in some compilers there's already a _SYS_QUEUE_H_ which is included first and doesn't have all functions
  134. #undef SLIST_HEAD
  135. #undef SLIST_HEAD_INITIALIZER
  136. #undef SLIST_ENTRY
  137. #undef SLIST_FIRST
  138. #undef SLIST_END
  139. #undef SLIST_EMPTY
  140. #undef SLIST_NEXT
  141. #undef SLIST_FOREACH
  142. #undef SLIST_FOREACH_SAFE
  143. #undef SLIST_INIT
  144. #undef SLIST_INSERT_AFTER
  145. #undef SLIST_INSERT_HEAD
  146. #undef SLIST_REMOVE_AFTER
  147. #undef SLIST_REMOVE_HEAD
  148. #undef SLIST_REMOVE
  149. #undef LIST_HEAD
  150. #undef LIST_HEAD_INITIALIZER
  151. #undef LIST_ENTRY
  152. #undef LIST_FIRST
  153. #undef LIST_END
  154. #undef LIST_EMPTY
  155. #undef LIST_NEXT
  156. #undef LIST_FOREACH
  157. #undef LIST_FOREACH_SAFE
  158. #undef LIST_INIT
  159. #undef LIST_INSERT_AFTER
  160. #undef LIST_INSERT_BEFORE
  161. #undef LIST_INSERT_HEAD
  162. #undef LIST_REMOVE
  163. #undef LIST_REPLACE
  164. #undef SIMPLEQ_HEAD
  165. #undef SIMPLEQ_HEAD_INITIALIZER
  166. #undef SIMPLEQ_ENTRY
  167. #undef SIMPLEQ_FIRST
  168. #undef SIMPLEQ_END
  169. #undef SIMPLEQ_EMPTY
  170. #undef SIMPLEQ_NEXT
  171. #undef SIMPLEQ_FOREACH
  172. #undef SIMPLEQ_FOREACH_SAFE
  173. #undef SIMPLEQ_INIT
  174. #undef SIMPLEQ_INSERT_HEAD
  175. #undef SIMPLEQ_INSERT_TAIL
  176. #undef SIMPLEQ_INSERT_AFTER
  177. #undef SIMPLEQ_REMOVE_HEAD
  178. #undef SIMPLEQ_REMOVE_AFTER
  179. #undef XSIMPLEQ_HEAD
  180. #undef XSIMPLEQ_ENTRY
  181. #undef XSIMPLEQ_XOR
  182. #undef XSIMPLEQ_FIRST
  183. #undef XSIMPLEQ_END
  184. #undef XSIMPLEQ_EMPTY
  185. #undef XSIMPLEQ_NEXT
  186. #undef XSIMPLEQ_FOREACH
  187. #undef XSIMPLEQ_FOREACH_SAFE
  188. #undef XSIMPLEQ_INIT
  189. #undef XSIMPLEQ_INSERT_HEAD
  190. #undef XSIMPLEQ_INSERT_TAIL
  191. #undef XSIMPLEQ_INSERT_AFTER
  192. #undef XSIMPLEQ_REMOVE_HEAD
  193. #undef XSIMPLEQ_REMOVE_AFTER
  194. #undef TAILQ_HEAD
  195. #undef TAILQ_HEAD_INITIALIZER
  196. #undef TAILQ_ENTRY
  197. #undef TAILQ_FIRST
  198. #undef TAILQ_END
  199. #undef TAILQ_NEXT
  200. #undef TAILQ_LAST
  201. #undef TAILQ_PREV
  202. #undef TAILQ_EMPTY
  203. #undef TAILQ_FOREACH
  204. #undef TAILQ_FOREACH_SAFE
  205. #undef TAILQ_FOREACH_REVERSE
  206. #undef TAILQ_FOREACH_REVERSE_SAFE
  207. #undef TAILQ_INIT
  208. #undef TAILQ_INSERT_HEAD
  209. #undef TAILQ_INSERT_TAIL
  210. #undef TAILQ_INSERT_AFTER
  211. #undef TAILQ_INSERT_BEFORE
  212. #undef TAILQ_REMOVE
  213. #undef TAILQ_REPLACE
  214. #undef CIRCLEQ_HEAD
  215. #undef CIRCLEQ_HEAD_INITIALIZER
  216. #undef CIRCLEQ_ENTRY
  217. #undef CIRCLEQ_FIRST
  218. #undef CIRCLEQ_LAST
  219. #undef CIRCLEQ_END
  220. #undef CIRCLEQ_NEXT
  221. #undef CIRCLEQ_PREV
  222. #undef CIRCLEQ_EMPTY
  223. #undef CIRCLEQ_FOREACH
  224. #undef CIRCLEQ_FOREACH_SAFE
  225. #undef CIRCLEQ_FOREACH_REVERSE
  226. #undef CIRCLEQ_FOREACH_REVERSE_SAFE
  227. #undef CIRCLEQ_INIT
  228. #undef CIRCLEQ_INSERT_AFTER
  229. #undef CIRCLEQ_INSERT_BEFORE
  230. #undef CIRCLEQ_INSERT_HEAD
  231. #undef CIRCLEQ_INSERT_TAIL
  232. #undef CIRCLEQ_REMOVE
  233. #undef CIRCLEQ_REPLACE
  234. #undef _SYS_QUEUE_H_
  235. #endif /* defined(__APPLE__) && defined(_SYS_QUEUE_H_) */
  236. #endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */
  237. #endif /* UA_ARCHITECTURE_POSIX */