Hi,
I hv a programme which returns the local ip(ipv4) address.
I want to improve this as to check the ip is whether ipv4 or ipv6 and return.
here is what i hv written so for to get ipv4 address.
Code:in_addr_t dalesa_get_iface( int sock ) { //in_addr_t l_iface; static struct ifreq ifreqs[4]; // assumes no more than 4 interfaces configured. struct ifconf ifconf; memset(&ifconf,0,sizeof(ifconf)); ifconf.ifc_buf = (char*) (ifreqs); ifconf.ifc_len = sizeof(ifreqs); ioctl( sock, SIOCGIFCONF, ( char* )&ifconf ); int nifaces = ifconf.ifc_len/sizeof(struct ifreq); int i ; for( i=0; i<nifaces; i++ ) { struct sockaddr sa = ifreqs[i].ifr_ifru.ifru_addr; struct sockaddr_in *sa_in = (struct sockaddr_in* )&sa; if ( sa_in->sin_addr.s_addr != 16777343) { printf("sa_in->sin_addr.sin_family = %i \n" ,AF_INET ); return sa_in->sin_addr.s_addr; } } return 16777343; // 127.0.0.1 }
please help me how this can this be improved in order to return ipv4 or ipv6 address?
thanks.



LinkBack URL
About LinkBacks


