Issue with multiple HTTPS(TLS) sockets to the same server

Board: nrf7002

nrfConnect SDK 2.4.0 or 2.4.99

IDE: VS code

I’m working on implementing SignalR communication protocol, It uses SSE(Server-Sent Events) communication, so once connected, one socket needs to remain open to receive new data, and we also need to send other messages over an additional socket.

I’m getting a fault when using a second socket, I set it up in the same way as I made the first one. I am modifying the solution found https://devzone.nordicsemi.com/f/nordic-q-a/100826/nrf7002dk-and-tls-sockets--7100-error/433368 to do this. It always faults weather the first socket gets used or not.

Here is the code to create the sockets (changes nslookup to google from my domain):

int sock;
int sock2;
int err;
struct sockaddr_in *sa;
struct addrinfo *rp;

struct sockaddr_in *sa2;
struct addrinfo *rp2;

nslookup("google.com", &rp);
print_addrinfo_results(&rp);

nslookup("google.com", &rp2);
print_addrinfo_results(&rp2);

printk("Connecting to HTTP Server:\n");

// Load in the TLS cert for HTTPS
tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
ca_certificate, sizeof(ca_certificate));


// Create Socket
sock = socket(rp->ai_family, rp->ai_socktype, IPPROTO_TLS_1_2); // IPPROTO_TCP //IPPROTO_TLS_1_2
if (sock < 0)
{
printk("Error creating socket\n");
return (-1);
}
// TLS settings
sec_tag_t sec_tag_opt[] = {
CA_CERTIFICATE_TAG,
};
err = setsockopt(sock, SOL_TLS, TLS_SEC_TAG_LIST,
sec_tag_opt, sizeof(sec_tag_opt));
if (err < 0)
{
LOG_ERR("Failed to set TLS security TAG list. Err: %d", errno);
(void)close(sock);
return -errno;
}

err = setsockopt(sock, SOL_TLS, TLS_HOSTNAME,
HTTP_HOST, sizeof(HTTP_HOST));
if (err < 0)
{
LOG_ERR("Failed to set TLS_HOSTNAME option. Err: %d", errno);
(void)close(sock);
return -errno;
}

// Iterate through until we get a successful connection
for (; rp != NULL; rp = rp->ai_next)
{
if (rp->ai_addr->sa_family == AF_INET)
{
// IPv4 Address
sa = (struct sockaddr_in *)rp->ai_addr;
sa->sin_port = htons(443);// this should be 443 (HTTPS port number)
connect(sock, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
if (sock > 0)
break;
}
}

// Create Socket 2
sock2 = socket(rp2->ai_family, rp2->ai_socktype, IPPROTO_TLS_1_2); // IPPROTO_TCP //IPPROTO_TLS_1_2
if (sock2 < 0)
{
printk("Error creating socket\n");
return (-1);
}
sec_tag_t sec_tag_opt2[] = {
CA_CERTIFICATE_TAG,
};
err = setsockopt(sock2, SOL_TLS, TLS_SEC_TAG_LIST,
sec_tag_opt2, sizeof(sec_tag_opt2));
if (err < 0)
{
LOG_ERR("Failed to set TLS security TAG list. Err: %d", errno);
(void)close(sock2);
return -errno;
}

err = setsockopt(sock2, SOL_TLS, TLS_HOSTNAME,
HTTP_HOST, sizeof(HTTP_HOST));
if (err < 0)
{
LOG_ERR("Failed to set TLS_HOSTNAME option. Err: %d", errno);
(void)close(sock2);
return -errno;
}

// Iterate through until we get a successful connection
for (; rp2 != NULL; rp2 = rp2->ai_next)
{
if (rp2->ai_addr->sa_family == AF_INET)
{
// IPv4 Address
sa2 = (struct sockaddr_in *)rp2->ai_addr;
sa2->sin_port = htons(443);// this should be 443 (HTTPS port number)
connect(sock2, (struct sockaddr *)sa2, sizeof(struct sockaddr_in));
if (sock2 > 0)
break;
}
}

Here is the error log:

<err> os: ***** MPU FAULT *****

[00:00:35.620,819] <err> os: Data Access Violation

[00:00:35.620,819] <err> os: MMFAR Address: 0x0

[00:00:35.620,819] <err> os: r0/a1: 0x00000000 r1/a2: 0x00000000 r2/a3: 0x00000000

[00:00:35.620,849] <err> os: r3/a4: 0x00000000 r12/ip: 0x00000413 r14/lr: 0x00004140

[00:00:35.620,849] <err> os: xpsr: 0x29000000

[00:00:35.620,880] <err> os: Faulting instruction address (r15/pc): 0x0004e620

[00:00:35.620,910] <err> os: >>> ZEPHYR FATAL ERROR 19: Unknown error on CPU 0

[00:00:35.620,941] <err> os: Current thread: 0x200049c8 (main)

......

[00:00:37.542,816] <err> fatal_error: Resetting system

Thanks for your help

  • Hi,

     

    Kyle1 said:
    The other issues I see is the body of the response coming back has been stripped out, I thought this was due to terminating the string at \r\n\r\n , but I removed that and still does not appear to be getting anything other than just the header data.

    You can take away the strstr() logic, and print the recv_buf directly to see the full content.

     

    Kyle1 said:

    This solution appears to work when WiFi gets connected, however a lot of the time it is not, I am seeing this in the logs, where it fails to connect to Wifi.


    [00:00:01.637,359] <inf> app: Registering IPv4 events
    [00:00:05.637,481] <err> app: Connecting to Wi-Fi failed. error: -1
    [00:00:05.637,512] <inf> app: Wait for Wi-fi connection

    Are you far away from the access point? If you run the wifi/shell sample, and do a scan there, what's the reported RSSI of your AP?

     

    Kind regards,

    Håkon

  • Sorry for the late reply, I just wanted to ensure I did not have any other issues.


    It turns out i needed to add a a 1 second delay before starting the WiFi in main, and the other issue was because I was requesting something with a large payload too big to handle, not needed for the communication I was implementing.

    Thanks for the help

  • Happy to hear that you got things working. Hope you have a wonderful day!

     

    Kind regards,

    Håkon

Related