3 * Copyright (C) 2004 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File mygethost.c: An example host command
23 * Author: S. Piccardi Jul. 2004
25 *****************************************************************************/
26 #include <sys/types.h> /* primitive system data types */
27 #include <sys/stat.h> /* file characteristics constants and functions */
28 #include <stdlib.h> /* C standard library */
29 #include <stdio.h> /* standard I/O library */
30 #include <unistd.h> /* unix standard library */
31 #include <arpa/inet.h> /* IP addresses conversion utilities */
32 #include <netdb.h> /* C resolver library */
33 #include <netinet/tcp.h> /* TCP socket option */
41 * Use gethostbyname and print results
43 /* Help printing routine */
46 int main(int argc, char *argv[])
49 * Variables definition
58 SOL_SOCKET, "SOL_SOCKET",
62 SOL_ICMPV6, "SOL_ICMPV6"
67 char buffer[INET6_ADDRSTRLEN];
69 * Input section: decode command line parameters
72 opterr = 0; /* don't want writing to stderr */
73 while ( (i = getopt(argc, argv, "h")) != -1) {
78 case 'h': /* help option */
79 printf("Wrong -h option use\n");
83 case '?': /* unrecognized options */
84 printf("Unrecognized options -%c\n",optopt);
86 default: /* should not reached */
90 /* ***********************************************************
92 * Options processing completed
96 * ***********************************************************/
97 if ((argc - optind) != 1) {
98 printf("Wrong number of arguments %d\n", argc - optind);
101 data = gethostbyname(argv[1]);
103 herror("Errore di risoluzione");
106 printf("Canonical name %s\n", data->h_name);
107 alias = data->h_aliases;
108 while (*alias != NULL) {
109 printf("Alias %s\n", *alias);
112 if (data->h_addrtype == AF_INET) {
113 printf("Address are IPv4\n");
114 } else if (data->h_addrtype == AF_INET6) {
115 printf("Address are IPv6\n");
117 printf("Tipo di indirizzo non valido\n");
120 alias = data->h_addr_list;
121 while (*alias != NULL) {
122 addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
123 printf("Indirizzo %s\n", addr);
129 * routine to print usage info and exit
132 printf("Program mygethost: do an hostname resolution \n");
134 printf(" mygethost [-h] hostname \n");
135 printf(" -h print this help\n");