1 #ifndef __SOCKET_TCP_SSL_H__ 2 #define __SOCKET_TCP_SSL_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> 18 extern EXPCL_PANDA_NATIVENET SSL_CTX *global_ssl_ctx;
22 const SSL_METHOD *meth;
23 SSLeay_add_ssl_algorithms();
25 meth = SSLv23_method();
26 SSL_load_error_strings();
28 global_ssl_ctx = SSL_CTX_new((SSL_METHOD *) meth);
32 SSL_CTX_free (global_ssl_ctx);
33 global_ssl_ctx =
nullptr;
36 bool isactive() {
return global_ssl_ctx !=
nullptr; };
42 class EXPCL_PANDA_NATIVENET Socket_TCP_SSL :
public Socket_IP {
44 inline Socket_TCP_SSL(SOCKET);
45 inline Socket_TCP_SSL() : _ssl(nullptr) {}
47 virtual inline ~Socket_TCP_SSL()
52 inline int SetNoDelay();
53 inline int SetLinger(
int interval_seconds = 0);
54 inline int DontLinger();
56 inline int SetSendBufferSize(
int insize);
58 inline int SendData(
const char *data,
int size);
59 inline int RecvData(
char *data,
int size);
60 inline bool ErrorIs_WouldBlocking(
int err);
62 inline SSL *get_ssl() {
return _ssl; };
64 inline void DetailErrorFormat(
void);
70 if (_ssl !=
nullptr) {
81 static void init_type() {
82 Socket_IP::init_type();
84 Socket_IP::get_class_type());
87 return get_class_type();
89 virtual TypeHandle force_init_type() {init_type();
return get_class_type();}
98 inline Socket_TCP_SSL::
99 Socket_TCP_SSL(SOCKET sck) : ::
Socket_IP(sck) {
103 _ssl = SSL_new(global_ssl_ctx);
104 if (_ssl ==
nullptr) {
119 inline int Socket_TCP_SSL::
123 ret1 = setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (
char *)&nodel,
sizeof(nodel));
135 SetLinger(
int interval_seconds) {
137 ll.l_linger = interval_seconds;
139 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *)&ll,
sizeof(linger));
156 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *)&ll,
sizeof(linger));
168 SetSendBufferSize(
int insize) {
169 if (setsockopt(_socket, (
int) SOL_SOCKET, (
int) SO_SNDBUF, (
char *) &insize,
sizeof(
int))) {
179 bool Socket_TCP_SSL::
182 if (_socket == BAD_SOCKET) {
186 if (DO_CONNECT(_socket, &theaddress.GetAddressInfo()) != 0) {
190 _ssl = SSL_new(global_ssl_ctx);
191 if (_ssl ==
nullptr) {
194 SSL_set_fd(_ssl, (
int)GetSocket());
195 if (SSL_connect(_ssl) == -1) {
207 inline int Socket_TCP_SSL::
208 SendData(
const char *data,
int size) {
209 if (_ssl ==
nullptr) {
215 return SSL_write(_ssl, data, size);
222 inline int Socket_TCP_SSL::
223 RecvData(
char *data,
int len) {
224 if (_ssl ==
nullptr) {
230 return SSL_read(_ssl, data, len);
236 inline bool Socket_TCP_SSL::
237 ErrorIs_WouldBlocking(
int err) {
238 if (_ssl ==
nullptr || err >= 0) {
239 nativenet_cat.warning()
240 <<
"Socket_TCP_SSL::ErrorIs_WouldBlocking->Called With Error number " 241 << err <<
" or _ssl is NULL\n";
245 int ssl_error_code = SSL_get_error(_ssl,err);
248 switch(ssl_error_code) {
249 case SSL_ERROR_WANT_READ:
250 case SSL_ERROR_WANT_WRITE:
251 case SSL_ERROR_WANT_CONNECT:
256 case SSL_ERROR_SYSCALL:
257 if(GETERROR() == LOCAL_BLOCKING_ERROR) {
277 inline void Socket_TCP_SSL::
278 DetailErrorFormat(
void) {
284 const char *file,*data;
288 es = CRYPTO_thread_id();
289 while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
290 ERR_error_string_n(l, buf,
sizeof(buf));
291 BIO_snprintf(buf2,
sizeof(buf2),
"***%lu:%s:%s:%d:%s\n", (
unsigned long) es, buf, file, line, (flags & ERR_TXT_STRING) ? data :
"NoText");
292 nativenet_cat.warning()
293 <<
"Socket_TCP_SSL::DetailErrorFormat->[" << buf2 <<
"]\n";
297 #endif // HAVE_OPENSSL 299 #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
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
sa_family_t get_family() const
Returns AF_INET if this is an IPv4 address, or AF_INET6 if this is an IPv6 address.
A simple place to store and manipulate tcp and port address for communication layer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
SOCKET GetSocket()
Gets the base socket type.