ns_turn_defs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #ifndef __IOADEFS__
  31. #define __IOADEFS__
  32. #define TURN_SERVER_VERSION "4.6.2"
  33. #define TURN_SERVER_VERSION_NAME "Gorst"
  34. #ifndef TURN_SERVER_BUILD_INFO
  35. #define TURN_SERVER_BUILD_INFO ""
  36. #endif
  37. #define TURN_SOFTWARE "Coturn-" TURN_SERVER_VERSION " '" TURN_SERVER_VERSION_NAME "'" TURN_SERVER_BUILD_INFO
  38. #if (defined(__unix__) || defined(unix)) && !defined(USG)
  39. #include <sys/param.h>
  40. #endif
  41. #if defined(__APPLE__)
  42. #define __APPLE_USE_RFC_3542
  43. #endif
  44. #if defined(WINDOWS)
  45. #include <process.h>
  46. #include <ws2tcpip.h>
  47. #else
  48. #include <arpa/inet.h>
  49. #include <net/if.h>
  50. #include <netinet/in.h>
  51. #include <netinet/tcp.h>
  52. #include <strings.h>
  53. #include <sys/socket.h>
  54. #include <unistd.h>
  55. #endif
  56. #include <ctype.h>
  57. #include <errno.h>
  58. #include <inttypes.h>
  59. #include <stdarg.h>
  60. #include <stdint.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <sys/types.h>
  65. #include <time.h>
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. #define nswap16(s) ntohs(s)
  70. #define nswap32(ul) ntohl(ul)
  71. #define nswap64(ull) ioa_ntoh64(ull)
  72. static inline uint64_t _ioa_ntoh64(uint64_t v) {
  73. #if BYTE_ORDER == LITTLE_ENDIAN
  74. uint8_t *src = (uint8_t *)&v;
  75. uint8_t *dst = src + 7;
  76. while (src < dst) {
  77. uint8_t vdst = *dst;
  78. *(dst--) = *src;
  79. *(src++) = vdst;
  80. }
  81. #elif BYTE_ORDER == BIG_ENDIAN
  82. /* OK */
  83. #else
  84. #error WRONG BYTE_ORDER SETTING
  85. #endif
  86. return v;
  87. }
  88. /* TTL */
  89. #define TTL_IGNORE ((int)(-1))
  90. #define TTL_DEFAULT (64)
  91. /* TOS */
  92. #define TOS_IGNORE ((int)(-1))
  93. #define TOS_DEFAULT (0)
  94. #define ioa_ntoh64 _ioa_ntoh64
  95. #define ioa_hton64 _ioa_ntoh64
  96. #if defined(WINDOWS)
  97. static inline int socket_errno(void) { return WSAGetLastError(); }
  98. static inline int socket_enomem(void) { return socket_errno() == WSA_NOT_ENOUGH_MEMORY; }
  99. static inline int socket_eintr(void) { return socket_errno() == WSAEINTR; }
  100. static inline int socket_ebadf(void) { return socket_errno() == WSAEBADF; }
  101. static inline int socket_eacces(void) { return socket_errno() == WSAEACCES; }
  102. static inline int socket_enobufs(void) { return socket_errno() == WSAENOBUFS; }
  103. static inline int socket_eagain(void) { return socket_errno() == WSATRY_AGAIN; }
  104. static inline int socket_ewouldblock(void) { return socket_errno() == WSAEWOULDBLOCK; }
  105. static inline int socket_einprogress(void) { return socket_errno() == WSAEINPROGRESS; }
  106. static inline int socket_econnreset(void) { return socket_errno() == WSAECONNRESET; }
  107. static inline int socket_econnrefused(void) { return socket_errno() == WSAECONNREFUSED; }
  108. static inline int socket_ehostdown(void) { return socket_errno() == WSAEHOSTDOWN; }
  109. static inline int socket_emsgsize(void) { return socket_errno() == WSAEMSGSIZE; }
  110. #else
  111. static inline int socket_errno(void) { return errno; }
  112. static inline int socket_eperm(void) { return socket_errno() == EPERM; }
  113. static inline int socket_enomem(void) { return socket_errno() == ENOMEM; }
  114. static inline int socket_eintr(void) { return socket_errno() == EINTR; }
  115. static inline int socket_ebadf(void) { return socket_errno() == EBADF; }
  116. static inline int socket_eacces(void) { return socket_errno() == EACCES; }
  117. static inline int socket_enobufs(void) { return socket_errno() == ENOBUFS; }
  118. static inline int socket_eagain(void) { return socket_errno() == EAGAIN; }
  119. #if defined(EWOULDBLOCK)
  120. static inline int socket_ewouldblock(void) { return socket_errno() == EWOULDBLOCK; }
  121. #else
  122. static inline int socket_ewouldblock(void) { return socket_errno() == EAGAIN; }
  123. #endif
  124. static inline int socket_einprogress(void) { return socket_errno() == EINPROGRESS; }
  125. static inline int socket_econnreset(void) { return socket_errno() == ECONNRESET; }
  126. static inline int socket_econnrefused(void) { return socket_errno() == ECONNREFUSED; }
  127. static inline int socket_ehostdown(void) { return socket_errno() == EHOSTDOWN; }
  128. static inline int socket_emsgsize(void) { return socket_errno() == EMSGSIZE; }
  129. #endif
  130. #define BUFFEREVENT_FREE(be) \
  131. do { \
  132. if (be) { \
  133. bufferevent_flush(be, EV_READ | EV_WRITE, BEV_FLUSH); \
  134. bufferevent_disable(be, EV_READ | EV_WRITE); \
  135. bufferevent_free(be); \
  136. be = NULL; \
  137. } \
  138. } while (0)
  139. #define turn_time() ((turn_time_t)time(NULL))
  140. typedef int vint;
  141. typedef vint *vintp;
  142. typedef uint32_t turn_time_t;
  143. #define turn_time_before(t1, t2) ((((int32_t)(t1)) - ((int32_t)(t2))) < 0)
  144. #if !defined(UNUSED_ARG)
  145. #define UNUSED_ARG(A) \
  146. do { \
  147. A = A; \
  148. } while (0)
  149. #endif
  150. #define MAX_STUN_MESSAGE_SIZE (65507)
  151. #define STUN_BUFFER_SIZE (MAX_STUN_MESSAGE_SIZE)
  152. #define UDP_STUN_BUFFER_SIZE (1024 << 4)
  153. #define NONCE_LENGTH_32BITS (4)
  154. #define DEFAULT_STUN_PORT (3478)
  155. #define DEFAULT_STUN_TLS_PORT (5349)
  156. #if BYTE_ORDER == LITTLE_ENDIAN
  157. #define DEFAULT_STUN_PORT_NBO (0x960D)
  158. #elif BYTE_ORDER == BIG_ENDIAN
  159. #define DEFAULT_STUN_PORT_NBO (0x0D96)
  160. #else
  161. #error WRONG BYTE_ORDER SETTING
  162. #endif
  163. // NOLINTBEGIN(clang-diagnostic-string-compare)
  164. #define STRCPY(dst, src) \
  165. do { \
  166. if ((const char *)(dst) != (const char *)(src)) { \
  167. if (sizeof(dst) == sizeof(char *)) \
  168. strcpy(((char *)(dst)), (const char *)(src)); \
  169. else { \
  170. size_t szdst = sizeof((dst)); \
  171. strncpy((char *)(dst), (const char *)(src), szdst); \
  172. ((char *)(dst))[szdst - 1] = 0; \
  173. } \
  174. } \
  175. } while (0)
  176. // NOLINTEND(clang-diagnostic-string-compare)
  177. //////////////// Bufferevents /////////////////////
  178. #define TURN_BUFFEREVENTS_OPTIONS (BEV_OPT_DEFER_CALLBACKS | BEV_OPT_THREADSAFE | BEV_OPT_UNLOCK_CALLBACKS)
  179. //////////////// KERNEL-LEVEL CHANNEL HANDLERS /////////
  180. #if !defined(TURN_CHANNEL_HANDLER_KERNEL)
  181. #define TURN_CHANNEL_HANDLER_KERNEL void *
  182. #endif
  183. #if !defined(CREATE_TURN_CHANNEL_KERNEL)
  184. #define CREATE_TURN_CHANNEL_KERNEL(channel_number, address_family_client, address_family_peer, protocol_client, \
  185. client_addr, local_addr, local_relay_addr, peer_addr) \
  186. ((TURN_CHANNEL_HANDLER_KERNEL)(1))
  187. #endif
  188. #if !defined(DELETE_TURN_CHANNEL_KERNEL)
  189. #define DELETE_TURN_CHANNEL_KERNEL(handler)
  190. #endif
  191. ////////////////////////////////////////////////////////
  192. #if !defined(IPPROTO_SCTP)
  193. #define TURN_NO_SCTP
  194. #endif
  195. #define CLIENT_DGRAM_SOCKET_TYPE SOCK_DGRAM
  196. #define CLIENT_DGRAM_SOCKET_PROTOCOL IPPROTO_IP
  197. #define CLIENT_STREAM_SOCKET_TYPE SOCK_STREAM
  198. #define CLIENT_STREAM_SOCKET_PROTOCOL IPPROTO_IP
  199. #define SCTP_CLIENT_STREAM_SOCKET_TYPE SOCK_STREAM
  200. #if !defined(TURN_NO_SCTP)
  201. #define SCTP_CLIENT_STREAM_SOCKET_PROTOCOL IPPROTO_SCTP
  202. #else
  203. #define SCTP_CLIENT_STREAM_SOCKET_PROTOCOL IPPROTO_IP
  204. #endif
  205. #define RELAY_DGRAM_SOCKET_TYPE SOCK_DGRAM
  206. #define RELAY_DGRAM_SOCKET_PROTOCOL IPPROTO_IP
  207. #define RELAY_STREAM_SOCKET_TYPE SOCK_STREAM
  208. #define RELAY_STREAM_SOCKET_PROTOCOL IPPROTO_IP
  209. #define ADMIN_STREAM_SOCKET_TYPE SOCK_STREAM
  210. #define ADMIN_STREAM_SOCKET_PROTOCOL IPPROTO_IP
  211. ////////////////////////////////////////////////////////
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif
  216. /* __IODEFS__ */