client: always set port even if sockfd already created
[platform/upstream/libwebsockets.git] / lib / client-handshake.c
index 4fb9d4c..42d1a9b 100644 (file)
@@ -213,15 +213,11 @@ lws_client_connect_2(struct lws *wsi)
 #endif
 
 #ifdef LWS_USE_IPV6
-               if (wsi->ipv6) {
-                       sa46.sa6.sin6_port = htons(port);
+               if (wsi->ipv6)
                        wsi->desc.sockfd = socket(AF_INET6, SOCK_STREAM, 0);
-               else
+               else
 #endif
-               {
-                       sa46.sa4.sin_port = htons(port);
                        wsi->desc.sockfd = socket(AF_INET, SOCK_STREAM, 0);
-               }
 
                if (!lws_socket_is_valid(wsi->desc.sockfd)) {
                        lwsl_warn("Unable to open socket\n");
@@ -276,11 +272,15 @@ lws_client_connect_2(struct lws *wsi)
        }
 
 #ifdef LWS_USE_IPV6
-       if (wsi->ipv6)
+       if (wsi->ipv6) {
+               sa46.sa6.sin6_port = htons(port);
                n = sizeof(struct sockaddr_in6);
-       else
+       else
 #endif
+       {
+               sa46.sa4.sin_port = htons(port);
                n = sizeof(struct sockaddr);
+       }
 
        if (connect(wsi->desc.sockfd, (const struct sockaddr *)&sa46, n) == -1 ||
            LWS_ERRNO == LWS_EISCONN) {
@@ -402,8 +402,8 @@ lws_client_connect_2(struct lws *wsi)
 
 oom4:
        /* we're closing, losing some rx is OK */
-       if (wsi->u.hdr.ah)
-               wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
+       lws_header_table_force_to_detachable_state(wsi);
+
        if (wsi->mode == LWSCM_HTTP_CLIENT ||
            wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED ||
            wsi->mode == LWSCM_WSCL_WAITING_CONNECT) {
@@ -454,15 +454,6 @@ lws_client_reset(struct lws **pwsi, int ssl, const char *address, int port,
        }
        wsi->redirects++;
 
-#ifdef LWS_OPENSSL_SUPPORT
-       wsi->use_ssl = ssl;
-#else
-       if (ssl) {
-               lwsl_err("%s: not configured for ssl\n", __func__);
-               return NULL;
-       }
-#endif
-
        p = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN);
        if (p)
                strncpy(origin, p, sizeof(origin) - 1);
@@ -479,11 +470,15 @@ lws_client_reset(struct lws **pwsi, int ssl, const char *address, int port,
        if (p)
                strncpy(method, p, sizeof(iface) - 1);
 
-       lwsl_debug("redirect ads='%s', port=%d, path='%s', ssl = %d\n",
+       lwsl_notice("redirect ads='%s', port=%d, path='%s', ssl = %d\n",
                   address, port, path, ssl);
 
        /* close the connection by hand */
 
+#ifdef LWS_OPENSSL_SUPPORT
+       lws_ssl_close(wsi);
+#endif
+
 #ifdef LWS_USE_LIBUV
        if (LWS_LIBUV_ENABLED(wsi->context)) {
                lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
@@ -502,6 +497,15 @@ lws_client_reset(struct lws **pwsi, int ssl, const char *address, int port,
 
        remove_wsi_socket_from_fds(wsi);
 
+#ifdef LWS_OPENSSL_SUPPORT
+       wsi->use_ssl = ssl;
+#else
+       if (ssl) {
+               lwsl_err("%s: not configured for ssl\n", __func__);
+               return NULL;
+       }
+#endif
+
        wsi->desc.sockfd = LWS_SOCK_INVALID;
        wsi->state = LWSS_CLIENT_UNCONNECTED;
        wsi->protocol = NULL;