eeb2e6925ca6d1f50e1e98011494c6c70cd9e1ce
[gapil.git] / sources / myhost.c
1 /* myhost.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 myhost.c: An example host command
22  *
23  * Author: S. Piccardi Jul. 2004
24  *
25  * $Id: myhost.c,v 1.1 2004/07/03 22:53:13 piccardi Exp $
26  *
27  *****************************************************************************/
28 #include <netdb.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>        /* C standard library */
32 #include <stdio.h>         /* I/O standard library */
33 #include <unistd.h>
34 #include <arpa/inet.h>
35 extern int h_errno;
36
37 #include "Gapil.h"
38 /*
39  * Program myhost
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     char **alias;
54     char *addr;
55     char buffer[INET6_ADDRSTRLEN];
56     /*
57      * Input section: decode command line parameters 
58      * Use getopt function
59      */
60     opterr = 0;  /* don't want writing to stderr */
61     while ( (i = getopt(argc, argv, "h")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':                                            /* help option */
67             printf("Wrong -h option use\n");
68             usage();
69             return -1;
70             break;
71         case '?':                                    /* unrecognized options */
72             printf("Unrecognized options -%c\n",optopt);
73             usage();
74         default:                                       /* should not reached */
75             usage();
76         }
77     }
78     /* ***********************************************************
79      * 
80      *           Options processing completed
81      *
82      *                Main code beginning
83      * 
84      * ***********************************************************/
85     if ((argc - optind) != 1) {
86         printf("Wrong number of arguments %d\n", argc - optind);
87         usage();
88     }
89     data = gethostbyname(argv[1]);
90     if (data == NULL) {
91         herror("Errore di risoluzione");
92         exit(1);
93     }
94     printf("Canonical name %s\n", data->h_name);
95     alias = data->h_aliases;
96     while (*alias != NULL) {
97         printf("Alias %s\n", *alias);
98         alias++;
99     }
100     if (data->h_addrtype == AF_INET) {
101         printf("Address are IPv4\n");
102     } else if (data->h_addrtype == AF_INET6) {
103         printf("Address are IPv6\n");
104     } else {
105         printf("Tipo di indirizzo non valido\n");
106         exit(1);
107     }
108     alias = data->h_addr_list;
109     while (*alias != NULL) {
110         addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer));
111         printf("Indirizzo %s\n", addr);
112         alias++;
113     }    
114     exit(0);
115 }
116 /*
117  * routine to print usage info and exit
118  */
119 void usage(void) {
120     printf("Program myhost: do an hostname resolution \n");
121     printf("Usage:\n");
122     printf("  myhost [-h] hostname \n");
123     printf("  -h           print this help\n");
124     exit(1);
125 }