ua_architecture.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* Enable POSIX features */
  11. #if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
  12. # define _XOPEN_SOURCE 600
  13. #endif
  14. #ifndef _DEFAULT_SOURCE
  15. # define _DEFAULT_SOURCE
  16. #endif
  17. /* On older systems we need to define _BSD_SOURCE.
  18. * _DEFAULT_SOURCE is an alias for that. */
  19. #ifndef _BSD_SOURCE
  20. # define _BSD_SOURCE
  21. #endif
  22. #include <errno.h>
  23. #include <arpa/inet.h>
  24. #include <netinet/in.h>
  25. #include <netdb.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/select.h>
  28. #ifndef UA_sleep_ms
  29. #define UA_sleep_ms(X) usleep(X * 1000)
  30. #else /* UA_sleep_ms */
  31. /* With this one can define its own UA_sleep_ms using a preprocessor define.
  32. E.g. see unit tests. */
  33. void UA_sleep_ms(size_t ms);
  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. /* Thread-Local Storage
  51. * --------------------
  52. * Thread-local storage is not required by the main library functionality. It is
  53. * only used for some testing strategies. ``UA_THREAD_LOCAL`` is empty if the
  54. * feature is not available. */
  55. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  56. # define UA_THREAD_LOCAL _Thread_local /* C11 */
  57. #elif defined(__cplusplus) && __cplusplus > 199711L
  58. # define UA_THREAD_LOCAL thread_local /* C++11 */
  59. #elif defined(__GNUC__)
  60. # define UA_THREAD_LOCAL __thread /* GNU extension */
  61. #else
  62. # define UA_THREAD_LOCAL
  63. #endif
  64. /* unsigned int for windows and workaround to a glibc bug */
  65. /* Additionally if GNU_LIBRARY is not defined, it may be using
  66. * musl libc (e.g. Docker Alpine) */
  67. #if defined(__OpenBSD__) || \
  68. (defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \
  69. (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \
  70. !defined(__GNU_LIBRARY__))
  71. # define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
  72. # define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
  73. #else
  74. # define UA_fd_set(fd, fds) FD_SET(fd, fds)
  75. # define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
  76. #endif
  77. #define UA_access access
  78. #define UA_IPV6 1
  79. #define UA_SOCKET int
  80. #define UA_INVALID_SOCKET -1
  81. #define UA_ERRNO errno
  82. #define UA_INTERRUPTED EINTR
  83. #define UA_AGAIN EAGAIN
  84. #define UA_EAGAIN EAGAIN
  85. #define UA_WOULDBLOCK EWOULDBLOCK
  86. #define UA_ERR_CONNECTION_PROGRESS EINPROGRESS
  87. #define UA_ENABLE_LOG_COLORS
  88. #define UA_getnameinfo getnameinfo
  89. #define UA_send send
  90. #define UA_recv recv
  91. #define UA_close close
  92. #define UA_select select
  93. #define UA_shutdown shutdown
  94. #define UA_socket socket
  95. #define UA_bind bind
  96. #define UA_listen listen
  97. #define UA_accept accept
  98. #define UA_connect connect
  99. #define UA_getaddrinfo getaddrinfo
  100. #define UA_getsockopt getsockopt
  101. #define UA_setsockopt setsockopt
  102. #define UA_freeaddrinfo freeaddrinfo
  103. #define UA_gethostname gethostname
  104. #include <stdlib.h>
  105. #define UA_free free
  106. #define UA_malloc malloc
  107. #define UA_calloc calloc
  108. #define UA_realloc realloc
  109. #include <stdio.h>
  110. #define UA_snprintf snprintf
  111. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  112. char *errno_str = strerror(errno); \
  113. LOG; \
  114. }
  115. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP(LOG) { \
  116. const char *errno_str = gai_strerror(errno); \
  117. LOG; \
  118. }
  119. #include "../ua_architecture_functions.h"
  120. #endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */
  121. #endif /* UA_ARCHITECTURE_POSIX */