Loading src/linux/event.cpp +4 −4 Changes for src/linux/event.cpp: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -92,7 +92,7 @@ namespace netplus { setevent.data.ptr = nullptr; if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, _ServerSocket->getSocket(),(struct epoll_event*)&setevent) < 0) { _ServerSocket->fd(),(struct epoll_event*)&setevent) < 0) { exception[NetException::Critical] << "initEventHandler: can't create epoll"; throw exception; } Loading Loading @@ -125,7 +125,7 @@ namespace netplus { continue; int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL, delcon->csock->getSocket(), 0); delcon->csock->fd(), 0); if (ect < 0) { if(errno==ENOENT) Loading Loading @@ -165,7 +165,7 @@ namespace netplus { setevent.events = EPOLLIN; setevent.data.ptr = ccon; int estate = epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->getSocket(), (struct epoll_event*)&setevent); int estate = epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->fd(), (struct epoll_event*)&setevent); if ( estate < 0 ) { exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll"; Loading Loading @@ -263,7 +263,7 @@ namespace netplus { setevent.events = events; setevent.data.ptr = curcon; if (epoll_ctl(_pollFD, EPOLL_CTL_MOD, curcon->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { curcon->csock->fd(), (struct epoll_event*)&setevent) < 0) { except[NetException::Error] << "_setEpollEvents: can change socket!"; throw except; } Loading src/linux/socket.cpp +81 −20 Changes for src/linux/socket.cpp: 81 added lines, 20 removed lines. Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "socket.h" #include "mbedtls/debug.h" #include "mbedtls/error.h" #define HIDDEN __attribute__ ((visibility ("hidden"))) Loading @@ -66,10 +67,6 @@ void netplus::socket::setnonblocking(){ } } int netplus::socket::getSocket(){ return _Socket; } netplus::tcp::tcp(const netplus::tcp& ctcp){ _Locked++; _Socket=ctcp._Socket; Loading Loading @@ -174,6 +171,10 @@ void netplus::tcp::listen(){ } } int netplus::tcp::fd(){ return _Socket; } int netplus::tcp::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -402,6 +403,10 @@ void netplus::udp::listen(){ } } int netplus::udp::fd(){ return _Socket; } int netplus::udp::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -531,36 +536,41 @@ netplus::ssl::ssl(const char *addr,int port,int maxconnections,int sockopts,cons mbedtls_net_init( &_Socket ); mbedtls_ssl_init( &_SSLCtx ); mbedtls_ssl_config_init( &_SSLConf ); mbedtls_x509_crt_init( &_Cacert ); mbedtls_pem_init( &_Cacert ); mbedtls_ctr_drbg_init( &_SSLCTR_DRBG ); int ret; mbedtls_entropy_init( &_SSLEntropy ); mbedtls_ssl_conf_rng(&_SSLConf, mbedtls_ctr_drbg_random, &_SSLCTR_DRBG); const char *pers = "libnet_ssl_server"; mbedtls_x509_crt_init( &_Cacert); char err_str[256]; size_t use_len; if( ( ret = mbedtls_ctr_drbg_seed( &_SSLCTR_DRBG, mbedtls_entropy_func, &_SSLEntropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_ctr_drbg_seed returned" << ret; mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_ctr_drbg_seed returned: " << err_str; throw exception; } if( ( ret = mbedtls_x509_crt_parse(&_Cacert, key,keylen) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_x509_crt_parse returned -0x" << -ret ; if( ( ret = mbedtls_pem_read_buffer(&_Cacert,"-----BEGIN CERTIFICATE-----","-----END CERTIFICATE-----",cert,nullptr,0,&use_len) ) != 0 ){ mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_x509_crt_parse returned: " << err_str ; throw exception; } if( ( ret = mbedtls_x509_crt_parse(&_Cacert, cert,certlen) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_x509_crt_parse returned -0x" << -ret ; if( ( ret = mbedtls_pem_read_buffer(&_Cacert,"-----BEGIN PRIVATE KEY-----","-----END PRIVATE KEY-----",key,nullptr,0,&use_len) ) != 0 ){ mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_x509_crt_parse returned: " << err_str ; throw exception; } mbedtls_ssl_conf_ca_chain( &_SSLConf, &_Cacert, NULL ); memset(_Addr,0,255); if(strlen(addr)<255){ Loading @@ -571,6 +581,7 @@ netplus::ssl::ssl(const char *addr,int port,int maxconnections,int sockopts,cons } _Port=port; } netplus::ssl::ssl(){ Loading @@ -588,15 +599,41 @@ netplus::ssl::~ssl(){ netplus::socket *netplus::ssl::accept(){ NetException exception; socket *csock=new ssl(); mbedtls_net_context myaddr; csock->_Socket = mbedtls_net_accept(&_Socket,&myaddr,nullptr,0,nullptr); if(csock->_Socket<0){ ssl *csock=new ssl(); int ret; char err_str[256]; mbedtls_ssl_conf_rng( &_SSLConf, mbedtls_ctr_drbg_random, &_SSLCTR_DRBG); if( (ret=mbedtls_net_accept(&_Socket,&csock->_Socket,nullptr,0,nullptr)) <0){ delete csock; csock=nullptr; exception[NetException::Error] << "Can't accept on Socket"; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } if( (ret=mbedtls_ssl_setup(&_SSLCtx,&csock->_SSLConf)) <0){ delete csock; csock=nullptr; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } mbedtls_ssl_set_bio(&_SSLCtx,&csock->_Socket, mbedtls_net_send, mbedtls_net_recv, NULL); while ((ret = mbedtls_ssl_handshake(&_SSLCtx)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { delete csock; csock=nullptr; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } } return csock; } Loading @@ -616,6 +653,10 @@ void netplus::ssl::listen(){ return; } int netplus::ssl::fd(){ return _Socket.fd; } int netplus::ssl::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -659,15 +700,35 @@ netplus::ssl* netplus::ssl::connect(){ char port[255]; snprintf(port,255,"%d",_Port); if ( mbedtls_net_connect(&_Socket,_Addr,port,MBEDTLS_NET_PROTO_TCP) < 0) { exception[NetException::Error] << "Socket connect: can't connect to server aborting !"; throw exception; } mbedtls_ssl_set_bio(&_SSLCtx,&_Socket, mbedtls_net_send, mbedtls_net_recv, NULL); int ret; if ( (ret = mbedtls_net_connect(&_Socket,_Addr,port,MBEDTLS_NET_PROTO_TCP)) < 0) { exception[NetException::Error] << "Socket connect: can't connect to server aborting: " << ret; while ((ret = mbedtls_ssl_handshake(&_SSLCtx)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { exception[NetException::Error] << "Can't connect on Socket"; throw exception; } } return this; } void netplus::ssl::setnonblocking(){ if(mbedtls_net_set_nonblock(&_Socket)<0){ NetException exception; exception[NetException::Error] << "Could not set ClientSocket nonblocking!"; throw exception; } } void netplus::ssl::getAddress(std::string &addr){ if(!_SocketPtr) return; Loading src/socket.h +10 −5 Changes for src/socket.h: 10 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ extern "C" { #include "mbedtls/ssl.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/entropy.h" #include "mbedtls/pem.h" } #pragma once Loading @@ -46,8 +47,7 @@ namespace netplus { public: socket(); virtual ~socket(); void setnonblocking(); int getSocket(); virtual void setnonblocking(); virtual socket *accept()=0; Loading @@ -63,10 +63,11 @@ namespace netplus { virtual void getAddress(std::string &addr)=0; int _Socket; virtual int fd()=0; void *_SocketPtr; unsigned int _SocketPtrSize; protected: int _Socket; int _Locked; }; Loading @@ -82,6 +83,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading Loading @@ -112,6 +114,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading Loading @@ -139,6 +142,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading @@ -147,6 +151,7 @@ namespace netplus { ssl* connect(); void setnonblocking(); void getAddress(std::string &addr); private: Loading @@ -159,7 +164,7 @@ namespace netplus { mbedtls_ctr_drbg_context _SSLCTR_DRBG; mbedtls_ssl_context _SSLCtx; mbedtls_ssl_config _SSLConf; mbedtls_x509_crt _Cacert; mbedtls_pem_context _Cacert; }; class quick : public socket{ Loading Loading
src/linux/event.cpp +4 −4 Changes for src/linux/event.cpp: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -92,7 +92,7 @@ namespace netplus { setevent.data.ptr = nullptr; if (epoll_ctl(_pollFD, EPOLL_CTL_ADD, _ServerSocket->getSocket(),(struct epoll_event*)&setevent) < 0) { _ServerSocket->fd(),(struct epoll_event*)&setevent) < 0) { exception[NetException::Critical] << "initEventHandler: can't create epoll"; throw exception; } Loading Loading @@ -125,7 +125,7 @@ namespace netplus { continue; int ect = epoll_ctl(_pollFD, EPOLL_CTL_DEL, delcon->csock->getSocket(), 0); delcon->csock->fd(), 0); if (ect < 0) { if(errno==ENOENT) Loading Loading @@ -165,7 +165,7 @@ namespace netplus { setevent.events = EPOLLIN; setevent.data.ptr = ccon; int estate = epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->getSocket(), (struct epoll_event*)&setevent); int estate = epoll_ctl(_pollFD, EPOLL_CTL_ADD, ccon->csock->fd(), (struct epoll_event*)&setevent); if ( estate < 0 ) { exception[NetException::Error] << "ConnectEventHandler: can't add socket to epoll"; Loading Loading @@ -263,7 +263,7 @@ namespace netplus { setevent.events = events; setevent.data.ptr = curcon; if (epoll_ctl(_pollFD, EPOLL_CTL_MOD, curcon->csock->getSocket(), (struct epoll_event*)&setevent) < 0) { curcon->csock->fd(), (struct epoll_event*)&setevent) < 0) { except[NetException::Error] << "_setEpollEvents: can change socket!"; throw except; } Loading
src/linux/socket.cpp +81 −20 Changes for src/linux/socket.cpp: 81 added lines, 20 removed lines. Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "socket.h" #include "mbedtls/debug.h" #include "mbedtls/error.h" #define HIDDEN __attribute__ ((visibility ("hidden"))) Loading @@ -66,10 +67,6 @@ void netplus::socket::setnonblocking(){ } } int netplus::socket::getSocket(){ return _Socket; } netplus::tcp::tcp(const netplus::tcp& ctcp){ _Locked++; _Socket=ctcp._Socket; Loading Loading @@ -174,6 +171,10 @@ void netplus::tcp::listen(){ } } int netplus::tcp::fd(){ return _Socket; } int netplus::tcp::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -402,6 +403,10 @@ void netplus::udp::listen(){ } } int netplus::udp::fd(){ return _Socket; } int netplus::udp::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -531,36 +536,41 @@ netplus::ssl::ssl(const char *addr,int port,int maxconnections,int sockopts,cons mbedtls_net_init( &_Socket ); mbedtls_ssl_init( &_SSLCtx ); mbedtls_ssl_config_init( &_SSLConf ); mbedtls_x509_crt_init( &_Cacert ); mbedtls_pem_init( &_Cacert ); mbedtls_ctr_drbg_init( &_SSLCTR_DRBG ); int ret; mbedtls_entropy_init( &_SSLEntropy ); mbedtls_ssl_conf_rng(&_SSLConf, mbedtls_ctr_drbg_random, &_SSLCTR_DRBG); const char *pers = "libnet_ssl_server"; mbedtls_x509_crt_init( &_Cacert); char err_str[256]; size_t use_len; if( ( ret = mbedtls_ctr_drbg_seed( &_SSLCTR_DRBG, mbedtls_entropy_func, &_SSLEntropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_ctr_drbg_seed returned" << ret; mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_ctr_drbg_seed returned: " << err_str; throw exception; } if( ( ret = mbedtls_x509_crt_parse(&_Cacert, key,keylen) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_x509_crt_parse returned -0x" << -ret ; if( ( ret = mbedtls_pem_read_buffer(&_Cacert,"-----BEGIN CERTIFICATE-----","-----END CERTIFICATE-----",cert,nullptr,0,&use_len) ) != 0 ){ mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_x509_crt_parse returned: " << err_str ; throw exception; } if( ( ret = mbedtls_x509_crt_parse(&_Cacert, cert,certlen) ) != 0 ){ exception[NetException::Critical] << " failed\n ! mbedtls_x509_crt_parse returned -0x" << -ret ; if( ( ret = mbedtls_pem_read_buffer(&_Cacert,"-----BEGIN PRIVATE KEY-----","-----END PRIVATE KEY-----",key,nullptr,0,&use_len) ) != 0 ){ mbedtls_strerror(ret, err_str, 256); exception[NetException::Critical] << "mbedtls_x509_crt_parse returned: " << err_str ; throw exception; } mbedtls_ssl_conf_ca_chain( &_SSLConf, &_Cacert, NULL ); memset(_Addr,0,255); if(strlen(addr)<255){ Loading @@ -571,6 +581,7 @@ netplus::ssl::ssl(const char *addr,int port,int maxconnections,int sockopts,cons } _Port=port; } netplus::ssl::ssl(){ Loading @@ -588,15 +599,41 @@ netplus::ssl::~ssl(){ netplus::socket *netplus::ssl::accept(){ NetException exception; socket *csock=new ssl(); mbedtls_net_context myaddr; csock->_Socket = mbedtls_net_accept(&_Socket,&myaddr,nullptr,0,nullptr); if(csock->_Socket<0){ ssl *csock=new ssl(); int ret; char err_str[256]; mbedtls_ssl_conf_rng( &_SSLConf, mbedtls_ctr_drbg_random, &_SSLCTR_DRBG); if( (ret=mbedtls_net_accept(&_Socket,&csock->_Socket,nullptr,0,nullptr)) <0){ delete csock; csock=nullptr; exception[NetException::Error] << "Can't accept on Socket"; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } if( (ret=mbedtls_ssl_setup(&_SSLCtx,&csock->_SSLConf)) <0){ delete csock; csock=nullptr; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } mbedtls_ssl_set_bio(&_SSLCtx,&csock->_Socket, mbedtls_net_send, mbedtls_net_recv, NULL); while ((ret = mbedtls_ssl_handshake(&_SSLCtx)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { delete csock; csock=nullptr; mbedtls_strerror(ret, err_str, 256); exception[NetException::Error] << "Can't accept on Socket: " << err_str; throw exception; } } return csock; } Loading @@ -616,6 +653,10 @@ void netplus::ssl::listen(){ return; } int netplus::ssl::fd(){ return _Socket.fd; } int netplus::ssl::getMaxconnections(){ return _Maxconnections; } Loading Loading @@ -659,15 +700,35 @@ netplus::ssl* netplus::ssl::connect(){ char port[255]; snprintf(port,255,"%d",_Port); if ( mbedtls_net_connect(&_Socket,_Addr,port,MBEDTLS_NET_PROTO_TCP) < 0) { exception[NetException::Error] << "Socket connect: can't connect to server aborting !"; throw exception; } mbedtls_ssl_set_bio(&_SSLCtx,&_Socket, mbedtls_net_send, mbedtls_net_recv, NULL); int ret; if ( (ret = mbedtls_net_connect(&_Socket,_Addr,port,MBEDTLS_NET_PROTO_TCP)) < 0) { exception[NetException::Error] << "Socket connect: can't connect to server aborting: " << ret; while ((ret = mbedtls_ssl_handshake(&_SSLCtx)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { exception[NetException::Error] << "Can't connect on Socket"; throw exception; } } return this; } void netplus::ssl::setnonblocking(){ if(mbedtls_net_set_nonblock(&_Socket)<0){ NetException exception; exception[NetException::Error] << "Could not set ClientSocket nonblocking!"; throw exception; } } void netplus::ssl::getAddress(std::string &addr){ if(!_SocketPtr) return; Loading
src/socket.h +10 −5 Changes for src/socket.h: 10 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ extern "C" { #include "mbedtls/ssl.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/entropy.h" #include "mbedtls/pem.h" } #pragma once Loading @@ -46,8 +47,7 @@ namespace netplus { public: socket(); virtual ~socket(); void setnonblocking(); int getSocket(); virtual void setnonblocking(); virtual socket *accept()=0; Loading @@ -63,10 +63,11 @@ namespace netplus { virtual void getAddress(std::string &addr)=0; int _Socket; virtual int fd()=0; void *_SocketPtr; unsigned int _SocketPtrSize; protected: int _Socket; int _Locked; }; Loading @@ -82,6 +83,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading Loading @@ -112,6 +114,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading Loading @@ -139,6 +142,7 @@ namespace netplus { socket *accept(); void bind(); void listen(); int fd(); int getMaxconnections(); Loading @@ -147,6 +151,7 @@ namespace netplus { ssl* connect(); void setnonblocking(); void getAddress(std::string &addr); private: Loading @@ -159,7 +164,7 @@ namespace netplus { mbedtls_ctr_drbg_context _SSLCTR_DRBG; mbedtls_ssl_context _SSLCtx; mbedtls_ssl_config _SSLConf; mbedtls_x509_crt _Cacert; mbedtls_pem_context _Cacert; }; class quick : public socket{ Loading