Error 116 Occurs after 12 hours of TLS socket connect()

Hello DevZone community,

I have been using the TLS socket in my application, which is executed whenever we need to call API or send logs.
However, I'm encountering an error after exactly 12 hours of operation. The error code returned is 116.

int mc_api_call(char* pReqMsg, int Msglen, char* pHost, int HostLen, char* pRcvBuf, int RcvBuflen)
{
	int err;
	int fd;
	int ret = -1;
	int sendbyte, recvbyte;
	size_t off;
	struct addrinfo *res;
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM,
	};

	k_sem_take(&mc_enable, K_FOREVER); 

	err = getaddrinfo(pHost, NULL, &hints, &res);
	if (err) {
		MLOG_ERROR("getaddrinfo() failed, err %d", errno);

		k_sem_give(&mc_enable);		

		return ret;
	}
#if 0
	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
#else
	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
#endif

#if 0
	fd = socket(AF_INET, SOCK_STREAM, res->ai_protocol);
#else
	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
#endif
	if (fd == -1) {
		// LOG_INF("Failed to open socket!");
		goto clean_up;
	}
	err = tls_setup(fd, pHost, HostLen);
	if (err) {
		goto clean_up;
	}

	MLOG_DEBUG("Connecting to %s", pHost);
	err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
	if (err) {
		MLOG_ERROR("connect() failed, err: %d", errno);
		goto clean_up;
	}

	off = 0;
	do {
		sendbyte = send(fd, pReqMsg+off, Msglen - off, 0);
		if (sendbyte < 0) {
			MLOG_ERROR("send() failed, err %d", errno);
			goto clean_up;
		}
		off += sendbyte;
	} while (off < sendbyte);
	MLOG_DEBUG("Send %d bytes", off);
	off = 0;

	memset(pRcvBuf, 0x00, RcvBuflen);
#if 0
	do{
		recvbyte = recv(fd, pRcvBuf+off, RcvBuflen - off, MSG_DONTWAIT);
		MLOG_DEBUG("Received %d bytes", recvbyte);
		if (recvbyte == 0) {
			break;
		}
		cnt++;
		if(recvbyte < 0)
		{
			if(cnt > 10)
			{
				MLOG_WARNING("%s", "Receive time out");
				break;
			}
			else
			{
				k_sleep(K_MSEC(1000));
				continue;
			}
		}
		else
		{
			cnt = 0;
		}
		off += recvbyte;
	} while (recvbyte < RcvBuflen /* peer closed connection */);
#else
	do {
		recvbyte = recv(fd, pRcvBuf+off, RcvBuflen - off, 0);
		MLOG_DEBUG("Received %d bytes", recvbyte);
		if (recvbyte < 0) {
			break;
		}
		off += recvbyte;
	} while (recvbyte != 0 /* peer closed connection */);
#endif

	MLOG_DEBUG("Received end %d bytes", off);
	ret = off;

clean_up:
	freeaddrinfo(res);
	(void)close(fd);

	k_sem_give(&mc_enable);
	return ret;
}


My Environment

NCS - SDK 1.9.1
Device - nrf9160 customized one

Error log

[2023/05/17 23:42:44] [my_thread] [ERROR] connect() failed, err: 116

I'm seeking assistance in understanding the possible causes of this error and any recommended solutions or workarounds to resolve it.
Any insights or experiences shared would be greatly appreciated.

Thank you in advance for your help!

Parents Reply Children
No Data
Related