ua_architecture.h 4.1 KB

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