rtmp_sys.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef __RTMP_SYS_H__
  2. #define __RTMP_SYS_H__
  3. /*
  4. * Copyright (C) 2010 Howard Chu
  5. *
  6. * This file is part of librtmp.
  7. *
  8. * librtmp is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as
  10. * published by the Free Software Foundation; either version 2.1,
  11. * or (at your option) any later version.
  12. *
  13. * librtmp is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with librtmp see the file COPYING. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/lgpl.html
  23. */
  24. #ifdef _WIN32
  25. #include <winsock2.h>
  26. #include <ws2tcpip.h>
  27. #if _MSC_VER < 1910 /* MSVC */
  28. #define snprintf _snprintf
  29. #endif
  30. #ifdef _MSC_VER /* MSVC */
  31. //#define snprintf _snprintf
  32. #define strcasecmp stricmp
  33. #define strncasecmp strnicmp
  34. //#define vsnprintf _vsnprintf
  35. #endif
  36. #define GetSockError() WSAGetLastError()
  37. #define SetSockError(e) WSASetLastError(e)
  38. #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
  39. #define EWOULDBLOCK WSAETIMEDOUT /* we don't use nonblocking, but we do use timeouts */
  40. #define sleep(n) Sleep(n*1000)
  41. #define msleep(n) Sleep(n)
  42. #define SET_RCVTIMEO(tv,s) int tv = s*1000
  43. #else /* !_WIN32 */
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <sys/times.h>
  47. #include <netdb.h>
  48. #include <unistd.h>
  49. #include <netinet/in.h>
  50. #include <netinet/tcp.h>
  51. #include <arpa/inet.h>
  52. #define GetSockError() errno
  53. #define SetSockError(e) errno = e
  54. #undef closesocket
  55. #define closesocket(s) close(s)
  56. #define msleep(n) usleep(n*1000)
  57. #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
  58. #endif
  59. #include "rtmp.h"
  60. #ifdef USE_POLARSSL
  61. #include <polarssl/version.h>
  62. #include <polarssl/net.h>
  63. #include <polarssl/ssl.h>
  64. #include <polarssl/havege.h>
  65. #if POLARSSL_VERSION_NUMBER < 0x01010000
  66. #define havege_random havege_rand
  67. #endif
  68. #if POLARSSL_VERSION_NUMBER >= 0x01020000
  69. #define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,ctx)
  70. #else
  71. #define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,resume,timeout,ctx)
  72. #endif
  73. typedef struct tls_ctx {
  74. havege_state hs;
  75. ssl_session ssn;
  76. } tls_ctx;
  77. typedef struct tls_server_ctx {
  78. havege_state *hs;
  79. x509_cert cert;
  80. rsa_context key;
  81. ssl_session ssn;
  82. const char *dhm_P, *dhm_G;
  83. } tls_server_ctx;
  84. #define TLS_CTX tls_ctx *
  85. #define TLS_client(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\
  86. ssl_set_endpoint(s, SSL_IS_CLIENT); ssl_set_authmode(s, SSL_VERIFY_NONE);\
  87. ssl_set_rng(s, havege_random, &ctx->hs);\
  88. ssl_set_ciphersuites(s, ssl_default_ciphersuites);\
  89. SSL_SET_SESSION(s, 1, 600, &ctx->ssn)
  90. #define TLS_server(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\
  91. ssl_set_endpoint(s, SSL_IS_SERVER); ssl_set_authmode(s, SSL_VERIFY_NONE);\
  92. ssl_set_rng(s, havege_random, ((tls_server_ctx*)ctx)->hs);\
  93. ssl_set_ciphersuites(s, ssl_default_ciphersuites);\
  94. SSL_SET_SESSION(s, 1, 600, &((tls_server_ctx*)ctx)->ssn);\
  95. ssl_set_own_cert(s, &((tls_server_ctx*)ctx)->cert, &((tls_server_ctx*)ctx)->key);\
  96. ssl_set_dh_param(s, ((tls_server_ctx*)ctx)->dhm_P, ((tls_server_ctx*)ctx)->dhm_G)
  97. #define TLS_setfd(s,fd) ssl_set_bio(s, net_recv, &fd, net_send, &fd)
  98. #define TLS_connect(s) ssl_handshake(s)
  99. #define TLS_accept(s) ssl_handshake(s)
  100. #define TLS_read(s,b,l) ssl_read(s,(unsigned char *)b,l)
  101. #define TLS_write(s,b,l) ssl_write(s,(unsigned char *)b,l)
  102. #define TLS_shutdown(s) ssl_close_notify(s)
  103. #define TLS_close(s) ssl_free(s); free(s)
  104. #elif defined(USE_GNUTLS)
  105. #include <gnutls/gnutls.h>
  106. typedef struct tls_ctx {
  107. gnutls_certificate_credentials_t cred;
  108. gnutls_priority_t prios;
  109. } tls_ctx;
  110. #define TLS_CTX tls_ctx *
  111. #define TLS_client(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_CLIENT); gnutls_priority_set(s, ctx->prios); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx->cred)
  112. #define TLS_server(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_SERVER); gnutls_priority_set_direct(s, "NORMAL", NULL); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx)
  113. #define TLS_setfd(s,fd) gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t)(long)fd)
  114. #define TLS_connect(s) gnutls_handshake(s)
  115. #define TLS_accept(s) gnutls_handshake(s)
  116. #define TLS_read(s,b,l) gnutls_record_recv(s,b,l)
  117. #define TLS_write(s,b,l) gnutls_record_send(s,b,l)
  118. #define TLS_shutdown(s) gnutls_bye(s, GNUTLS_SHUT_RDWR)
  119. #define TLS_close(s) gnutls_deinit(s)
  120. #else /* USE_OPENSSL */
  121. #define TLS_CTX SSL_CTX *
  122. #define TLS_client(ctx,s) s = SSL_new(ctx)
  123. #define TLS_server(ctx,s) s = SSL_new(ctx)
  124. #define TLS_setfd(s,fd) SSL_set_fd(s,fd)
  125. #define TLS_connect(s) SSL_connect(s)
  126. #define TLS_accept(s) SSL_accept(s)
  127. #define TLS_read(s,b,l) SSL_read(s,b,l)
  128. #define TLS_write(s,b,l) SSL_write(s,b,l)
  129. #define TLS_shutdown(s) SSL_shutdown(s)
  130. #define TLS_close(s) SSL_free(s)
  131. #endif
  132. #endif