Spostato il vecchi myhost.c e creato nuovo programma mygetaddr per provare
authorSimone Piccardi <piccardi@gnulinux.it>
Mon, 8 Nov 2004 23:22:56 +0000 (23:22 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Mon, 8 Nov 2004 23:22:56 +0000 (23:22 +0000)
getaddrinfo.

listati/mygetaddr.c [new file with mode: 0644]
listati/mygethost.c [new file with mode: 0644]
listati/myhost.c [deleted file]
sockctrl.tex
sources/mygetaddr.c [new file with mode: 0644]
sources/mygethost.c [new file with mode: 0644]
sources/myhost.c [deleted file]

diff --git a/listati/mygetaddr.c b/listati/mygetaddr.c
new file mode 100644 (file)
index 0000000..1499e2b
--- /dev/null
@@ -0,0 +1,56 @@
+    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);
+}
diff --git a/listati/mygethost.c b/listati/mygethost.c
new file mode 100644 (file)
index 0000000..0378058
--- /dev/null
@@ -0,0 +1,42 @@
+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);
+}
diff --git a/listati/myhost.c b/listati/myhost.c
deleted file mode 100644 (file)
index 0378058..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-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);
-}
index 07f3e18c1f12044b62c1f2db7f8e1b72a100c9a9..8f6e63426e0d2b8315e55254a127d735e033e96b 100644 (file)
@@ -705,20 +705,20 @@ suoi risultati.
 \begin{figure}[!htb]
   \footnotesize \centering
   \begin{minipage}[c]{15cm}
 \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.}
   \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
 \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
 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
 
 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
@@ -1456,6 +1456,23 @@ lista illustrata in fig.~\ref{fig:sock_addrinfo_list}.
   \label{fig:sock_addrinfo_list}
 \end{figure}
 
   \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
 
 Una volta estratti i risultati dalla \textit{linked list} puntata da
 \param{res} si dovrà avere cura di disallocare opportunamente tutta la
diff --git a/sources/mygetaddr.c b/sources/mygetaddr.c
new file mode 100644 (file)
index 0000000..aa83006
--- /dev/null
@@ -0,0 +1,136 @@
+/* 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);
+}
diff --git a/sources/mygethost.c b/sources/mygethost.c
new file mode 100644 (file)
index 0000000..76cf844
--- /dev/null
@@ -0,0 +1,125 @@
+/* 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);
+}
diff --git a/sources/myhost.c b/sources/myhost.c
deleted file mode 100644 (file)
index 76cf844..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-/* 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);
-}