ua_architecture.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #if UA_MULTITHREADING >= 100
  133. #include <pthread.h>
  134. #define Sleep(x) sleep(x / 1000)
  135. #define UA_LOCK_TYPE(mutexName) pthread_mutex_t mutexName; \
  136. pthread_mutexattr_t mutexName##_attr; \
  137. int mutexName##Counter;
  138. #define UA_LOCK_INIT(mutexName) pthread_mutexattr_init(&mutexName##_attr); \
  139. pthread_mutexattr_settype(&mutexName##_attr, PTHREAD_MUTEX_RECURSIVE); \
  140. pthread_mutex_init(&mutexName, &mutexName##_attr); \
  141. mutexName##Counter = 0;
  142. #define UA_LOCK_DESTROY(mutexName) pthread_mutex_destroy(&mutexName); \
  143. pthread_mutexattr_destroy(&mutexName##_attr);
  144. #define UA_LOCK(mutexName) pthread_mutex_lock(&mutexName); \
  145. UA_assert(++(mutexName##Counter) == 1); \
  146. #define UA_UNLOCK(mutexName) UA_assert(--(mutexName##Counter) == 0); \
  147. pthread_mutex_unlock(&mutexName);
  148. #define UA_LOCK_ASSERT(mutexName, num) UA_assert(mutexName##Counter == num);
  149. #else
  150. #define UA_LOCK_TYPE(mutexName)
  151. #define UA_LOCK_INIT(mutexName)
  152. #define UA_LOCK_DESTROY(mutexName)
  153. #define UA_LOCK(mutexName)
  154. #define UA_UNLOCK(mutexName)
  155. #define UA_LOCK_ASSERT(mutexName, num)
  156. #endif
  157. #include <open62541/architecture_functions.h>
  158. #if defined(__APPLE__) && defined(_SYS_QUEUE_H_)
  159. // in some compilers there's already a _SYS_QUEUE_H_ which is included first and doesn't have all functions
  160. #undef SLIST_HEAD
  161. #undef SLIST_HEAD_INITIALIZER
  162. #undef SLIST_ENTRY
  163. #undef SLIST_FIRST
  164. #undef SLIST_END
  165. #undef SLIST_EMPTY
  166. #undef SLIST_NEXT
  167. #undef SLIST_FOREACH
  168. #undef SLIST_FOREACH_SAFE
  169. #undef SLIST_INIT
  170. #undef SLIST_INSERT_AFTER
  171. #undef SLIST_INSERT_HEAD
  172. #undef SLIST_REMOVE_AFTER
  173. #undef SLIST_REMOVE_HEAD
  174. #undef SLIST_REMOVE
  175. #undef LIST_HEAD
  176. #undef LIST_HEAD_INITIALIZER
  177. #undef LIST_ENTRY
  178. #undef LIST_FIRST
  179. #undef LIST_END
  180. #undef LIST_EMPTY
  181. #undef LIST_NEXT
  182. #undef LIST_FOREACH
  183. #undef LIST_FOREACH_SAFE
  184. #undef LIST_INIT
  185. #undef LIST_INSERT_AFTER
  186. #undef LIST_INSERT_BEFORE
  187. #undef LIST_INSERT_HEAD
  188. #undef LIST_REMOVE
  189. #undef LIST_REPLACE
  190. #undef SIMPLEQ_HEAD
  191. #undef SIMPLEQ_HEAD_INITIALIZER
  192. #undef SIMPLEQ_ENTRY
  193. #undef SIMPLEQ_FIRST
  194. #undef SIMPLEQ_END
  195. #undef SIMPLEQ_EMPTY
  196. #undef SIMPLEQ_NEXT
  197. #undef SIMPLEQ_FOREACH
  198. #undef SIMPLEQ_FOREACH_SAFE
  199. #undef SIMPLEQ_INIT
  200. #undef SIMPLEQ_INSERT_HEAD
  201. #undef SIMPLEQ_INSERT_TAIL
  202. #undef SIMPLEQ_INSERT_AFTER
  203. #undef SIMPLEQ_REMOVE_HEAD
  204. #undef SIMPLEQ_REMOVE_AFTER
  205. #undef XSIMPLEQ_HEAD
  206. #undef XSIMPLEQ_ENTRY
  207. #undef XSIMPLEQ_XOR
  208. #undef XSIMPLEQ_FIRST
  209. #undef XSIMPLEQ_END
  210. #undef XSIMPLEQ_EMPTY
  211. #undef XSIMPLEQ_NEXT
  212. #undef XSIMPLEQ_FOREACH
  213. #undef XSIMPLEQ_FOREACH_SAFE
  214. #undef XSIMPLEQ_INIT
  215. #undef XSIMPLEQ_INSERT_HEAD
  216. #undef XSIMPLEQ_INSERT_TAIL
  217. #undef XSIMPLEQ_INSERT_AFTER
  218. #undef XSIMPLEQ_REMOVE_HEAD
  219. #undef XSIMPLEQ_REMOVE_AFTER
  220. #undef TAILQ_HEAD
  221. #undef TAILQ_HEAD_INITIALIZER
  222. #undef TAILQ_ENTRY
  223. #undef TAILQ_FIRST
  224. #undef TAILQ_END
  225. #undef TAILQ_NEXT
  226. #undef TAILQ_LAST
  227. #undef TAILQ_PREV
  228. #undef TAILQ_EMPTY
  229. #undef TAILQ_FOREACH
  230. #undef TAILQ_FOREACH_SAFE
  231. #undef TAILQ_FOREACH_REVERSE
  232. #undef TAILQ_FOREACH_REVERSE_SAFE
  233. #undef TAILQ_INIT
  234. #undef TAILQ_INSERT_HEAD
  235. #undef TAILQ_INSERT_TAIL
  236. #undef TAILQ_INSERT_AFTER
  237. #undef TAILQ_INSERT_BEFORE
  238. #undef TAILQ_REMOVE
  239. #undef TAILQ_REPLACE
  240. #undef CIRCLEQ_HEAD
  241. #undef CIRCLEQ_HEAD_INITIALIZER
  242. #undef CIRCLEQ_ENTRY
  243. #undef CIRCLEQ_FIRST
  244. #undef CIRCLEQ_LAST
  245. #undef CIRCLEQ_END
  246. #undef CIRCLEQ_NEXT
  247. #undef CIRCLEQ_PREV
  248. #undef CIRCLEQ_EMPTY
  249. #undef CIRCLEQ_FOREACH
  250. #undef CIRCLEQ_FOREACH_SAFE
  251. #undef CIRCLEQ_FOREACH_REVERSE
  252. #undef CIRCLEQ_FOREACH_REVERSE_SAFE
  253. #undef CIRCLEQ_INIT
  254. #undef CIRCLEQ_INSERT_AFTER
  255. #undef CIRCLEQ_INSERT_BEFORE
  256. #undef CIRCLEQ_INSERT_HEAD
  257. #undef CIRCLEQ_INSERT_TAIL
  258. #undef CIRCLEQ_REMOVE
  259. #undef CIRCLEQ_REPLACE
  260. #undef _SYS_QUEUE_H_
  261. #endif /* defined(__APPLE__) && defined(_SYS_QUEUE_H_) */
  262. #endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */
  263. #endif /* UA_ARCHITECTURE_POSIX */