Ciao a tutti
come da topic, vorrei conosere l'IP di un device in particolare.
Se vado con le primitive standard, ovvero faccio una cosa del tipo:
codice:
in_addr_t get_my_IP() {
/* have you ever seen a hostname longer than a screen (80cols)?*/
char name[80]; /*store my hostname*/
struct hostent * hostent_ptr;
int ret;
ret = gethostname (name, 80);
fprintf(stderr,"My hostname: %s\n",name);
if(ret == -1) {
/*printf ("ERROR gethostname() failed, Errno=%d \nDescription: %s\n", errno, strerror(errno));*/
return 0;
}
hostent_ptr = gethostbyname(name);
if(hostent_ptr == NULL) {
/*printf ("ERROR gethostbyname() failed, h_errno=%d \nDescription: %s\n",
* h_errno, hstrerror(h_errno));*/
return 0;
}
int i = 0;
for (i = 0; hostent_ptr->h_addr_list[i] != NULL ; i++ ) {
struct in_addr addr;
memcpy(&addr, hostent_ptr->h_addr_list[i], sizeof(struct in_addr));
fprintf(stderr,"Address %d = %s\n",i,inet_ntoa(addr));
}
/*h_addr_list contains IPs of this host in network byte order */
return ((struct in_addr *)hostent_ptr->h_addr_list[0])->s_addr; /*get the
first IP.*/
}
Lui mi ritorna solo quello di loopback, ovvero 127.0.0.1. Io invece dovrei conoscre quello di un'altra interfaccia, oppure conoscerli tutti.
Io prendo gia il nome dell'interfaccia in un altro modo, ma non so se esiste qualcosa per conoscere l'IP di quella particolare interfaccia.
Come gia detto, ni alternativa mi va bene anche di conoscere tutti gli IP locali, anche senza nomi delle interfaccie..
Qualche idea?