From 699d861e0003b48969273696d5000072986699e8 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 6 Aug 2020 06:49:30 -0300 Subject: [PATCH] Still refactoring network modules. --- src/core/host.c | 30 ++++++++---------------------- src/include/networking.h | 8 ++++++++ src/network_modules/default/main.c | 17 +---------------- src/network_modules/select.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/core/host.c b/src/core/host.c index e0f20a1..f3935c4 100644 --- a/src/core/host.c +++ b/src/core/host.c @@ -62,6 +62,7 @@ #include #include #include +#include /** * @brief Called from timer to attempt an automatic reconnection. @@ -368,6 +369,7 @@ LIB3270_EXPORT int lib3270_set_url(H3270 *h, const char *n) if(!n) return errno = ENOENT; +/* static const struct _sch { char ssl; @@ -386,37 +388,21 @@ LIB3270_EXPORT int lib3270_set_url(H3270 *h, const char *n) { 0, "telnet://", "telnet" } }; - +*/ lib3270_autoptr(char) str = strdup(n); - char * hostname = str; - const char * srvc = "telnet"; + char * hostname = lib3270_set_network_module_from_url(h,str); + const char * srvc; char * ptr; char * query = ""; int f; trace("%s(%s)",__FUNCTION__,str); -#ifdef HAVE_LIBSSLx - h->ssl.enabled = 0; -#endif // HAVE_LIBSSL - - for(f=0;f < sizeof(sch)/sizeof(sch[0]);f++) - { - size_t sz = strlen(sch[f].text); - if(!strncasecmp(hostname,sch[f].text,sz)) - { -#ifdef HAVE_LIBSSLx - h->ssl.enabled = sch[f].ssl; -#endif // HAVE_LIBSSL - srvc = sch[f].srvc; - hostname += sz; - break; - } - } - - if(!*hostname) + if(!(hostname && *hostname)) return 0; + srvc = h->network.module->service; + ptr = strchr(hostname,':'); if(ptr) { diff --git a/src/include/networking.h b/src/include/networking.h index 762499a..3143f7e 100644 --- a/src/include/networking.h +++ b/src/include/networking.h @@ -190,6 +190,14 @@ LIB3270_INTERNAL int lib3270_network_send_failed(H3270 *hSession); /** + * @breif Select the network context from URL. + * + * @return Pointer to the hostname or NULL if failed (sets errno). + * + */ + LIB3270_INTERNAL const char * lib3270_set_network_module_from_url(H3270 *hSession, const char *url); + + /** * @brief Select the default (unsecure) network context. * * @param hSession TN3270 Session handle. diff --git a/src/network_modules/default/main.c b/src/network_modules/default/main.c index d395bd9..fff9e91 100644 --- a/src/network_modules/default/main.c +++ b/src/network_modules/default/main.c @@ -87,8 +87,6 @@ } static int unsecure_network_getsockname(const H3270 *hSession, struct sockaddr *addr, socklen_t *addrlen) { - if(hSession->network.context->sock < 0) - return -(errno = ENOTCONN); return getsockname(hSession->network.context->sock, addr, addrlen); } @@ -160,27 +158,14 @@ static int unsecure_network_is_connected(const H3270 *hSession) { } static int unsecure_network_setsockopt(H3270 *hSession, int level, int optname, const void *optval, size_t optlen) { - - if(hSession->network.context->sock < 0) { - errno = ENOTCONN; - return -1; - } - return setsockopt(hSession->network.context->sock, level, optname, optval, optlen); - } static int unsecure_network_getsockopt(H3270 *hSession, int level, int optname, void *optval, socklen_t *optlen) { - - if(hSession->network.context->sock < 0) { - errno = ENOTCONN; - return -1; - } - return getsockopt(hSession->network.context->sock, level, optname, optval, optlen); } -static int unsecure_network_init(H3270 GNUC_UNUSED(*hSession), LIB3270_NETWORK_STATE GNUC_UNUSED(*state)) { +static int unsecure_network_init(H3270 GNUC_UNUSED(*hSession)) { return 0; } diff --git a/src/network_modules/select.c b/src/network_modules/select.c index 08d7384..f54ba85 100644 --- a/src/network_modules/select.c +++ b/src/network_modules/select.c @@ -35,4 +35,34 @@ #include #include #include + #include #include + #include + + /*--[ Implement ]------------------------------------------------------------------------------------*/ + + const char * lib3270_set_network_module_from_url(H3270 *hSession, const char *url) { + + static const struct { + const char *scheme; ///< @brief URL scheme for module. + void (*activate)(H3270 *hSession); ///< @brief Selection method. + } modules[] = { + + { "tn3270://", lib3270_set_default_network_module }, + + }; + + size_t ix; + + for(ix=0;ix < (sizeof(modules)/sizeof(modules[0])); ix++) { + + size_t len = strlen(modules[ix].scheme); + if(!strncasecmp(url,modules[ix].scheme,len)) { + modules[ix].activate(hSession); + return url+len; + } + + } + + return NULL; + } -- libgit2 0.21.2