Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C] Conoscere l'IP di un device

    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?

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Come vedi in

    return ((struct in_addr *)hostent_ptr->h_addr_list[0])->s_addr; /*get the first IP.*/

    viene restituito il primo IP della lista trovata.

    Prova a dare un'occhiata all'elemento

    h_addr_list[1]

  3. #3
    Eh ci avevo pensato, ma se lo faccio mi prendo un secmentation fault.

    Poco sopra inoltre provo a stampare tutto con un for e lui mi stampa solo 127.0.0.1

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.