1 #ifndef __SOCKET_TCP_SSL_H__ 2 #define __SOCKET_TCP_SSL_H__ 5 #include "config_nativenet.h" 7 #include "numeric_types.h" 11 #include <openssl/rsa.h> 12 #include <openssl/crypto.h> 13 #include <openssl/x509.h> 14 #include <openssl/pem.h> 15 #include <openssl/ssl.h> 16 #include <openssl/err.h> 25 extern EXPCL_PANDA_NATIVENET SSL_CTX *global_ssl_ctx;
32 const SSL_METHOD *meth;
33 SSLeay_add_ssl_algorithms();
35 meth = SSLv23_method();
36 SSL_load_error_strings();
38 global_ssl_ctx = SSL_CTX_new ((SSL_METHOD *) meth);
43 SSL_CTX_free (global_ssl_ctx);
44 global_ssl_ctx = NULL;
48 bool isactive() {
return global_ssl_ctx != NULL; };
52 class EXPCL_PANDA_NATIVENET Socket_TCP_SSL :
public Socket_IP 56 inline Socket_TCP_SSL(SOCKET);
57 inline Socket_TCP_SSL() : _ssl(NULL) {}
59 virtual inline ~Socket_TCP_SSL()
64 inline int SetNoDelay();
65 inline int SetLinger(
int interval_seconds = 0);
66 inline int DontLinger();
68 inline int SetSendBufferSize(
int insize);
70 inline int SendData(
const char * data,
int size);
71 inline int RecvData(
char * data,
int size);
72 inline bool ErrorIs_WouldBlocking(
int err);
74 inline SSL * get_ssl() {
return _ssl; };
76 inline void DetailErrorFormat(
void);
94 static void init_type() {
95 Socket_IP::init_type();
96 register_type(_type_handle,
"Socket_TCP_SSL",
97 Socket_IP::get_class_type());
100 return get_class_type();
102 virtual TypeHandle force_init_type() {init_type();
return get_class_type();}
114 inline Socket_TCP_SSL::Socket_TCP_SSL(SOCKET sck) : ::
Socket_IP(sck)
118 _ssl = SSL_new (global_ssl_ctx);
133 inline int Socket_TCP_SSL::SetNoDelay()
137 ret1 = setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (
char *) & nodel,
sizeof(nodel));
149 int Socket_TCP_SSL::SetLinger(
int interval_seconds)
152 ll.l_linger = interval_seconds;
154 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *) & ll,
sizeof(linger));
167 int Socket_TCP_SSL::DontLinger()
172 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *) & ll,
sizeof(linger));
184 int Socket_TCP_SSL::SetSendBufferSize(
int insize)
186 if (setsockopt(_socket, (
int) SOL_SOCKET, (
int) SO_SNDBUF, (
char *) &insize,
sizeof(
int)))
196 bool Socket_TCP_SSL::ActiveOpen(
const Socket_Address & theaddress)
198 _socket = DO_NEWTCP();
199 if (_socket == BAD_SOCKET)
202 if (DO_CONNECT(_socket, &theaddress.GetAddressInfo()) != 0)
206 _ssl = SSL_new (global_ssl_ctx);
210 if(SSL_connect(_ssl) == -1)
226 inline int Socket_TCP_SSL::SendData(
const char * data,
int size)
233 return SSL_write(_ssl, data, size);
245 inline int Socket_TCP_SSL::RecvData(
char * data,
int len)
252 return SSL_read(_ssl, data, len);
262 inline bool Socket_TCP_SSL::ErrorIs_WouldBlocking(
int err)
264 if(_ssl == NULL || err >= 0)
266 nativenet_cat.warning()
267 <<
"Socket_TCP_SSL::ErrorIs_WouldBlocking->Called With Error number " 268 << err <<
" or _ssl is NULL\n";
272 int ssl_error_code = SSL_get_error(_ssl,err);
275 switch(ssl_error_code)
277 case SSL_ERROR_WANT_READ:
278 case SSL_ERROR_WANT_WRITE:
279 case SSL_ERROR_WANT_CONNECT:
284 case SSL_ERROR_SYSCALL:
285 if(GETERROR() == LOCAL_BLOCKING_ERROR)
304 inline void Socket_TCP_SSL::DetailErrorFormat(
void)
311 const char *file,*data;
315 es=CRYPTO_thread_id();
316 while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
318 ERR_error_string_n(l, buf,
sizeof( buf) );
319 BIO_snprintf(buf2,
sizeof(buf2),
"***%lu:%s:%s:%d:%s\n", (
unsigned long) es, buf,file, line, (flags & ERR_TXT_STRING) ? data :
"NoText");
320 nativenet_cat.warning()
321 <<
"Socket_TCP_SSL::DetailErrorFormat->[" << buf2 <<
"]\n";
325 #endif // HAVE_OPENSSL 327 #endif //__SOCKET_TCP_SSL_H__ Base functionality for a INET domain Socket this call should be the starting point for all other unix...
int SetNonBlocking()
this function will throw a socket into non-blocking mode
A simple place to store and munipulate tcp and port address for communication layer.
TypeHandle is the identifier used to differentiate C++ class types.
SOCKET GetSocket()
Gets the base socket type.