Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / gethost.c
1 /* gethost.c
2  * 
3  * Copyright (C) 2005 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  * Program gethost.c: 
22  * Program to test gethostbyname and getaddrinfo
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: gethost -h give all info's
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #define _GNU_SOURCE
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdlib.h>      /* C standard library */
36 #include <unistd.h>      /* unix standard library */
37 #include <stdio.h>       /* standard I/O library */
38 #include <string.h>      /* C strings library */
39 #include <sys/types.h>   /* primitive system data types */
40 #include <sys/socket.h>  /* socket constants, types and functions */
41 #include <netdb.h>       /* C resolver library */
42
43
44 /* 
45  * Function and globals definitions
46  */
47 void usage(void);  /* Help printing routine */
48
49 /*
50  * Main program
51  */
52 int main(int argc, char *argv[])
53 {
54     /* 
55      * Variables definition  
56      */
57     int i;
58     int use = 0;
59     struct hostent *host;
60     struct in_addr addr;
61     struct addrinfo hint;
62     struct addrinfo *address;
63     struct sockaddr_in *socket;
64     /*
65      * Input section: decode command line parameters 
66      * Use getopt function
67      */
68     opterr = 0;  /* don't want writing to stderr */
69     while ( (i = getopt(argc, argv, "han")) != -1) {
70         switch (i) {
71         /* 
72          * Handling options 
73          */ 
74         case 'h':   /* help option */
75             printf("Wrong -h option use\n");
76             usage();
77             return -1;
78             break;
79         case 'a':   /* help option */
80             use = 1;
81             break;
82         case 'n':   /* help option */
83             use = 0;
84             break;
85         case '?':   /* unrecognized options */
86             printf("Unrecognized options -%c\n",optopt);
87             usage();
88         default:    /* should not reached */
89             usage();
90         }
91     }
92     /* ***********************************************************
93      * 
94      *           Options processing completed
95      *
96      *                Main code beginning
97      * 
98      * ***********************************************************/
99     if ((argc-optind)!= 1)  {
100         printf("From %d arguments, removed %d options\n", argc, optind);
101         usage();
102     }
103     if (use == 0) {
104         host = gethostbyname(argv[optind]);
105         printf("gethostbyname %s\n", argv[optind]);
106         printf("Official host name %s\n", host->h_name);
107         printf("Address Type %d\n", host->h_addrtype);
108         printf("Address Lenght %d\n", host->h_length);
109         addr.s_addr = *( (unsigned long *)host->h_addr);
110         printf("Address  %s\n", inet_ntoa(addr));
111     } else {
112 /*      host = getipnodebyname(argv[optind], AF_INET, 0, &i); */
113 /*      if (!host) printf("Error\n"); */
114 /*      printf("getipnodebyname %s, error %d\n", argv[optind], i); */
115 /*      printf("Official host name %s\n", host->h_name); */
116 /*      printf("Address Type %d\n", host->h_addrtype); */
117 /*      printf("Address Lenght %d\n", host->h_length); */
118 /*      addr.s_addr = *( (unsigned long *)host->h_addr); */
119 /*      printf("Address  %s\n", inet_ntoa(addr)); */
120         hint.ai_flags = 0;
121         hint.ai_family = PF_INET;
122         hint.ai_socktype = 0;
123         hint.ai_protocol = 0;
124         hint.ai_addrlen = 0;
125         hint.ai_addr = NULL;
126         hint.ai_canonname = NULL;
127         hint.ai_next = NULL;
128         if (i = getaddrinfo(argv[optind], "telnet", &hint, &address)) {
129             printf("getaddrinfo %s = %s \n", argv[optind], gai_strerror(i));
130         } else {
131             printf("Address flag %d\n", address->ai_flags);
132             printf("Address family %d\n", address->ai_family);
133             printf("Address socket type %d\n", address->ai_socktype);
134             printf("Address protocol %d\n", address->ai_protocol);
135             printf("Address lenght %d\n", address->ai_addrlen);
136             printf("Canonical name %s\n", address->ai_canonname);
137             socket = (struct sockaddr_in *) address->ai_addr;
138             printf("Address %s\n", inet_ntoa(socket->sin_addr));
139             printf("Port %d\n", htons(socket->sin_port));
140             printf("Family %d\n", socket->sin_family);
141             
142         }
143     }
144     return 0;
145 }
146 /*
147  * routine to print usage info and exit
148  */
149 void usage(void) {
150     printf("Program gehost: test host name functions  \n");
151     printf("Usage:\n");
152     printf("  gethost [-h] address \n");
153     printf("  -h           print this help\n");
154     printf("  -a           use getaddrinfo\n");
155     printf("  -n           use gethostbyname\n");
156     
157     exit(1);
158 }
159