ua_architecture.h 6.8 KB

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