ua_architecture.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
  8. #define PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
  9. /* Enable POSIX features */
  10. #if !defined(_XOPEN_SOURCE)
  11. # define _XOPEN_SOURCE 600
  12. #endif
  13. #ifndef _DEFAULT_SOURCE
  14. # define _DEFAULT_SOURCE
  15. #endif
  16. /* On older systems we need to define _BSD_SOURCE.
  17. * _DEFAULT_SOURCE is an alias for that. */
  18. #ifndef _BSD_SOURCE
  19. # define _BSD_SOURCE
  20. #endif
  21. #include <errno.h>
  22. #include <arpa/inet.h>
  23. #include <netinet/in.h>
  24. #include <netdb.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/select.h>
  27. #define UA_sleep_ms(X) usleep(X * 1000)
  28. #define OPTVAL_TYPE int
  29. #include <fcntl.h>
  30. #include <unistd.h> // read, write, close
  31. #ifdef __QNX__
  32. # include <sys/socket.h>
  33. #endif
  34. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  35. # include <sys/param.h>
  36. # if defined(BSD)
  37. # include<sys/socket.h>
  38. # endif
  39. #endif
  40. #if !defined(__CYGWIN__)
  41. # include <netinet/tcp.h>
  42. #endif
  43. /* unsigned int for windows and workaround to a glibc bug */
  44. /* Additionally if GNU_LIBRARY is not defined, it may be using
  45. * musl libc (e.g. Docker Alpine) */
  46. #if defined(__OpenBSD__) || \
  47. (defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \
  48. (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \
  49. !defined(__GNU_LIBRARY__))
  50. # define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
  51. # define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
  52. #else
  53. # define UA_fd_set(fd, fds) FD_SET(fd, fds)
  54. # define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
  55. #endif
  56. #define UA_IPV6 1
  57. #define UA_SOCKET int
  58. #define UA_INVALID_SOCKET -1
  59. #define UA_ERRNO errno
  60. #define UA_INTERRUPTED EINTR
  61. #define UA_AGAIN EAGAIN
  62. #define UA_EAGAIN EAGAIN
  63. #define UA_WOULDBLOCK EWOULDBLOCK
  64. #define UA_ERR_CONNECTION_PROGRESS EINPROGRESS
  65. #include "ua_types.h"
  66. #define ua_send send
  67. #define ua_recv recv
  68. #define ua_close close
  69. #define ua_select select
  70. #define ua_shutdown shutdown
  71. #define ua_socket socket
  72. #define ua_bind bind
  73. #define ua_listen listen
  74. #define ua_accept accept
  75. #define ua_connect connect
  76. #define ua_translate_error gai_strerror
  77. #define ua_getaddrinfo getaddrinfo
  78. #define ua_getsockopt getsockopt
  79. #define ua_setsockopt setsockopt
  80. #define ua_freeaddrinfo freeaddrinfo
  81. static UA_INLINE uint32_t socket_set_blocking(UA_SOCKET sockfd){
  82. int opts = fcntl(sockfd, F_GETFL);
  83. if(opts < 0 || fcntl(sockfd, F_SETFL, opts & (~O_NONBLOCK)) < 0)
  84. return UA_STATUSCODE_BADINTERNALERROR;
  85. return UA_STATUSCODE_GOOD;
  86. }
  87. static UA_INLINE uint32_t socket_set_nonblocking(UA_SOCKET sockfd){
  88. int opts = fcntl(sockfd, F_GETFL);
  89. if(opts < 0 || fcntl(sockfd, F_SETFL, opts | O_NONBLOCK) < 0)
  90. return UA_STATUSCODE_BADINTERNALERROR;
  91. return UA_STATUSCODE_GOOD;
  92. }
  93. #include <stdio.h>
  94. #define ua_snprintf snprintf
  95. static UA_INLINE void ua_initialize_architecture_network(void){
  96. return;
  97. }
  98. static UA_INLINE void ua_deinitialize_architecture_network(void){
  99. return;
  100. }
  101. #endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */