Vai al commento



Post raccomandati

Ecco un piccolo listato in C che, dato in input un hostname, restituisce uno o più indirizzi IP associati ad esso:


/*
* getIPv4byhost
* Copyright (C) 2011 Viper
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above copyright
* holders shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization.
*/
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
extern int h_errno;
int main(int argc, char** argv)
{
struct hostent *host;
char **alias;
char buf[INET_ADDRSTRLEN];
char *hostname = argv[1];
if (argc < 2) {
fprintf(stderr, "Too few arguments\n");
return 1;
} else if (argc > 2) {
fprintf(stderr, "Too many arguments\n");
return 1;
}
host = gethostbyname(hostname);
if(host != NULL) {
printf("%s:\n", host->h_name);
alias = host->h_aliases;
while (*alias != NULL) {
printf("Alias: %s\n", *alias);
alias++;
}
if (host->h_addrtype != AF_INET) {
printf("Huh?\n");
return 1;
}
alias = host->h_addr_list;
while(*alias != NULL) {
printf("IPv4 address: %s\n", \
inet_ntop(AF_INET, *alias, buf, sizeof(buf)));
alias++;
}
} else {
fprintf(stderr, "Error: %s\n", hstrerror(h_errno));
return 1;
}
return 0;
}

Questi sono gli esempi di esecuzione:


$ ./getIPv4byhost localhost
localhost:
IPv4 address: 127.0.0.1
$ ./getIPv4byhost pokemonmillennium.net
pokemonmillennium.net:
IPv4 address: 72.167.201.44
$ ./getIPv4byhost google.it
google.it:
IPv4 address: 74.125.39.105
IPv4 address: 74.125.39.147
IPv4 address: 74.125.39.103
IPv4 address: 74.125.39.104
IPv4 address: 74.125.39.106
IPv4 address: 74.125.39.99

Per i dettagli tecnici sul funzionamento, consiglio la lettura di Wikipedia e di testi relativi all'architettura delle reti.

Link al commento
Condividi su altre piattaforme

Archiviata

La discussione è ora archiviata e chiusa ad ulteriori risposte.

  • Utenti nella discussione   0 utenti

    • Nessun utente registrato sta visualizzando questa pagina.
×
×
  • Crea...