how to close secure websocket properly

Discussion to talk about software related topics only.
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

Re: how to close secure websocket properly

Post by RebootExpert »

Thanks Jon, I set m_fd_tcp to public and rebuild system files.

My thought to close ssl_fd first is incorrect. close the ssl_fd before close ws_fd in the same task doesn't solve the problem in 2.9.5, but work fine in 2.9.3
Interestingly, if I close the ssl_fd in a different task, application work completely fine. (false in 2.9.5, true in 2.9.3)

One thing I notice by using your code, whenever the return ws is null, I am able to close the ws_fd. If the ws is not null, and it's a secure ws, it's always fail to close the ws.

task to monitor ws fd

Code: Select all

void wsTask(void *pd)
{
	char msg[ETHER_BUFFER_SIZE];
    fd_set read_fds;
    fd_set error_fds;

    FD_ZERO( &read_fds );
    FD_ZERO( &error_fds );
	printf("------------wsTask running----------------\r\n");
	while(true)
	{
		OSTimeDly(TICKS_PER_SECOND/8);
		for(int i =0; i < ws_vector.size(); i++)
		{
			FD_SET(ws_vector[i], &read_fds);
			FD_SET(ws_vector[i], &error_fds);
			select(1, &read_fds, NULL, &error_fds, 0);
			if (FD_ISSET(ws_vector[i], &error_fds))
			{
				printf("webpage is closed by user, removed ws = %d from ws_vector\r\n", ws_vector[i]);
				int sslFd = 0;
				NB::WebSocket* ws = NB::WebSocket::GetWebSocketRecord( ws_vector[i] );
				if( ws != nullptr )
				{
					sslFd = ws->m_fd_tcp;
					printf("ws is not null\r\n");
//					if(IsSSLfd(sslFd))
//					{
//						printf("sslFd = %d\r\n", sslFd);
//						close(sslFd);
//						printf("close sslFd succeed\r\n");
//					}
				}
				else
					printf("ws is null\r\n");
				close(ws_vector[i]);
				printf("close ws= %d\r\n\r\n", ws_vector[i]);
				ws_vector[i] = -1;
				ws_vector.erase(ws_vector.begin() + i);
				i--;
				FD_ZERO( &read_fds );
				FD_ZERO( &error_fds );
				continue;
			}
			...
		}
	}	
}
2.9.5 debug output
added ws=38 to ws_vector, associate sock = 25, sock is not ssl fd //non secure webpage open
webpage is closed by user, removed ws = 38 from ws_vector
ws is not null
close ws= 38

added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is null
close ws= 39

added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is null
close ws= 39

added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is not null
//fail to close. no print out
Last edited by RebootExpert on Wed Mar 17, 2021 9:36 am, edited 1 time in total.
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

Re: how to close secure websocket properly

Post by RebootExpert »

I switch back to 2.9.3 since it works fine by using Jon code, the only thing I just need to change m_fd_tcp to public in the WebSocket class and rebuild the system file.
User avatar
Jon
Posts: 79
Joined: Mon Feb 05, 2018 10:54 am

Re: how to close secure websocket properly

Post by Jon »

Hi RebootExpert,

Thank you very much for taking the time to run that and report what you found. I'll test this in 2.9.5 and see if I can't figure out why it's having issues. It sounds like we might have some additional changes to make. In the meantime, I'm glad to hear that you've found a solution for your current issue. Please let us know if you need anything else.

Kind Regards,
Jon
Post Reply