From 4c6bdffb5a1a4746b241d8323faaf3a49a4633a9 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Mon, 8 Nov 2004 23:22:56 +0000 Subject: [PATCH 1/1] Spostato il vecchi myhost.c e creato nuovo programma mygetaddr per provare getaddrinfo. --- listati/mygetaddr.c | 56 ++++++++++++ listati/{myhost.c => mygethost.c} | 0 sockctrl.tex | 27 ++++-- sources/mygetaddr.c | 136 ++++++++++++++++++++++++++++++ sources/{myhost.c => mygethost.c} | 0 5 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 listati/mygetaddr.c rename listati/{myhost.c => mygethost.c} (100%) create mode 100644 sources/mygetaddr.c rename sources/{myhost.c => mygethost.c} (100%) diff --git a/listati/mygetaddr.c b/listati/mygetaddr.c new file mode 100644 index 0000000..1499e2b --- /dev/null +++ b/listati/mygetaddr.c @@ -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/myhost.c b/listati/mygethost.c similarity index 100% rename from listati/myhost.c rename to listati/mygethost.c diff --git a/sockctrl.tex b/sockctrl.tex index 07f3e18..8f6e634 100644 --- a/sockctrl.tex +++ b/sockctrl.tex @@ -705,20 +705,20 @@ suoi risultati. \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 @@ -1456,6 +1456,23 @@ lista illustrata in fig.~\ref{fig:sock_addrinfo_list}. \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 diff --git a/sources/mygetaddr.c b/sources/mygetaddr.c new file mode 100644 index 0000000..aa83006 --- /dev/null +++ b/sources/mygetaddr.c @@ -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 +#include +#include +#include /* C standard library */ +#include /* I/O standard library */ +#include +#include +#include +#include + +#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/myhost.c b/sources/mygethost.c similarity index 100% rename from sources/myhost.c rename to sources/mygethost.c -- 2.30.2