<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/100377/nrf9160-dns-getaddrinfo-error</link><description>Hello everyone, 
 I&amp;#39;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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 13 Jun 2023 08:58:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/100377/nrf9160-dns-getaddrinfo-error" /><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/430663?ContentTypeID=1</link><pubDate>Tue, 13 Jun 2023 08:58:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d93cba3-320e-493d-b100-dfae45faafb6</guid><dc:creator>Lamdev</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Thanks for your help,&lt;/p&gt;
&lt;p&gt;I found this solution that works great and come from&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.3.0/nrf/samples/nrf9160/modem_shell/README.html" rel="noopener noreferrer" target="_blank"&gt;Modem Shell example&lt;/a&gt;&amp;nbsp;and more precisely in in sock folder.&lt;/p&gt;
&lt;p&gt;I share solution maybe this can help someone.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;int getIP_fromHost(void){
    
    // struct addrinfo res;
    // struct addrinfo hint;

    int err = -EINVAL;

    struct sock_info *socket_info = reserve_socket_id();
    if (socket_info == NULL) {
		printk(&amp;quot;socket creation failed; MAX SOCKETS exceeeded&amp;quot;);
        return 1;
	}

    /* Get address */
	err = sock_getaddrinfo(socket_info, 1, 2, &amp;quot;your_udp_server&amp;quot;, 1800, 0);
	if (err) {
		printk(&amp;quot;err get address : %d \r\n&amp;quot;,err);
        return 1;
	}

    char addr_str[INET_ADDRSTRLEN];
    for (struct addrinfo *ai = socket_info-&amp;gt;addrinfo; ai != NULL; ai = ai-&amp;gt;ai_next) {
        zsock_inet_ntop(ai-&amp;gt;ai_family, &amp;amp;((struct sockaddr_in *)ai-&amp;gt;ai_addr)-&amp;gt;sin_addr, addr_str, sizeof(addr_str));
       	printk(&amp;quot;IP address: %s\n&amp;quot;, addr_str);
    }

    strcpy(ip_UDPServer,addr_str);
    
    // Clean up.
    freeaddrinfo(socket_info-&amp;gt;addrinfo); 

    return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static struct sock_info *reserve_socket_id(void)
{
	struct sock_info *socket_info = NULL;
	int socket_id = 0;

	while (socket_id &amp;lt; MAX_SOCKETS) {
		if (!sockets[socket_id].in_use) {
			socket_info = &amp;amp;(sockets[socket_id]);
			//sock_info_clear(socket_info);

            //freeaddrinfo(socket_info-&amp;gt;addrinfo);
            memset(socket_info, 0, sizeof(struct sock_info));
            socket_info-&amp;gt;id = -1;
	        socket_info-&amp;gt;fd = -1;
	        socket_info-&amp;gt;log_receive_data = true;
	        socket_info-&amp;gt;recv_data_len_expected = -1;
	        socket_info-&amp;gt;recv_print_format = SOCK_RECV_PRINT_FORMAT_STR;


			socket_info-&amp;gt;id = socket_id;
			socket_info-&amp;gt;send_info.parent = socket_info;
			break;
		}
		socket_id++;
	}
	return socket_info;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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 = AF_INET,       //1
		.ai_socktype = SOCK_DGRAM,  //2
        .ai_next = NULL,
        .ai_addr = NULL,
        .ai_protocol = 0   //any protocol
	};
		
    //char pdn_serv[12];
	//char *service = NULL;

	

	err = getaddrinfo(address, NULL, &amp;amp;hints, &amp;amp;socket_info-&amp;gt;addrinfo);
		
    if (err) {
			if (err == DNS_EAI_SYSTEM) {
				printk(&amp;quot;getaddrinfo() failed, err %d errno %d&amp;quot;, err, errno);
			} else {
				printk(&amp;quot;getaddrinfo() failed, err %d&amp;quot;, err);
			}
			return -EADDRNOTAVAIL;
		}

	/* Set port to address info */
		
	((struct sockaddr_in *)socket_info-&amp;gt;addrinfo-&amp;gt;ai_addr)-&amp;gt;sin_port =htons(1800);
		
	return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best reagrds,&lt;/p&gt;
&lt;p&gt;Lam&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/429397?ContentTypeID=1</link><pubDate>Mon, 05 Jun 2023 16:58:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:853ee43b-db8f-4a50-a215-a06889d6d12c</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Lam,&lt;/p&gt;
&lt;p&gt;I added the following codes(commented lines) in NCS240 nrf\samples\nrf9160\upd\src\main.c.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;// #define DOMAIN_NAME &amp;quot;gate04.telemesure.net&amp;quot;

static int server_init(void)
{
	struct sockaddr_in *server4 = ((struct sockaddr_in *)&amp;amp;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, &amp;amp;hints, &amp;amp;result);
        // if (err != 0) {
        //         printk(&amp;quot;getaddrinfo, error: %d\n&amp;quot;, err);
        // }
        // ipaddr = (struct sockaddr_in *)result-&amp;gt;ai_addr;
	// ip = (uint8_t *)&amp;amp;(ipaddr-&amp;gt;sin_addr.s_addr);
	// printk(&amp;quot;Server IP address: %d.%d.%d.%d\n&amp;quot;, ip[0], ip[1], ip[2], ip[3]);

	server4-&amp;gt;sin_family = AF_INET;
	server4-&amp;gt;sin_port = htons(CONFIG_UDP_SERVER_PORT);

	inet_pton(AF_INET, CONFIG_UDP_SERVER_ADDRESS_STATIC,
		  &amp;amp;server4-&amp;gt;sin_addr);

	return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and get the IP address&amp;nbsp;54.36.99.9 which is the same as what I get from PC ping command.&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;2023-06-05T16:51:48.374Z DEBUG modem &amp;lt;&amp;lt; *** Booting Zephyr OS build v3.3.99-ncs1 ***
2023-06-05T16:51:48.380Z DEBUG modem &amp;lt;&amp;lt; UDP sample has started
2023-06-05T16:51:51.377Z DEBUG modem &amp;lt;&amp;lt; LTE cell changed: Cell ID: 51546114, Tracking area: 30601
2023-06-05T16:51:51.682Z DEBUG modem &amp;lt;&amp;lt; RRC mode: Connected
2023-06-05T16:51:52.748Z DEBUG modem &amp;lt;&amp;lt; Network registration status: Connected - home network
2023-06-05T16:51:52.764Z DEBUG modem &amp;lt;&amp;lt; PSM parameter update: TAU: 3600, Active time: 0
2023-06-05T16:51:53.028Z DEBUG modem &amp;lt;&amp;lt; Server IP address: 54.36.99.9&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Remember to set&amp;nbsp;&lt;span&gt;CONFIG_SERIAL&lt;/span&gt;&lt;span&gt;=y in prj.conf in order to get log printout.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/429191?ContentTypeID=1</link><pubDate>Mon, 05 Jun 2023 07:50:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b7501c3-3d7a-4389-8822-90b9ba7e969a</guid><dc:creator>Lamdev</dc:creator><description>&lt;p&gt;Hi Charlie,&lt;/p&gt;
&lt;p&gt;Yes the code running without error but the IP address that i receive not match on the address IP of my hostname.&lt;/p&gt;
&lt;p&gt;host.slb.com is not my hostname but correspond to the ip adress that i get from the code but it&amp;#39;s a wrong IP address.&lt;/p&gt;
&lt;p&gt;Do i have something to configure to enable resolution of a hostname other than implement my code ?&lt;/p&gt;
&lt;p&gt;My UDP server is :&amp;nbsp;gate04.telemesure.net&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Lam&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/429182?ContentTypeID=1</link><pubDate>Mon, 05 Jun 2023 07:35:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd0527a7-4829-4cd9-b5b3-26f18d886af1</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Lam,&lt;/p&gt;
&lt;p&gt;I am not sure if I understand your questions correctly.&lt;/p&gt;
&lt;p&gt;Do you mean you do not get an error report but can still not get IP from the host &lt;span&gt;host.slb.com&lt;/span&gt;?&lt;/p&gt;
&lt;p&gt;I tried to ping&amp;nbsp;&lt;span&gt;host.slb.com on my PC and it can not be found. Did you try another hostname like google.com?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Charlie&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/429006?ContentTypeID=1</link><pubDate>Fri, 02 Jun 2023 12:25:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dfd46de8-8ace-43a4-90fd-78808d05e950</guid><dc:creator>Lamdev</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks for your answer,&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have implement this function because i&amp;#39;m thinking i don&amp;#39;t need to especially reserve a socket because i only use one.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Now, i have implement this function and i don&amp;#39;t have any error and find an IP address for this hostname :&amp;nbsp;host.slb.com&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know tis hostname and this not match to the hostname i configure at the start.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static struct sock_info *reserve_socket_id(void)
{
	struct sock_info *socket_info = NULL;
	int socket_id = 0;

	while (socket_id &amp;lt; MAX_SOCKETS) {
		if (!sockets[socket_id].in_use) {
			socket_info = &amp;amp;(sockets[socket_id]);
			//sock_info_clear(socket_info);

            freeaddrinfo(socket_info-&amp;gt;addrinfo);
            memset(socket_info, 0, sizeof(struct sock_info));
            socket_info-&amp;gt;id = -1;
	        socket_info-&amp;gt;fd = -1;
	        socket_info-&amp;gt;log_receive_data = true;
	        socket_info-&amp;gt;recv_data_len_expected = -1;
	        socket_info-&amp;gt;recv_print_format = SOCK_RECV_PRINT_FORMAT_STR;


			socket_info-&amp;gt;id = socket_id;
			socket_info-&amp;gt;send_info.parent = socket_info;
			break;
		}
		socket_id++;
	}
	return socket_info;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Do you have any advice ?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Lam&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF9160 DNS getaddrinfo error</title><link>https://devzone.nordicsemi.com/thread/428944?ContentTypeID=1</link><pubDate>Fri, 02 Jun 2023 09:12:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2178686b-d2b4-4d92-a8f8-adfc17c5082b</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Lamdev,&lt;/p&gt;
&lt;p&gt;Looks &amp;quot;reserve_socket_id()&amp;quot; also does some initialization job for socke_info creation. Is there any special reason you comment it out?&lt;/p&gt;
&lt;p&gt;Best regard,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>