<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Coding Relic - Latest Comments in Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.disqus.com/</link><description>Random Musings about Software in an Embedded World</description><atom:link href="https://codingrelic.disqus.com/code_snippet_so_bindtodevice/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 24 Aug 2017 20:41:09 -0000</lastBuildDate><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-3485591230</link><description>&lt;p&gt;I am studying "fping" and SO_BINDTODEVICE is necessary with "-I" option; thanks for this note!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ks7000</dc:creator><pubDate>Thu, 24 Aug 2017 20:41:09 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-1704325480</link><description>&lt;p&gt;After bind and issuing the setsockopt ,What happens if the interface is deleted and recreated wit the same name?? (In case of dynamic interfaces like IPIP interfaces )&lt;br&gt;Can I read and write data from/to the socket fd ?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">krishna</dc:creator><pubDate>Thu, 20 Nov 2014 20:16:22 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-1047600366</link><description>&lt;p&gt;Hi Denton,&lt;br&gt;How i could use SO_BINDTODEVICE to bind to specific interface (i mean "eth1"), without hard-coding any ipaddress for binding. this means whenever the ipaddress changes i ensure the server should be compactible enough i accept the packet still with specfic to interface (eth1)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">siva</dc:creator><pubDate>Tue, 17 Sep 2013 02:37:55 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-957799269</link><description>&lt;p&gt;Hi Denton,&lt;/p&gt;&lt;p&gt;I hope you could help me. We are trying to develop a software which is using SO_BINDTODEVICE to send IPv4 and IPv6 packets with raw sockets.&lt;/p&gt;&lt;p&gt;Let me know If I'm wrong but we think that SO_BINDTODEVICE use the entries of the routing table which affects the interface we want to use and discard other routes. With this in mind we create a default route (with different priority) per each interface. With IPv4 the behaviour is the expected but when we use IPv6 and want to send a packet trough an interface different to the one with the default route with highest priority,  we obtain a network unreachable. Does the behaviou of this option differ from IPV4 to IPv6.&lt;/p&gt;&lt;p&gt;Thanks in advance&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">alopez</dc:creator><pubDate>Wed, 10 Jul 2013 15:30:11 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-947225673</link><description>&lt;p&gt;Do not use SO_BINDTODEVICE because this is root-only feature. Use bind() before connect() and create different routing tables for each interface with iproute2!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Андрей Аладьев</dc:creator><pubDate>Sun, 30 Jun 2013 14:55:58 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-904747600</link><description>&lt;p&gt;how can I bind a MAC multicast address to a virtual LAN interface?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">arr</dc:creator><pubDate>Wed, 22 May 2013 07:44:09 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-576002402</link><description>&lt;p&gt;What will happen if ip forwarding is disabled? I presume that you can always predict the interface socket will be using in the case ip forwarding is disabled without using SO_BINDTODEVICE &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">colobot</dc:creator><pubDate>Wed, 04 Jul 2012 06:12:02 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-462906959</link><description>&lt;p&gt;Hi Denton,&lt;br&gt;I don't think the statement "bind() does not control anything about the routing of transmitted packets." is accurate.  &lt;br&gt;ip(8) can be used to alter routing tables so the routing entry (which specifies the interface to use) is based on the source address. Therefore binding to a particular source address can affect the routing of transmitted packets.&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eli</dc:creator><pubDate>Sun, 11 Mar 2012 16:53:37 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-444729504</link><description>&lt;p&gt; Thanks, yeah i  have been doing the same. In fact the above two links i pasted are using it.&lt;/p&gt;&lt;p&gt;I had to modify existing application written with recv_from to use recvmsg to get that since IP_PKTINFO should be set before the read call to have the details available.&lt;/p&gt;&lt;p&gt;The socket is however non blocking and was initially bound to an interface (so upon a received response, i need to determine which interface was this response received upon from out of many sockets).&lt;/p&gt;&lt;p&gt;I have gotten it to work as a stand alone e.g. for loop back. But integrated into production code, the interface index returned is always zero.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">fkl</dc:creator><pubDate>Tue, 21 Feb 2012 08:25:24 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-444079300</link><description>&lt;p&gt;I've used IP_PKTINFO for this. There is also an IP6_PKTINFO which I have not personally used, but looks similar.&lt;/p&gt;&lt;p&gt;You set IP_PKTINFO in the flags passed to recvmsg. The control block will contain an ip_pktinfo structure indicating the ingress interface.&lt;/p&gt;&lt;p&gt;This is from memory, but some searching should turn up more specific information.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DGentry</dc:creator><pubDate>Mon, 20 Feb 2012 11:11:03 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-444073298</link><description>&lt;p&gt;This has been very useful code for me in my socket programming. Thanks.&lt;/p&gt;&lt;p&gt;A problem that i don't seem to get around after using the above code and searching on internet is.&lt;/p&gt;&lt;p&gt;"How to check the name of interface on which a packet is received?" This socket was previously bound using SO_BINDTODIVICE and works perfectly. Now on the way back messages are received and i need to determine which interface got the response.&lt;/p&gt;&lt;p&gt;I have tried the following but they are resulting in segmentation faults.&lt;/p&gt;&lt;p&gt;&lt;a href="http://stackoverflow.com/questions/3940612/c-dgram-socket-get-the-receiver-address" rel="nofollow noopener" target="_blank" title="http://stackoverflow.com/questions/3940612/c-dgram-socket-get-the-receiver-address"&gt;http://stackoverflow.com/qu...&lt;/a&gt;&lt;br&gt;&lt;a href="http://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from" rel="nofollow noopener" target="_blank" title="http://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from"&gt;http://stackoverflow.com/qu...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Thanks,&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">fkl</dc:creator><pubDate>Mon, 20 Feb 2012 11:01:52 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-374041858</link><description>&lt;p&gt;Thanks Its so useful&lt;br&gt; &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Balu </dc:creator><pubDate>Mon, 28 Nov 2011 02:32:40 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-127599272</link><description>&lt;p&gt;I think setsockopt needs device index rather than its human-readable name, so first you need to run ioctl(s, SIOCGIFINDEX, &amp;amp;ifr) to get the index from the name and then pass the struct to setsockopt.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jackhab</dc:creator><pubDate>Tue, 11 Jan 2011 09:27:56 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-48011299</link><description>&lt;p&gt;There does need to be a route to the destination IP address. The IP stack will use the information from the routing table, such as the MAC address of the next hop router. SO_BINDTODEVICE simply forces the packet to egress from a particular interface, instead of the one the route pointed to.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DGentry</dc:creator><pubDate>Sun, 02 May 2010 07:17:27 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-47850217</link><description>&lt;p&gt;what are  the  routing   configuration  required in  linux   for  this  to  work&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Vineet Joshi</dc:creator><pubDate>Sat, 01 May 2010 09:07:55 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-32595250</link><description>&lt;p&gt;  Here is sample code to retrieve all interfaces present in the system and print their IP address, though the Disqus comment formatting has mangled the indentation. If you know the IP address you are looking for, you could check for it in sin-&amp;gt;sin_addr and SO_BINDTODEVICE to that ifr_name.&lt;br&gt;  If you find a simpler way to do this, please comment here as I'd like to know it.&lt;/p&gt;&lt;p&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br&gt;#include &amp;lt;string.h&amp;gt;&lt;br&gt;#include &amp;lt;sys socket.h=""&amp;gt;&lt;br&gt;#include &amp;lt;sys ioctl.h=""&amp;gt;&lt;br&gt;#include &amp;lt;linux if.h=""&amp;gt;&lt;br&gt;#include &amp;lt;netinet in.h=""&amp;gt;&lt;br&gt;#include &amp;lt;arpa inet.h=""&amp;gt;&lt;/p&gt;&lt;p&gt;int main()&lt;br&gt;{&lt;br&gt;   struct ifreq *ifr;&lt;br&gt;   struct ifconf ifc;&lt;br&gt;   int s, rc, i;&lt;br&gt;   int numif;&lt;/p&gt;&lt;p&gt;   // find number of interfaces.&lt;br&gt;   memset(&amp;amp;ifc,0,sizeof(ifc));&lt;br&gt;   ifc.ifc_ifcu.ifcu_req = NULL;&lt;br&gt;   ifc.ifc_len = 0;&lt;/p&gt;&lt;p&gt;   if ((s = socket(AF_INET, SOCK_DGRAM, 0)) &amp;lt; 0) {&lt;br&gt;     perror("socket");&lt;br&gt;     exit(1);&lt;br&gt;   }&lt;/p&gt;&lt;p&gt;   if ((rc = ioctl(s, SIOCGIFCONF, &amp;amp;ifc)) &amp;lt; 0) {&lt;br&gt;     perror("ioctl");&lt;br&gt;     exit(2);&lt;br&gt;   }&lt;/p&gt;&lt;p&gt;   numif = ifc.ifc_len / sizeof(struct ifreq);&lt;br&gt;   if ((ifr = malloc(ifc.ifc_len)) == NULL) {&lt;br&gt;     perror("malloc");&lt;br&gt;     exit(3);&lt;br&gt;   }&lt;br&gt;   ifc.ifc_ifcu.ifcu_req = ifr;&lt;/p&gt;&lt;p&gt;   if ((rc = ioctl(s, SIOCGIFCONF, &amp;amp;ifc)) &amp;lt; 0) {&lt;br&gt;     perror("ioctl2");&lt;br&gt;     exit(4);&lt;br&gt;   }&lt;br&gt;   close(s);&lt;/p&gt;&lt;p&gt;   for(i = 0; i &amp;lt; numif; i++) {&lt;br&gt;     struct ifreq *r = &amp;amp;ifr[i];&lt;br&gt;     struct sockaddr_in *sin = (struct sockaddr_in *)&amp;amp;r-&amp;gt;ifr_addr;&lt;/p&gt;&lt;p&gt;     printf("%-8s : %s\n", r-&amp;gt;ifr_name, inet_ntoa(sin-&amp;gt;sin_addr));&lt;br&gt;   }&lt;/p&gt;&lt;p&gt;   exit(0);&lt;br&gt;}&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DGentry</dc:creator><pubDate>Thu, 04 Feb 2010 08:46:14 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-32398151</link><description>&lt;p&gt;How can i find an interface to bind to based on destination ip?&lt;br&gt;Thanks in advance.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Vijay Nag</dc:creator><pubDate>Tue, 02 Feb 2010 12:15:47 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-20537060</link><description>&lt;p&gt;I'm glad to be of assistance.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DGentry</dc:creator><pubDate>Mon, 19 Oct 2009 16:40:06 -0000</pubDate></item><item><title>Re: Code Snippet: SO_BINDTODEVICE</title><link>http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html#comment-20534478</link><description>&lt;p&gt;Very useful information! Thanks&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Yong Hoon</dc:creator><pubDate>Mon, 19 Oct 2009 16:35:57 -0000</pubDate></item></channel></rss>