ua_architecture.h 4.0 KB

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