<?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>EPERM error on setsockopt() on UDP socket</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/54952/eperm-error-on-setsockopt-on-udp-socket</link><description>Hi,, I&amp;#39;m trying to set a recv timeout on a UDP socket (coap) using setsockopt() and getting an EPERM error. I&amp;#39;ve tried the setsockopt() call both before and after the (successful) connect(), with the same result. Am I doing this right? Or is there a problem</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 30 Nov 2019 16:10:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/54952/eperm-error-on-setsockopt-on-udp-socket" /><item><title>RE: EPERM error on setsockopt() on UDP socket</title><link>https://devzone.nordicsemi.com/thread/222895?ContentTypeID=1</link><pubDate>Sat, 30 Nov 2019 16:10:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9fdf16d3-fac4-4a31-bddb-164f5c1aecc6</guid><dc:creator>krog1</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;H&amp;aring;kon,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks for the response. I copied the ntp sample into my tree, and added the setsockopt() code into the blocking_connect() function. I get the same error, although I realized it&amp;#39;s actually EINVAL, not EPERM (I was looking at the return code, not errno).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
{
	int err;

	do {
		err = connect(fd, local_addr, len);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

    // BEGIN CLB INSERT

    // Set the timeout here for responses.
    struct timeval tv;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*) &amp;amp;tv, sizeof(tv));
    if (err &amp;lt; 0) {
        printk(&amp;quot;setsockopt() failed, err = %d, errno = %d.\n&amp;quot;, err, errno);
    } else {
        printk(&amp;quot;setsockopt() succeeded.\n&amp;quot;);
    }

    // END CLB INSERT

	return err;
}
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Output:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS v1.14.99-ncs1 *****
getaddrinfo err: 0
client_fd: 2
bind err: 0
setsockopt() failed, err = -1, errno = 22.
connect err: 22
sendto ret: 48
Flags: 36
Stratum: 2
Poll: 3
Precision: 4294967273
Root delay: 788725760
Root dispersion: 2416246784
Ref ID: 2496412354
Ref timestamp: 37841176532514736270
Orig Timestamp: 00
Receive timestamp: 37841180551702783404
Transmit: 37841180551703091902
Unix time: 1575129255
Time: Sat Nov 30 15:54:15 2019
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The only difference I can see at this point is the version; I&amp;#39;m still at 1.14.99-ncs1. This might be the issue, but unfortunately, I&amp;#39;m not at a point where I can upgrade to check this. For now, I&amp;#39;ll change to NOWAIT polling with sleep for the recv, and revisit the setsockopt() error when I upgrade. I&amp;#39;ll post a reply to this when I confirm that the version is the issue.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Chris.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: EPERM error on setsockopt() on UDP socket</title><link>https://devzone.nordicsemi.com/thread/222566?ContentTypeID=1</link><pubDate>Thu, 28 Nov 2019 10:02:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fcc9a050-afd1-4b75-847a-5ea4214f140f</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It worked when injecting your code into this ntp sample:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/Rallare/fw-nrfconnect-nrf/blob/nrf9160_samples/samples/nrf9160/ntp/src/main.c"&gt;https://github.com/Rallare/fw-nrfconnect-nrf/blob/nrf9160_samples/samples/nrf9160/ntp/src/main.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS build v2.0.99-ncs1-rc1-11-g9be97c63368a *****
getaddrinfo err: 0
client_fd: 3
setsockopt() succeeded.bind err: 0
sendto ret: 48
Flags: 36
Stratum: 2
Poll: 3
Precision: 4294967273
Root delay: 788725760
Root dispersion: 721813504
Ref ID: 348928706
Ref timestamp: 3783923305111899737
Orig Timestamp: 00
Receive timestamp: 37839236151286592714
Transmit: 37839236151286882532
Unix time: 1574934815
Time: Thu Nov 28 09:53:35 2019
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[quote user=""][/quote]&lt;/p&gt;
&lt;p&gt;for (addr = result; addr != NULL; addr = addr-&amp;gt;ai_next) {&lt;/p&gt;
&lt;p&gt;sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);&lt;br /&gt; if (sock &amp;lt; 0) {&lt;br /&gt; err = sock;&lt;br /&gt; continue;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;err = connect(sock, addr-&amp;gt;ai_addr, addr-&amp;gt;ai_addrlen);&lt;br /&gt; if (err &amp;lt; 0) {&lt;br /&gt; continue;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Compared to your solution, I placed the setsockopt call just after creating the socket. I tried your exact order, and it also succeeded at my end.&lt;/p&gt;
&lt;p&gt;I had a suspicion on the struct timeval, but tested it with newlib and minimal libc (the default libc in zephyr), and no problems there either.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m running on &amp;quot;v1.1-branch&amp;quot;. Which revision of nrf are you testing on?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>