ua_architecture.h 7.7 KB

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