ua_architecture.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <open62541/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_getsockname getsockname
  95. #define UA_inet_pton inet_pton
  96. #if UA_IPV6
  97. # define UA_if_nametoindex if_nametoindex
  98. #endif
  99. #ifdef UA_ENABLE_MALLOC_SINGLETON
  100. extern void * (*UA_globalMalloc)(size_t size);
  101. extern void (*UA_globalFree)(void *ptr);
  102. extern void * (*UA_globalCalloc)(size_t nelem, size_t elsize);
  103. extern void * (*UA_globalRealloc)(void *ptr, size_t size);
  104. # define UA_free(ptr) UA_globalFree(ptr)
  105. # define UA_malloc(size) UA_globalMalloc(size)
  106. # define UA_calloc(num, size) UA_globalCalloc(num, size)
  107. # define UA_realloc(ptr, size) UA_globalRealloc(ptr, size)
  108. #endif
  109. #include <stdlib.h>
  110. #ifndef UA_free
  111. # define UA_free free
  112. #endif
  113. #ifndef UA_malloc
  114. # define UA_malloc malloc
  115. #endif
  116. #ifndef UA_calloc
  117. # define UA_calloc calloc
  118. #endif
  119. #ifndef UA_realloc
  120. # define UA_realloc realloc
  121. #endif
  122. #include <stdio.h>
  123. #define UA_snprintf snprintf
  124. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  125. char *errno_str = strerror(errno); \
  126. LOG; \
  127. }
  128. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP(LOG) { \
  129. const char *errno_str = gai_strerror(errno); \
  130. LOG; \
  131. }
  132. #include <open62541/architecture_functions.h>
  133. #if defined(__APPLE__) && defined(_SYS_QUEUE_H_)
  134. // in some compilers there's already a _SYS_QUEUE_H_ which is included first and doesn't have all functions
  135. #undef SLIST_HEAD
  136. #undef SLIST_HEAD_INITIALIZER
  137. #undef SLIST_ENTRY
  138. #undef SLIST_FIRST
  139. #undef SLIST_END
  140. #undef SLIST_EMPTY
  141. #undef SLIST_NEXT
  142. #undef SLIST_FOREACH
  143. #undef SLIST_FOREACH_SAFE
  144. #undef SLIST_INIT
  145. #undef SLIST_INSERT_AFTER
  146. #undef SLIST_INSERT_HEAD
  147. #undef SLIST_REMOVE_AFTER
  148. #undef SLIST_REMOVE_HEAD
  149. #undef SLIST_REMOVE
  150. #undef LIST_HEAD
  151. #undef LIST_HEAD_INITIALIZER
  152. #undef LIST_ENTRY
  153. #undef LIST_FIRST
  154. #undef LIST_END
  155. #undef LIST_EMPTY
  156. #undef LIST_NEXT
  157. #undef LIST_FOREACH
  158. #undef LIST_FOREACH_SAFE
  159. #undef LIST_INIT
  160. #undef LIST_INSERT_AFTER
  161. #undef LIST_INSERT_BEFORE
  162. #undef LIST_INSERT_HEAD
  163. #undef LIST_REMOVE
  164. #undef LIST_REPLACE
  165. #undef SIMPLEQ_HEAD
  166. #undef SIMPLEQ_HEAD_INITIALIZER
  167. #undef SIMPLEQ_ENTRY
  168. #undef SIMPLEQ_FIRST
  169. #undef SIMPLEQ_END
  170. #undef SIMPLEQ_EMPTY
  171. #undef SIMPLEQ_NEXT
  172. #undef SIMPLEQ_FOREACH
  173. #undef SIMPLEQ_FOREACH_SAFE
  174. #undef SIMPLEQ_INIT
  175. #undef SIMPLEQ_INSERT_HEAD
  176. #undef SIMPLEQ_INSERT_TAIL
  177. #undef SIMPLEQ_INSERT_AFTER
  178. #undef SIMPLEQ_REMOVE_HEAD
  179. #undef SIMPLEQ_REMOVE_AFTER
  180. #undef XSIMPLEQ_HEAD
  181. #undef XSIMPLEQ_ENTRY
  182. #undef XSIMPLEQ_XOR
  183. #undef XSIMPLEQ_FIRST
  184. #undef XSIMPLEQ_END
  185. #undef XSIMPLEQ_EMPTY
  186. #undef XSIMPLEQ_NEXT
  187. #undef XSIMPLEQ_FOREACH
  188. #undef XSIMPLEQ_FOREACH_SAFE
  189. #undef XSIMPLEQ_INIT
  190. #undef XSIMPLEQ_INSERT_HEAD
  191. #undef XSIMPLEQ_INSERT_TAIL
  192. #undef XSIMPLEQ_INSERT_AFTER
  193. #undef XSIMPLEQ_REMOVE_HEAD
  194. #undef XSIMPLEQ_REMOVE_AFTER
  195. #undef TAILQ_HEAD
  196. #undef TAILQ_HEAD_INITIALIZER
  197. #undef TAILQ_ENTRY
  198. #undef TAILQ_FIRST
  199. #undef TAILQ_END
  200. #undef TAILQ_NEXT
  201. #undef TAILQ_LAST
  202. #undef TAILQ_PREV
  203. #undef TAILQ_EMPTY
  204. #undef TAILQ_FOREACH
  205. #undef TAILQ_FOREACH_SAFE
  206. #undef TAILQ_FOREACH_REVERSE
  207. #undef TAILQ_FOREACH_REVERSE_SAFE
  208. #undef TAILQ_INIT
  209. #undef TAILQ_INSERT_HEAD
  210. #undef TAILQ_INSERT_TAIL
  211. #undef TAILQ_INSERT_AFTER
  212. #undef TAILQ_INSERT_BEFORE
  213. #undef TAILQ_REMOVE
  214. #undef TAILQ_REPLACE
  215. #undef CIRCLEQ_HEAD
  216. #undef CIRCLEQ_HEAD_INITIALIZER
  217. #undef CIRCLEQ_ENTRY
  218. #undef CIRCLEQ_FIRST
  219. #undef CIRCLEQ_LAST
  220. #undef CIRCLEQ_END
  221. #undef CIRCLEQ_NEXT
  222. #undef CIRCLEQ_PREV
  223. #undef CIRCLEQ_EMPTY
  224. #undef CIRCLEQ_FOREACH
  225. #undef CIRCLEQ_FOREACH_SAFE
  226. #undef CIRCLEQ_FOREACH_REVERSE
  227. #undef CIRCLEQ_FOREACH_REVERSE_SAFE
  228. #undef CIRCLEQ_INIT
  229. #undef CIRCLEQ_INSERT_AFTER
  230. #undef CIRCLEQ_INSERT_BEFORE
  231. #undef CIRCLEQ_INSERT_HEAD
  232. #undef CIRCLEQ_INSERT_TAIL
  233. #undef CIRCLEQ_REMOVE
  234. #undef CIRCLEQ_REPLACE
  235. #undef _SYS_QUEUE_H_
  236. #endif /* defined(__APPLE__) && defined(_SYS_QUEUE_H_) */
  237. #endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */
  238. #endif /* UA_ARCHITECTURE_POSIX */