Add support for building with gnutls with nettle as backend
[rtmpdump.git] / librtmp / rtmp_sys.h
blob478c59f7941456a3bc31134158f400321f067df5
1 #ifndef __RTMP_SYS_H__
2 #define __RTMP_SYS_H__
3 /*
4 * Copyright (C) 2010 Howard Chu
6 * This file is part of librtmp.
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.
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.
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
25 #ifdef _WIN32
27 #include <winsock2.h>
28 #include <ws2tcpip.h>
30 #ifdef _MSC_VER /* MSVC */
31 #define snprintf _snprintf
32 #define strcasecmp stricmp
33 #define strncasecmp strnicmp
34 #define vsnprintf _vsnprintf
35 #endif
37 #define GetSockError() WSAGetLastError()
38 #define SetSockError(e) WSASetLastError(e)
39 #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
40 #define EWOULDBLOCK WSAETIMEDOUT /* we don't use nonblocking, but we do use timeouts */
41 #define sleep(n) Sleep(n*1000)
42 #define msleep(n) Sleep(n)
43 #define SET_RCVTIMEO(tv,s) int tv = s*1000
44 #else /* !_WIN32 */
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/times.h>
48 #include <netdb.h>
49 #include <unistd.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
53 #define GetSockError() errno
54 #define SetSockError(e) errno = e
55 #undef closesocket
56 #define closesocket(s) close(s)
57 #define msleep(n) usleep(n*1000)
58 #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
59 #endif
61 #include "rtmp.h"
63 #ifdef USE_POLARSSL
64 #include <polarssl/net.h>
65 #include <polarssl/ssl.h>
66 #include <polarssl/havege.h>
67 typedef struct tls_ctx {
68 havege_state hs;
69 ssl_session ssn;
70 } tls_ctx;
71 #define TLS_CTX tls_ctx *
72 #define TLS_client(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\
73 ssl_set_endpoint(s, SSL_IS_CLIENT); ssl_set_authmode(s, SSL_VERIFY_NONE);\
74 ssl_set_rng(s, havege_rand, &ctx->hs);\
75 ssl_set_ciphersuites(s, ssl_default_ciphersuites);\
76 ssl_set_session(s, 1, 600, &ctx->ssn)
77 #define TLS_setfd(s,fd) ssl_set_bio(s, net_recv, &fd, net_send, &fd)
78 #define TLS_connect(s) ssl_handshake(s)
79 #define TLS_read(s,b,l) ssl_read(s,(unsigned char *)b,l)
80 #define TLS_write(s,b,l) ssl_write(s,(unsigned char *)b,l)
81 #define TLS_shutdown(s) ssl_close_notify(s)
82 #define TLS_close(s) ssl_free(s); free(s)
84 #elif defined(USE_GNUTLS) || defined(USE_GNUTLS_NETTLE)
85 #include <gnutls/gnutls.h>
86 typedef struct tls_ctx {
87 gnutls_certificate_credentials_t cred;
88 gnutls_priority_t prios;
89 } tls_ctx;
90 #define TLS_CTX tls_ctx *
91 #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)
92 #define TLS_setfd(s,fd) gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t)(long)fd)
93 #define TLS_connect(s) gnutls_handshake(s)
94 #define TLS_read(s,b,l) gnutls_record_recv(s,b,l)
95 #define TLS_write(s,b,l) gnutls_record_send(s,b,l)
96 #define TLS_shutdown(s) gnutls_bye(s, GNUTLS_SHUT_RDWR)
97 #define TLS_close(s) gnutls_deinit(s)
99 #else /* USE_OPENSSL */
100 #define TLS_CTX SSL_CTX *
101 #define TLS_client(ctx,s) s = SSL_new(ctx)
102 #define TLS_setfd(s,fd) SSL_set_fd(s,fd)
103 #define TLS_connect(s) SSL_connect(s)
104 #define TLS_read(s,b,l) SSL_read(s,b,l)
105 #define TLS_write(s,b,l) SSL_write(s,b,l)
106 #define TLS_shutdown(s) SSL_shutdown(s)
107 #define TLS_close(s) SSL_free(s)
109 #endif
110 #endif