Skip to content

Commit

Permalink
hotplace rev.612 grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
princeb612 committed Sep 24, 2024
1 parent f3029b8 commit 92985fc
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# history

* Revision 612
* [changed] get_lasterror

* Revision 611
* [added] basic_stream::vprintf, ansi_string::vprintf
* [changed] network_server, network_session, network_session_manager, dtls_server_socket

* Revision 610
* [changed] create_listener SO_REUSEPORT

* Revision 609

* Revision 607
* [changed] sprintf
* [deprecated] close_listener (see close_socket)
Expand Down
11 changes: 9 additions & 2 deletions sdk/base/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ enum errorcode_t {
* // windows
* ret = GetLastError();
*/
static inline return_t get_lasterror(int code) {
enum errorflag_t {
wsaerror = 1,
};
static inline return_t get_lasterror(int code, int flags = 0) {
return_t ret = errorcode_t::success;
#if defined __linux__
// errno.h 1~133
Expand All @@ -308,7 +311,11 @@ static inline return_t get_lasterror(int code) {
}
}
#elif defined _WIN32 || defined _WIN64
ret = GetLastError();
if (0 == flags) {
ret = GetLastError();
} else if (errorflag_t::wsaerror & flags) {
ret = WSAGetLastError();
}
#endif
return ret;
}
Expand Down
25 changes: 14 additions & 11 deletions sdk/io/system/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ return_t create_socket(socket_t* socket_created, sockaddr_storage_t* sockaddr_cr
snprintf(string_port, RTL_NUMBER_OF(string_port), "%d", port);
ret_function = getaddrinfo(address_pointer, string_port, &hints, &addrinf);
if (0 != ret_function) {
ret = get_lasterror(ret_function);
ret = get_lasterror(ret_function, wsaerror);
__leave2;
}

Expand All @@ -100,7 +100,7 @@ return_t create_socket(socket_t* socket_created, sockaddr_storage_t* sockaddr_cr
} while (nullptr != addrinf_traverse);

if (INVALID_SOCKET == s) {
ret = get_lasterror(s);
ret = get_lasterror(s, wsaerror);
__leave2;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ return_t create_listener(unsigned int size_vector, unsigned int* vector_family,
snprintf(port_value, sizeof(port_value), ("%d"), port);
ret_function = getaddrinfo(nullptr, port_value, &hints, &addrinf);
if (0 != ret_function) {
ret = get_lasterror(ret_function);
ret = get_lasterror(ret_function, wsaerror);
__leave2;
}

Expand All @@ -204,7 +204,7 @@ return_t create_listener(unsigned int size_vector, unsigned int* vector_family,
WSA_FLAG_OVERLAPPED);
#endif
if (INVALID_SOCKET == sock) {
ret = get_lasterror(sock);
ret = get_lasterror(sock, wsaerror);
__leave2;
}

Expand All @@ -222,7 +222,7 @@ return_t create_listener(unsigned int size_vector, unsigned int* vector_family,

ret_function = bind(sock, addrinf_traverse->ai_addr, (int)addrinf_traverse->ai_addrlen);
if (0 != ret_function) {
ret = get_lasterror(ret_function);
ret = get_lasterror(ret_function, wsaerror);
__leave2;
}

Expand All @@ -236,7 +236,7 @@ return_t create_listener(unsigned int size_vector, unsigned int* vector_family,

ret_function = listen(sock, SOMAXCONN);
if (-1 == ret_function) {
ret = get_lasterror(ret_function);
ret = get_lasterror(ret_function, wsaerror);
__leave2;
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@ return_t connect_socket_addr(socket_t sock, const sockaddr* addr, socklen_t addr
if (0 == ret_routine) {
ret = errorcode_t::timeout;
} else if (ret_routine < 0) {
ret = get_lasterror(ret_routine);
ret = get_lasterror(ret_routine, wsaerror);
}
}
}
Expand Down Expand Up @@ -422,7 +422,10 @@ return_t wait_socket(socket_t sock, uint32 milliSeconds, uint32 flags) {
if (0 == ret_select) {
ret = errorcode_t::timeout;
} else if (0 > ret_select) {
ret = get_lasterror(ret_select);
ret = get_lasterror(ret_select, wsaerror);
if (0 == ret) {
printf("-----------------\n");
}
}

return ret;
Expand All @@ -447,7 +450,7 @@ return_t set_sock_nbio(socket_t sock, uint32 nbio_mode) {
ret_fcntl = ioctlsocket(sock, FIONBIO, &nbio_mode);
#endif
if (-1 == ret_fcntl) {
ret = get_lasterror(ret_fcntl);
ret = get_lasterror(ret_fcntl, wsaerror);
}
return ret;
}
Expand Down Expand Up @@ -477,7 +480,7 @@ return_t addr_to_sockaddr(sockaddr_storage_t* storage, const char* address, uint
}

if (-1 == rc) {
ret = get_lasterror(rc);
ret = get_lasterror(rc, wsaerror);
} else if (0 == rc) {
ret = errorcode_t::bad_format;
}
Expand All @@ -492,7 +495,7 @@ return_t typeof_socket(socket_t sock, int& type) {
return_t ret = errorcode_t::success;
socklen_t optlen = sizeof(type);
int rc = getsockopt(sock, SOL_SOCKET, SO_TYPE, (char*)&type, &optlen);
ret = get_lasterror(rc);
ret = get_lasterror(rc, wsaerror);
return ret;
}

Expand Down
8 changes: 1 addition & 7 deletions sdk/net/server/network_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,7 @@ return_t network_server::accept_routine(network_multiplexer_context_t* handle) {
accpt_ctx.mplexer_context = handle;

ret = svr_socket->accept(listen_sock, &accpt_ctx.cli_socket, (struct sockaddr*)&accpt_ctx.client_addr, &accpt_ctx.client_addr_len);
if (INVALID_SOCKET == accpt_ctx.cli_socket) {
#if defined __MINGW32__
/* mingw environments GetLastError () return 0 */
ret = errorcode_t::canceled;
#else
ret = get_lasterror(ret);
#endif
if (errorcode_t::success != ret) {
__leave2;
}

Expand Down
12 changes: 7 additions & 5 deletions sdk/net/tls/tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ return_t transport_layer_security::accept(tls_context_t** handle, socket_t fd) {

set_sock_nbio(fd, 1);

ret = do_accept(context);
do_accept(context);

set_sock_nbio(fd, 0);

Expand Down Expand Up @@ -434,9 +434,10 @@ return_t transport_layer_security::do_connect(socket_t fd, SSL* ssl, uint32 wto,
break;
}
if (1 == rc) {
ret = wait_socket(fd, wto * 1000, flags);
auto test = wait_socket(fd, wto * 1000, flags);
rc = (success == test) ? 1 : -1;
}
} while ((success == ret) && (1 != SSL_is_init_finished(ssl)));
} while ((1 == rc) && (1 != SSL_is_init_finished(ssl)));
} catch (...) {
/*
* openssl-1.0.1i SSL_connect crash
Expand Down Expand Up @@ -563,9 +564,10 @@ return_t transport_layer_security::do_accept(tls_context_t* handle) {
break;
}
if (1 == rc) {
ret = wait_socket(fd, 1 * 1000, flags);
auto test = wait_socket(fd, 1 * 1000, flags);
rc = (success == test) ? 1 : -1;
}
} while ((success == ret) && (1 != SSL_is_init_finished(ssl)));
} while ((1 == rc) && (1 != SSL_is_init_finished(ssl)));

if (rc < 1) {
ret = get_opensslerror(rc);
Expand Down
23 changes: 3 additions & 20 deletions sdk/net/tls/x509cert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ static int set_default_passwd_callback_routine(char* buf, int num, int rwflag, v
return len;
}

return_t x509cert_open(uint32 flag, SSL_CTX** context, const char* cert_file, const char* key_file, const char* password, const char* chain_file,
const char* cacert_file) {
return_t x509cert_open(uint32 flag, SSL_CTX** context, const char* cert_file, const char* key_file, const char* password, const char* chain_file) {
return_t ret = errorcode_t::success;
SSL_CTX* ssl_ctx = nullptr;
SSL* ssl = nullptr;
Expand Down Expand Up @@ -256,21 +255,6 @@ return_t x509cert_open(uint32 flag, SSL_CTX** context, const char* cert_file, co
__leave2;
}
}
// CA certificate
if (cacert_file) {
check = SSL_CTX_load_verify_locations(ssl_ctx, cacert_file, nullptr);
ret = get_opensslerror(check);
if (errorcode_t::success != ret) {
__leave2;
}
{
check = SSL_CTX_set_default_verify_file(ssl_ctx);
ret = get_opensslerror(check);
if (errorcode_t::success != ret) {
__leave2;
}
}
}

// ~ not_before ~ not_after ~
// invalid valid invalid
Expand Down Expand Up @@ -335,9 +319,8 @@ return_t x509cert_open(uint32 flag, SSL_CTX** context, const char* cert_file, co

x509cert::x509cert(uint32 flag) : _ctx(nullptr) { x509cert_open_simple(flag, &_ctx); }

x509cert::x509cert(uint32 flag, const char* cert_file, const char* key_file, const char* password, const char* chain_file, const char* cacert_file)
: _ctx(nullptr) {
x509cert_open(flag, &_ctx, cert_file, key_file, password, chain_file, cacert_file);
x509cert::x509cert(uint32 flag, const char* cert_file, const char* key_file, const char* password, const char* chain_file) : _ctx(nullptr) {
x509cert_open(flag, &_ctx, cert_file, key_file, password, chain_file);
}

x509cert::~x509cert() {
Expand Down
20 changes: 17 additions & 3 deletions sdk/net/tls/x509cert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ return_t x509cert_open_simple(uint32 flag, SSL_CTX** context);
* works good, password parameter useless
*/
return_t x509cert_open(uint32 flag, SSL_CTX** context, const char* cert_file, const char* key_file, const char* password = nullptr,
const char* chain_file = nullptr, const char* cacert_file = nullptr);
const char* chain_file = nullptr);

class x509cert {
public:
Expand All @@ -109,15 +109,29 @@ class x509cert {
* @param const char* password [inopt]
* @param const char* chain_file [inopt]
*/
x509cert(uint32 flag, const char* cert_file, const char* key_file, const char* password = nullptr, const char* chain_file = nullptr,
const char* cacert_file = nullptr);
x509cert(uint32 flag, const char* cert_file, const char* key_file, const char* password = nullptr, const char* chain_file = nullptr);
~x509cert();

/**
* SSL_CTX_set_cipher_list
*/
x509cert& set_cipher_list(const char* list);
/**
* DH_generate_parameters_ex, SSL_CTX_set_tmp_dh
*/
x509cert& set_use_dh(int bits);
/**
* SSL_CTX_set_verify
*/
x509cert& set_verify(int mode);
/**
* SSL_CTX_set_alpn_select_cb
*/
x509cert& enable_alpn_h2(bool enable);

/**
* @brief call openssl api
*/
SSL_CTX* get_ctx();

private:
Expand Down

0 comments on commit 92985fc

Please sign in to comment.