Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating socket compatible with IPv4 and IPv6
    primarykey
    data
    text
    <p>I am writing a program which can read the IP address that I have been assigned on a specific network interface (VLAN: eth0.32). I won't know if I have been assigned an IPv4 or a IPv6 address so I try to write it protocol family agnostic. </p> <p>The way it works is: with a for, I navigate through the list of available network interfaces and I stop at the point where I find my VLAN (eth0.32) to read the IP address. Anyways, at this point of development I just want that it works for IPv4 but leave it ready when I want to implement IPv6 support.</p> <p>The program works and reads the IPv4 address if I create the socket normally:</p> <pre><code>sd=socket(PF_INET, SOCK_DGRAM, 0); </code></pre> <p>But as I won't be able to read IPv6 addresses with this socket family (PF_INET=IPv4) I do it like this:</p> <pre><code>sd=socket(PF_INET6, SOCK_DGRAM, 0); setsockopt(sd, SOL_SOCKET, IPV6_V6ONLY, 0, sizeof(int)); </code></pre> <p>The problem is that with this IPv6 socket, it fails to accomplish the if condition to read the ip address:</p> <pre><code>if (ioctl(sd, SIOCGIFCONF, &amp;ifc) == 0) </code></pre> <p>For more information, this is my whole code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/ioctl.h&gt; #include &lt;netinet/in.h&gt; #include &lt;netdb.h&gt; #include &lt;net/if.h&gt; int main(int argc, char **argv) { struct ifconf ifc; struct ifreq ifr[MAX_NETWORK_INTERFACES]; int ifc_num, addr, i; static uint8 sd=0; sd=socket(PF_INET6, SOCK_DGRAM, 0); setsockopt(sd, SOL_SOCKET, IPV6_V6ONLY, 0, sizeof(int)); //I change the socket option IPV6_V6ONLY to false, so it should be compatible with IPv4 if (sd &gt; 0) { ifc.ifc_len = sizeof(ifr); ifc.ifc_ifcu.ifcu_buf = (caddr_t)ifr; //Buffer address if (ioctl(sd, SIOCGIFCONF, &amp;ifc) == 0) { ifc_num = ifc.ifc_len / sizeof(struct ifreq); //Number of network interfaces found for (i = 0; i &lt; ifc_num; i++) { if(ifr[i].ifr_addr.sa_family != AF_INET) //If it is not an IPv4 address, we do nothing { continue; } if (strcmp(ifr[i].ifr_ifrn.ifrn_name,"eth0.32")==0) { if (ioctl(sd, SIOCGIFADDR, &amp;ifr[i]) == 0) // HERE IS WHERE THE IPv6 SOCKET DOESN'T ENTER!!! { addr = ntohl(((struct sockaddr_in *)(&amp;ifr[i].ifr_addr))-&gt;sin_addr.s_addr); } fclose(fp); break; // end loop } } } } return addr; } </code></pre> <p>Thanks in advance for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload