Aggiornamento note copyright
[gapil.git] / sources / mygethost.c
1 /* mygethost.c
2  * 
3  * Copyright (C) 2004 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /*****************************************************************************
20  *
21  * File mygethost.c: An example host command
22  *
23  * Author: S. Piccardi Jul. 2004
24  *
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 */
34
35 extern int h_errno;
36
37 #include "Gapil.h"
38 /*
39  * Program mygethost
40  *
41  * Use gethostbyname and print results
42  */
43 /* Help printing routine */
44 void usage(void);
45
46 int main(int argc, char *argv[]) 
47 {
48 /* 
49  * Variables definition
50  */
51     int i;
52     struct hostent *data;
53
54     struct sock_level {
55         int level;
56         char * name;
57     } sock_level[] = {
58         SOL_SOCKET, "SOL_SOCKET",
59         SOL_IP,     "SOL_IP",
60         SOL_TCP,    "SOL_TCP",
61         SOL_IPV6,   "SOL_IPV6",
62         SOL_ICMPV6, "SOL_ICMPV6"
63     };
64
65     char **alias;
66     char *addr;
67     char buffer[INET6_ADDRSTRLEN];
68     /*
69      * Input section: decode command line parameters 
70      * Use getopt function
71      */
72     opterr = 0;  /* don't want writing to stderr */
73     while ( (i = getopt(argc, argv, "h")) != -1) {
74         switch (i) {
75         /* 
76          * Handling options 
77          */ 
78         case 'h':                                            /* help option */
79             printf("Wrong -h option use\n");
80             usage();
81             return -1;
82             break;
83         case '?':                                    /* unrecognized options */
84             printf("Unrecognized options -%c\n",optopt);
85             usage();
86         default:                                       /* should not reached */
87             usage();
88         }
89     }
90     /* ***********************************************************
91      * 
92      *           Options processing completed
93      *
94      *                Main code beginning
95      * 
96      * ***********************************************************/
97     if ((argc - optind) != 1) {
98         printf("Wrong number of arguments %d\n", argc - optind);
99         usage();
100     }
101     data = gethostbyname(argv[1]);
102     if (data == NULL) {
103         herror("Errore di risoluzione");
104         exit(1);
105     }
106     printf("Canonical name %s\n", data->h_name);
107     alias = data->h_aliases;
108     while (*alias != NULL) {
109         printf("Alias %s\n", *alias);
110         alias++;
111     }
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");
116     } else {
117         printf("Tipo di indirizzo non valido\n");
118         exit(1);
119     }
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);
124         alias++;
125     }    
126     exit(0);
127 }
128 /*
129  * routine to print usage info and exit
130  */
131 void usage(void) {
132     printf("Program mygethost: do an hostname resolution \n");
133     printf("Usage:\n");
134     printf("  mygethost [-h] hostname \n");
135     printf("  -h   print this help\n");
136     exit(1);
137 }