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