getaddrinfo.
--- /dev/null
+ struct addrinfo hint;
+ struct addrinfo *res, *ptr;
+ int ret, port;
+ struct sockaddr_in *addr;
+ struct sockaddr_in6 *addr6;
+ char buffer[INET6_ADDRSTRLEN];
+ char *string;
+ ...
+ if ((argc - optind) != 2) {
+ printf("Wrong number of arguments %d\n", argc - optind);
+ usage();
+ }
+/* main body */
+ ret = getaddrinfo(argv[1], argv[2], NULL, &res);
+ if (ret != 0) {
+ printf("Resolution error %s\n", gai_strerror(ret));
+ exit(1);
+ }
+ ptr = res;
+ printf("Canonical name %s\n", ptr->ai_canonname);
+ do {
+ if (ptr->ai_family == PF_INET) {
+ printf("IPv4 address: \n");
+ addr = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr->sin_port);
+ string = inet_ntop(addr->sin_family, &addr->sin_addr,
+ buffer, sizeof(buffer));
+ } else if (ptr->ai_family == PF_INET6) {
+ printf("IPv6 address: \n");
+ addr6 = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr6->sin6_port);
+ string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr,
+ buffer, sizeof(buffer));
+ } else {
+ printf("Address family error\n");
+ exit(1);
+ }
+ printf("\tIndirizzo %s\n", string);
+ printf("\tProtocollo %i\n", ptr->ai_protocol);
+ printf("\tPorta %i\n", port);
+
+
+ ptr = ptr->ai_next;
+ } while (ptr != NULL);
+ exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program mygethost: do an hostname resolution \n");
+ printf("Usage:\n");
+ printf(" mygethost [-h] hostname service\n");
+ printf(" -h print this help\n");
+ exit(1);
+}
--- /dev/null
+int main(int argc, char *argv[])
+{
+/*
+ * Variables definition
+ */
+ int i;
+ struct hostent *data;
+ char **alias;
+ char *addr;
+ char buffer[INET6_ADDRSTRLEN];
+ ...
+ if ((argc - optind) != 1) {
+ printf("Wrong number of arguments %d\n", argc - optind);
+ usage();
+ }
+ data = gethostbyname(argv[1]);
+ if (data == NULL) {
+ herror("Errore di risoluzione");
+ exit(1);
+ }
+ printf("Canonical name %s\n", data->h_name);
+ alias = data->h_aliases;
+ while (*alias != NULL) {
+ printf("Alias %s\n", *alias);
+ alias++;
+ }
+ if (data->h_addrtype == AF_INET) {
+ printf("Address are IPv4\n");
+ } else if (data->h_addrtype == AF_INET6) {
+ printf("Address are IPv6\n");
+ } else {
+ printf("Tipo di indirizzo non valido\n");
+ exit(1);
+ }
+ alias = data->h_addr_list;
+ while (*alias != NULL) {
+ addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
+ printf("Indirizzo %s\n", addr);
+ alias++;
+ }
+ exit(0);
+}
+++ /dev/null
-int main(int argc, char *argv[])
-{
-/*
- * Variables definition
- */
- int i;
- struct hostent *data;
- char **alias;
- char *addr;
- char buffer[INET6_ADDRSTRLEN];
- ...
- if ((argc - optind) != 1) {
- printf("Wrong number of arguments %d\n", argc - optind);
- usage();
- }
- data = gethostbyname(argv[1]);
- if (data == NULL) {
- herror("Errore di risoluzione");
- exit(1);
- }
- printf("Canonical name %s\n", data->h_name);
- alias = data->h_aliases;
- while (*alias != NULL) {
- printf("Alias %s\n", *alias);
- alias++;
- }
- if (data->h_addrtype == AF_INET) {
- printf("Address are IPv4\n");
- } else if (data->h_addrtype == AF_INET6) {
- printf("Address are IPv6\n");
- } else {
- printf("Tipo di indirizzo non valido\n");
- exit(1);
- }
- alias = data->h_addr_list;
- while (*alias != NULL) {
- addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
- printf("Indirizzo %s\n", addr);
- alias++;
- }
- exit(0);
-}
\begin{figure}[!htb]
\footnotesize \centering
\begin{minipage}[c]{15cm}
- \includecodesample{listati/myhost.c}
+ \includecodesample{listati/mygethost.c}
\end{minipage}
\normalsize
\caption{Esempio di codice per la risoluzione di un indirizzo.}
- \label{fig:myhost_example}
+ \label{fig:mygethost_example}
\end{figure}
Vediamo allora un primo esempio dell'uso delle funzioni di risoluzione, in
-fig.~\ref{fig:myhost_example} è riportato un estratto del codice di un
+fig.~\ref{fig:mygethost_example} è riportato un estratto del codice di un
programma che esegue una semplice interrogazione al \textit{resolver} usando
\func{gethostbyname} e poi ne stampa a video i risultati. Al solito il
sorgente completo, che comprende il trattamento delle opzioni ed una funzione
-per stampare un messaggio di aiuto, è nel file \texttt{myhost.c} dei sorgenti
-allegati alla guida.
+per stampare un messaggio di aiuto, è nel file \texttt{mygethost.c} dei
+sorgenti allegati alla guida.
Il programma richiede un solo argomento che specifichi il nome da cercare,
senza il quale (\texttt{\small 12--15}) esce con un errore. Dopo di che
\label{fig:sock_addrinfo_list}
\end{figure}
+Come primo esempio di uso di \func{getaddrinfo} vediamo un programma
+elementare di interrogazione del resolver, basato questa funzione, il cui
+corpo principale è riportato in fig.. Il codice
+del programma è nel file \texttt{mygetaddr.c}, dei sorgenti allegati alla
+guida.
+
+\begin{figure}[!htb]
+ \footnotesize \centering
+ \begin{minipage}[c]{15cm}
+ \includecodesample{listati/mygetaddr.c}
+ \end{minipage}
+ \normalsize
+ \caption{Esempio di codice per la risoluzione di un indirizzo.}
+ \label{fig:mygethost_example}
+\end{figure}
+
+
Una volta estratti i risultati dalla \textit{linked list} puntata da
\param{res} si dovrà avere cura di disallocare opportunamente tutta la
--- /dev/null
+/* mygetaddr.c
+ *
+ * Copyright (C) 2004 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*****************************************************************************
+ *
+ * File mygetaddr.c: An example host command
+ *
+ * Author: S. Piccardi Nov. 2004
+ *
+ * $Id$
+ *
+ *****************************************************************************/
+#include <netdb.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h> /* C standard library */
+#include <stdio.h> /* I/O standard library */
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "Gapil.h"
+/*
+ * Program myhost
+ *
+ * Use gethostbyname and print results
+ */
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/*
+ * Variables definition
+ */
+ int i;
+ struct addrinfo hint;
+ struct addrinfo *res, *ptr;
+ int ret, port;
+ struct sockaddr_in *addr;
+ struct sockaddr_in6 *addr6;
+ char buffer[INET6_ADDRSTRLEN];
+ char *string;
+ /*
+ * Input section: decode command line parameters
+ * Use getopt function
+ */
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "h")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h': /* help option */
+ printf("Wrong -h option use\n");
+ usage();
+ return -1;
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ if ((argc - optind) != 2) {
+ printf("Wrong number of arguments %d\n", argc - optind);
+ usage();
+ }
+
+ ret = getaddrinfo(argv[1], argv[2], NULL, &res); /* main call */
+ if (ret != 0) { /* on error exit */
+ printf("Resolution error %s\n", gai_strerror(ret));
+ exit(1);
+ }
+ ptr = res; /* store pointer */
+ printf("Canonical name %s\n", ptr->ai_canonname); /* print cname */
+ do {
+ if (ptr->ai_family == PF_INET) {
+ printf("IPv4 address: \n");
+ addr = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr->sin_port);
+ string = inet_ntop(addr->sin_family, &addr->sin_addr,
+ buffer, sizeof(buffer));
+ } else if (ptr->ai_family == PF_INET6) {
+ printf("IPv6 address: \n");
+ addr6 = (struct sockaddr_in *) ptr->ai_addr;
+ port = ntohs(addr6->sin6_port);
+ string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr,
+ buffer, sizeof(buffer));
+ } else {
+ printf("Address family error\n");
+ exit(1);
+ }
+ printf("\tIndirizzo %s\n", string);
+ printf("\tProtocollo %i\n", ptr->ai_protocol);
+ printf("\tPorta %i\n", port);
+
+
+ ptr = ptr->ai_next;
+ } while (ptr != NULL);
+ exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program mygethost: do an hostname resolution \n");
+ printf("Usage:\n");
+ printf(" mygethost [-h] hostname service\n");
+ printf(" -h print this help\n");
+ exit(1);
+}
--- /dev/null
+/* mygethost.c
+ *
+ * Copyright (C) 2004 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*****************************************************************************
+ *
+ * File mygethost.c: An example host command
+ *
+ * Author: S. Piccardi Jul. 2004
+ *
+ * $Id$
+ *
+ *****************************************************************************/
+#include <netdb.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h> /* C standard library */
+#include <stdio.h> /* I/O standard library */
+#include <unistd.h>
+#include <arpa/inet.h>
+extern int h_errno;
+
+#include "Gapil.h"
+/*
+ * Program myhost
+ *
+ * Use gethostbyname and print results
+ */
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/*
+ * Variables definition
+ */
+ int i;
+ struct hostent *data;
+ char **alias;
+ char *addr;
+ char buffer[INET6_ADDRSTRLEN];
+ /*
+ * Input section: decode command line parameters
+ * Use getopt function
+ */
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "h")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h': /* help option */
+ printf("Wrong -h option use\n");
+ usage();
+ return -1;
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ if ((argc - optind) != 1) {
+ printf("Wrong number of arguments %d\n", argc - optind);
+ usage();
+ }
+ data = gethostbyname(argv[1]);
+ if (data == NULL) {
+ herror("Errore di risoluzione");
+ exit(1);
+ }
+ printf("Canonical name %s\n", data->h_name);
+ alias = data->h_aliases;
+ while (*alias != NULL) {
+ printf("Alias %s\n", *alias);
+ alias++;
+ }
+ if (data->h_addrtype == AF_INET) {
+ printf("Address are IPv4\n");
+ } else if (data->h_addrtype == AF_INET6) {
+ printf("Address are IPv6\n");
+ } else {
+ printf("Tipo di indirizzo non valido\n");
+ exit(1);
+ }
+ alias = data->h_addr_list;
+ while (*alias != NULL) {
+ addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
+ printf("Indirizzo %s\n", addr);
+ alias++;
+ }
+ exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program mygethost: do an hostname resolution \n");
+ printf("Usage:\n");
+ printf(" mygethost [-h] hostname \n");
+ printf(" -h print this help\n");
+ exit(1);
+}
+++ /dev/null
-/* mygethost.c
- *
- * Copyright (C) 2004 Simone Piccardi
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-/*****************************************************************************
- *
- * File mygethost.c: An example host command
- *
- * Author: S. Piccardi Jul. 2004
- *
- * $Id$
- *
- *****************************************************************************/
-#include <netdb.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdlib.h> /* C standard library */
-#include <stdio.h> /* I/O standard library */
-#include <unistd.h>
-#include <arpa/inet.h>
-extern int h_errno;
-
-#include "Gapil.h"
-/*
- * Program myhost
- *
- * Use gethostbyname and print results
- */
-/* Help printing routine */
-void usage(void);
-
-int main(int argc, char *argv[])
-{
-/*
- * Variables definition
- */
- int i;
- struct hostent *data;
- char **alias;
- char *addr;
- char buffer[INET6_ADDRSTRLEN];
- /*
- * Input section: decode command line parameters
- * Use getopt function
- */
- opterr = 0; /* don't want writing to stderr */
- while ( (i = getopt(argc, argv, "h")) != -1) {
- switch (i) {
- /*
- * Handling options
- */
- case 'h': /* help option */
- printf("Wrong -h option use\n");
- usage();
- return -1;
- break;
- case '?': /* unrecognized options */
- printf("Unrecognized options -%c\n",optopt);
- usage();
- default: /* should not reached */
- usage();
- }
- }
- /* ***********************************************************
- *
- * Options processing completed
- *
- * Main code beginning
- *
- * ***********************************************************/
- if ((argc - optind) != 1) {
- printf("Wrong number of arguments %d\n", argc - optind);
- usage();
- }
- data = gethostbyname(argv[1]);
- if (data == NULL) {
- herror("Errore di risoluzione");
- exit(1);
- }
- printf("Canonical name %s\n", data->h_name);
- alias = data->h_aliases;
- while (*alias != NULL) {
- printf("Alias %s\n", *alias);
- alias++;
- }
- if (data->h_addrtype == AF_INET) {
- printf("Address are IPv4\n");
- } else if (data->h_addrtype == AF_INET6) {
- printf("Address are IPv6\n");
- } else {
- printf("Tipo di indirizzo non valido\n");
- exit(1);
- }
- alias = data->h_addr_list;
- while (*alias != NULL) {
- addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
- printf("Indirizzo %s\n", addr);
- alias++;
- }
- exit(0);
-}
-/*
- * routine to print usage info and exit
- */
-void usage(void) {
- printf("Program mygethost: do an hostname resolution \n");
- printf("Usage:\n");
- printf(" mygethost [-h] hostname \n");
- printf(" -h print this help\n");
- exit(1);
-}