NRF9160 DNS getaddrinfo error

Hello everyone,

I'm trying to connect to my UDP server with only the hostname.

I have validate my connection to my UDP server with an IP static and know i want to use an hostname to find my IP and establish a connection to my UDP server.

My issue occur when i try to get this IP address.

I have try an example to establish a connection between my board and my UDP server. To do that i have use this example : Modem Shell

I have test this command : sock connect -a "address"' -p 1800 -f inet -t dgram  -> All OK

After the first command, i have test this one: sock send -i 0 -d testing -> my UDP server correctly receive the message

So i have study the code of this example to be able to get the IP address of a specific address.

This is my code for the moment : 

int server_init(void)
{
    int err = -EINVAL;

    struct sock_info *socket_info; // = reserve_socket_id();
    if (socket_info == NULL) {
		printk("socket creation failed; MAX SOCKETS exceeeded");
	}

    /* Get address */
    
	err = sock_getaddrinfo(socket_info, 1, 2, "myaddress.net", 1800, 0);
	if (err) {
		printk("err get address : %d \r\n",err);
        //goto connect_error;
	}

    printk("OK\r\n");

	return 0;
}

static int sock_getaddrinfo(
	struct sock_info *socket_info,
	int family,
	int type,
	char *address,
	int port,
	int pdn_cid)
{
	int err;

	
	struct addrinfo hints = {
		.ai_family = 1,
		.ai_socktype = 2,
	};
		
    char pdn_serv[12];
	char *service = NULL;

	

	err = getaddrinfo(address, service, &hints, &socket_info->addrinfo);
		
    if (err) {
			if (err == DNS_EAI_SYSTEM) {
				printk("getaddrinfo() failed, err %d errno %d", err, errno);
			} else {
				printk("getaddrinfo() failed, err %d", err);
			}
			return -EADDRNOTAVAIL;
		}

	/* Set port to address info */
		
	((struct sockaddr_in *)socket_info->addrinfo->ai_addr)->sin_port =htons(1800);
		
	
	return 0;
}

My code compile with no problem and after a flash when i debug initialisation of my module, i have this : 

1) socket creation failed; MAX SOCKETS exceeeded

2) getaddrinfo() failed, err -11 errno 115

3) err get address : -125"

 I understand the first error, it's because my socket_info is NULL, that not a problem because for the moment i need only 1 socket

The line where the problem come from is : err = getaddrinfo(address, service, &hints, &socket_info->addrinfo);

And i have no idea how to solve this problem,

Do you have advice for me ?

 Best regards,

Lam

  • Hi Lamdev,

    Looks "reserve_socket_id()" also does some initialization job for socke_info creation. Is there any special reason you comment it out?

    Best regard,

    Charlie

  • Hi,

    Thanks for your answer,

    I don't have implement this function because i'm thinking i don't need to especially reserve a socket because i only use one.

    Now, i have implement this function and i don't have any error and find an IP address for this hostname : host.slb.com

    I don't know tis hostname and this not match to the hostname i configure at the start.

    static struct sock_info *reserve_socket_id(void)
    {
    	struct sock_info *socket_info = NULL;
    	int socket_id = 0;
    
    	while (socket_id < MAX_SOCKETS) {
    		if (!sockets[socket_id].in_use) {
    			socket_info = &(sockets[socket_id]);
    			//sock_info_clear(socket_info);
    
                freeaddrinfo(socket_info->addrinfo);
                memset(socket_info, 0, sizeof(struct sock_info));
                socket_info->id = -1;
    	        socket_info->fd = -1;
    	        socket_info->log_receive_data = true;
    	        socket_info->recv_data_len_expected = -1;
    	        socket_info->recv_print_format = SOCK_RECV_PRINT_FORMAT_STR;
    
    
    			socket_info->id = socket_id;
    			socket_info->send_info.parent = socket_info;
    			break;
    		}
    		socket_id++;
    	}
    	return socket_info;
    }
    

    Do you have any advice ?

    Best regards,

    Lam

  • Hi Lam,

    I am not sure if I understand your questions correctly.

    Do you mean you do not get an error report but can still not get IP from the host host.slb.com?

    I tried to ping host.slb.com on my PC and it can not be found. Did you try another hostname like google.com?

    Best regards,

    Charlie

  • Hi Charlie,

    Yes the code running without error but the IP address that i receive not match on the address IP of my hostname.

    host.slb.com is not my hostname but correspond to the ip adress that i get from the code but it's a wrong IP address.

    Do i have something to configure to enable resolution of a hostname other than implement my code ?

    My UDP server is : gate04.telemesure.net

    Best regards,

    Lam 

  • Hi Lam,

    I added the following codes(commented lines) in NCS240 nrf\samples\nrf9160\upd\src\main.c.

    // #define DOMAIN_NAME "gate04.telemesure.net"
    
    static int server_init(void)
    {
    	struct sockaddr_in *server4 = ((struct sockaddr_in *)&host_addr);
    
            // int err;
            // struct sockaddr_in *ipaddr;
            // uint8_t *ip;
            // struct addrinfo *result;
            // struct addrinfo hints = {
            //         .ai_family = AF_INET,      /* Allow IPv4 or IPv6 */
            //         .ai_socktype = SOCK_DGRAM, /* Datagram socket */
            //         .ai_protocol = 0          /* Any protocol */
            // };
            // char *service = NULL;
            // err = getaddrinfo(DOMAIN_NAME, service, &hints, &result);
            // if (err != 0) {
            //         printk("getaddrinfo, error: %d\n", err);
            // }
            // ipaddr = (struct sockaddr_in *)result->ai_addr;
    	// ip = (uint8_t *)&(ipaddr->sin_addr.s_addr);
    	// printk("Server IP address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
    
    	server4->sin_family = AF_INET;
    	server4->sin_port = htons(CONFIG_UDP_SERVER_PORT);
    
    	inet_pton(AF_INET, CONFIG_UDP_SERVER_ADDRESS_STATIC,
    		  &server4->sin_addr);
    
    	return 0;
    }

    and get the IP address 54.36.99.9 which is the same as what I get from PC ping command.

    2023-06-05T16:51:48.374Z DEBUG modem << *** Booting Zephyr OS build v3.3.99-ncs1 ***
    2023-06-05T16:51:48.380Z DEBUG modem << UDP sample has started
    2023-06-05T16:51:51.377Z DEBUG modem << LTE cell changed: Cell ID: 51546114, Tracking area: 30601
    2023-06-05T16:51:51.682Z DEBUG modem << RRC mode: Connected
    2023-06-05T16:51:52.748Z DEBUG modem << Network registration status: Connected - home network
    2023-06-05T16:51:52.764Z DEBUG modem << PSM parameter update: TAU: 3600, Active time: 0
    2023-06-05T16:51:53.028Z DEBUG modem << Server IP address: 54.36.99.9

    Remember to set CONFIG_SERIAL=y in prj.conf in order to get log printout.

    Best regards,

    Charlie

Related